Skip to content

Commit 4035ab0

Browse files
sapklafriks
authored andcommitted
Fix git lfs path (#3016)
* add suffix .git * Remove Content-Type on GET methods * Add migrations to force creation before any further potential migration
1 parent c80d147 commit 4035ab0

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

integrations/api_repo_lfs_locks_test.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ func TestAPILFSLocksNotStarted(t *testing.T) {
2323
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
2424
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
2525

26-
req := NewRequestf(t, "GET", "/%s/%s/info/lfs/locks", user.Name, repo.Name)
26+
req := NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks", user.Name, repo.Name)
2727
MakeRequest(t, req, http.StatusNotFound)
28-
req = NewRequestf(t, "POST", "/%s/%s/info/lfs/locks", user.Name, repo.Name)
28+
req = NewRequestf(t, "POST", "/%s/%s.git/info/lfs/locks", user.Name, repo.Name)
2929
MakeRequest(t, req, http.StatusNotFound)
30-
req = NewRequestf(t, "GET", "/%s/%s/info/lfs/locks/verify", user.Name, repo.Name)
30+
req = NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks/verify", user.Name, repo.Name)
3131
MakeRequest(t, req, http.StatusNotFound)
32-
req = NewRequestf(t, "GET", "/%s/%s/info/lfs/locks/10/unlock", user.Name, repo.Name)
32+
req = NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks/10/unlock", user.Name, repo.Name)
3333
MakeRequest(t, req, http.StatusNotFound)
3434
}
3535

@@ -39,9 +39,8 @@ func TestAPILFSLocksNotLogin(t *testing.T) {
3939
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
4040
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
4141

42-
req := NewRequestf(t, "GET", "/%s/%s/info/lfs/locks", user.Name, repo.Name)
42+
req := NewRequestf(t, "GET", "/%s/%s.git/info/lfs/locks", user.Name, repo.Name)
4343
req.Header.Set("Accept", "application/vnd.git-lfs+json")
44-
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
4544
resp := MakeRequest(t, req, http.StatusForbidden)
4645
var lfsLockError api.LFSLockError
4746
DecodeJSON(t, resp, &lfsLockError)
@@ -102,7 +101,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
102101
//create locks
103102
for _, test := range tests {
104103
session := loginUser(t, test.user.Name)
105-
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path})
104+
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks", test.repo.FullName()), map[string]string{"path": test.path})
106105
req.Header.Set("Accept", "application/vnd.git-lfs+json")
107106
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
108107
session.MakeRequest(t, req, test.httpResult)
@@ -116,9 +115,8 @@ func TestAPILFSLocksLogged(t *testing.T) {
116115
//check creation
117116
for _, test := range resultsTests {
118117
session := loginUser(t, test.user.Name)
119-
req := NewRequestf(t, "GET", "/%s/info/lfs/locks", test.repo.FullName())
118+
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
120119
req.Header.Set("Accept", "application/vnd.git-lfs+json")
121-
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
122120
resp := session.MakeRequest(t, req, http.StatusOK)
123121
var lfsLocks api.LFSLockList
124122
DecodeJSON(t, resp, &lfsLocks)
@@ -128,7 +126,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
128126
assert.WithinDuration(t, test.locksTimes[i], lock.LockedAt, 1*time.Second)
129127
}
130128

131-
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks/verify", test.repo.FullName()), map[string]string{})
129+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/verify", test.repo.FullName()), map[string]string{})
132130
req.Header.Set("Accept", "application/vnd.git-lfs+json")
133131
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
134132
resp = session.MakeRequest(t, req, http.StatusOK)
@@ -152,7 +150,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
152150
//remove all locks
153151
for _, test := range deleteTests {
154152
session := loginUser(t, test.user.Name)
155-
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{})
153+
req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/%s.git/info/lfs/locks/%s/unlock", test.repo.FullName(), test.lockID), map[string]string{})
156154
req.Header.Set("Accept", "application/vnd.git-lfs+json")
157155
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
158156
resp := session.MakeRequest(t, req, http.StatusOK)
@@ -165,9 +163,8 @@ func TestAPILFSLocksLogged(t *testing.T) {
165163
// check that we don't have any lock
166164
for _, test := range resultsTests {
167165
session := loginUser(t, test.user.Name)
168-
req := NewRequestf(t, "GET", "/%s/info/lfs/locks", test.repo.FullName())
166+
req := NewRequestf(t, "GET", "/%s.git/info/lfs/locks", test.repo.FullName())
169167
req.Header.Set("Accept", "application/vnd.git-lfs+json")
170-
req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
171168
resp := session.MakeRequest(t, req, http.StatusOK)
172169
var lfsLocks api.LFSLockList
173170
DecodeJSON(t, resp, &lfsLocks)

models/migrations/migrations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var migrations = []Migration{
110110
NewMigration("add commit status table", addCommitStatus),
111111
// v30 -> 31
112112
NewMigration("add primary key to external login user", addExternalLoginUserPK),
113-
// 31 -> 32
113+
// v31 -> 32
114114
NewMigration("add field for login source synchronization", addLoginSourceSyncEnabledColumn),
115115
// v32 -> v33
116116
NewMigration("add units for team", addUnitsToRepoTeam),
@@ -146,6 +146,8 @@ var migrations = []Migration{
146146
NewMigration("add deleted branches", addDeletedBranch),
147147
// v48 -> v49
148148
NewMigration("add repo indexer status", addRepoIndexerStatus),
149+
// v49 -> v50
150+
NewMigration("add lfs lock table", addLFSLock),
149151
}
150152

151153
// Migrate database to current version

models/migrations/v49.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"fmt"
9+
"time"
10+
11+
"code.gitea.io/gitea/models"
12+
13+
"github.com/go-xorm/xorm"
14+
)
15+
16+
func addLFSLock(x *xorm.Engine) error {
17+
// LFSLock see models/lfs_lock.go
18+
type LFSLock struct {
19+
ID int64 `xorm:"pk autoincr"`
20+
RepoID int64 `xorm:"INDEX NOT NULL"`
21+
Owner *models.User `xorm:"-"`
22+
OwnerID int64 `xorm:"INDEX NOT NULL"`
23+
Path string `xorm:"TEXT"`
24+
Created time.Time `xorm:"created"`
25+
}
26+
27+
if err := x.Sync2(new(LFSLock)); err != nil {
28+
return fmt.Errorf("Sync2: %v", err)
29+
}
30+
return nil
31+
}

modules/lfs/locks.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package lfs
77
import (
88
"encoding/json"
99
"strconv"
10+
"strings"
1011

1112
"code.gitea.io/gitea/models"
1213
"code.gitea.io/gitea/modules/context"
@@ -16,13 +17,19 @@ import (
1617
"gopkg.in/macaron.v1"
1718
)
1819

19-
func checkRequest(req macaron.Request) int {
20+
func checkRequest(req macaron.Request, post bool) int {
2021
if !setting.LFS.StartServer {
2122
return 404
2223
}
23-
if !MetaMatcher(req) || req.Header.Get("Content-Type") != metaMediaType {
24+
if !MetaMatcher(req) {
2425
return 400
2526
}
27+
if post {
28+
mediaParts := strings.Split(req.Header.Get("Content-Type"), ";")
29+
if mediaParts[0] != metaMediaType {
30+
return 400
31+
}
32+
}
2633
return 200
2734
}
2835

@@ -52,7 +59,7 @@ func handleLockListOut(ctx *context.Context, lock *models.LFSLock, err error) {
5259

5360
// GetListLockHandler list locks
5461
func GetListLockHandler(ctx *context.Context) {
55-
status := checkRequest(ctx.Req)
62+
status := checkRequest(ctx.Req, false)
5663
if status != 200 {
5764
writeStatus(ctx, status)
5865
return
@@ -113,7 +120,7 @@ func GetListLockHandler(ctx *context.Context) {
113120

114121
// PostLockHandler create lock
115122
func PostLockHandler(ctx *context.Context) {
116-
status := checkRequest(ctx.Req)
123+
status := checkRequest(ctx.Req, true)
117124
if status != 200 {
118125
writeStatus(ctx, status)
119126
return
@@ -157,7 +164,7 @@ func PostLockHandler(ctx *context.Context) {
157164

158165
// VerifyLockHandler list locks for verification
159166
func VerifyLockHandler(ctx *context.Context) {
160-
status := checkRequest(ctx.Req)
167+
status := checkRequest(ctx.Req, true)
161168
if status != 200 {
162169
writeStatus(ctx, status)
163170
return
@@ -204,7 +211,7 @@ func VerifyLockHandler(ctx *context.Context) {
204211

205212
// UnLockHandler delete locks
206213
func UnLockHandler(ctx *context.Context) {
207-
status := checkRequest(ctx.Req)
214+
status := checkRequest(ctx.Req, true)
208215
if status != 200 {
209216
writeStatus(ctx, status)
210217
return

routers/routes/routes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ func RegisterRoutes(m *macaron.Macaron) {
679679
}, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes(), context.LoadRepoUnits())
680680

681681
m.Group("/:reponame", func() {
682-
m.Group("/info/lfs", func() {
682+
m.Group("\\.git/info/lfs", func() {
683683
m.Post("/objects/batch", lfs.BatchHandler)
684684
m.Get("/objects/:oid/:filename", lfs.ObjectOidHandler)
685685
m.Any("/objects/:oid", lfs.ObjectOidHandler)

0 commit comments

Comments
 (0)