From c05717a5f086c172e67b0bb9cd351e0e972de93d Mon Sep 17 00:00:00 2001 From: Unknwon Date: Tue, 4 Apr 2017 20:42:18 -0400 Subject: [PATCH] models/mirror: feed git.IsRepoURLAccessible with raw mirror address --- models/mirror.go | 24 +++++++++++++++--------- routers/api/v1/repo/repo.go | 2 +- routers/repo/http.go | 1 - routers/repo/repo.go | 4 ++-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/models/mirror.go b/models/mirror.go index 754ee25fa..30a0155f4 100644 --- a/models/mirror.go +++ b/models/mirror.go @@ -121,13 +121,13 @@ func (m *Mirror) readAddress() { log.Error(2, "Load: %v", err) return } - m.address = unescapeMirrorCredentials(cfg.Section("remote \"origin\"").Key("url").Value()) + m.address = cfg.Section("remote \"origin\"").Key("url").Value() } -// HandleCloneUserCredentials replaces user credentials from HTTP/HTTPS URL +// HandleMirrorCredentials replaces user credentials from HTTP/HTTPS URL // with placeholder . -// It will fail for any other forms of clone addresses. -func HandleCloneUserCredentials(url string, mosaics bool) string { +// It returns original string if protocol is not HTTP/HTTPS. +func HandleMirrorCredentials(url string, mosaics bool) string { i := strings.Index(url, "@") if i == -1 { return url @@ -145,21 +145,27 @@ func HandleCloneUserCredentials(url string, mosaics bool) string { // Address returns mirror address from Git repository config without credentials. func (m *Mirror) Address() string { m.readAddress() - return HandleCloneUserCredentials(m.address, false) + return HandleMirrorCredentials(m.address, false) } // MosaicsAddress returns mirror address from Git repository config with credentials under mosaics. func (m *Mirror) MosaicsAddress() string { m.readAddress() - return HandleCloneUserCredentials(m.address, true) + return HandleMirrorCredentials(m.address, true) } -// FullAddress returns mirror address from Git repository config. -func (m *Mirror) FullAddress() string { +// RawAddress returns raw mirror address directly from Git repository config. +func (m *Mirror) RawAddress() string { m.readAddress() return m.address } +// FullAddress returns mirror address from Git repository config with unescaped credentials. +func (m *Mirror) FullAddress() string { + m.readAddress() + return unescapeMirrorCredentials(m.address) +} + // escapeCredentials returns mirror address with escaped credentials. func escapeMirrorCredentials(addr string) string { start, end, found := findPasswordInMirrorAddress(addr) @@ -191,7 +197,7 @@ func (m *Mirror) runSync() bool { // Do a fast-fail testing against on repository URL to ensure it is accessible under // good condition to prevent long blocking on URL resolution without syncing anything. if !git.IsRepoURLAccessible(git.NetworkOptions{ - URL: m.FullAddress(), + URL: m.RawAddress(), Timeout: 10 * time.Second, }) { desc := fmt.Sprintf("Source URL of mirror repository '%s' is not accessible: %s", m.Repo.FullName(), m.MosaicsAddress()) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index d7109f78b..e0f62baf5 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -270,7 +270,7 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) { log.Error(4, "DeleteRepository: %v", errDelete) } } - ctx.Error(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true)) + ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true)) return } diff --git a/routers/repo/http.go b/routers/repo/http.go index 7c1f690e7..b3b6aa1a3 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -111,7 +111,6 @@ func HTTPContexter() macaron.Handler { askCredentials(c, http.StatusUnauthorized, "") return } - fmt.Println(authUsername, authPassword) authUser, err := models.UserSignIn(authUsername, authPassword) if err != nil && !errors.IsUserNotExist(err) { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 3626ac49c..a3e5291d9 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -217,11 +217,11 @@ func MigratePost(ctx *context.Context, f form.MigrateRepo) { if strings.Contains(err.Error(), "Authentication failed") || strings.Contains(err.Error(), "could not read Username") { ctx.Data["Err_Auth"] = true - ctx.RenderWithErr(ctx.Tr("form.auth_failed", models.HandleCloneUserCredentials(err.Error(), true)), MIGRATE, &f) + ctx.RenderWithErr(ctx.Tr("form.auth_failed", models.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f) return } else if strings.Contains(err.Error(), "fatal:") { ctx.Data["Err_CloneAddr"] = true - ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", models.HandleCloneUserCredentials(err.Error(), true)), MIGRATE, &f) + ctx.RenderWithErr(ctx.Tr("repo.migrate.failed", models.HandleMirrorCredentials(err.Error(), true)), MIGRATE, &f) return }