Skip to content

Commit

Permalink
Abstract hash function usage
Browse files Browse the repository at this point in the history
Refactor Hash interfaces and centralize hash function. This will
allow easier introduction of different hash function later on.
  • Loading branch information
AdamMajer committed Nov 21, 2023
1 parent a6a674e commit 44bd4fc
Show file tree
Hide file tree
Showing 121 changed files with 832 additions and 557 deletions.
7 changes: 5 additions & 2 deletions cmd/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ Gitea or set your environment appropriately.`, "")
oldCommitIDs[count] = string(fields[0])
newCommitIDs[count] = string(fields[1])
refFullNames[count] = git.RefName(fields[2])
if refFullNames[count] == git.BranchPrefix+"master" && newCommitIDs[count] != git.EmptySHA && count == total {

hash, _ := git.HashFromString(newCommitIDs[count])
if refFullNames[count] == git.BranchPrefix+"master" && !hash.IsZero() && count == total {
masterPushed = true
}
count++
Expand Down Expand Up @@ -669,7 +671,8 @@ Gitea or set your environment appropriately.`, "")
if err != nil {
return err
}
if rs.OldOID != git.EmptySHA {
hash, _ := git.HashFromString(rs.OldOID)
if !hash.IsZero() {
err = writeDataPktLine(ctx, os.Stdout, []byte("option old-oid "+rs.OldOID))
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions models/git/branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ func TestAddDeletedBranch(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
firstBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{ID: 1})
hash, err := git.HashTypeFromString(repo.HashType)

assert.NoError(t, err)
assert.True(t, firstBranch.IsDeleted)
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, firstBranch.Name, firstBranch.DeletedByID))
assert.NoError(t, git_model.AddDeletedBranch(db.DefaultContext, repo.ID, "branch2", int64(1)))
Expand All @@ -30,14 +32,14 @@ func TestAddDeletedBranch(t *testing.T) {
assert.True(t, secondBranch.IsDeleted)

commit := &git.Commit{
ID: git.MustIDFromString(secondBranch.CommitID),
ID: hash.MustIDFromString(secondBranch.CommitID),
CommitMessage: secondBranch.CommitMessage,
Committer: &git.Signature{
When: secondBranch.CommitTime.AsLocalTime(),
},
}

err := git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
err = git_model.UpdateBranch(db.DefaultContext, repo.ID, secondBranch.PusherID, secondBranch.Name, commit)
assert.NoError(t, err)
}

Expand Down
19 changes: 8 additions & 11 deletions models/git/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ WHEN NOT MATCHED

// GetNextCommitStatusIndex retried 3 times to generate a resource index
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
if !git.IsValidSHAPattern(sha) {
_, err := git.HashFromString(sha)
if err != nil {
return 0, git.ErrInvalidSHA{SHA: sha}
}

Expand Down Expand Up @@ -423,41 +424,37 @@ func FindRepoRecentCommitStatusContexts(ctx context.Context, repoID int64, befor
type NewCommitStatusOptions struct {
Repo *repo_model.Repository
Creator *user_model.User
SHA string
SHA git.Hash
CommitStatus *CommitStatus
}

// NewCommitStatus save commit statuses into database
func NewCommitStatus(ctx context.Context, opts NewCommitStatusOptions) error {
if opts.Repo == nil {
return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA)
return fmt.Errorf("NewCommitStatus[nil, %s]: no repository specified", opts.SHA.String())
}

repoPath := opts.Repo.RepoPath()
if opts.Creator == nil {
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
}

if _, err := git.NewIDFromString(opts.SHA); err != nil {
return fmt.Errorf("NewCommitStatus[%s, %s]: invalid sha: %w", repoPath, opts.SHA, err)
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA.String())
}

ctx, committer, err := db.TxContext(ctx)
if err != nil {
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA.String(), err)
}
defer committer.Close()

// Get the next Status Index
idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA)
idx, err := GetNextCommitStatusIndex(ctx, opts.Repo.ID, opts.SHA.String())
if err != nil {
return fmt.Errorf("generate commit status index failed: %w", err)
}

opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
opts.CommitStatus.SHA = opts.SHA
opts.CommitStatus.SHA = opts.SHA.String()
opts.CommitStatus.CreatorID = opts.Creator.ID
opts.CommitStatus.RepoID = opts.Repo.ID
opts.CommitStatus.Index = idx
Expand Down
7 changes: 6 additions & 1 deletion models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -179,6 +180,7 @@ type Repository struct {
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
Topics []string `xorm:"TEXT JSON"`
HashType string

TrustModel TrustModelType

Expand Down Expand Up @@ -274,6 +276,8 @@ func (repo *Repository) AfterLoad() {
repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns

repo.HashType = "sha1"
}

// LoadAttributes loads attributes of the repository.
Expand Down Expand Up @@ -313,7 +317,8 @@ func (repo *Repository) HTMLURL() string {
// CommitLink make link to by commit full ID
// note: won't check whether it's an right id
func (repo *Repository) CommitLink(commitID string) (result string) {
if commitID == "" || commitID == "0000000000000000000000000000000000000000" {
hash, _ := git.HashTypeFromString(repo.HashType)
if commitID == "" || commitID == hash.Empty().String() {
result = ""
} else {
result = repo.Link() + "/commit/" + url.PathEscape(commitID)
Expand Down
2 changes: 1 addition & 1 deletion modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
return
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) == git.SHAFullLength {
} else if len(refName) == ctx.Repo.GitRepo.Hash.FullLength() {
ctx.Repo.CommitID = refName
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
}
// For legacy and API support only full commit sha
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) == git.SHAFullLength {
if len(parts) > 0 && len(parts[0]) == repo.GitRepo.Hash.FullLength() {
repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
Expand Down Expand Up @@ -871,7 +871,7 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
return getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsTagExist)
case RepoRefCommit:
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= repo.GitRepo.Hash.FullLength() {
repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
Expand Down Expand Up @@ -997,7 +997,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
return cancel
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) >= 7 && len(refName) <= git.SHAFullLength {
} else if len(refName) >= 7 && len(refName) <= ctx.Repo.GitRepo.Hash.FullLength() {
ctx.Repo.IsViewCommit = true
ctx.Repo.CommitID = refName

Expand All @@ -1007,7 +1007,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
return cancel
}
// If short commit ID add canonical link header
if len(refName) < git.SHAFullLength {
if len(refName) < ctx.Repo.GitRepo.Hash.FullLength() {
ctx.RespHeader().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
}
Expand Down
30 changes: 15 additions & 15 deletions modules/git/batch_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
// ReadBatchLine reads the header line from cat-file --batch
// We expect:
// <sha> SP <type> SP <size> LF
// sha is a 40byte not 20byte here
// sha is a hex encoded here
func ReadBatchLine(rd *bufio.Reader) (sha []byte, typ string, size int64, err error) {
typ, err = rd.ReadString('\n')
if err != nil {
Expand Down Expand Up @@ -251,20 +251,19 @@ headerLoop:
}

// git tree files are a list:
// <mode-in-ascii> SP <fname> NUL <20-byte SHA>
// <mode-in-ascii> SP <fname> NUL <binary Hash>
//
// Unfortunately this 20-byte notation is somewhat in conflict to all other git tools
// Therefore we need some method to convert these 20-byte SHAs to a 40-byte SHA
// Therefore we need some method to convert these binary hashes to hex hashes

// constant hextable to help quickly convert between 20byte and 40byte hashes
// constant hextable to help quickly convert between binary and hex representation
const hextable = "0123456789abcdef"

// To40ByteSHA converts a 20-byte SHA into a 40-byte sha. Input and output can be the
// same 40 byte slice to support in place conversion without allocations.
// BinToHexHeash converts a binary Hash into a hex encoded one. Input and output can be the
// same byte slice to support in place conversion without allocations.
// This is at least 100x quicker that hex.EncodeToString
// NB This requires that out is a 40-byte slice
func To40ByteSHA(sha, out []byte) []byte {
for i := 19; i >= 0; i-- {
func BinToHexHash(hash HashType, sha, out []byte) []byte {
for i := hash.FullLength()/2 - 1; i >= 0; i-- {
v := sha[i]
vhi, vlo := v>>4, v&0x0f
shi, slo := hextable[vhi], hextable[vlo]
Expand All @@ -278,10 +277,10 @@ func To40ByteSHA(sha, out []byte) []byte {
// It is recommended therefore to pass in an fnameBuf large enough to avoid almost all allocations
//
// Each line is composed of:
// <mode-in-ascii-dropping-initial-zeros> SP <fname> NUL <20-byte SHA>
// <mode-in-ascii-dropping-initial-zeros> SP <fname> NUL <binary HASH>
//
// We don't attempt to convert the 20-byte SHA to 40-byte SHA to save a lot of time
func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fname, sha []byte, n int, err error) {
// We don't attempt to convert the raw HASH to save a lot of time
func ParseTreeLine(hash HashType, rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fname, sha []byte, n int, err error) {
var readBytes []byte

// Read the Mode & fname
Expand Down Expand Up @@ -324,11 +323,12 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
fnameBuf = fnameBuf[:len(fnameBuf)-1]
fname = fnameBuf

// Deal with the 20-byte SHA
// Deal with the binary hash
idx = 0
for idx < 20 {
hashLen := hash.FullLength() / 2
for idx < hashLen {
var read int
read, err = rd.Read(shaBuf[idx:20])
read, err = rd.Read(shaBuf[idx:hashLen])
n += read
if err != nil {
return mode, fname, sha, n, err
Expand Down
22 changes: 12 additions & 10 deletions modules/git/blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"os"
"regexp"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
Expand All @@ -30,14 +29,13 @@ type BlameReader struct {
done chan error
lastSha *string
ignoreRevsFile *string
hash HashType
}

func (r *BlameReader) UsesIgnoreRevs() bool {
return r.ignoreRevsFile != nil
}

var shaLineRegex = regexp.MustCompile("^([a-z0-9]{40})")

// NextPart returns next part of blame (sequential code lines with the same commit)
func (r *BlameReader) NextPart() (*BlamePart, error) {
var blamePart *BlamePart
Expand All @@ -61,16 +59,19 @@ func (r *BlameReader) NextPart() (*BlamePart, error) {
continue
}

lines := shaLineRegex.FindSubmatch(line)
if lines != nil {
sha1 := string(lines[1])
var hash string
hashLen := r.hash.FullLength()

if len(line) > hashLen && line[hashLen] == ' ' && r.hash.IsValid(string(line[0:hashLen])) {
hash = string(line[0:hashLen])
}
if len(hash) > 0 {
if blamePart == nil {
blamePart = &BlamePart{sha1, make([]string, 0)}
blamePart = &BlamePart{hash, make([]string, 0)}
}

if blamePart.Sha != sha1 {
r.lastSha = &sha1
if blamePart.Sha != hash {
r.lastSha = &hash
// need to munch to end of line...
for isPrefix {
_, isPrefix, err = r.bufferedReader.ReadLine()
Expand Down Expand Up @@ -113,7 +114,7 @@ func (r *BlameReader) Close() error {
}

// CreateBlameReader creates reader for given repository, commit and file
func CreateBlameReader(ctx context.Context, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) {
func CreateBlameReader(ctx context.Context, hash HashType, repoPath string, commit *Commit, file string, bypassBlameIgnore bool) (*BlameReader, error) {
var ignoreRevsFile *string
if CheckGitVersionAtLeast("2.23") == nil && !bypassBlameIgnore {
ignoreRevsFile = tryCreateBlameIgnoreRevsFile(commit)
Expand Down Expand Up @@ -162,6 +163,7 @@ func CreateBlameReader(ctx context.Context, repoPath string, commit *Commit, fil
bufferedReader: bufferedReader,
done: done,
ignoreRevsFile: ignoreRevsFile,
hash: hash,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions modules/git/blame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestReadingBlameOutput(t *testing.T) {
}

for _, bypass := range []bool{false, true} {
blameReader, err := CreateBlameReader(ctx, "./tests/repos/repo5_pulls", commit, "README.md", bypass)
blameReader, err := CreateBlameReader(ctx, &Sha1HashType{}, "./tests/repos/repo5_pulls", commit, "README.md", bypass)
assert.NoError(t, err)
assert.NotNil(t, blameReader)
defer blameReader.Close()
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestReadingBlameOutput(t *testing.T) {
commit, err := repo.GetCommit(c.CommitID)
assert.NoError(t, err)

blameReader, err := CreateBlameReader(ctx, "./tests/repos/repo6_blame", commit, "blame.txt", c.Bypass)
blameReader, err := CreateBlameReader(ctx, repo.Hash, "./tests/repos/repo6_blame", commit, "blame.txt", c.Bypass)
assert.NoError(t, err)
assert.NotNil(t, blameReader)
defer blameReader.Close()
Expand Down
2 changes: 1 addition & 1 deletion modules/git/blob_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// Blob represents a Git object.
type Blob struct {
ID SHA1
ID Hash

gogitEncodedObj plumbing.EncodedObject
name string
Expand Down
2 changes: 1 addition & 1 deletion modules/git/blob_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

// Blob represents a Git object.
type Blob struct {
ID SHA1
ID Hash

gotSize bool
size int64
Expand Down
Loading

0 comments on commit 44bd4fc

Please sign in to comment.