forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
39 lines (33 loc) · 978 Bytes
/
Copy pathdiff.go
File metadata and controls
39 lines (33 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package diff
import (
"bytes"
"sort"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// ParseGitDiffNameStatus returns the paths changedA and changedB for commits
// A and B respectively. It expects to be parsing the output of the command
// git diff -z --name-status --no-renames A B.
func ParseGitDiffNameStatus(out []byte) (changedA, changedB []string, err error) {
if len(out) == 0 {
return nil, nil, nil
}
slices := bytes.Split(bytes.TrimRight(out, "\x00"), []byte{0})
if len(slices)%2 != 0 {
return nil, nil, errors.New("uneven pairs")
}
for i := 0; i < len(slices); i += 2 {
path := string(slices[i+1])
switch slices[i][0] {
case 'D': // no longer appears in B
changedA = append(changedA, path)
case 'M':
changedA = append(changedA, path)
changedB = append(changedB, path)
case 'A': // doesn't exist in A
changedB = append(changedB, path)
}
}
sort.Strings(changedA)
sort.Strings(changedB)
return changedA, changedB, nil
}