Skip to content

Commit c6a4f30

Browse files
committed
Refactor to use modules/mirror
Some functions in services/mirror are moved to modules/mirror package. This approach solves the circular imports problem without introducting a seperate pushmirror package.
1 parent bcabe51 commit c6a4f30

File tree

4 files changed

+65
-73
lines changed

4 files changed

+65
-73
lines changed

services/pushmirror/mirror_push.go renamed to modules/mirror/mirror_push.go

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,25 @@
1-
// Copyright 2022 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 pushmirror
1+
package mirror
62

73
import (
84
"context"
95
"errors"
106
"fmt"
117
"io"
128
"regexp"
13-
"strings"
149
"time"
1510

1611
repo_model "code.gitea.io/gitea/models/repo"
1712
"code.gitea.io/gitea/modules/git"
1813
"code.gitea.io/gitea/modules/lfs"
1914
"code.gitea.io/gitea/modules/log"
2015
"code.gitea.io/gitea/modules/process"
21-
"code.gitea.io/gitea/modules/repository"
2216
"code.gitea.io/gitea/modules/setting"
2317
"code.gitea.io/gitea/modules/timeutil"
2418
"code.gitea.io/gitea/modules/util"
2519
)
2620

2721
var stripExitStatus = regexp.MustCompile(`exit status \d+ - `)
2822

29-
// AddPushMirrorRemote registers the push mirror remote.
30-
func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error {
31-
addRemoteAndConfig := func(addr, path string) error {
32-
cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr)
33-
if strings.Contains(addr, "://") && strings.Contains(addr, "@") {
34-
cmd.SetDescription(fmt.Sprintf("remote add %s --mirror=push %s [repo_path: %s]", m.RemoteName, util.SanitizeCredentialURLs(addr), path))
35-
} else {
36-
cmd.SetDescription(fmt.Sprintf("remote add %s --mirror=push %s [repo_path: %s]", m.RemoteName, addr, path))
37-
}
38-
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: path}); err != nil {
39-
return err
40-
}
41-
if _, _, err := git.NewCommand(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunStdString(&git.RunOpts{Dir: path}); err != nil {
42-
return err
43-
}
44-
if _, _, err := git.NewCommand(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunStdString(&git.RunOpts{Dir: path}); err != nil {
45-
return err
46-
}
47-
return nil
48-
}
49-
50-
if err := addRemoteAndConfig(addr, m.Repo.RepoPath()); err != nil {
51-
return err
52-
}
53-
54-
if m.Repo.HasWiki() {
55-
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
56-
if len(wikiRemoteURL) > 0 {
57-
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
58-
return err
59-
}
60-
}
61-
}
62-
63-
return nil
64-
}
65-
66-
// RemovePushMirrorRemote removes the push mirror remote.
67-
func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error {
68-
cmd := git.NewCommand(ctx, "remote", "rm", m.RemoteName)
69-
_ = m.GetRepository()
70-
71-
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: m.Repo.RepoPath()}); err != nil {
72-
return err
73-
}
74-
75-
if m.Repo.HasWiki() {
76-
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: m.Repo.WikiPath()}); err != nil {
77-
// The wiki remote may not exist
78-
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
79-
}
80-
}
81-
82-
return nil
83-
}
84-
8523
// SyncPushMirror starts the sync of the push mirror and schedules the next run.
8624
func SyncPushMirror(ctx context.Context, mirrorID int64) bool {
8725
log.Trace("SyncPushMirror [mirror: %d]", mirrorID)

modules/notification/mirror/mirror.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
user_model "code.gitea.io/gitea/models/user"
1212
"code.gitea.io/gitea/modules/graceful"
1313
"code.gitea.io/gitea/modules/log"
14+
mirror_module "code.gitea.io/gitea/modules/mirror"
1415
"code.gitea.io/gitea/modules/notification/base"
1516
"code.gitea.io/gitea/modules/process"
1617
"code.gitea.io/gitea/modules/repository"
17-
pushmirror_service "code.gitea.io/gitea/services/pushmirror"
1818
)
1919

2020
type mirrorNotifier struct {
@@ -41,6 +41,6 @@ func (m *mirrorNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_m
4141

4242
for _, mirror := range pushMirrors {
4343
// TODO: push mirror likely will benefit from using a queue
44-
pushmirror_service.SyncPushMirror(ctx, mirror.ID)
44+
mirror_module.SyncPushMirror(ctx, mirror.ID)
4545
}
4646
}

services/mirror/mirror.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
repo_model "code.gitea.io/gitea/models/repo"
1212
"code.gitea.io/gitea/modules/graceful"
1313
"code.gitea.io/gitea/modules/log"
14+
mirror_module "code.gitea.io/gitea/modules/mirror"
1415
"code.gitea.io/gitea/modules/queue"
1516
"code.gitea.io/gitea/modules/setting"
1617
)
@@ -41,7 +42,7 @@ func doMirrorSync(ctx context.Context, req *SyncRequest) {
4142
}
4243
switch req.Type {
4344
case PushMirrorType:
44-
_ = SyncPushMirror(ctx, req.ReferenceID)
45+
_ = mirror_module.SyncPushMirror(ctx, req.ReferenceID)
4546
case PullMirrorType:
4647
_ = SyncPullMirror(ctx, req.ReferenceID)
4748
default:

services/mirror/mirror_push.go

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,69 @@
55
package mirror
66

77
import (
8-
// Implementations related to push mirrors are in `services/pushmirror` package
9-
// to avoid circular imports
10-
"code.gitea.io/gitea/services/pushmirror"
8+
"context"
9+
"fmt"
10+
"strings"
11+
12+
repo_model "code.gitea.io/gitea/models/repo"
13+
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/repository"
16+
"code.gitea.io/gitea/modules/util"
1117
)
1218

1319
// AddPushMirrorRemote registers the push mirror remote.
14-
var AddPushMirrorRemote = pushmirror.AddPushMirrorRemote
20+
func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error {
21+
addRemoteAndConfig := func(addr, path string) error {
22+
cmd := git.NewCommand(ctx, "remote", "add", "--mirror=push", m.RemoteName, addr)
23+
if strings.Contains(addr, "://") && strings.Contains(addr, "@") {
24+
cmd.SetDescription(fmt.Sprintf("remote add %s --mirror=push %s [repo_path: %s]", m.RemoteName, util.SanitizeCredentialURLs(addr), path))
25+
} else {
26+
cmd.SetDescription(fmt.Sprintf("remote add %s --mirror=push %s [repo_path: %s]", m.RemoteName, addr, path))
27+
}
28+
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: path}); err != nil {
29+
return err
30+
}
31+
if _, _, err := git.NewCommand(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*").RunStdString(&git.RunOpts{Dir: path}); err != nil {
32+
return err
33+
}
34+
if _, _, err := git.NewCommand(ctx, "config", "--add", "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*").RunStdString(&git.RunOpts{Dir: path}); err != nil {
35+
return err
36+
}
37+
return nil
38+
}
39+
40+
if err := addRemoteAndConfig(addr, m.Repo.RepoPath()); err != nil {
41+
return err
42+
}
43+
44+
if m.Repo.HasWiki() {
45+
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
46+
if len(wikiRemoteURL) > 0 {
47+
if err := addRemoteAndConfig(wikiRemoteURL, m.Repo.WikiPath()); err != nil {
48+
return err
49+
}
50+
}
51+
}
52+
53+
return nil
54+
}
1555

1656
// RemovePushMirrorRemote removes the push mirror remote.
17-
var RemovePushMirrorRemote = pushmirror.RemovePushMirrorRemote
57+
func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error {
58+
cmd := git.NewCommand(ctx, "remote", "rm", m.RemoteName)
59+
_ = m.GetRepository()
60+
61+
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: m.Repo.RepoPath()}); err != nil {
62+
return err
63+
}
64+
65+
if m.Repo.HasWiki() {
66+
if _, _, err := cmd.RunStdString(&git.RunOpts{Dir: m.Repo.WikiPath()}); err != nil {
67+
// The wiki remote may not exist
68+
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
69+
}
70+
}
1871

19-
// SyncPushMirror starts the sync of the push mirror and schedules the next run.
20-
var SyncPushMirror = pushmirror.SyncPushMirror
72+
return nil
73+
}

0 commit comments

Comments
 (0)