Skip to content

Commit 40f41dc

Browse files
authored
Correctly adjust mirror url (#6593) (#6594)
1 parent a63b9fb commit 40f41dc

File tree

5 files changed

+70
-15
lines changed

5 files changed

+70
-15
lines changed

models/repo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,9 +1077,11 @@ func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
10771077
}
10781078
}
10791079

1080-
if err := cleanUpMigrateGitConfig(repo.GitConfigPath()); err != nil {
1081-
return repo, fmt.Errorf("cleanUpMigrateGitConfig: %v", err)
1080+
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
1081+
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
1082+
return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
10821083
}
1084+
10831085
if repo.HasUncyclo() {
10841086
if err := cleanUpMigrateGitConfig(path.Join(repo.UncycloPath(), "config")); err != nil {
10851087
return repo, fmt.Errorf("cleanUpMigrateGitConfig (wiki): %v", err)

models/repo_mirror.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020

2121
"github.com/Unknwon/com"
2222
"github.com/go-xorm/xorm"
23-
"gopkg.in/ini.v1"
2423
)
2524

2625
// MirrorQueue holds an UniqueQueue object of the mirror
@@ -71,11 +70,18 @@ func (m *Mirror) ScheduleNextUpdate() {
7170
}
7271

7372
func remoteAddress(repoPath string) (string, error) {
74-
cfg, err := ini.Load(GitConfigPath(repoPath))
73+
cmd := git.NewCommand("remote", "get-url", "origin")
74+
result, err := cmd.RunInDir(repoPath)
7575
if err != nil {
76+
if strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
77+
return "", nil
78+
}
7679
return "", err
7780
}
78-
return cfg.Section("remote \"origin\"").Key("url").Value(), nil
81+
if len(result) > 0 {
82+
return result[:len(result)-1], nil
83+
}
84+
return "", nil
7985
}
8086

8187
func (m *Mirror) readAddress() {
@@ -115,14 +121,15 @@ func (m *Mirror) FullAddress() string {
115121

116122
// SaveAddress writes new address to Git repository config.
117123
func (m *Mirror) SaveAddress(addr string) error {
118-
configPath := m.Repo.GitConfigPath()
119-
cfg, err := ini.Load(configPath)
120-
if err != nil {
121-
return fmt.Errorf("Load: %v", err)
124+
repoPath := m.Repo.RepoPath()
125+
// Remove old origin
126+
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
127+
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
128+
return err
122129
}
123130

124-
cfg.Section("remote \"origin\"").Key("url").SetValue(addr)
125-
return cfg.SaveToIndent(configPath, "\t")
131+
_, err = git.NewCommand("remote", "add", "origin", addr).RunInDir(repoPath)
132+
return err
126133
}
127134

128135
// gitShortEmptySha Git short empty SHA

options/locale/locale_en-US.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,9 @@ mirror_prune_desc = Remove obsolete remote-tracking references
566566
mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync.
567567
mirror_interval_invalid = The mirror interval is not valid.
568568
mirror_address = Clone From URL
569-
mirror_address_desc = Include any required authorization credentials in the URL.
569+
mirror_address_desc = Include any required authorization credentials in the URL. These must be url escaped as appropriate
570+
mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly.
571+
mirror_address_protocol_invalid = The provided url is invalid. Only http(s):// or git:// locations can be mirrored from.
570572
mirror_last_synced = Last Synchronized
571573
watchers = Watchers
572574
stargazers = Stargazers

routers/repo/setting.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ package repo
77

88
import (
99
"errors"
10+
"net/url"
11+
"regexp"
1012
"strings"
1113
"time"
1214

1315
"code.gitea.io/git"
14-
1516
"code.gitea.io/gitea/models"
1617
"code.gitea.io/gitea/modules/auth"
1718
"code.gitea.io/gitea/modules/base"
@@ -21,6 +22,8 @@ import (
2122
"code.gitea.io/gitea/modules/util"
2223
"code.gitea.io/gitea/modules/validation"
2324
"code.gitea.io/gitea/routers/utils"
25+
26+
"github.com/mvdan/xurls"
2427
)
2528

2629
const (
@@ -33,6 +36,8 @@ const (
3336
tplProtectedBranch base.TplName = "repo/settings/protected_branch"
3437
)
3538

39+
var validFormAddress *regexp.Regexp
40+
3641
// Settings show a repository's settings page
3742
func Settings(ctx *context.Context) {
3843
ctx.Data["Title"] = ctx.Tr("repo.settings")
@@ -146,7 +151,38 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
146151
return
147152
}
148153
}
149-
if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
154+
155+
// Validate the form.MirrorAddress
156+
u, err := url.Parse(form.MirrorAddress)
157+
if err != nil {
158+
ctx.Data["Err_MirrorAddress"] = true
159+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
160+
return
161+
}
162+
163+
if u.Opaque != "" || !(u.Scheme == "http" || u.Scheme == "https" || u.Scheme == "git") {
164+
ctx.Data["Err_MirrorAddress"] = true
165+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_protocol_invalid"), tplSettingsOptions, &form)
166+
return
167+
}
168+
169+
// Now use xurls
170+
address := validFormAddress.FindString(form.MirrorAddress)
171+
if address != form.MirrorAddress && form.MirrorAddress != "" {
172+
ctx.Data["Err_MirrorAddress"] = true
173+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
174+
return
175+
}
176+
177+
if u.EscapedPath() == "" || u.Host == "" || !u.IsAbs() {
178+
ctx.Data["Err_MirrorAddress"] = true
179+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
180+
return
181+
}
182+
183+
address = u.String()
184+
185+
if err := ctx.Repo.Mirror.SaveAddress(address); err != nil {
150186
ctx.ServerError("SaveAddress", err)
151187
return
152188
}
@@ -683,3 +719,11 @@ func DeleteDeployKey(ctx *context.Context) {
683719
"redirect": ctx.Repo.RepoLink + "/settings/keys",
684720
})
685721
}
722+
723+
func init() {
724+
var err error
725+
validFormAddress, err = xurls.StrictMatchingScheme(`(https?)|(git)://`)
726+
if err != nil {
727+
panic(err)
728+
}
729+
}

templates/repo/settings/options.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<label for="interval">{{.i18n.Tr "repo.mirror_interval"}}</label>
5959
<input id="interval" name="interval" value="{{.MirrorInterval}}">
6060
</div>
61-
<div class="field">
61+
<div class="field {{if .Err_MirrorAddress}}error{{end}}">
6262
<label for="mirror_address">{{.i18n.Tr "repo.mirror_address"}}</label>
6363
<input id="mirror_address" name="mirror_address" value="{{.Mirror.FullAddress}}" required>
6464
<p class="help">{{.i18n.Tr "repo.mirror_address_desc"}}</p>

0 commit comments

Comments
 (0)