Skip to content

Commit e9bc2c7

Browse files
wolfogredelvhlafrikslunny
authored
Use complete SHA to create and query commit status (#22244) (#22257)
Backport #22244. Fix #13485. Co-authored-by: delvh <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: delvh <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent 9b4da56 commit e9bc2c7

File tree

19 files changed

+68
-23
lines changed

19 files changed

+68
-23
lines changed

models/activities/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (a *Action) GetRefLink() string {
272272
return a.GetRepoLink() + "/src/branch/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.BranchPrefix))
273273
case strings.HasPrefix(a.RefName, git.TagPrefix):
274274
return a.GetRepoLink() + "/src/tag/" + util.PathEscapeSegments(strings.TrimPrefix(a.RefName, git.TagPrefix))
275-
case len(a.RefName) == 40 && git.IsValidSHAPattern(a.RefName):
275+
case len(a.RefName) == git.SHAFullLength && git.IsValidSHAPattern(a.RefName):
276276
return a.GetRepoLink() + "/src/commit/" + a.RefName
277277
default:
278278
// FIXME: we will just assume it's a branch - this was the old way - at some point we may want to enforce that there is always a ref here.

models/git/commit_status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,10 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
281281
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
282282
}
283283

284+
if _, err := git.NewIDFromString(opts.SHA); err != nil {
285+
return fmt.Errorf("NewCommitStatus[%s, %s]: invalid sha: %w", repoPath, opts.SHA, err)
286+
}
287+
284288
ctx, committer, err := db.TxContext()
285289
if err != nil {
286290
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", opts.Repo.ID, opts.Creator.ID, opts.SHA, err)

modules/context/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
388388
return
389389
}
390390
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
391-
} else if len(refName) == 40 {
391+
} else if len(refName) == git.SHAFullLength {
392392
ctx.Repo.CommitID = refName
393393
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
394394
if err != nil {

modules/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
816816
}
817817
// For legacy and API support only full commit sha
818818
parts := strings.Split(path, "/")
819-
if len(parts) > 0 && len(parts[0]) == 40 {
819+
if len(parts) > 0 && len(parts[0]) == git.SHAFullLength {
820820
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
821821
return parts[0]
822822
}
@@ -852,7 +852,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
852852
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
853853
case RepoRefCommit:
854854
parts := strings.Split(path, "/")
855-
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
855+
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
856856
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
857857
return parts[0]
858858
}
@@ -961,7 +961,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
961961
return
962962
}
963963
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
964-
} else if len(refName) >= 7 && len(refName) <= 40 {
964+
} else if len(refName) >= 7 && len(refName) <= git.SHAFullLength {
965965
ctx.Repo.IsViewCommit = true
966966
ctx.Repo.CommitID = refName
967967

@@ -971,7 +971,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
971971
return
972972
}
973973
// If short commit ID add canonical link header
974-
if len(refName) < 40 {
974+
if len(refName) < git.SHAFullLength {
975975
ctx.RespHeader().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
976976
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
977977
}

modules/git/repo_commit_gogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (repo *Repository) RemoveReference(name string) error {
4242

4343
// ConvertToSHA1 returns a Hash object from a potential ID string
4444
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
45-
if len(commitID) == 40 {
45+
if len(commitID) == SHAFullLength {
4646
sha1, err := NewIDFromString(commitID)
4747
if err == nil {
4848
return sha1, nil

modules/git/repo_commit_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
138138

139139
// ConvertToSHA1 returns a Hash object from a potential ID string
140140
func (repo *Repository) ConvertToSHA1(commitID string) (SHA1, error) {
141-
if len(commitID) == 40 && IsValidSHAPattern(commitID) {
141+
if len(commitID) == SHAFullLength && IsValidSHAPattern(commitID) {
142142
sha1, err := NewIDFromString(commitID)
143143
if err == nil {
144144
return sha1, nil

modules/git/repo_index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
// ReadTreeToIndex reads a treeish to the index
1919
func (repo *Repository) ReadTreeToIndex(treeish string, indexFilename ...string) error {
20-
if len(treeish) != 40 {
20+
if len(treeish) != SHAFullLength {
2121
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(treeish).RunStdString(&RunOpts{Dir: repo.Path})
2222
if err != nil {
2323
return err

modules/git/repo_tree_gogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
2020

2121
// GetTree find the tree object in the repository.
2222
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
23-
if len(idStr) != 40 {
23+
if len(idStr) != SHAFullLength {
2424
res, _, err := NewCommand(repo.Ctx, "rev-parse", "--verify").AddDynamicArguments(idStr).RunStdString(&RunOpts{Dir: repo.Path})
2525
if err != nil {
2626
return nil, err

modules/git/repo_tree_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
6767

6868
// GetTree find the tree object in the repository.
6969
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
70-
if len(idStr) != 40 {
70+
if len(idStr) != SHAFullLength {
7171
res, err := repo.GetRefCommitID(idStr)
7272
if err != nil {
7373
return nil, err

modules/git/sha1.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const EmptySHA = "0000000000000000000000000000000000000000"
1818
// EmptyTreeSHA is the SHA of an empty tree
1919
const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
2020

21+
// SHAFullLength is the full length of a git SHA
22+
const SHAFullLength = 40
23+
2124
// SHAPattern can be used to determine if a string is an valid sha
2225
var shaPattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
2326

@@ -51,7 +54,7 @@ func MustIDFromString(s string) SHA1 {
5154
func NewIDFromString(s string) (SHA1, error) {
5255
var id SHA1
5356
s = strings.TrimSpace(s)
54-
if len(s) != 40 {
57+
if len(s) != SHAFullLength {
5558
return id, fmt.Errorf("Length must be 40: %s", s)
5659
}
5760
b, err := hex.DecodeString(s)

0 commit comments

Comments
 (0)