Skip to content

Commit

Permalink
Fix inconsistencies in patch format
Browse files Browse the repository at this point in the history
Add "a/" and "b/" prefixes to names on "---" and "+++" lines. Reorder
lines to match Git. Only print mode lines when the mode is changing.
  • Loading branch information
bluekeyes committed Aug 10, 2024
1 parent debe110 commit fe252ed
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,18 @@ func (f *File) String() string {
writeQuotedName(&diff, "b/"+bName)
diff.WriteByte('\n')

diff.WriteString("--- ")
if f.OldName == "" {
diff.WriteString("/dev/null")
} else {
writeQuotedName(&diff, f.OldName)
}
diff.WriteByte('\n')

diff.WriteString("+++ ")
if f.NewName == "" {
diff.WriteString("/dev/null")
} else {
writeQuotedName(&diff, f.NewName)
}
diff.WriteByte('\n')

if f.OldMode != 0 {
if f.IsDelete {
fmt.Fprintf(&diff, "deleted file mode %o\n", f.OldMode)
} else {
} else if f.NewMode != 0 {
fmt.Fprintf(&diff, "old mode %o\n", f.OldMode)
}
}

if f.NewMode != 0 {
if f.IsNew {
fmt.Fprintf(&diff, "new file mode %o\n", f.NewMode)
} else {
} else if f.OldMode != 0 {
fmt.Fprintf(&diff, "new mode %o\n", f.NewMode)
}
}
Expand Down Expand Up @@ -135,12 +119,31 @@ func (f *File) String() string {

if f.OldOIDPrefix != "" && f.NewOIDPrefix != "" {
fmt.Fprintf(&diff, "index %s..%s", f.OldOIDPrefix, f.NewOIDPrefix)
if f.OldMode != 0 {
if f.OldMode != 0 && !f.IsDelete {
fmt.Fprintf(&diff, " %o", f.OldMode)
}
diff.WriteByte('\n')
}

// The "---" and "+++" lines only appear for patches with fragments
if len(f.TextFragments) > 0 || f.BinaryFragment != nil {
diff.WriteString("--- ")
if f.OldName == "" {
diff.WriteString("/dev/null")
} else {
writeQuotedName(&diff, "a/"+f.OldName)
}
diff.WriteByte('\n')

diff.WriteString("+++ ")
if f.NewName == "" {
diff.WriteString("/dev/null")
} else {
writeQuotedName(&diff, "b/"+f.NewName)
}
diff.WriteByte('\n')
}

if f.IsBinary {
// TODO(bkeyes): add string method for BinaryFragments
} else {
Expand Down Expand Up @@ -192,7 +195,15 @@ func (f *TextFragment) String() string {
// Header returns a git diff header of this fragment. See [File.String] for
// more details on this format.
func (f *TextFragment) Header() string {
return fmt.Sprintf("@@ -%d,%d +%d,%d @@ %s", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines, f.Comment)
var hdr strings.Builder

fmt.Fprintf(&hdr, "@@ -%d,%d +%d,%d @@", f.OldPosition, f.OldLines, f.NewPosition, f.NewLines)
if f.Comment != "" {
hdr.WriteByte(' ')
hdr.WriteString(f.Comment)
}

return hdr.String()
}

// Validate checks that the fragment is self-consistent and appliable. Validate
Expand Down

0 comments on commit fe252ed

Please sign in to comment.