Skip to content

Commit

Permalink
Return preamble when a patch has no files (#46)
Browse files Browse the repository at this point in the history
While empty patches with only a header were parsable, the parser
discarded the preamble content. This meant callers had to handle this
case specially. Now, if we reach the end of the input without finding a
file, Parse() returns the full content of the patch as the preamble.
  • Loading branch information
bluekeyes authored Jul 15, 2024
1 parent a00d2cc commit 0a4e55f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gitdiff/file_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *parser) ParseNextFileHeader() (*File, string, error) {
return nil, "", err
}
}
return nil, "", nil
return nil, preamble.String(), nil
}

func (p *parser) ParseGitFileHeader() (*File, error) {
Expand Down
6 changes: 3 additions & 3 deletions gitdiff/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func Parse(r io.Reader) ([]*File, string, error) {
if err != nil {
return files, preamble, err
}
if len(files) == 0 {
preamble = pre
}
if file == nil {
break
}
Expand All @@ -50,9 +53,6 @@ func Parse(r io.Reader) ([]*File, string, error) {
}
}

if len(files) == 0 {
preamble = pre
}
files = append(files, file)
}

Expand Down
18 changes: 16 additions & 2 deletions gitdiff/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,24 @@ this is another line
--- could this be a header?
nope, it's just some dashes
`,
Output: nil,
Preamble: "",
Output: nil,
Preamble: `
this is a line
this is another line
--- could this be a header?
nope, it's just some dashes
`,
},
"detatchedFragmentLike": {
Input: `
a wild fragment appears?
@@ -1,3 +1,4 ~1,5 @@
`,
Output: nil,
Preamble: `
a wild fragment appears?
@@ -1,3 +1,4 ~1,5 @@
`,
},
"detatchedFragment": {
Input: `
Expand Down Expand Up @@ -426,6 +435,11 @@ Date: Tue Apr 2 22:55:40 2019 -0700
},
Preamble: textPreamble,
},
"noFiles": {
InputFile: "testdata/no_files.patch",
Output: nil,
Preamble: textPreamble,
},
"newBinaryFile": {
InputFile: "testdata/new_binary_file.patch",
Output: []*File{
Expand Down
8 changes: 8 additions & 0 deletions gitdiff/testdata/no_files.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
commit 5d9790fec7d95aa223f3d20936340bf55ff3dcbe
Author: Morton Haypenny <[email protected]>
Date: Tue Apr 2 22:55:40 2019 -0700

A file with multiple fragments.

The content is arbitrary.

0 comments on commit 0a4e55f

Please sign in to comment.