Skip to content

Commit b570980

Browse files
authored
Add owners for deleted files (#70)
1 parent cc6958e commit b570980

File tree

3 files changed

+133
-4
lines changed

3 files changed

+133
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Code Ownership & Review Assignment Tool - GitHub CODEOWNERS but better
44

55
[![Go Report Card](https://goreportcard.com/badge/github.com/multimediallc/codeowners-plus)](https://goreportcard.com/report/github.com/multimediallc/codeowners-plus?kill_cache=1)
66
[![Tests](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml/badge.svg)](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml)
7-
![Coverage](https://img.shields.io/badge/Coverage-81.6%25-brightgreen)
7+
![Coverage](https://img.shields.io/badge/Coverage-81.7%25-brightgreen)
88
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
99
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
1010

internal/git/diff.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,23 @@ type changesSinceContext struct {
111111
olderDiff []*diff.FileDiff
112112
}
113113

114+
func diffToFilename(d *diff.FileDiff) string {
115+
if d.NewName == "/dev/null" {
116+
// For deleted files, NewName is "/dev/null", so use OrigName instead
117+
return d.OrigName[2:]
118+
}
119+
return d.NewName[2:]
120+
}
121+
114122
// Parse the diff output to get the file names and hunks
115123
func toDiffFiles(fileDiffs []*diff.FileDiff) ([]codeowners.DiffFile, error) {
116124
diffFiles := make([]codeowners.DiffFile, 0, len(fileDiffs))
117125

118126
for _, d := range fileDiffs {
127+
fileName := diffToFilename(d)
128+
119129
newDiffFile := codeowners.DiffFile{
120-
FileName: d.NewName[2:],
130+
FileName: fileName,
121131
Hunks: make([]codeowners.HunkRange, 0, len(d.Hunks)),
122132
}
123133
for _, hunk := range d.Hunks {
@@ -147,8 +157,10 @@ func changesSince(context changesSinceContext) ([]codeowners.DiffFile, error) {
147157
diffFiles := make([]codeowners.DiffFile, 0, len(context.newerDiff))
148158

149159
for _, d := range context.newerDiff {
160+
fileName := diffToFilename(d)
161+
150162
newDiffFile := codeowners.DiffFile{
151-
FileName: d.NewName[2:],
163+
FileName: fileName,
152164
Hunks: make([]codeowners.HunkRange, 0, len(d.Hunks)),
153165
}
154166
for _, hunk := range d.Hunks {
@@ -177,8 +189,10 @@ func getGitDiff(data DiffContext, executor gitCommandExecutor) ([]*diff.FileDiff
177189
return nil, err
178190
}
179191
gitDiff = slices.DeleteFunc(gitDiff, func(d *diff.FileDiff) bool {
192+
fileName := diffToFilename(d)
193+
180194
for _, dir := range data.IgnoreDirs {
181-
if strings.HasPrefix(d.NewName[2:], dir) {
195+
if strings.HasPrefix(fileName, dir) {
182196
return true
183197
}
184198
}

internal/git/diff_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,33 @@ func TestNewDiff(t *testing.T) {
110110
"file2.go": 1,
111111
},
112112
},
113+
{
114+
name: "ignore deleted files in ignored directories",
115+
context: DiffContext{
116+
Base: "main",
117+
Head: "feature",
118+
Dir: ".",
119+
IgnoreDirs: []string{"ignored/"},
120+
},
121+
mockOutput: `diff --git a/file1.go b/file1.go
122+
index abc..def 100644
123+
--- a/file1.go
124+
+++ b/file1.go
125+
@@ -10,0 +11 @@ func Example() {
126+
+ fmt.Println("New line")
127+
diff --git a/ignored/deleted.go b/dev/null
128+
deleted file mode 100644
129+
index ghi..0000000
130+
--- a/ignored/deleted.go
131+
+++ /dev/null
132+
@@ -1 +0,0 @@
133+
-content`,
134+
expectedErr: false,
135+
expectedFiles: 1,
136+
expectedHunks: map[string]int{
137+
"file1.go": 1,
138+
},
139+
},
113140
}
114141

115142
for _, tc := range tt {
@@ -389,6 +416,94 @@ func TestToDiffFiles(t *testing.T) {
389416
},
390417
},
391418
},
419+
{
420+
name: "deleted file",
421+
fileDiffs: []*diff.FileDiff{
422+
{
423+
OrigName: "a/deleted.go",
424+
NewName: "/dev/null",
425+
Hunks: []*diff.Hunk{
426+
{
427+
NewStartLine: 0,
428+
NewLines: 0,
429+
},
430+
},
431+
},
432+
},
433+
expected: []codeowners.DiffFile{
434+
{
435+
FileName: "deleted.go",
436+
Hunks: []codeowners.HunkRange{
437+
{
438+
Start: 0,
439+
End: -1,
440+
},
441+
},
442+
},
443+
},
444+
},
445+
{
446+
name: "mixed: added, modified, and deleted files",
447+
fileDiffs: []*diff.FileDiff{
448+
{
449+
NewName: "b/added.go",
450+
Hunks: []*diff.Hunk{
451+
{
452+
NewStartLine: 1,
453+
NewLines: 5,
454+
},
455+
},
456+
},
457+
{
458+
NewName: "b/modified.go",
459+
Hunks: []*diff.Hunk{
460+
{
461+
NewStartLine: 10,
462+
NewLines: 2,
463+
},
464+
},
465+
},
466+
{
467+
OrigName: "a/deleted.go",
468+
NewName: "/dev/null",
469+
Hunks: []*diff.Hunk{
470+
{
471+
NewStartLine: 0,
472+
NewLines: 0,
473+
},
474+
},
475+
},
476+
},
477+
expected: []codeowners.DiffFile{
478+
{
479+
FileName: "added.go",
480+
Hunks: []codeowners.HunkRange{
481+
{
482+
Start: 1,
483+
End: 5,
484+
},
485+
},
486+
},
487+
{
488+
FileName: "modified.go",
489+
Hunks: []codeowners.HunkRange{
490+
{
491+
Start: 10,
492+
End: 11,
493+
},
494+
},
495+
},
496+
{
497+
FileName: "deleted.go",
498+
Hunks: []codeowners.HunkRange{
499+
{
500+
Start: 0,
501+
End: -1,
502+
},
503+
},
504+
},
505+
},
506+
},
392507
}
393508

394509
for _, tc := range tt {

0 commit comments

Comments
 (0)