mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-30 19:08:37 +00:00 
			
		
		
		
	Use complete SHA to create and query commit status (#22244)
Fix #13485. Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
		| @@ -33,6 +33,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 { | ||||
| @@ -65,3 +67,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() | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user