Skip to content

Commit 6cf09cc

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

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
@@ -279,6 +279,10 @@ func NewCommitStatus(opts NewCommitStatusOptions) error {
279279
return fmt.Errorf("NewCommitStatus[%s, %s]: no user specified", repoPath, opts.SHA)
280280
}
281281

282+
if _, err := git.NewIDFromString(opts.SHA); err != nil {
283+
return fmt.Errorf("NewCommitStatus[%s, %s]: invalid sha: %w", repoPath, opts.SHA, err)
284+
}
285+
282286
ctx, committer, err := db.TxContext(db.DefaultContext)
283287
if err != nil {
284288
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
@@ -387,7 +387,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
387387
return
388388
}
389389
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
390-
} else if len(refName) == 40 {
390+
} else if len(refName) == git.SHAFullLength {
391391
ctx.Repo.CommitID = refName
392392
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
393393
if err != nil {

modules/context/repo.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
817817
}
818818
// For legacy and API support only full commit sha
819819
parts := strings.Split(path, "/")
820-
if len(parts) > 0 && len(parts[0]) == 40 {
820+
if len(parts) > 0 && len(parts[0]) == git.SHAFullLength {
821821
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
822822
return parts[0]
823823
}
@@ -853,7 +853,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
853853
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
854854
case RepoRefCommit:
855855
parts := strings.Split(path, "/")
856-
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
856+
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= git.SHAFullLength {
857857
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
858858
return parts[0]
859859
}
@@ -962,7 +962,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
962962
return
963963
}
964964
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
965-
} else if len(refName) >= 7 && len(refName) <= 40 {
965+
} else if len(refName) >= 7 && len(refName) <= git.SHAFullLength {
966966
ctx.Repo.IsViewCommit = true
967967
ctx.Repo.CommitID = refName
968968

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

modules/git/repo_commit_gogit.go

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

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

modules/git/repo_commit_nogogit.go

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

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

modules/git/repo_index.go

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

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

modules/git/repo_tree_gogit.go

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

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

modules/git/repo_tree_nogogit.go

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

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

modules/git/sha1.go

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

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

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

0 commit comments

Comments
 (0)