aboutsummaryrefslogtreecommitdiff
path: root/models/mirror.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/mirror.go')
-rw-r--r--models/mirror.go30
1 files changed, 29 insertions, 1 deletions
diff --git a/models/mirror.go b/models/mirror.go
index 4820520e..46ef7452 100644
--- a/models/mirror.go
+++ b/models/mirror.go
@@ -6,6 +6,7 @@ package models
import (
"fmt"
+ "net/url"
"strings"
"time"
@@ -119,6 +120,33 @@ func (m *Mirror) FullAddress() string {
return m.address
}
+// escapeCredentials returns mirror address with escaped credentials.
+func escapeMirrorCredentials(addr string) string {
+ // Find end of credentials (start of path)
+ end := strings.LastIndex(addr, "@")
+ if end == -1 {
+ return addr
+ }
+
+ // Find delimiter of credentials (end of username)
+ start := strings.Index(addr, "://")
+ if start == -1 {
+ return addr
+ }
+ start += 3
+ delim := strings.Index(addr[:start], ":")
+ if delim == -1 {
+ return addr
+ }
+ delim += 1
+
+ if start+delim > end {
+ return addr // No password portion presented
+ }
+
+ return addr[:start+delim] + url.QueryEscape(addr[start+delim:end]) + addr[end:]
+}
+
// SaveAddress writes new address to Git repository config.
func (m *Mirror) SaveAddress(addr string) error {
configPath := m.Repo.GitConfigPath()
@@ -127,7 +155,7 @@ func (m *Mirror) SaveAddress(addr string) error {
return fmt.Errorf("Load: %v", err)
}
- cfg.Section("remote \"origin\"").Key("url").SetValue(addr)
+ cfg.Section("remote \"origin\"").Key("url").SetValue(escapeMirrorCredentials(addr))
return cfg.SaveToIndent(configPath, "\t")
}