Skip to content

Commit

Permalink
Support shortened commit SHAs in URLs (go-gitea#6450)
Browse files Browse the repository at this point in the history
This supports using a git SHA of any length between 7 and 40
characters in URLs (e.g. /src/commit/SHA/...).  Previously
only commit SHAs of the full 40 character length were supported.

The RepoRefAny ref type is used in one place, in the
/api/v1/user/repo/raw API endpoint where it is used to guess whether
the remainder of the path is a ref name followed by a file path
or merely a file path.  There is no good way to guess whether
a shortened SHA is intended in that circumstance (e.g.,
/raw/beefcafe/README.txt could be /README.txt in the beefcafe{..32}
commit, or /beefcafe/README.txt on the master branch).  For this
case, we don't support shortened SHAs and only match on the full
one.

Signed-off-by: James E. Blair <jeblair@redhat.com>
  • Loading branch information
James E. Blair committed Mar 28, 2019
1 parent 2019983 commit 341f624
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,10 @@ const (
RepoRefBranch
// RepoRefTag tag
RepoRefTag
// RepoRefCommit commit
// RepoRefCommit short or long commit
RepoRefCommit
// RepoRefFullCommit long commit only
RepoRefFullCommit
// RepoRefBlob blob
RepoRefBlob
)
Expand Down Expand Up @@ -511,7 +513,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 @@ -523,12 +525,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 @@ -611,7 +619,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

0 comments on commit 341f624

Please sign in to comment.