Skip to content

Commit ac2e2c5

Browse files
committed
Fix mode on index lines, add more test cases
1 parent 9d11cc2 commit ac2e2c5

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

gitdiff/gitdiff.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ func (f *File) String() string {
119119

120120
if f.OldOIDPrefix != "" && f.NewOIDPrefix != "" {
121121
fmt.Fprintf(&diff, "index %s..%s", f.OldOIDPrefix, f.NewOIDPrefix)
122-
if f.OldMode != 0 && !f.IsDelete {
122+
123+
// Mode is only included on the index line when it is not changing
124+
if f.OldMode != 0 && ((f.NewMode == 0 && !f.IsDelete) || f.OldMode == f.NewMode) {
123125
fmt.Fprintf(&diff, " %o", f.OldMode)
124126
}
127+
125128
diff.WriteByte('\n')
126129
}
127130

gitdiff/gitdiff_string_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import (
88

99
func TestFile_String(t *testing.T) {
1010
sources := []string{
11+
"testdata/string/copy.patch",
1112
"testdata/string/delete.patch",
1213
"testdata/string/mode.patch",
14+
"testdata/string/mode_modify.patch",
1315
"testdata/string/modify.patch",
1416
"testdata/string/new.patch",
1517
"testdata/string/new_empty.patch",
1618
"testdata/string/new_mode.patch",
1719
"testdata/string/rename.patch",
1820
"testdata/string/rename_modify.patch",
21+
"testdata/string/copy_modify.patch",
1922
}
2023

2124
for _, src := range sources {

gitdiff/testdata/string/copy.patch

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
diff --git a/file.txt b/numbers.txt
2+
similarity index 100%
3+
copy from file.txt
4+
copy to numbers.txt
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
diff --git a/file.txt b/numbers.txt
2+
similarity index 57%
3+
copy from file.txt
4+
copy to numbers.txt
5+
index c9e9e05..6c4a3e0 100644
6+
--- a/file.txt
7+
+++ b/numbers.txt
8+
@@ -1,6 +1,6 @@
9+
one
10+
two
11+
-three
12+
+three three three
13+
four
14+
five
15+
six
16+
@@ -8,3 +8,5 @@ seven
17+
eight
18+
nine
19+
ten
20+
+eleven
21+
+twelve
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
diff --git a/script.sh b/script.sh
2+
old mode 100644
3+
new mode 100755
4+
index 7a870bd..68d501e
5+
--- a/script.sh
6+
+++ b/script.sh
7+
@@ -1,2 +1,2 @@
8+
#!/bin/bash
9+
-echo "Hello World"
10+
+echo "Hello, World!"

0 commit comments

Comments
 (0)