Skip to content

Commit bac388d

Browse files
zeripathtechknowlogick
authored andcommitted
Correctly adjust mirror url (#6593) (#6595)
1 parent 4ff6eff commit bac388d

File tree

12 files changed

+2102
-21
lines changed

12 files changed

+2102
-21
lines changed

.drone.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pipeline:
8282
when:
8383
event: [ push, tag, pull_request ]
8484

85-
test:
85+
unit-test:
8686
image: golang:1.11
8787
pull: true
8888
group: test
@@ -94,7 +94,7 @@ pipeline:
9494
event: [ push, pull_request ]
9595
branch: [ master ]
9696

97-
test:
97+
release-test:
9898
image: golang:1.11
9999
pull: true
100100
group: test
@@ -106,7 +106,7 @@ pipeline:
106106
event: [ push, pull_request ]
107107
branch: [ release/* ]
108108

109-
test:
109+
tag-test:
110110
image: golang:1.11
111111
pull: true
112112
group: test
@@ -146,7 +146,7 @@ pipeline:
146146
event: [ push, pull_request ]
147147
branch: [ master ]
148148

149-
test-mysql:
149+
tag-test-mysql:
150150
image: golang:1.11
151151
pull: true
152152
group: test
@@ -240,12 +240,13 @@ pipeline:
240240
event: [ push ]
241241
branch: [ master ]
242242

243-
docker:
243+
release-docker:
244244
image: plugins/docker:17.12
245245
pull: true
246246
secrets: [ docker_username, docker_password ]
247247
repo: gitea/gitea
248248
tags: [ '${DRONE_BRANCH##release/v}' ]
249+
cache_from: gitea/gitea
249250
when:
250251
event: [ push ]
251252
branch: [ release/* ]
@@ -255,6 +256,7 @@ pipeline:
255256
secrets: [ docker_username, docker_password ]
256257
pull: true
257258
repo: gitea/gitea
259+
cache_from: gitea/gitea
258260
default_tags: true
259261
when:
260262
event: [ push, tag ]
@@ -271,7 +273,7 @@ pipeline:
271273
when:
272274
event: [ push, tag ]
273275

274-
release:
276+
tag-release:
275277
image: plugins/s3:1
276278
pull: true
277279
secrets: [ aws_access_key_id, aws_secret_access_key ]
@@ -285,7 +287,7 @@ pipeline:
285287
when:
286288
event: [ tag ]
287289

288-
release:
290+
release-branch-release:
289291
image: plugins/s3:1
290292
pull: true
291293
secrets: [ aws_access_key_id, aws_secret_access_key ]

Gopkg.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

models/repo.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,11 @@ func CleanUpMigrateInfo(repo *Repository) (*Repository, error) {
10661066
}
10671067
}
10681068

1069-
if err := cleanUpMigrateGitConfig(repo.GitConfigPath()); err != nil {
1070-
return repo, fmt.Errorf("cleanUpMigrateGitConfig: %v", err)
1069+
_, err := git.NewCommand("remote", "remove", "origin").RunInDir(repoPath)
1070+
if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
1071+
return repo, fmt.Errorf("CleanUpMigrateInfo: %v", err)
10711072
}
1073+
10721074
if repo.HasWiki() {
10731075
if err := cleanUpMigrateGitConfig(path.Join(repo.WikiPath(), "config")); err != nil {
10741076
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
@@ -528,7 +528,9 @@ mirror_prune_desc = Remove obsolete remote-tracking references
528528
mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync.
529529
mirror_interval_invalid = The mirror interval is not valid.
530530
mirror_address = Clone From URL
531-
mirror_address_desc = Include any required authorization credentials in the URL.
531+
mirror_address_desc = Include any required authorization credentials in the URL. These must be url escaped as appropriate
532+
mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly.
533+
mirror_address_protocol_invalid = The provided url is invalid. Only http(s):// or git:// locations can be mirrored from.
532534
mirror_last_synced = Last Synchronized
533535
watchers = Watchers
534536
stargazers = Stargazers

routers/repo/setting.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package repo
77

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

@@ -21,6 +23,8 @@ import (
2123
"code.gitea.io/gitea/modules/util"
2224
"code.gitea.io/gitea/modules/validation"
2325
"code.gitea.io/gitea/routers/utils"
26+
27+
"github.com/mvdan/xurls"
2428
)
2529

2630
const (
@@ -33,6 +37,8 @@ const (
3337
tplProtectedBranch base.TplName = "repo/settings/protected_branch"
3438
)
3539

40+
var validFormAddress *regexp.Regexp
41+
3642
// Settings show a repository's settings page
3743
func Settings(ctx *context.Context) {
3844
ctx.Data["Title"] = ctx.Tr("repo.settings")
@@ -140,7 +146,38 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
140146
return
141147
}
142148
}
143-
if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
149+
150+
// Validate the form.MirrorAddress
151+
u, err := url.Parse(form.MirrorAddress)
152+
if err != nil {
153+
ctx.Data["Err_MirrorAddress"] = true
154+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
155+
return
156+
}
157+
158+
if u.Opaque != "" || !(u.Scheme == "http" || u.Scheme == "https" || u.Scheme == "git") {
159+
ctx.Data["Err_MirrorAddress"] = true
160+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_protocol_invalid"), tplSettingsOptions, &form)
161+
return
162+
}
163+
164+
// Now use xurls
165+
address := validFormAddress.FindString(form.MirrorAddress)
166+
if address != form.MirrorAddress && form.MirrorAddress != "" {
167+
ctx.Data["Err_MirrorAddress"] = true
168+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
169+
return
170+
}
171+
172+
if u.EscapedPath() == "" || u.Host == "" || !u.IsAbs() {
173+
ctx.Data["Err_MirrorAddress"] = true
174+
ctx.RenderWithErr(ctx.Tr("repo.mirror_address_url_invalid"), tplSettingsOptions, &form)
175+
return
176+
}
177+
178+
address = u.String()
179+
180+
if err := ctx.Repo.Mirror.SaveAddress(address); err != nil {
144181
ctx.ServerError("SaveAddress", err)
145182
return
146183
}
@@ -618,3 +655,11 @@ func DeleteDeployKey(ctx *context.Context) {
618655
"redirect": ctx.Repo.RepoLink + "/settings/keys",
619656
})
620657
}
658+
659+
func init() {
660+
var err error
661+
validFormAddress, err = xurls.StrictMatchingScheme(`(https?)|(git)://`)
662+
if err != nil {
663+
panic(err)
664+
}
665+
}

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>

vendor/github.com/mvdan/xurls/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)