fork of go-gitdiff with jj support

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.

authored by Billy Keyes and committed by GitHub 981bc4b6 1575e0c4

Changed files
+23 -2
gitdiff
+2 -2
gitdiff/file_header.go
··· 324 324 } 325 325 326 326 func parseGitHeaderOldMode(f *File, line, defaultName string) (err error) { 327 - f.OldMode, err = parseMode(line) 327 + f.OldMode, err = parseMode(strings.TrimSpace(line)) 328 328 return 329 329 } 330 330 331 331 func parseGitHeaderNewMode(f *File, line, defaultName string) (err error) { 332 - f.NewMode, err = parseMode(line) 332 + f.NewMode, err = parseMode(strings.TrimSpace(line)) 333 333 return 334 334 } 335 335
+21
gitdiff/file_header_test.go
··· 486 486 OldMode: os.FileMode(0100644), 487 487 }, 488 488 }, 489 + "oldModeWithTrailingSpace": { 490 + Line: "old mode 100644\r\n", 491 + OutputFile: &File{ 492 + OldMode: os.FileMode(0100644), 493 + }, 494 + }, 489 495 "invalidOldMode": { 490 496 Line: "old mode rw\n", 491 497 Err: true, ··· 496 502 NewMode: os.FileMode(0100755), 497 503 }, 498 504 }, 505 + "newModeWithTrailingSpace": { 506 + Line: "new mode 100755\r\n", 507 + OutputFile: &File{ 508 + NewMode: os.FileMode(0100755), 509 + }, 510 + }, 499 511 "invalidNewMode": { 500 512 Line: "new mode rwx\n", 501 513 Err: true, ··· 511 523 }, 512 524 "newFileMode": { 513 525 Line: "new file mode 100755\n", 526 + DefaultName: "dir/file.txt", 527 + OutputFile: &File{ 528 + NewName: "dir/file.txt", 529 + NewMode: os.FileMode(0100755), 530 + IsNew: true, 531 + }, 532 + }, 533 + "newFileModeWithTrailingSpace": { 534 + Line: "new file mode 100755\r\n", 514 535 DefaultName: "dir/file.txt", 515 536 OutputFile: &File{ 516 537 NewName: "dir/file.txt",