Skip to content

Commit

Permalink
show deleted files in diff (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl authored May 24, 2024
1 parent a872855 commit 8510d3b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"regexp"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func parseBlock(line string) (Block, error) {
}

func parseFile(line string) (File, error) {
r := regexp.MustCompile(`^\+\+\+ b\/(.*)`)
r := regexp.MustCompile(`^(?:\-\-\- a\/|\+\+\+ b\/)(.*)`)
matches := r.FindAllStringSubmatch(line, -1)
if len(matches) != 1 {
return File{}, errors.New("match not found")
Expand All @@ -82,7 +83,15 @@ func ParseDiff(lines string) (Diff, error) {
continue
}
if file.Name != "" {
diff.Files = append(diff.Files, file)
fileExists := slices.ContainsFunc(diff.Files, func(f File) bool {
return f.Name == file.Name
})
// the file name might have been identified with
// `--- a/(.*)` or `+++ b/(.*)` but if identified with both,
// don't append a second file with the same name.
if !fileExists {
diff.Files = append(diff.Files, file)
}
}
lastFile := &diff.Files[len(diff.Files)-1]
block, err := parseBlock(v)
Expand Down
8 changes: 8 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,11 @@ func TestParseDiffNewFile(t *testing.T) {
t.Fatalf("Expected filename .gitignore")
}
}

func TestParseDiffDeletedFile(t *testing.T) {
contents, _ := os.ReadFile("./test/deleted-file.diff")
actual, _ := ParseDiff(string(contents))
if len(actual.Files) != 1 {
t.Fatalf("Expected 1 file")
}
}
18 changes: 18 additions & 0 deletions parser/test/deleted-file.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/parser/newfile.txt b/parser/newfile.txt
deleted file mode 100644
index 1901ba9..0000000
--- a/parser/newfile.txt
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-

0 comments on commit 8510d3b

Please sign in to comment.