mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Consolidate API for getting single commit (#11368)
* Allow Git ref for /repos/{owner}/{repo}/git/commits/{sha}
* Consolidate API for getting single commit
* Fix tests and do it differently
Co-authored-by: zeripath <art27@cantab.net>
			
			
This commit is contained in:
		
				
					committed by
					
						
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							59b9b77a0d
						
					
				
				
					commit
					32b8172e56
				
			@@ -817,14 +817,13 @@ func RegisterRoutes(m *macaron.Macaron) {
 | 
			
		||||
				m.Group("/commits", func() {
 | 
			
		||||
					m.Get("", repo.GetAllCommits)
 | 
			
		||||
					m.Group("/:ref", func() {
 | 
			
		||||
						m.Get("", repo.GetSingleCommitByRef)
 | 
			
		||||
						m.Get("/status", repo.GetCombinedCommitStatusByRef)
 | 
			
		||||
						m.Get("/statuses", repo.GetCommitStatusesByRef)
 | 
			
		||||
					})
 | 
			
		||||
				}, reqRepoReader(models.UnitTypeCode))
 | 
			
		||||
				m.Group("/git", func() {
 | 
			
		||||
					m.Group("/commits", func() {
 | 
			
		||||
						m.Get("/:sha", repo.GetSingleCommitBySHA)
 | 
			
		||||
						m.Get("/:sha", repo.GetSingleCommit)
 | 
			
		||||
					})
 | 
			
		||||
					m.Get("/refs", repo.GetGitAllRefs)
 | 
			
		||||
					m.Get("/refs/*", repo.GetGitRefs)
 | 
			
		||||
 
 | 
			
		||||
@@ -21,9 +21,9 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/routers/api/v1/utils"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GetSingleCommitBySHA get a commit via sha
 | 
			
		||||
func GetSingleCommitBySHA(ctx *context.APIContext) {
 | 
			
		||||
	// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommitBySHA
 | 
			
		||||
// GetSingleCommit get a commit via sha
 | 
			
		||||
func GetSingleCommit(ctx *context.APIContext) {
 | 
			
		||||
	// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
 | 
			
		||||
	// ---
 | 
			
		||||
	// summary: Get a single commit from a repository
 | 
			
		||||
	// produces:
 | 
			
		||||
@@ -41,7 +41,7 @@ func GetSingleCommitBySHA(ctx *context.APIContext) {
 | 
			
		||||
	//   required: true
 | 
			
		||||
	// - name: sha
 | 
			
		||||
	//   in: path
 | 
			
		||||
	//   description: the commit hash
 | 
			
		||||
	//   description: a git ref or commit sha
 | 
			
		||||
	//   type: string
 | 
			
		||||
	//   required: true
 | 
			
		||||
	// responses:
 | 
			
		||||
@@ -53,54 +53,13 @@ func GetSingleCommitBySHA(ctx *context.APIContext) {
 | 
			
		||||
	//     "$ref": "#/responses/notFound"
 | 
			
		||||
 | 
			
		||||
	sha := ctx.Params(":sha")
 | 
			
		||||
	if !git.SHAPattern.MatchString(sha) {
 | 
			
		||||
		ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid sha: %s", sha))
 | 
			
		||||
	if (validation.GitRefNamePatternInvalid.MatchString(sha) || !validation.CheckGitRefAdditionalRulesValid(sha)) && !git.SHAPattern.MatchString(sha) {
 | 
			
		||||
		ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	getCommit(ctx, sha)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetSingleCommitByRef get a commit via ref
 | 
			
		||||
func GetSingleCommitByRef(ctx *context.APIContext) {
 | 
			
		||||
	// swagger:operation GET /repos/{owner}/{repo}/commits/{ref} repository repoGetSingleCommitByRef
 | 
			
		||||
	// ---
 | 
			
		||||
	// summary: Get a single commit from a repository
 | 
			
		||||
	// produces:
 | 
			
		||||
	// - application/json
 | 
			
		||||
	// parameters:
 | 
			
		||||
	// - name: owner
 | 
			
		||||
	//   in: path
 | 
			
		||||
	//   description: owner of the repo
 | 
			
		||||
	//   type: string
 | 
			
		||||
	//   required: true
 | 
			
		||||
	// - name: repo
 | 
			
		||||
	//   in: path
 | 
			
		||||
	//   description: name of the repo
 | 
			
		||||
	//   type: string
 | 
			
		||||
	//   required: true
 | 
			
		||||
	// - name: ref
 | 
			
		||||
	//   in: path
 | 
			
		||||
	//   description: a git ref
 | 
			
		||||
	//   type: string
 | 
			
		||||
	//   required: true
 | 
			
		||||
	// responses:
 | 
			
		||||
	//   "200":
 | 
			
		||||
	//     "$ref": "#/responses/Commit"
 | 
			
		||||
	//   "422":
 | 
			
		||||
	//     "$ref": "#/responses/validationError"
 | 
			
		||||
	//   "404":
 | 
			
		||||
	//     "$ref": "#/responses/notFound"
 | 
			
		||||
 | 
			
		||||
	ref := ctx.Params("ref")
 | 
			
		||||
 | 
			
		||||
	if validation.GitRefNamePatternInvalid.MatchString(ref) || !validation.CheckGitRefAdditionalRulesValid(ref) {
 | 
			
		||||
		ctx.Error(http.StatusUnprocessableEntity, "no valid sha", fmt.Sprintf("no valid ref: %s", ref))
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	getCommit(ctx, ref)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getCommit(ctx *context.APIContext, identifier string) {
 | 
			
		||||
	gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
 | 
			
		||||
	if err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user