Skip to content

Commit 3c46a3d

Browse files
lunnyChristopherHX
andauthored
Fix bug when pushing mirror with wiki (#36795) (#36807)
Fix #36736 Backport #36795 Co-authored-by: ChristopherHX <christopher.homberger@web.de>
1 parent 5552eff commit 3c46a3d

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

cmd/hook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Gitea or set your environment appropriately.`, "")
205205
PullRequestID: prID,
206206
DeployKeyID: deployKeyID,
207207
ActionPerm: int(actionPerm),
208+
IsWiki: isWiki,
208209
}
209210

210211
scanner := bufio.NewScanner(os.Stdin)
@@ -366,6 +367,7 @@ Gitea or set your environment appropriately.`, "")
366367
GitPushOptions: pushOptions(),
367368
PullRequestID: prID,
368369
PushTrigger: repo_module.PushTrigger(os.Getenv(repo_module.EnvPushTrigger)),
370+
IsWiki: isWiki,
369371
}
370372
oldCommitIDs := make([]string, hookBatchSize)
371373
newCommitIDs := make([]string, hookBatchSize)
@@ -513,6 +515,7 @@ Gitea or set your environment appropriately.`, "")
513515

514516
reader := bufio.NewReader(os.Stdin)
515517
repoUser := os.Getenv(repo_module.EnvRepoUsername)
518+
isWiki, _ := strconv.ParseBool(os.Getenv(repo_module.EnvRepoIsWiki))
516519
repoName := os.Getenv(repo_module.EnvRepoName)
517520
pusherID, _ := strconv.ParseInt(os.Getenv(repo_module.EnvPusherID), 10, 64)
518521
pusherName := os.Getenv(repo_module.EnvPusherName)
@@ -590,6 +593,7 @@ Gitea or set your environment appropriately.`, "")
590593
UserName: pusherName,
591594
UserID: pusherID,
592595
GitPushOptions: make(map[string]string),
596+
IsWiki: isWiki,
593597
}
594598
hookOptions.OldCommitIDs = make([]string, 0, hookBatchSize)
595599
hookOptions.NewCommitIDs = make([]string, 0, hookBatchSize)

routers/private/hook_pre_receive.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
149149
gitRepo := ctx.Repo.GitRepo
150150
objectFormat := ctx.Repo.GetObjectFormat()
151151

152-
if branchName == repo.DefaultBranch && newCommitID == objectFormat.EmptyObjectID().String() {
152+
defaultBranch := repo.DefaultBranch
153+
if ctx.opts.IsWiki && repo.DefaultWikiBranch != "" {
154+
defaultBranch = repo.DefaultWikiBranch
155+
}
156+
if branchName == defaultBranch && newCommitID == objectFormat.EmptyObjectID().String() {
153157
log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)
154158
ctx.JSON(http.StatusForbidden, private.Response{
155159
UserMsg: fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName),

tests/integration/mirror_push_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/services/migrations"
2121
mirror_service "code.gitea.io/gitea/services/mirror"
2222
repo_service "code.gitea.io/gitea/services/repository"
23+
wiki_service "code.gitea.io/gitea/services/wiki"
2324
"code.gitea.io/gitea/tests"
2425

2526
"github.com/stretchr/testify/assert"
@@ -29,6 +30,10 @@ func TestMirrorPush(t *testing.T) {
2930
onGiteaRun(t, testMirrorPush)
3031
}
3132

33+
func TestMirrorPushWikiDefaultBranchMismatch(t *testing.T) {
34+
onGiteaRun(t, testMirrorPushWikiDefaultBranchMismatch)
35+
}
36+
3237
func testMirrorPush(t *testing.T, u *url.URL) {
3338
setting.Migrations.AllowLocalNetworks = true
3439
assert.NoError(t, migrations.Init())
@@ -77,6 +82,45 @@ func testMirrorPush(t *testing.T, u *url.URL) {
7782
assert.Empty(t, mirrors)
7883
}
7984

85+
func testMirrorPushWikiDefaultBranchMismatch(t *testing.T, u *url.URL) {
86+
setting.Migrations.AllowLocalNetworks = true
87+
assert.NoError(t, migrations.Init())
88+
89+
_ = db.TruncateBeans(t.Context(), &repo_model.PushMirror{})
90+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
91+
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
92+
93+
mirrorRepo, err := repo_service.CreateRepositoryDirectly(t.Context(), user, user, repo_service.CreateRepoOptions{
94+
Name: "test-push-mirror-wiki",
95+
}, true)
96+
assert.NoError(t, err)
97+
98+
assert.NoError(t, wiki_service.AddWikiPage(t.Context(), user, mirrorRepo, wiki_service.WebPath("Home"), "Mirror wiki content", "init wiki"))
99+
100+
mirrorRepo.DefaultBranch = "mirror-head"
101+
assert.NoError(t, repo_model.UpdateRepositoryColsNoAutoTime(t.Context(), mirrorRepo, "default_branch"))
102+
103+
gitRepo, err := gitrepo.OpenRepository(t.Context(), mirrorRepo.WikiStorageRepo())
104+
assert.NoError(t, err)
105+
defer gitRepo.Close()
106+
107+
wikiCommitID, err := gitrepo.GetBranchCommitID(t.Context(), mirrorRepo.WikiStorageRepo(), mirrorRepo.DefaultWikiBranch)
108+
assert.NoError(t, err)
109+
assert.NoError(t, gitRepo.CreateBranch("mirror-head", wikiCommitID))
110+
111+
session := loginUser(t, user.Name)
112+
113+
pushMirrorURL := fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(user.Name), url.PathEscape(mirrorRepo.Name))
114+
testCreatePushMirror(t, session, user.Name, srcRepo.Name, pushMirrorURL, user.LowerName, userPassword, "0")
115+
116+
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(t.Context(), srcRepo.ID, db.ListOptions{})
117+
assert.NoError(t, err)
118+
assert.Len(t, mirrors, 1)
119+
120+
ok := mirror_service.SyncPushMirror(t.Context(), mirrors[0].ID)
121+
assert.True(t, ok)
122+
}
123+
80124
func testCreatePushMirror(t *testing.T, session *TestSession, owner, repo, address, username, password, interval string) {
81125
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(owner), url.PathEscape(repo)), map[string]string{
82126
"_csrf": GetUserCSRFToken(t, session),

0 commit comments

Comments
 (0)