Abstract hash function usage (#28138)

Refactor Hash interfaces and centralize hash function. This will allow
easier introduction of different hash function later on.

This forms the "no-op" part of the SHA256 enablement patch.
This commit is contained in:
Adam Majer 2023-12-13 21:02:00 +00:00 committed by GitHub
parent 064f05204c
commit cbf923e87b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
122 changed files with 947 additions and 594 deletions

View file

@ -9,7 +9,6 @@ import (
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
@ -36,10 +35,6 @@ type ArchiveRequest struct {
CommitID string
}
// SHA1 hashes will only go up to 40 characters, but SHA256 hashes will go all
// the way to 64.
var shaRegex = regexp.MustCompile(`^[0-9a-f]{4,64}$`)
// ErrUnknownArchiveFormat request archive format is not supported
type ErrUnknownArchiveFormat struct {
RequestFormat string
@ -96,30 +91,13 @@ func NewRequest(repoID int64, repo *git.Repository, uri string) (*ArchiveRequest
r.refName = strings.TrimSuffix(uri, ext)
var err error
// Get corresponding commit.
if repo.IsBranchExist(r.refName) {
r.CommitID, err = repo.GetBranchCommitID(r.refName)
if err != nil {
return nil, err
}
} else if repo.IsTagExist(r.refName) {
r.CommitID, err = repo.GetTagCommitID(r.refName)
if err != nil {
return nil, err
}
} else if shaRegex.MatchString(r.refName) {
if repo.IsCommitExist(r.refName) {
r.CommitID = r.refName
} else {
return nil, git.ErrNotExist{
ID: r.refName,
}
}
} else {
commitID, err := repo.ConvertToGitID(r.refName)
if err != nil {
return nil, RepoRefNotFoundError{RefName: r.refName}
}
r.CommitID = commitID.String()
return r, nil
}