Skip to content

Commit b3e5b96

Browse files
authored
Fix clone mixed bug (go-gitea#35810)
Fix go-gitea#35807
1 parent 1dac4d1 commit b3e5b96

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

cmd/serv.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
asymkey_model "code.gitea.io/gitea/models/asymkey"
1919
git_model "code.gitea.io/gitea/models/git"
2020
"code.gitea.io/gitea/models/perm"
21-
"code.gitea.io/gitea/models/repo"
21+
repo_model "code.gitea.io/gitea/models/repo"
2222
"code.gitea.io/gitea/modules/git"
2323
"code.gitea.io/gitea/modules/git/gitcmd"
2424
"code.gitea.io/gitea/modules/json"
@@ -207,7 +207,7 @@ func runServ(ctx context.Context, c *cli.Command) error {
207207
username := repoPathFields[0]
208208
reponame := strings.TrimSuffix(repoPathFields[1], ".git") // “the-repo-name" or "the-repo-name.wiki"
209209

210-
if !repo.IsValidSSHAccessRepoName(reponame) {
210+
if !repo_model.IsValidSSHAccessRepoName(reponame) {
211211
return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
212212
}
213213

@@ -253,10 +253,12 @@ func runServ(ctx context.Context, c *cli.Command) error {
253253
return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
254254
}
255255

256-
// LowerCase and trim the repoPath as that's how they are stored.
257-
// This should be done after splitting the repoPath into username and reponame
258-
// so that username and reponame are not affected.
259-
repoPath = strings.ToLower(results.OwnerName + "/" + results.RepoName + ".git")
256+
// because the original repoPath maybe redirected, we need to use the returned actual repository information
257+
if results.IsWiki {
258+
repoPath = repo_model.RelativeWikiPath(results.OwnerName, results.RepoName)
259+
} else {
260+
repoPath = repo_model.RelativePath(results.OwnerName, results.RepoName)
261+
}
260262

261263
// LFS SSH protocol
262264
if verb == git.CmdVerbLfsTransfer {

tests/integration/wiki_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"strings"
1212
"testing"
1313

14+
auth_model "code.gitea.io/gitea/models/auth"
1415
"code.gitea.io/gitea/modules/git"
16+
api "code.gitea.io/gitea/modules/structs"
1517
"code.gitea.io/gitea/modules/util"
1618
"code.gitea.io/gitea/tests"
1719

@@ -71,3 +73,46 @@ func Test_RepoWikiPages(t *testing.T) {
7173
assert.Equal(t, expectedPagePaths[i], pagePath)
7274
})
7375
}
76+
77+
func Test_WikiClone(t *testing.T) {
78+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
79+
username := "user2"
80+
reponame := "repo1"
81+
wikiPath := username + "/" + reponame + ".wiki.git"
82+
keyname := "my-testing-key"
83+
baseAPITestContext := NewAPITestContext(t, username, "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
84+
85+
u.Path = wikiPath
86+
87+
t.Run("Clone HTTP", func(t *testing.T) {
88+
defer tests.PrintCurrentTest(t)()
89+
90+
dstLocalPath := t.TempDir()
91+
assert.NoError(t, git.Clone(t.Context(), u.String(), dstLocalPath, git.CloneRepoOptions{}))
92+
content, err := os.ReadFile(filepath.Join(dstLocalPath, "Home.md"))
93+
assert.NoError(t, err)
94+
assert.Equal(t, "# Home page\n\nThis is the home page!\n", string(content))
95+
})
96+
97+
t.Run("Clone SSH", func(t *testing.T) {
98+
defer tests.PrintCurrentTest(t)()
99+
100+
dstLocalPath := t.TempDir()
101+
sshURL := createSSHUrl(wikiPath, u)
102+
103+
withKeyFile(t, keyname, func(keyFile string) {
104+
var keyID int64
105+
t.Run("CreateUserKey", doAPICreateUserKey(baseAPITestContext, "test-key", keyFile, func(t *testing.T, key api.PublicKey) {
106+
keyID = key.ID
107+
}))
108+
assert.NotZero(t, keyID)
109+
110+
// Setup clone folder
111+
assert.NoError(t, git.Clone(t.Context(), sshURL.String(), dstLocalPath, git.CloneRepoOptions{}))
112+
content, err := os.ReadFile(filepath.Join(dstLocalPath, "Home.md"))
113+
assert.NoError(t, err)
114+
assert.Equal(t, "# Home page\n\nThis is the home page!\n", string(content))
115+
})
116+
})
117+
})
118+
}

0 commit comments

Comments
 (0)