Skip to content

Commit

Permalink
Parse empty git status lines
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed May 21, 2020
1 parent 9acf823 commit 2b3755c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
18 changes: 17 additions & 1 deletion internal/git/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,21 @@ func ParseStatusPorcelainV2(output []byte) (*Status, error) {
return nil, ParseError(text)
}
}
return status, s.Err()
if err := s.Err(); err != nil {
return nil, err
}
if status.Empty() {
return nil, nil
}
return status, nil
}

// Empty returns true if s is empty.
func (s *Status) Empty() bool {
return s == nil || true &&
len(s.Ignored) == 0 &&
len(s.Ordinary) == 0 &&
len(s.RenamedOrCopied) == 0 &&
len(s.Unmerged) == 0 &&
len(s.Untracked) == 0
}
11 changes: 9 additions & 2 deletions internal/git/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ func TestParseStatusPorcelainV2(t *testing.T) {
for _, tc := range []struct {
name string
outputStr string
expectedEmpty bool
expectedStatus *Status
}{
{
name: "empty",
outputStr: "",
expectedEmpty: true,
},
{
name: "added",
outputStr: "1 A. N... 000000 100644 100644 0000000000000000000000000000000000000000 cea5c3500651a923bacd80f960dd20f04f71d509 main.go\n",
Expand Down Expand Up @@ -53,7 +59,7 @@ func TestParseStatusPorcelainV2(t *testing.T) {
},
{
name: "update",
outputStr: "1 .M N... 100644 100644 100644 353dbbb3c29a80fb44d4e26dac111739d25294db 353dbbb3c29a80fb44d4e26dac111739d25294db cmd/gitvcs.go\n",
outputStr: "1 .M N... 100644 100644 100644 353dbbb3c29a80fb44d4e26dac111739d25294db 353dbbb3c29a80fb44d4e26dac111739d25294db cmd/git.go\n",
expectedStatus: &Status{
Ordinary: []OrdinaryStatus{
{
Expand All @@ -65,7 +71,7 @@ func TestParseStatusPorcelainV2(t *testing.T) {
MW: 0o100644,
HH: "353dbbb3c29a80fb44d4e26dac111739d25294db",
HI: "353dbbb3c29a80fb44d4e26dac111739d25294db",
Path: "cmd/gitvcs.go",
Path: "cmd/git.go",
},
},
},
Expand Down Expand Up @@ -159,6 +165,7 @@ func TestParseStatusPorcelainV2(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
actualStatus, err := ParseStatusPorcelainV2([]byte(tc.outputStr))
require.NoError(t, err)
assert.Equal(t, tc.expectedEmpty, actualStatus.Empty())
assert.Equal(t, tc.expectedStatus, actualStatus)
})
}
Expand Down

0 comments on commit 2b3755c

Please sign in to comment.