Skip to content

Commit 96b4780

Browse files
cybeappleboy
authored andcommitted
Gracefully handle bare repositories on API operations. (#1932)
Signed-off-by: Dennis Keitzel <[email protected]>
1 parent f2fcd9d commit 96b4780

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

models/repo_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
3939
// GetBranch returns a branch by it's name
4040
func (repo *Repository) GetBranch(branch string) (*Branch, error) {
4141
if !git.IsBranchExist(repo.RepoPath(), branch) {
42-
return nil, &ErrBranchNotExist{branch}
42+
return nil, ErrBranchNotExist{branch}
4343
}
4444
return &Branch{
4545
Path: repo.RepoPath(),

routers/api/v1/repo/branch.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package repo
77
import (
88
api "code.gitea.io/sdk/gitea"
99

10+
"code.gitea.io/gitea/models"
1011
"code.gitea.io/gitea/modules/context"
1112
"code.gitea.io/gitea/routers/api/v1/convert"
1213
)
@@ -16,7 +17,11 @@ import (
1617
func GetBranch(ctx *context.APIContext) {
1718
branch, err := ctx.Repo.Repository.GetBranch(ctx.Params(":branchname"))
1819
if err != nil {
19-
ctx.Error(500, "GetBranch", err)
20+
if models.IsErrBranchNotExist(err) {
21+
ctx.Error(404, "GetBranch", err)
22+
} else {
23+
ctx.Error(500, "GetBranch", err)
24+
}
2025
return
2126
}
2227

routers/api/v1/repo/file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ func GetRawFile(ctx *context.APIContext) {
2020
return
2121
}
2222

23+
if ctx.Repo.Repository.IsBare {
24+
ctx.Status(404)
25+
return
26+
}
27+
2328
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
2429
if err != nil {
2530
if git.IsErrNotExist(err) {

0 commit comments

Comments
 (0)