From aa3e0d357eb173ce818831903627a278af15ccf0 Mon Sep 17 00:00:00 2001 From: Billy Keyes Date: Tue, 19 Mar 2019 22:35:52 -0700 Subject: [PATCH] Clarify behavior when parsing "index" lines The parsed OIDs are usually prefixes, not full hashes, so rename the fields appropriately. Also note that values that are too long to be valid OIDs are still accepted by the library. --- README.md | 6 +++--- gitdiff/file_header.go | 6 +++++- gitdiff/file_header_test.go | 16 ++++++++-------- gitdiff/gitdiff.go | 6 +++--- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 693a0b6..30bbf73 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,6 @@ In development, most functionality is currently missing, incomplete, or broken. Unicode file names are handled; these are bugs, so please report any issues of this type. -3. When reading headers, object IDs are always loaded from `index` lines, - regardless of their length. Git calls these fields `{new,old}_oid_prefix` - but appears to only load full-length OIDs, ignoring abbreviations. +3. When reading headers, this library does not validate that OIDs present on an + `index` line are shorter than or equal to the maximum hash length, as this + requires knowing if the repository used SHA1 or SHA256 hashes. diff --git a/gitdiff/file_header.go b/gitdiff/file_header.go index f5711e4..9437f56 100644 --- a/gitdiff/file_header.go +++ b/gitdiff/file_header.go @@ -161,13 +161,17 @@ func parseGitHeaderScore(f *File, line, defaultName string) error { func parseGitHeaderIndex(f *File, line, defaultName string) error { const sep = ".." + // note that git stops parsing if the OIDs are too long to be valid + // checking this requires knowing if the repository uses SHA1 or SHA256 + // hashes, which we don't know, so we just skip that check + parts := strings.SplitN(line, " ", 2) oids := strings.SplitN(parts[0], sep, 2) if len(oids) < 2 { return fmt.Errorf("invalid index line: missing %q", sep) } - f.OldOID, f.NewOID = oids[0], oids[1] + f.OldOIDPrefix, f.NewOIDPrefix = oids[0], oids[1] if len(parts) > 1 { return parseGitHeaderOldMode(f, parts[1], defaultName) diff --git a/gitdiff/file_header_test.go b/gitdiff/file_header_test.go index e102908..646d739 100644 --- a/gitdiff/file_header_test.go +++ b/gitdiff/file_header_test.go @@ -291,24 +291,24 @@ func TestParseGitHeaderData(t *testing.T) { "indexFullSHA1AndMode": { Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098 100644\n", OutputFile: &File{ - OldOID: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14", - NewOID: "04fab916d8f938173cbb8b93469855f0e838f098", - OldMode: os.FileMode(0100644), + OldOIDPrefix: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14", + NewOIDPrefix: "04fab916d8f938173cbb8b93469855f0e838f098", + OldMode: os.FileMode(0100644), }, }, "indexFullSHA1NoMode": { Line: "index 79c6d7f7b7e76c75b3d238f12fb1323f2333ba14..04fab916d8f938173cbb8b93469855f0e838f098\n", OutputFile: &File{ - OldOID: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14", - NewOID: "04fab916d8f938173cbb8b93469855f0e838f098", + OldOIDPrefix: "79c6d7f7b7e76c75b3d238f12fb1323f2333ba14", + NewOIDPrefix: "04fab916d8f938173cbb8b93469855f0e838f098", }, }, "indexAbbrevSHA1AndMode": { Line: "index 79c6d7..04fab9 100644\n", OutputFile: &File{ - OldOID: "79c6d7", - NewOID: "04fab9", - OldMode: os.FileMode(0100644), + OldOIDPrefix: "79c6d7", + NewOIDPrefix: "04fab9", + OldMode: os.FileMode(0100644), }, }, "indexInvalid": { diff --git a/gitdiff/gitdiff.go b/gitdiff/gitdiff.go index c41e6f4..cea3a89 100644 --- a/gitdiff/gitdiff.go +++ b/gitdiff/gitdiff.go @@ -18,9 +18,9 @@ type File struct { OldMode os.FileMode NewMode os.FileMode - OldOID string - NewOID string - Score int + OldOIDPrefix string + NewOIDPrefix string + Score int Fragments []*Fragment }