Skip to content

Commit ef78309

Browse files
sapklunny
authored andcommitted
Improve LFS tests + fix lfs url refs + keep path upper/lowercase in db. (#3092)
* Add failing test * Fix urls * Improve url in tests * improve testing * Remove debug code * Add deps * LFS corner-case : Search on lower but store with case * Temporary comment of blocking action * fix hooks * Use temporary repo for git client test * Use userPassword in place of hard-coded password
1 parent aecfc56 commit ef78309

File tree

13 files changed

+154
-29
lines changed

13 files changed

+154
-29
lines changed

integrations/git_test.go

Lines changed: 139 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@ package integrations
66

77
import (
88
"context"
9-
"fmt"
109
"io/ioutil"
10+
"math/rand"
1111
"net"
1212
"net/http"
13+
"net/url"
1314
"os"
1415
"path/filepath"
1516
"testing"
1617
"time"
1718

1819
"code.gitea.io/git"
20+
"code.gitea.io/gitea/modules/setting"
21+
api "code.gitea.io/sdk/gitea"
1922

2023
"github.com/Unknwon/com"
2124
"github.com/stretchr/testify/assert"
2225
)
2326

24-
func onGiteaWebRun(t *testing.T, callback func(*testing.T, string)) {
27+
func onGiteaWebRun(t *testing.T, callback func(*testing.T, *url.URL)) {
2528
s := http.Server{
2629
Handler: mac,
2730
}
2831

29-
listener, err := net.Listen("tcp", "")
32+
u, err := url.Parse(setting.AppURL)
33+
assert.NoError(t, err)
34+
listener, err := net.Listen("tcp", u.Host)
3035
assert.NoError(t, err)
3136

3237
defer func() {
@@ -37,24 +42,144 @@ func onGiteaWebRun(t *testing.T, callback func(*testing.T, string)) {
3742

3843
go s.Serve(listener)
3944

40-
_, port, err := net.SplitHostPort(listener.Addr().String())
41-
assert.NoError(t, err)
42-
43-
callback(t, fmt.Sprintf("http://localhost:%s/", port))
45+
callback(t, u)
4446
}
4547

46-
func TestClone_ViaHTTP_NoLogin(t *testing.T) {
48+
func TestGit(t *testing.T) {
4749
prepareTestEnv(t)
4850

49-
onGiteaWebRun(t, func(t *testing.T, urlPrefix string) {
50-
dstPath, err := ioutil.TempDir("", "repo1")
51+
onGiteaWebRun(t, func(t *testing.T, u *url.URL) {
52+
dstPath, err := ioutil.TempDir("", "repo-tmp-17")
5153
assert.NoError(t, err)
5254
defer os.RemoveAll(dstPath)
55+
u.Path = "user2/repo1.git"
5356

54-
err = git.Clone(fmt.Sprintf("%suser2/repo1.git", urlPrefix),
55-
dstPath, git.CloneRepoOptions{})
56-
assert.NoError(t, err)
57+
t.Run("Standard", func(t *testing.T) {
58+
59+
t.Run("CloneNoLogin", func(t *testing.T) {
60+
dstLocalPath, err := ioutil.TempDir("", "repo1")
61+
assert.NoError(t, err)
62+
defer os.RemoveAll(dstLocalPath)
63+
err = git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{})
64+
assert.NoError(t, err)
65+
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
66+
})
67+
68+
t.Run("CreateRepo", func(t *testing.T) {
69+
session := loginUser(t, "user2")
70+
req := NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{
71+
AutoInit: true,
72+
Description: "Temporary repo",
73+
Name: "repo-tmp-17",
74+
Private: false,
75+
Gitignores: "",
76+
License: "WTFPL",
77+
Readme: "Default",
78+
})
79+
session.MakeRequest(t, req, http.StatusCreated)
80+
})
81+
82+
u.Path = "user2/repo-tmp-17.git"
83+
u.User = url.UserPassword("user2", userPassword)
84+
t.Run("Clone", func(t *testing.T) {
85+
err = git.Clone(u.String(), dstPath, git.CloneRepoOptions{})
86+
assert.NoError(t, err)
87+
assert.True(t, com.IsExist(filepath.Join(dstPath, "README.md")))
88+
})
89+
90+
t.Run("PushCommit", func(t *testing.T) {
91+
data := make([]byte, 1024)
92+
_, err := rand.Read(data)
93+
assert.NoError(t, err)
94+
tmpFile, err := ioutil.TempFile(dstPath, "data-file-")
95+
defer tmpFile.Close()
96+
_, err = tmpFile.Write(data)
97+
assert.NoError(t, err)
98+
99+
//Commit
100+
err = git.AddChanges(dstPath, false, filepath.Base(tmpFile.Name()))
101+
assert.NoError(t, err)
102+
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
103+
Committer: &git.Signature{
104+
105+
Name: "User Two",
106+
When: time.Now(),
107+
},
108+
Author: &git.Signature{
109+
110+
Name: "User Two",
111+
When: time.Now(),
112+
},
113+
Message: "Testing commit",
114+
})
115+
assert.NoError(t, err)
116+
117+
//Push
118+
err = git.Push(dstPath, git.PushOptions{
119+
Branch: "master",
120+
Remote: u.String(),
121+
Force: false,
122+
})
123+
assert.NoError(t, err)
124+
})
125+
})
126+
t.Run("LFS", func(t *testing.T) {
127+
t.Run("PushCommit", func(t *testing.T) {
128+
/* Generate random file */
129+
data := make([]byte, 1024)
130+
_, err := rand.Read(data)
131+
assert.NoError(t, err)
132+
tmpFile, err := ioutil.TempFile(dstPath, "data-file-")
133+
defer tmpFile.Close()
134+
_, err = tmpFile.Write(data)
135+
assert.NoError(t, err)
136+
137+
//Setup git LFS
138+
_, err = git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
139+
assert.NoError(t, err)
140+
_, err = git.NewCommand("lfs").AddArguments("track", "data-file-*").RunInDir(dstPath)
141+
assert.NoError(t, err)
142+
143+
//Commit
144+
err = git.AddChanges(dstPath, false, ".gitattributes", filepath.Base(tmpFile.Name()))
145+
assert.NoError(t, err)
146+
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
147+
Committer: &git.Signature{
148+
149+
Name: "User Two",
150+
When: time.Now(),
151+
},
152+
Author: &git.Signature{
153+
154+
Name: "User Two",
155+
When: time.Now(),
156+
},
157+
Message: "Testing LFS ",
158+
})
159+
assert.NoError(t, err)
160+
161+
//Push
162+
u.User = url.UserPassword("user2", userPassword)
163+
err = git.Push(dstPath, git.PushOptions{
164+
Branch: "master",
165+
Remote: u.String(),
166+
Force: false,
167+
})
168+
assert.NoError(t, err)
169+
})
170+
t.Run("Locks", func(t *testing.T) {
171+
_, err = git.NewCommand("remote").AddArguments("set-url", "origin", u.String()).RunInDir(dstPath) //TODO add test ssh git-lfs-creds
172+
assert.NoError(t, err)
173+
_, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(dstPath)
174+
assert.NoError(t, err)
175+
_, err = git.NewCommand("lfs").AddArguments("lock", "README.md").RunInDir(dstPath)
176+
assert.NoError(t, err)
177+
_, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(dstPath)
178+
assert.NoError(t, err)
179+
_, err = git.NewCommand("lfs").AddArguments("unlock", "README.md").RunInDir(dstPath)
180+
assert.NoError(t, err)
181+
})
57182

58-
assert.True(t, com.IsExist(filepath.Join(dstPath, "README.md")))
183+
})
59184
})
60185
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' post-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' pre-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' update $1 $2 $3
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' post-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' pre-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' update $1 $2 $3
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' post-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' pre-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"$GITEA_ROOT/gitea" hook --config='integrations/app.ini' update $1 $2 $3
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3

models/lfs_lock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (l *LFSLock) AfterLoad() {
3636
}
3737

3838
func cleanPath(p string) string {
39-
return strings.ToLower(path.Clean(p))
39+
return path.Clean(p)
4040
}
4141

4242
// APIFormat convert a Release to lfs.LFSLock
@@ -73,8 +73,8 @@ func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
7373
// GetLFSLock returns release by given path.
7474
func GetLFSLock(repoID int64, path string) (*LFSLock, error) {
7575
path = cleanPath(path)
76-
rel := &LFSLock{RepoID: repoID, Path: path}
77-
has, err := x.Get(rel)
76+
rel := &LFSLock{RepoID: repoID}
77+
has, err := x.Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
7878
if err != nil {
7979
return nil, err
8080
}

modules/lfs/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ type ObjectError struct {
6868

6969
// ObjectLink builds a URL linking to the object.
7070
func (v *RequestVars) ObjectLink() string {
71-
return setting.AppURL + path.Join(v.User, v.Repo, "info/lfs/objects", v.Oid)
71+
return setting.AppURL + path.Join(v.User, v.Repo+".git", "info/lfs/objects", v.Oid)
7272
}
7373

7474
// VerifyLink builds a URL for verifying the object.
7575
func (v *RequestVars) VerifyLink() string {
76-
return setting.AppURL + path.Join(v.User, v.Repo, "info/lfs/verify")
76+
return setting.AppURL + path.Join(v.User, v.Repo+".git", "info/lfs/verify")
7777
}
7878

7979
// link provides a structure used to build a hypermedia representation of an HTTP link.

routers/repo/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
179179
ctx.Data["IsLFSFile"] = true
180180
ctx.Data["FileSize"] = size
181181
filenameBase64 := base64.RawURLEncoding.EncodeToString([]byte(blob.Name()))
182-
ctx.Data["RawFileLink"] = fmt.Sprintf("%s%s/info/lfs/objects/%s/%s", setting.AppURL, ctx.Repo.Repository.FullName(), oid, filenameBase64)
182+
ctx.Data["RawFileLink"] = fmt.Sprintf("%s%s.git/info/lfs/objects/%s/%s", setting.AppURL, ctx.Repo.Repository.FullName(), oid, filenameBase64)
183183
}
184184
}
185185
}

0 commit comments

Comments
 (0)