Skip to content

Commit

Permalink
Skip file headers if FromFile/ToFile are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Dec 8, 2015
1 parent e8554b8 commit 509b2ff
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions difflib/difflib.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,13 +585,15 @@ func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
if len(diff.ToDate) > 0 {
toDate = "\t" + diff.ToDate
}
err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol)
if err != nil {
return err
}
err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol)
if err != nil {
return err
if diff.FromFile != "" || diff.ToFile != "" {
err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol)
if err != nil {
return err
}
err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol)
if err != nil {
return err
}
}
}
first, last := g[0], g[len(g)-1]
Expand Down Expand Up @@ -710,8 +712,10 @@ func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
if len(diff.ToDate) > 0 {
toDate = "\t" + diff.ToDate
}
wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
if diff.FromFile != "" || diff.ToFile != "" {
wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
}
}

first, last := g[0], g[len(g)-1]
Expand Down

2 comments on commit 509b2ff

@gonzalop
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks compatibility to other tools that deal with unified diffs. The format of a unified diff always has the file headers ->
http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html#Detailed-Unified

@pmezard
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct and I was reluctant to introduce the change though I see the point of it. But what is done is done and I would rather not change it again (and avoid future changes in this code), since working around it is not too hard.

Please sign in to comment.