Skip to content

Support shortened commit SHAs in URLs (#6450) #6452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,10 @@ const (
RepoRefBranch
// RepoRefTag tag
RepoRefTag
// RepoRefCommit commit
// RepoRefCommit short or long commit
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add a value in the middle of the enums.

RepoRefCommit
// RepoRefFullCommit long commit only
RepoRefFullCommit
// RepoRefBlob blob
RepoRefBlob
)
Expand Down Expand Up @@ -527,7 +529,7 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
if refName := getRefName(ctx, RepoRefTag); len(refName) > 0 {
return refName
}
if refName := getRefName(ctx, RepoRefCommit); len(refName) > 0 {
if refName := getRefName(ctx, RepoRefFullCommit); len(refName) > 0 {
return refName
}
if refName := getRefName(ctx, RepoRefBlob); len(refName) > 0 {
Expand All @@ -539,12 +541,18 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
case RepoRefTag:
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
case RepoRefCommit:
case RepoRefFullCommit:
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) == 40 {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
case RepoRefCommit:
parts := strings.Split(path, "/")
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= 40 {
ctx.Repo.TreePath = strings.Join(parts[1:], "/")
return parts[0]
}
case RepoRefBlob:
_, err := ctx.Repo.GitRepo.GetBlob(path)
if err != nil {
Expand Down Expand Up @@ -627,7 +635,7 @@ func RepoRefByType(refType RepoRefType) macaron.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.IsViewCommit = true
ctx.Repo.CommitID = refName

Expand Down