Repository: bluekeyes/go-gitdiff (go)

Issue:
gitdiff: line 63: git file header: invalid mode line: invalid syntax
Looks like your library is a bit out-of-date? Line 63 for me in sample I was testing with looks like this:

```
new file mode 100644
```

Pull request description:
Fix parsing of mode lines with trailing space
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 can cause other problems so it is best to avoid this transform, but if it does happen, it shouldn't cause a parse error.

Fixes #37. I might want to revisit this in the future in favor of something more like `strtoul()`, but for now, I think it is still better to fail on obviously wrong numbers than to silently parse some part of them.

Patch:
diff --git a/gitdiff/file_header.go b/gitdiff/file_header.go
index 58904b4..77ceb5d 100644
--- a/gitdiff/file_header.go
+++ b/gitdiff/file_header.go
@@ -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
 }
 


Test patch:
diff --git a/gitdiff/file_header_test.go b/gitdiff/file_header_test.go
index 102795c..ef29833 100644
--- a/gitdiff/file_header_test.go
+++ b/gitdiff/file_header_test.go
@@ -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{