mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Gracefully handle bare repositories on API operations. (#1932)
Signed-off-by: Dennis Keitzel <github@pinshot.net>
This commit is contained in:
		@@ -39,7 +39,7 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
 | 
				
			|||||||
// GetBranch returns a branch by it's name
 | 
					// GetBranch returns a branch by it's name
 | 
				
			||||||
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
 | 
					func (repo *Repository) GetBranch(branch string) (*Branch, error) {
 | 
				
			||||||
	if !git.IsBranchExist(repo.RepoPath(), branch) {
 | 
						if !git.IsBranchExist(repo.RepoPath(), branch) {
 | 
				
			||||||
		return nil, &ErrBranchNotExist{branch}
 | 
							return nil, ErrBranchNotExist{branch}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &Branch{
 | 
						return &Branch{
 | 
				
			||||||
		Path: repo.RepoPath(),
 | 
							Path: repo.RepoPath(),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ package repo
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	api "code.gitea.io/sdk/gitea"
 | 
						api "code.gitea.io/sdk/gitea"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"code.gitea.io/gitea/models"
 | 
				
			||||||
	"code.gitea.io/gitea/modules/context"
 | 
						"code.gitea.io/gitea/modules/context"
 | 
				
			||||||
	"code.gitea.io/gitea/routers/api/v1/convert"
 | 
						"code.gitea.io/gitea/routers/api/v1/convert"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -16,7 +17,11 @@ import (
 | 
				
			|||||||
func GetBranch(ctx *context.APIContext) {
 | 
					func GetBranch(ctx *context.APIContext) {
 | 
				
			||||||
	branch, err := ctx.Repo.Repository.GetBranch(ctx.Params(":branchname"))
 | 
						branch, err := ctx.Repo.Repository.GetBranch(ctx.Params(":branchname"))
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 | 
							if models.IsErrBranchNotExist(err) {
 | 
				
			||||||
 | 
								ctx.Error(404, "GetBranch", err)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
			ctx.Error(500, "GetBranch", err)
 | 
								ctx.Error(500, "GetBranch", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,11 @@ func GetRawFile(ctx *context.APIContext) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if ctx.Repo.Repository.IsBare {
 | 
				
			||||||
 | 
							ctx.Status(404)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
 | 
						blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		if git.IsErrNotExist(err) {
 | 
							if git.IsErrNotExist(err) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user