diff options
author | Jason Song <i@wolfogre.com> | 2022-12-27 21:12:49 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-27 21:12:49 +0800 |
commit | 6cf09ccab402fe84a5313e1d3e755e284ebbc845 (patch) | |
tree | b1fad807c2a16fded8b6bda84583c421ec86230c /services | |
parent | 90237d8abd0e6479c1464ac0f32fff6a2ce4a0b4 (diff) | |
download | gitea-6cf09ccab402fe84a5313e1d3e755e284ebbc845.tar.gz gitea-6cf09ccab402fe84a5313e1d3e755e284ebbc845.zip |
Use complete SHA to create and query commit status (#22244)
Fix #13485.
Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services')
-rw-r--r-- | services/pull/check.go | 12 | ||||
-rw-r--r-- | services/pull/merge.go | 2 | ||||
-rw-r--r-- | services/pull/temp_repo.go | 2 | ||||
-rw-r--r-- | services/repository/files/commit.go | 5 | ||||
-rw-r--r-- | services/repository/files/tree.go | 2 |
5 files changed, 13 insertions, 10 deletions
diff --git a/services/pull/check.go b/services/pull/check.go index ed4b18107c..86460cd49c 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -199,19 +199,19 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com return nil, fmt.Errorf("ReadFile(%s): %w", headFile, err) } commitID := string(commitIDBytes) - if len(commitID) < 40 { + if len(commitID) < git.SHAFullLength { return nil, fmt.Errorf(`ReadFile(%s): invalid commit-ID "%s"`, headFile, commitID) } - cmd := commitID[:40] + ".." + pr.BaseBranch + cmd := commitID[:git.SHAFullLength] + ".." + pr.BaseBranch // Get the commit from BaseBranch where the pull request got merged mergeCommit, _, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse").AddDynamicArguments(cmd). RunStdString(&git.RunOpts{Dir: "", Env: []string{"GIT_INDEX_FILE=" + indexTmpPath, "GIT_DIR=" + pr.BaseRepo.RepoPath()}}) if err != nil { return nil, fmt.Errorf("git rev-list --ancestry-path --merges --reverse: %w", err) - } else if len(mergeCommit) < 40 { + } else if len(mergeCommit) < git.SHAFullLength { // PR was maybe fast-forwarded, so just use last commit of PR - mergeCommit = commitID[:40] + mergeCommit = commitID[:git.SHAFullLength] } gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) @@ -220,9 +220,9 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com } defer gitRepo.Close() - commit, err := gitRepo.GetCommit(mergeCommit[:40]) + commit, err := gitRepo.GetCommit(mergeCommit[:git.SHAFullLength]) if err != nil { - return nil, fmt.Errorf("GetMergeCommit[%v]: %w", mergeCommit[:40], err) + return nil, fmt.Errorf("GetMergeCommit[%v]: %w", mergeCommit[:git.SHAFullLength], err) } return commit, nil diff --git a/services/pull/merge.go b/services/pull/merge.go index 1c42c1c17b..41ba45c177 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -839,7 +839,7 @@ func MergedManually(pr *issues_model.PullRequest, doer *user_model.User, baseGit return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: repo_model.MergeStyleManuallyMerged} } - if len(commitID) < 40 { + if len(commitID) < git.SHAFullLength { return fmt.Errorf("Wrong commit ID") } diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index 842719467f..d49a15cea0 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -166,7 +166,7 @@ func createTemporaryRepo(ctx context.Context, pr *issues_model.PullRequest) (str var headBranch string if pr.Flow == issues_model.PullRequestFlowGithub { headBranch = git.BranchPrefix + pr.HeadBranch - } else if len(pr.HeadCommitID) == 40 { // for not created pull request + } else if len(pr.HeadCommitID) == git.SHAFullLength { // for not created pull request headBranch = pr.HeadCommitID } else { headBranch = pr.GetGitRefName() diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index c49b03d608..74f9eb868d 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -29,9 +29,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato } defer closer.Close() - if _, err := gitRepo.GetCommit(sha); err != nil { + if commit, err := gitRepo.GetCommit(sha); err != nil { gitRepo.Close() return fmt.Errorf("GetCommit[%s]: %w", sha, err) + } else if len(sha) != git.SHAFullLength { + // use complete commit sha + sha = commit.ID.String() } gitRepo.Close() diff --git a/services/repository/files/tree.go b/services/repository/files/tree.go index 1aa6d0df36..f4304ea630 100644 --- a/services/repository/files/tree.go +++ b/services/repository/files/tree.go @@ -49,7 +49,7 @@ func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git copy(treeURL[apiURLLen:], "/git/trees/") // 40 is the size of the sha1 hash in hexadecimal format. - copyPos := len(treeURL) - 40 + copyPos := len(treeURL) - git.SHAFullLength if perPage <= 0 || perPage > setting.API.DefaultGitTreesPerPage { perPage = setting.API.DefaultGitTreesPerPage |