diff --git a/commands/status/process.go b/commands/status/process.go index 6b2e7cf..cb4163b 100644 --- a/commands/status/process.go +++ b/commands/status/process.go @@ -153,9 +153,17 @@ func ProcessChanges(s *bufio.Scanner, root string) ([]*StatusItem, error) { // ...if chunk represents a rename or copy op, need to append another chunk // to get the full change item, with NUL manually reinserted because scanner // will extract past it. - if (chunk[0] == 'R' || chunk[0] == 'C') && s.Scan() { - chunk = append(chunk, '\x00') - chunk = append(chunk, s.Bytes()...) + // + // Note that the underlying slice from previous scanner.Bytes() MAY be + // overridden by subsequent scans, so need to copy it to a new slice + // first before scanning to get the next token. + if chunk[0] == 'R' || chunk[0] == 'C' { + composite := make([]byte, len(chunk)) + copy(composite, chunk) + s.Scan() + composite = append(composite, '\x00') + composite = append(composite, s.Bytes()...) + chunk = composite } statuses, err := processChange(chunk, wd, root) if err != nil { diff --git a/commands/status/process_test.go b/commands/status/process_test.go index 353d8ec..7b76bdc 100644 --- a/commands/status/process_test.go +++ b/commands/status/process_test.go @@ -277,3 +277,28 @@ func TestExtractBranch(t *testing.T) { }) } } + +/* +// Test to verify https://github.com/mroth/scmpuff/issues/26. +// +// Leaving commented out since unlikely to encounter this exact issue again in +// future, and I'm not sure about importing the user's datafile into this repo. +// +// If needed again, the data file is attached to the issue as `output.txt`. + +func TestBrokenProcessChanges(t *testing.T) { + dat, err := ioutil.ReadFile("testdata/cjfuller_sample.dat") + if err != nil { + t.Fatal(err) + } + s := bufio.NewScanner(bytes.NewReader(dat)) + s.Split(nulSplitFunc) + actual, err := ProcessChanges(s, "") + if err != nil { + t.Fatal(err) + } + if len(actual) != 270 { // `git status -s | wc -l` in replicated repo + t.Errorf("expected %v changes, got %v", 270, len(actual)) + } +} +*/