Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/control/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ func (p *planeImpl) gitDir(ctx context.Context, dir string) (string, error) {
return ws.Dir(), nil
}

// git runs a git subcommand in dir and returns trimmed stdout (stderr folded in
// on failure for a useful error).
// git runs a git subcommand in dir and removes only Git's trailing line ending
// from stdout (stderr is folded in on failure for a useful error). Leading
// whitespace is protocol data for commands such as `status --porcelain=v1`:
// trimming it shifts the XY columns and corrupts the first changed path.
func git(ctx context.Context, dir string, args ...string) (string, error) {
return gitWithEnvironment(ctx, dir, nil, args...)
}
Expand All @@ -53,7 +55,7 @@ func gitWithEnvironment(ctx context.Context, dir string, environment []string, a
}
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), msg)
}
return strings.TrimSpace(out.String()), nil
return strings.TrimRight(out.String(), "\r\n"), nil
}

// GitStatus reports branch, dirty state, changed files, and ahead/behind vs the
Expand Down
18 changes: 18 additions & 0 deletions pkg/control/vcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ func TestGitStatusCleanThenDirty(t *testing.T) {
}
}

func TestGitStatusPreservesLeadingPorcelainStatusColumn(t *testing.T) {
dir := initGitRepo(t)
if err := os.WriteFile(filepath.Join(dir, "README.md"), []byte("changed\n"), 0o600); err != nil {
t.Fatal(err)
}

status, err := New().GitStatus(t.Context(), dir)
if err != nil {
t.Fatal(err)
}
if len(status.Files) != 1 {
t.Fatalf("files = %+v, want one modified file", status.Files)
}
if got := status.Files[0]; got.Path != "README.md" || got.Code != " M" || got.Staged {
t.Fatalf("modified file = %+v, want path README.md, code %q, staged false", got, " M")
}
}

func TestGitStatusReportsContainingRepositoryRootFromNestedDirectory(t *testing.T) {
dir := initGitRepo(t)
nested := filepath.Join(dir, "services", "api")
Expand Down
Loading