mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-01 12:22:11 +00:00
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:
parent
064f05204c
commit
cbf923e87b
122 changed files with 947 additions and 594 deletions
|
@ -215,24 +215,29 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
|
|||
return nil, fmt.Errorf("GetFullCommitID(%s) in %s: %w", prHeadRef, pr.BaseRepo.FullName(), err)
|
||||
}
|
||||
|
||||
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%-v OpenRepository: %w", pr.BaseRepo, err)
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
objectFormat, err := gitRepo.GetObjectFormat()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%-v GetObjectFormat: %w", pr.BaseRepo, err)
|
||||
}
|
||||
|
||||
// Get the commit from BaseBranch where the pull request got merged
|
||||
mergeCommit, _, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse").
|
||||
AddDynamicArguments(prHeadCommitID + ".." + pr.BaseBranch).
|
||||
RunStdString(&git.RunOpts{Dir: pr.BaseRepo.RepoPath()})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err)
|
||||
} else if len(mergeCommit) < git.SHAFullLength {
|
||||
} else if len(mergeCommit) < objectFormat.FullLength() {
|
||||
// PR was maybe fast-forwarded, so just use last commit of PR
|
||||
mergeCommit = prHeadCommitID
|
||||
}
|
||||
mergeCommit = strings.TrimSpace(mergeCommit)
|
||||
|
||||
gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%-v OpenRepository: %w", pr.BaseRepo, err)
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
commit, err := gitRepo.GetCommit(mergeCommit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetMergeCommit[%s]: %w", mergeCommit, err)
|
||||
|
|
|
@ -486,7 +486,8 @@ func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *use
|
|||
return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: repo_model.MergeStyleManuallyMerged}
|
||||
}
|
||||
|
||||
if len(commitID) < git.SHAFullLength {
|
||||
objectFormat, _ := baseGitRepo.GetObjectFormat()
|
||||
if len(commitID) != objectFormat.FullLength() {
|
||||
return fmt.Errorf("Wrong commit ID")
|
||||
}
|
||||
|
||||
|
|
|
@ -129,6 +129,9 @@ func (e *errMergeConflict) Error() string {
|
|||
|
||||
func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, gitRepo *git.Repository) error {
|
||||
log.Trace("Attempt to merge:\n%v", file)
|
||||
|
||||
objectFormat, _ := gitRepo.GetObjectFormat()
|
||||
|
||||
switch {
|
||||
case file.stage1 != nil && (file.stage2 == nil || file.stage3 == nil):
|
||||
// 1. Deleted in one or both:
|
||||
|
@ -145,7 +148,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
|
|||
// 2. Added in ours but not in theirs or identical in both
|
||||
//
|
||||
// Not a genuine conflict just add to the index
|
||||
if err := gitRepo.AddObjectToIndex(file.stage2.mode, git.MustIDFromString(file.stage2.sha), file.stage2.path); err != nil {
|
||||
if err := gitRepo.AddObjectToIndex(file.stage2.mode, objectFormat.MustIDFromString(file.stage2.sha), file.stage2.path); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
@ -158,7 +161,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
|
|||
// 4. Added in theirs but not ours:
|
||||
//
|
||||
// Not a genuine conflict just add to the index
|
||||
return gitRepo.AddObjectToIndex(file.stage3.mode, git.MustIDFromString(file.stage3.sha), file.stage3.path)
|
||||
return gitRepo.AddObjectToIndex(file.stage3.mode, objectFormat.MustIDFromString(file.stage3.sha), file.stage3.path)
|
||||
case file.stage1 == nil:
|
||||
// 5. Created by new in both
|
||||
//
|
||||
|
@ -219,7 +222,7 @@ func attemptMerge(ctx context.Context, file *unmergedFile, tmpBasePath string, g
|
|||
return err
|
||||
}
|
||||
hash = strings.TrimSpace(hash)
|
||||
return gitRepo.AddObjectToIndex(file.stage2.mode, git.MustIDFromString(hash), file.stage2.path)
|
||||
return gitRepo.AddObjectToIndex(file.stage2.mode, objectFormat.MustIDFromString(hash), file.stage2.path)
|
||||
default:
|
||||
if file.stage1 != nil {
|
||||
return &errMergeConflict{file.stage1.path}
|
||||
|
|
|
@ -327,7 +327,8 @@ func AddTestPullRequestTask(doer *user_model.User, repoID int64, branch string,
|
|||
}
|
||||
if err == nil {
|
||||
for _, pr := range prs {
|
||||
if newCommitID != "" && newCommitID != git.EmptySHA {
|
||||
objectFormat, _ := git.GetObjectFormatOfRepo(ctx, pr.BaseRepo.RepoPath())
|
||||
if newCommitID != "" && newCommitID != objectFormat.Empty().String() {
|
||||
changed, err := checkIfPRContentChanged(ctx, pr, oldCommitID, newCommitID)
|
||||
if err != nil {
|
||||
log.Error("checkIfPRContentChanged: %v", err)
|
||||
|
|
|
@ -93,8 +93,14 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
|||
|
||||
baseRepoPath := pr.BaseRepo.RepoPath()
|
||||
headRepoPath := pr.HeadRepo.RepoPath()
|
||||
objectFormat, err := git.GetObjectFormatOfRepo(ctx, baseRepoPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to fetch ObjectFormat of repository %s: %v", baseRepoPath, err)
|
||||
cancel()
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if err := git.InitRepository(ctx, tmpBasePath, false); err != nil {
|
||||
if err := git.InitRepository(ctx, tmpBasePath, false, objectFormat); err != nil {
|
||||
log.Error("Unable to init tmpBasePath for %-v: %v", pr, err)
|
||||
cancel()
|
||||
return nil, nil, err
|
||||
|
@ -172,7 +178,7 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
|
|||
var headBranch string
|
||||
if pr.Flow == issues_model.PullRequestFlowGithub {
|
||||
headBranch = git.BranchPrefix + pr.HeadBranch
|
||||
} else if len(pr.HeadCommitID) == git.SHAFullLength { // for not created pull request
|
||||
} else if len(pr.HeadCommitID) == objectFormat.FullLength() { // for not created pull request
|
||||
headBranch = pr.HeadCommitID
|
||||
} else {
|
||||
headBranch = pr.GetGitRefName()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue