Skip to content

Commit

Permalink
raw file API: add support for shortened commit SHA
Browse files Browse the repository at this point in the history
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
  • Loading branch information
noerw committed Sep 29, 2021
1 parent d4fc683 commit df834a7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion modules/context/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
return
}
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
} else if len(refName) == 40 {
} else if len(refName) >= 7 && len(refName) <= 40 {
ctx.Repo.CommitID = refName
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
if err != nil {
Expand Down
10 changes: 4 additions & 6 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
if refName := getRefName(ctx, RepoRefTag); len(refName) > 0 {
return refName
}
// For legacy and API support only full commit sha
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) == 40 {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
return refName
}
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
return refName
Expand All @@ -710,7 +707,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
case RepoRefCommit:
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
hasShaLength := len(parts[0]) >= 7 && len(parts[0]) <= 40
if len(parts) > 1 && hasShaLength && ctx.Repo.GitRepo.IsCommitExist(parts[0]) {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
Expand Down

0 comments on commit df834a7

Please sign in to comment.