Browse Source

Fix git lfs path (#3016)

* add suffix .git

* Remove Content-Type on GET methods

* Add migrations to force creation before any further potential migration
Antoine GIRARD 1 year ago
parent
commit
4035ab05fa

+ 10 - 13
integrations/api_repo_lfs_locks_test.go

@@ -23,13 +23,13 @@ func TestAPILFSLocksNotStarted(t *testing.T) {
23 23
 	user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
24 24
 	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
25 25
 
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)
27 27
 	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)
29 29
 	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)
31 31
 	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)
33 33
 	MakeRequest(t, req, http.StatusNotFound)
34 34
 }
35 35
 
@@ -39,9 +39,8 @@ func TestAPILFSLocksNotLogin(t *testing.T) {
39 39
 	user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
40 40
 	repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
41 41
 
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)
43 43
 	req.Header.Set("Accept", "application/vnd.git-lfs+json")
44
-	req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
45 44
 	resp := MakeRequest(t, req, http.StatusForbidden)
46 45
 	var lfsLockError api.LFSLockError
47 46
 	DecodeJSON(t, resp, &lfsLockError)
@@ -102,7 +101,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
102 101
 	//create locks
103 102
 	for _, test := range tests {
104 103
 		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})
106 105
 		req.Header.Set("Accept", "application/vnd.git-lfs+json")
107 106
 		req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
108 107
 		session.MakeRequest(t, req, test.httpResult)
@@ -116,9 +115,8 @@ func TestAPILFSLocksLogged(t *testing.T) {
116 115
 	//check creation
117 116
 	for _, test := range resultsTests {
118 117
 		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())
120 119
 		req.Header.Set("Accept", "application/vnd.git-lfs+json")
121
-		req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
122 120
 		resp := session.MakeRequest(t, req, http.StatusOK)
123 121
 		var lfsLocks api.LFSLockList
124 122
 		DecodeJSON(t, resp, &lfsLocks)
@@ -128,7 +126,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
128 126
 			assert.WithinDuration(t, test.locksTimes[i], lock.LockedAt, 1*time.Second)
129 127
 		}
130 128
 
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{})
132 130
 		req.Header.Set("Accept", "application/vnd.git-lfs+json")
133 131
 		req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
134 132
 		resp = session.MakeRequest(t, req, http.StatusOK)
@@ -152,7 +150,7 @@ func TestAPILFSLocksLogged(t *testing.T) {
152 150
 	//remove all locks
153 151
 	for _, test := range deleteTests {
154 152
 		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{})
156 154
 		req.Header.Set("Accept", "application/vnd.git-lfs+json")
157 155
 		req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
158 156
 		resp := session.MakeRequest(t, req, http.StatusOK)
@@ -165,9 +163,8 @@ func TestAPILFSLocksLogged(t *testing.T) {
165 163
 	// check that we don't have any lock
166 164
 	for _, test := range resultsTests {
167 165
 		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())
169 167
 		req.Header.Set("Accept", "application/vnd.git-lfs+json")
170
-		req.Header.Set("Content-Type", "application/vnd.git-lfs+json")
171 168
 		resp := session.MakeRequest(t, req, http.StatusOK)
172 169
 		var lfsLocks api.LFSLockList
173 170
 		DecodeJSON(t, resp, &lfsLocks)

+ 3 - 1
models/migrations/migrations.go

@@ -110,7 +110,7 @@ var migrations = []Migration{
110 110
 	NewMigration("add commit status table", addCommitStatus),
111 111
 	// v30 -> 31
112 112
 	NewMigration("add primary key to external login user", addExternalLoginUserPK),
113
-	// 31 -> 32
113
+	// v31 -> 32
114 114
 	NewMigration("add field for login source synchronization", addLoginSourceSyncEnabledColumn),
115 115
 	// v32 -> v33
116 116
 	NewMigration("add units for team", addUnitsToRepoTeam),
@@ -146,6 +146,8 @@ var migrations = []Migration{
146 146
 	NewMigration("add deleted branches", addDeletedBranch),
147 147
 	// v48 -> v49
148 148
 	NewMigration("add repo indexer status", addRepoIndexerStatus),
149
+	// v49 -> v50
150
+	NewMigration("add lfs lock table", addLFSLock),
149 151
 }
150 152
 
151 153
 // Migrate database to current version

+ 31 - 0
models/migrations/v49.go

@@ -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
+}

+ 13 - 6
modules/lfs/locks.go

@@ -7,6 +7,7 @@ package lfs
7 7
 import (
8 8
 	"encoding/json"
9 9
 	"strconv"
10
+	"strings"
10 11
 
11 12
 	"code.gitea.io/gitea/models"
12 13
 	"code.gitea.io/gitea/modules/context"
@@ -16,13 +17,19 @@ import (
16 17
 	"gopkg.in/macaron.v1"
17 18
 )
18 19
 
19
-func checkRequest(req macaron.Request) int {
20
+func checkRequest(req macaron.Request, post bool) int {
20 21
 	if !setting.LFS.StartServer {
21 22
 		return 404
22 23
 	}
23
-	if !MetaMatcher(req) || req.Header.Get("Content-Type") != metaMediaType {
24
+	if !MetaMatcher(req) {
24 25
 		return 400
25 26
 	}
27
+	if post {
28
+		mediaParts := strings.Split(req.Header.Get("Content-Type"), ";")
29
+		if mediaParts[0] != metaMediaType {
30
+			return 400
31
+		}
32
+	}
26 33
 	return 200
27 34
 }
28 35
 
@@ -52,7 +59,7 @@ func handleLockListOut(ctx *context.Context, lock *models.LFSLock, err error) {
52 59
 
53 60
 // GetListLockHandler list locks
54 61
 func GetListLockHandler(ctx *context.Context) {
55
-	status := checkRequest(ctx.Req)
62
+	status := checkRequest(ctx.Req, false)
56 63
 	if status != 200 {
57 64
 		writeStatus(ctx, status)
58 65
 		return
@@ -113,7 +120,7 @@ func GetListLockHandler(ctx *context.Context) {
113 120
 
114 121
 // PostLockHandler create lock
115 122
 func PostLockHandler(ctx *context.Context) {
116
-	status := checkRequest(ctx.Req)
123
+	status := checkRequest(ctx.Req, true)
117 124
 	if status != 200 {
118 125
 		writeStatus(ctx, status)
119 126
 		return
@@ -157,7 +164,7 @@ func PostLockHandler(ctx *context.Context) {
157 164
 
158 165
 // VerifyLockHandler list locks for verification
159 166
 func VerifyLockHandler(ctx *context.Context) {
160
-	status := checkRequest(ctx.Req)
167
+	status := checkRequest(ctx.Req, true)
161 168
 	if status != 200 {
162 169
 		writeStatus(ctx, status)
163 170
 		return
@@ -204,7 +211,7 @@ func VerifyLockHandler(ctx *context.Context) {
204 211
 
205 212
 // UnLockHandler delete locks
206 213
 func UnLockHandler(ctx *context.Context) {
207
-	status := checkRequest(ctx.Req)
214
+	status := checkRequest(ctx.Req, true)
208 215
 	if status != 200 {
209 216
 		writeStatus(ctx, status)
210 217
 		return

+ 1 - 1
routers/routes/routes.go

@@ -679,7 +679,7 @@ func RegisterRoutes(m *macaron.Macaron) {
679 679
 		}, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes(), context.LoadRepoUnits())
680 680
 
681 681
 		m.Group("/:reponame", func() {
682
-			m.Group("/info/lfs", func() {
682
+			m.Group("\\.git/info/lfs", func() {
683 683
 				m.Post("/objects/batch", lfs.BatchHandler)
684 684
 				m.Get("/objects/:oid/:filename", lfs.ObjectOidHandler)
685 685
 				m.Any("/objects/:oid", lfs.ObjectOidHandler)