mirror of
https://github.com/bluekeyes/go-gitdiff
synced 2026-06-08 13:18:30 +00:00
Fix parsing of mode lines with trailing space (#38)
If a patch is passed through a system that converts line endings to '\r\n', mode lines end up with trailing whitespace that confuses strconv.ParseInt. In Git, this is avoided by using strtoul() for parsing, which stops at the first non-digit character. Changing line endings in patch content itself will cause other problems so it is best to avoid this transform, but if it does happen, it shouldn't cause a parse error.
This commit is contained in:
@@ -324,12 +324,12 @@ func parseGitHeaderNewName(f *File, line, defaultName string) error {
|
||||
}
|
||||
|
||||
func parseGitHeaderOldMode(f *File, line, defaultName string) (err error) {
|
||||
f.OldMode, err = parseMode(line)
|
||||
f.OldMode, err = parseMode(strings.TrimSpace(line))
|
||||
return
|
||||
}
|
||||
|
||||
func parseGitHeaderNewMode(f *File, line, defaultName string) (err error) {
|
||||
f.NewMode, err = parseMode(line)
|
||||
f.NewMode, err = parseMode(strings.TrimSpace(line))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -486,6 +486,12 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
OldMode: os.FileMode(0100644),
|
||||
},
|
||||
},
|
||||
"oldModeWithTrailingSpace": {
|
||||
Line: "old mode 100644\r\n",
|
||||
OutputFile: &File{
|
||||
OldMode: os.FileMode(0100644),
|
||||
},
|
||||
},
|
||||
"invalidOldMode": {
|
||||
Line: "old mode rw\n",
|
||||
Err: true,
|
||||
@@ -496,6 +502,12 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
NewMode: os.FileMode(0100755),
|
||||
},
|
||||
},
|
||||
"newModeWithTrailingSpace": {
|
||||
Line: "new mode 100755\r\n",
|
||||
OutputFile: &File{
|
||||
NewMode: os.FileMode(0100755),
|
||||
},
|
||||
},
|
||||
"invalidNewMode": {
|
||||
Line: "new mode rwx\n",
|
||||
Err: true,
|
||||
@@ -518,6 +530,15 @@ func TestParseGitHeaderData(t *testing.T) {
|
||||
IsNew: true,
|
||||
},
|
||||
},
|
||||
"newFileModeWithTrailingSpace": {
|
||||
Line: "new file mode 100755\r\n",
|
||||
DefaultName: "dir/file.txt",
|
||||
OutputFile: &File{
|
||||
NewName: "dir/file.txt",
|
||||
NewMode: os.FileMode(0100755),
|
||||
IsNew: true,
|
||||
},
|
||||
},
|
||||
"copyFrom": {
|
||||
Line: "copy from dir/file.txt\n",
|
||||
OutputFile: &File{
|
||||
|
||||
Reference in New Issue
Block a user