git: support SHA-256 in rev-list scanner

This code path has several 40-based constants.  Use the ObjectIDLengths
and ObjectIDRegex variables to parse these values in a way that works
for either SHA-1 or SHA-256.
This commit is contained in:
brian m. carlson 2020-06-10 21:32:13 +00:00
parent cf98b1c440
commit 29c9fdeffa
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1

@ -291,6 +291,8 @@ func nonZeroShas(all []string) []string {
return nz
}
var startsWithObjectID = regexp.MustCompile(fmt.Sprintf(`\A%s`, ObjectIDRegex))
// Name is an optional field that gives the name of the object (if the object is
// a tree, blob).
//
@ -341,19 +343,23 @@ func (s *RevListScanner) scan() ([]byte, string, error) {
}
line := strings.TrimSpace(s.s.Text())
if len(line) < 40 {
if len(line) < ObjectIDLengths[0] {
return nil, "", nil
}
sha1, err := hex.DecodeString(line[:40])
oidhex := startsWithObjectID.FindString(line)
if len(oidhex) == 0 {
return nil, "", fmt.Errorf("missing object id in line (got %q)", line)
}
oid, err := hex.DecodeString(oidhex)
if err != nil {
return nil, "", err
}
var name string
if len(line) > 40 {
name = line[41:]
if len(line) > len(oidhex) {
name = line[len(oidhex)+1:]
}
return sha1, name, nil
return oid, name, nil
}