Skip to content

Commit 56a0a9c

Browse files
authored
Fix git empty check and HEAD request (#33690)
1 parent 8ae46d9 commit 56a0a9c

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

routers/web/repo/githttp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func httpBase(ctx *context.Context) *serviceHandler {
7878
strings.HasSuffix(ctx.Req.URL.Path, "git-upload-archive") {
7979
isPull = true
8080
} else {
81-
isPull = ctx.Req.Method == "GET"
81+
isPull = ctx.Req.Method == "HEAD" || ctx.Req.Method == "GET"
8282
}
8383

8484
var accessMode perm.AccessMode

services/context/api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ func RepoRefForAPI(next http.Handler) http.Handler {
291291
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
292292
ctx := GetAPIContext(req)
293293

294+
if ctx.Repo.Repository.IsEmpty {
295+
ctx.APIErrorNotFound("repository is empty")
296+
return
297+
}
298+
294299
if ctx.Repo.GitRepo == nil {
295300
ctx.APIErrorInternal(fmt.Errorf("no open git repo"))
296301
return

tests/integration/empty_repo_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ func TestEmptyRepoAddFile(t *testing.T) {
6060
defer tests.PrepareTestEnv(t)()
6161

6262
session := loginUser(t, "user30")
63+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository)
64+
65+
// test web page
6366
req := NewRequest(t, "GET", "/user30/empty")
6467
resp := session.MakeRequest(t, req, http.StatusOK)
6568
bodyString := resp.Body.String()
6669
assert.Contains(t, bodyString, "empty-repo-guide")
6770
assert.True(t, test.IsNormalPageCompleted(bodyString))
6871

72+
// test api
73+
req = NewRequest(t, "GET", "/api/v1/repos/user30/empty/raw/main/README.md").AddTokenAuth(token)
74+
session.MakeRequest(t, req, http.StatusNotFound)
75+
76+
// create a new file
6977
req = NewRequest(t, "GET", "/user30/empty/_new/"+setting.Repository.DefaultBranch)
7078
resp = session.MakeRequest(t, req, http.StatusOK)
7179
doc := NewHTMLParser(t, resp.Body).Find(`input[name="commit_choice"]`)

tests/integration/git_smart_http_test.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import (
99
"net/url"
1010
"testing"
1111

12+
"code.gitea.io/gitea/modules/util"
13+
1214
"github.com/stretchr/testify/assert"
15+
"github.com/stretchr/testify/require"
1316
)
1417

1518
func TestGitSmartHTTP(t *testing.T) {
@@ -18,51 +21,55 @@ func TestGitSmartHTTP(t *testing.T) {
1821

1922
func testGitSmartHTTP(t *testing.T, u *url.URL) {
2023
kases := []struct {
21-
p string
22-
code int
24+
method, path string
25+
code int
2326
}{
2427
{
25-
p: "user2/repo1/info/refs",
28+
path: "user2/repo1/info/refs",
2629
code: http.StatusOK,
2730
},
2831
{
29-
p: "user2/repo1/HEAD",
32+
method: "HEAD",
33+
path: "user2/repo1/info/refs",
34+
code: http.StatusOK,
35+
},
36+
{
37+
path: "user2/repo1/HEAD",
3038
code: http.StatusOK,
3139
},
3240
{
33-
p: "user2/repo1/objects/info/alternates",
41+
path: "user2/repo1/objects/info/alternates",
3442
code: http.StatusNotFound,
3543
},
3644
{
37-
p: "user2/repo1/objects/info/http-alternates",
45+
path: "user2/repo1/objects/info/http-alternates",
3846
code: http.StatusNotFound,
3947
},
4048
{
41-
p: "user2/repo1/../../custom/conf/app.ini",
49+
path: "user2/repo1/../../custom/conf/app.ini",
4250
code: http.StatusNotFound,
4351
},
4452
{
45-
p: "user2/repo1/objects/info/../../../../custom/conf/app.ini",
53+
path: "user2/repo1/objects/info/../../../../custom/conf/app.ini",
4654
code: http.StatusNotFound,
4755
},
4856
{
49-
p: `user2/repo1/objects/info/..\..\..\..\custom\conf\app.ini`,
57+
path: `user2/repo1/objects/info/..\..\..\..\custom\conf\app.ini`,
5058
code: http.StatusBadRequest,
5159
},
5260
}
5361

5462
for _, kase := range kases {
55-
t.Run(kase.p, func(t *testing.T) {
56-
p := u.String() + kase.p
57-
req, err := http.NewRequest("GET", p, nil)
58-
assert.NoError(t, err)
63+
t.Run(kase.path, func(t *testing.T) {
64+
req, err := http.NewRequest(util.IfZero(kase.method, "GET"), u.String()+kase.path, nil)
65+
require.NoError(t, err)
5966
req.SetBasicAuth("user2", userPassword)
6067
resp, err := http.DefaultClient.Do(req)
61-
assert.NoError(t, err)
68+
require.NoError(t, err)
6269
defer resp.Body.Close()
6370
assert.EqualValues(t, kase.code, resp.StatusCode)
6471
_, err = io.ReadAll(resp.Body)
65-
assert.NoError(t, err)
72+
require.NoError(t, err)
6673
})
6774
}
6875
}

0 commit comments

Comments
 (0)