Skip to content

Commit df834a7

Browse files
committed
raw file API: add support for shortened commit SHA
we just deprecated this API, but this is a short fix to #17179 A more minimal fix would be to check if len(parts) > 1 in line context/repo.go#L698
1 parent d4fc683 commit df834a7

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

modules/context/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
413413
return
414414
}
415415
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
416-
} else if len(refName) == 40 {
416+
} else if len(refName) >= 7 && len(refName) <= 40 {
417417
ctx.Repo.CommitID = refName
418418
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
419419
if err != nil {

modules/context/repo.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
693693
if refName := getRefName(ctx, RepoRefTag); len(refName) > 0 {
694694
return refName
695695
}
696-
// For legacy and API support only full commit sha
697-
parts := strings.Split(path, "/")
698-
if len(parts) > 0 && len(parts[0]) == 40 {
699-
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
700-
return parts[0]
696+
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
697+
return refName
701698
}
702699
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
703700
return refName
@@ -710,7 +707,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
710707
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
711708
case RepoRefCommit:
712709
parts := strings.Split(path, "/")
713-
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
710+
hasShaLength := len(parts[0]) >= 7 && len(parts[0]) <= 40
711+
if len(parts) > 1 && hasShaLength && ctx.Repo.GitRepo.IsCommitExist(parts[0]) {
714712
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
715713
return parts[0]
716714
}

0 commit comments

Comments
 (0)