commands/command_migrate.go: print newline conditionally

Sometimes when invoking 'git lfs migrate import', mysterious output like
the following can be seen:

    $ git lfs migrate import
    migrate: override changes in your working copy? [Y/n] y

    migrate: changes in your working copy will be overridden ...
    migrate: Fetching remote refs: ..., done

Where an extra newline is printed between the answer, 'y', and the next
line of output from the migrator.

Instead, let's only print that secondary newline when one isn't given in
the answer. This should never be the case (c.f., ReadString()), but will
harden the code to changes like opening /dev/tty in raw mode and reading
character-by-character.

To do so, ensure that the answer doesn't satisfy:

    strings.HasSuffix(answer, '\n')

...and print a newline iff it doesn't.

In a related sense, ignore io.EOF errors.
This commit is contained in:
Taylor Blau 2018-07-16 13:14:20 -05:00
parent ad369e55ff
commit 5a6734f9e1

@ -319,13 +319,16 @@ func ensureWorkingCopyClean(in io.Reader, out io.Writer) {
L:
for {
fmt.Fprintf(out, "migrate: override changes in your working copy? [Y/n] ")
s, _, err := answer.ReadLine()
s, err := answer.ReadString('\n')
if err != nil {
if err == io.EOF {
break L
}
ExitWithError(errors.Wrap(err,
"fatal: could not read answer"))
}
switch strings.TrimSpace(string(s)) {
switch strings.TrimSpace(s) {
case "n", "N":
proceed = false
break L
@ -333,7 +336,10 @@ func ensureWorkingCopyClean(in io.Reader, out io.Writer) {
proceed = true
break L
}
fmt.Fprintf(out, "\n")
if !strings.HasSuffix(s, "\n") {
fmt.Fprintf(out, "\n")
}
}
}