From b553ea45ee29f5fc4f123c9f1083b90104ec4fe4 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 13 Dec 2014 16:46:00 -0500 Subject: [PATCH] modes/repo: incorrect SSH clone URL for #742 --- gogs.go | 2 +- models/repo.go | 21 ++++++++++++++++++ modules/middleware/context.go | 42 ++++++++++++++++------------------- modules/middleware/repo.go | 9 ++++---- routers/api/v1/repo.go | 13 +++++------ templates/.VERSION | 2 +- 6 files changed, 52 insertions(+), 37 deletions(-) diff --git a/gogs.go b/gogs.go index 88cc03a876..4f57e82c31 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/modules/setting" ) -const APP_VER = "0.5.8.1212 Beta" +const APP_VER = "0.5.8.1213 Beta" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/repo.go b/models/repo.go index bf46696dfb..3ea4efc7f0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -241,6 +241,27 @@ func IsRepositoryExist(u *User, repoName string) (bool, error) { return com.IsDir(RepoPath(u.Name, repoName)), nil } +// CloneLink represents different types of clone URLs of repository. +type CloneLink struct { + SSH string + HTTPS string + Git string +} + +// CloneLink returns clone URLs of repository. +func (repo *Repository) CloneLink() (cl CloneLink, err error) { + if err = repo.GetOwner(); err != nil { + return cl, err + } + if setting.SshPort != 22 { + cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.Domain, setting.SshPort, repo.Owner.LowerName, repo.LowerName) + } else { + cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, repo.Owner.LowerName, repo.LowerName) + } + cl.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, repo.Owner.LowerName, repo.LowerName) + return cl, nil +} + var ( illegalEquals = []string{"debug", "raw", "install", "api", "avatar", "user", "org", "help", "stars", "issues", "pulls", "commits", "repo", "template", "admin", "new"} illegalSuffixs = []string{".git", ".keys"} diff --git a/modules/middleware/context.go b/modules/middleware/context.go index fb33c48e0e..fc7814401f 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -39,29 +39,25 @@ type Context struct { IsBasicAuth bool Repo struct { - IsOwner bool - IsTrueOwner bool - IsWatching bool - IsBranch bool - IsTag bool - IsCommit bool - IsAdmin bool // Current user is admin level. - HasAccess bool - Repository *models.Repository - Owner *models.User - Commit *git.Commit - Tag *git.Tag - GitRepo *git.Repository - BranchName string - TagName string - TreeName string - CommitId string - RepoLink string - CloneLink struct { - SSH string - HTTPS string - Git string - } + IsOwner bool + IsTrueOwner bool + IsWatching bool + IsBranch bool + IsTag bool + IsCommit bool + IsAdmin bool // Current user is admin level. + HasAccess bool + Repository *models.Repository + Owner *models.User + Commit *git.Commit + Tag *git.Tag + GitRepo *git.Repository + BranchName string + TagName string + TreeName string + CommitId string + RepoLink string + CloneLink models.CloneLink CommitsCount int Mirror *models.Mirror } diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 1716194925..d143d8a86b 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -386,12 +386,11 @@ func RepoAssignment(redirect bool, args ...bool) macaron.Handler { ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner ctx.Data["IsRepositoryTrueOwner"] = ctx.Repo.IsTrueOwner - if setting.SshPort != 22 { - ctx.Repo.CloneLink.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.Domain, setting.SshPort, u.LowerName, repo.LowerName) - } else { - ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.Domain, u.LowerName, repo.LowerName) + ctx.Repo.CloneLink, err = repo.CloneLink() + if err != nil { + ctx.Handle(500, "CloneLink", err) + return } - ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", setting.AppUrl, u.LowerName, repo.LowerName) ctx.Data["CloneLink"] = ctx.Repo.CloneLink if ctx.Repo.Repository.IsGoget { diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index 6f23723164..fbf9c73ea7 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -23,20 +23,19 @@ import ( // ToApiRepository converts repository to API format. func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository { - sshUrlFmt := "%s@%s:%s/%s.git" - if setting.SshPort != 22 { - sshUrlFmt = "ssh://%s@%s:%d/%s/%s.git" + cl, err := repo.CloneLink() + if err != nil { + log.Error(4, "CloneLink: %v", err) } - htmlUrl := setting.AppUrl + owner.Name + "/" + repo.Name return &api.Repository{ Id: repo.Id, Owner: *ToApiUser(owner), FullName: owner.Name + "/" + repo.Name, Private: repo.IsPrivate, Fork: repo.IsFork, - HtmlUrl: htmlUrl, - SshUrl: fmt.Sprintf(sshUrlFmt, setting.RunUser, setting.Domain, owner.LowerName, repo.LowerName), - CloneUrl: htmlUrl + ".git", + HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name, + CloneUrl: cl.HTTPS, + SshUrl: cl.SSH, Permissions: permission, } } diff --git a/templates/.VERSION b/templates/.VERSION index e5ab0b331a..6c8af6290f 100644 --- a/templates/.VERSION +++ b/templates/.VERSION @@ -1 +1 @@ -0.5.8.1212 Beta \ No newline at end of file +0.5.8.1213 Beta \ No newline at end of file