summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/status.go1
-rw-r--r--routers/api/v1/utils/git.go29
-rw-r--r--routers/web/repo/commit.go2
3 files changed, 31 insertions, 1 deletions
diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go
index 97ef69a6ea..7bc0edeadc 100644
--- a/routers/api/v1/repo/status.go
+++ b/routers/api/v1/repo/status.go
@@ -184,6 +184,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
ctx.Error(http.StatusBadRequest, "ref/sha not given", nil)
return
}
+ sha = utils.MustConvertToSHA1(ctx.Context, sha)
repo := ctx.Repo.Repository
listOptions := utils.GetListOptions(ctx)
diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go
index 816f8b3595..d05a86c0cd 100644
--- a/routers/api/v1/utils/git.go
+++ b/routers/api/v1/utils/git.go
@@ -34,6 +34,8 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string {
}
}
+ sha = MustConvertToSHA1(ctx.Context, sha)
+
if ctx.Repo.GitRepo != nil {
err := ctx.Repo.GitRepo.AddLastCommitCache(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, ref != sha), ctx.Repo.Repository.FullName(), sha)
if err != nil {
@@ -66,3 +68,30 @@ func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (str
}
return "", "", nil
}
+
+// ConvertToSHA1 returns a full-length SHA1 from a potential ID string
+func ConvertToSHA1(ctx *context.Context, commitID string) (git.SHA1, error) {
+ if len(commitID) == git.SHAFullLength && git.IsValidSHAPattern(commitID) {
+ sha1, err := git.NewIDFromString(commitID)
+ if err == nil {
+ return sha1, nil
+ }
+ }
+
+ gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository.RepoPath())
+ if err != nil {
+ return git.SHA1{}, fmt.Errorf("RepositoryFromContextOrOpen: %w", err)
+ }
+ defer closer.Close()
+
+ return gitRepo.ConvertToSHA1(commitID)
+}
+
+// MustConvertToSHA1 returns a full-length SHA1 string from a potential ID string, or returns origin input if it can't convert to SHA1
+func MustConvertToSHA1(ctx *context.Context, commitID string) string {
+ sha, err := ConvertToSHA1(ctx, commitID)
+ if err != nil {
+ return commitID
+ }
+ return sha.String()
+}
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go
index a6553256b0..b56cf928cd 100644
--- a/routers/web/repo/commit.go
+++ b/routers/web/repo/commit.go
@@ -284,7 +284,7 @@ func Diff(ctx *context.Context) {
}
return
}
- if len(commitID) != 40 {
+ if len(commitID) != git.SHAFullLength {
commitID = commit.ID.String()
}