From 105f97e61ce3d9f2bc8ce178d0ffcbf531bd91b3 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 30 Mar 2014 10:09:59 +0800 Subject: remove Context.IsValid & verify repo in middleware repo.go --- modules/middleware/context.go | 14 +++++++- modules/middleware/repo.go | 74 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 16 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/context.go b/modules/middleware/context.go index d81ab999..a6aa58ee 100644 --- a/modules/middleware/context.go +++ b/modules/middleware/context.go @@ -18,6 +18,7 @@ import ( "github.com/codegangsta/martini" "github.com/gogits/cache" + "github.com/gogits/git" "github.com/gogits/session" "github.com/gogits/gogs/models" @@ -41,11 +42,18 @@ type Context struct { csrfToken string Repo struct { - IsValid bool IsOwner bool IsWatching bool + IsBranch bool + IsTag bool + IsCommit bool Repository *models.Repository Owner *models.User + Commit *git.Commit + GitRepo *git.Repository + BranchName string + CommitId string + RepoLink string CloneLink struct { SSH string HTTPS string @@ -98,6 +106,10 @@ func (ctx *Context) Handle(status int, title string, err error) { ctx.HTML(status, fmt.Sprintf("status/%d", status)) } +func (ctx *Context) Debug(msg string, args ...interface{}) { + log.Debug(msg, args...) +} + func (ctx *Context) GetCookie(name string) string { cookie, err := ctx.Req.Cookie(name) if err != nil { diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 54b735af..d1b68b05 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -11,6 +11,8 @@ import ( "github.com/codegangsta/martini" + "github.com/gogits/git" + "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" ) @@ -25,8 +27,12 @@ func RepoAssignment(redirect bool) martini.Handler { err error ) + userName := params["username"] + repoName := params["reponame"] + branchName := params["branchname"] + // get repository owner - ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(params["username"]) + ctx.Repo.IsOwner = ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) if !ctx.Repo.IsOwner { user, err = models.GetUserByName(params["username"]) @@ -51,10 +57,8 @@ func RepoAssignment(redirect bool) martini.Handler { return } - ctx.Repo.Owner = user - // get repository - repo, err := models.GetRepositoryByName(user.Id, params["reponame"]) + repo, err := models.GetRepositoryByName(user.Id, repoName) if err != nil { if err == models.ErrRepoNotExist { ctx.Handle(404, "RepoAssignment", err) @@ -62,29 +66,69 @@ func RepoAssignment(redirect bool) martini.Handler { ctx.Redirect("/") return } - ctx.Handle(200, "RepoAssignment", err) + ctx.Handle(404, "RepoAssignment", err) + return + } + ctx.Repo.Repository = repo + + gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) + if err != nil { + ctx.Handle(404, "RepoAssignment Invalid repo", err) return } + ctx.Repo.GitRepo = gitRepo + + detect: + if len(branchName) > 0 { + // TODO check tag + if models.IsBranchExist(user.Name, repoName, branchName) { + ctx.Repo.IsBranch = true + ctx.Repo.BranchName = branchName + + ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName) + if err != nil { + ctx.Handle(404, "RepoAssignment invalid branch", nil) + return + } + + ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String() + + } else if len(branchName) == 40 { + ctx.Repo.IsCommit = true + ctx.Repo.CommitId = branchName + ctx.Repo.BranchName = branchName + + ctx.Repo.Commit, err = gitRepo.GetCommit(branchName) + if err != nil { + ctx.Handle(404, "RepoAssignment invalid commit", nil) + return + } + } else { + ctx.Handle(404, "RepoAssignment invalid repo", nil) + return + } + + } else { + branchName = "master" + goto detect + } - ctx.Repo.IsValid = true - if ctx.User != nil { + if ctx.IsSigned { ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id) } - ctx.Repo.Repository = repo + + ctx.Repo.Owner = user ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName) ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName) + ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name - if len(params["branchname"]) == 0 { - params["branchname"] = "master" - } - ctx.Data["Branchname"] = params["branchname"] - - ctx.Data["IsRepositoryValid"] = true + ctx.Data["BranchName"] = ctx.Repo.BranchName + ctx.Data["CommitId"] = ctx.Repo.CommitId ctx.Data["Repository"] = repo ctx.Data["Owner"] = user ctx.Data["Title"] = user.Name + "/" + repo.Name ctx.Data["CloneLink"] = ctx.Repo.CloneLink - ctx.Data["RepositoryLink"] = ctx.Data["Title"] + ctx.Data["RepoLink"] = ctx.Repo.RepoLink ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching } -- cgit v1.2.3 From 578d981d7e196728dc0685f62a211383d4c808fa Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 30 Mar 2014 11:38:41 +0800 Subject: display bare repo page --- modules/middleware/repo.go | 32 ++++++++++++-------- routers/repo/repo.go | 10 ------- templates/repo/single.tmpl | 4 --- templates/repo/single_bare.tmpl | 66 +++++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 55 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index d1b68b05..deb28286 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -73,11 +73,30 @@ func RepoAssignment(redirect bool) martini.Handler { gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) if err != nil { - ctx.Handle(404, "RepoAssignment Invalid repo", err) + ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) return } ctx.Repo.GitRepo = gitRepo + ctx.Repo.Owner = user + ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name + + ctx.Data["Title"] = user.Name + "/" + repo.Name + ctx.Data["Repository"] = repo + ctx.Data["Owner"] = user + ctx.Data["RepoLink"] = ctx.Repo.RepoLink + ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner + + ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName) + ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName) + ctx.Data["CloneLink"] = ctx.Repo.CloneLink + + if repo.IsBare { + ctx.Data["IsBareRepo"] = true + ctx.HTML(200, "repo/single_bare") + return + } + detect: if len(branchName) > 0 { // TODO check tag @@ -117,19 +136,8 @@ func RepoAssignment(redirect bool) martini.Handler { ctx.Repo.IsWatching = models.IsWatching(ctx.User.Id, repo.Id) } - ctx.Repo.Owner = user - ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName) - ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName) - ctx.Repo.RepoLink = "/" + user.Name + "/" + repo.Name - ctx.Data["BranchName"] = ctx.Repo.BranchName ctx.Data["CommitId"] = ctx.Repo.CommitId - ctx.Data["Repository"] = repo - ctx.Data["Owner"] = user - ctx.Data["Title"] = user.Name + "/" + repo.Name - ctx.Data["CloneLink"] = ctx.Repo.CloneLink - ctx.Data["RepoLink"] = ctx.Repo.RepoLink - ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner ctx.Data["IsRepositoryWatching"] = ctx.Repo.IsWatching } } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index c9c9af1e..27c806a3 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -77,10 +77,6 @@ func Single(ctx *middleware.Context, params martini.Params) { if err != nil { ctx.Handle(404, "repo.Single(GetBranches)", err) return - } else if ctx.Repo.Repository.IsBare { - ctx.Data["IsBareRepo"] = true - ctx.HTML(200, "repo/single") - return } ctx.Data["Branches"] = brs @@ -264,12 +260,6 @@ func Setting(ctx *middleware.Context, params martini.Params) { ctx.Data["IsRepoToolbarSetting"] = true - if ctx.Repo.Repository.IsBare { - ctx.Data["IsBareRepo"] = true - ctx.HTML(200, "repo/setting") - return - } - var title string if t, ok := ctx.Data["Title"].(string); ok { title = t diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index abaa4e89..0bf7b0aa 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -4,9 +4,6 @@ {{template "repo/toolbar" .}}
- {{if .IsBareRepo}} - {{template "repo/single_bare" .}} - {{else}}
{{ $n := len .Treenames}} {{if not .IsFile}}{{end}} @@ -38,7 +35,6 @@ {{else}} {{template "repo/single_list" .}} {{end}} - {{end}}
{{template "base/footer" .}} diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index ed182ad2..fe0e3aa3 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -1,31 +1,39 @@ -
-
-

Quick Guide

-
-
-

Clone this repository

-
- - - - - - - - +{{template "base/head" .}} +{{template "base/navbar" .}} +{{template "repo/nav" .}} +
+
+
+
+

Quick Guide

+
+
+

Clone this repository

+
+ + + + + + + + +
+

We recommend every repository include a README, LICENSE, and .gitignore.

+
+

Create a new repository on the command line

+
touch README.md
+        git init
+        git add README.md
+        git commit -m "first commit"
+        git remote add origin 
+        git push -u origin master
+
+

Push an existing repository from the command line

+
git remote add origin 
+        git push -u origin master
+
-

We recommend every repository include a README, LICENSE, and .gitignore.

-
-

Create a new repository on the command line

-
touch README.md
-git init
-git add README.md
-git commit -m "first commit"
-git remote add origin 
-git push -u origin master
-
-

Push an existing repository from the command line

-
git remote add origin 
-git push -u origin master
-
\ No newline at end of file +
+{{template "base/footer" .}} -- cgit v1.2.3 From d6c9e3413a982fd64a74e774d4a2b8157f39f299 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 30 Mar 2014 13:30:17 +0800 Subject: fix display bare repo --- modules/middleware/repo.go | 88 ++++++++++++++++++++++++----------------- routers/repo/repo.go | 1 + templates/repo/single_bare.tmpl | 13 +++--- templates/repo/toolbar.tmpl | 2 +- web.go | 13 +++--- 5 files changed, 68 insertions(+), 49 deletions(-) (limited to 'modules/middleware') diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index deb28286..559e90c0 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -17,10 +17,20 @@ import ( "github.com/gogits/gogs/modules/base" ) -func RepoAssignment(redirect bool) martini.Handler { +func RepoAssignment(redirect bool, args ...bool) martini.Handler { return func(ctx *Context, params martini.Params) { - // assign false first - ctx.Data["IsRepositoryValid"] = false + // valid brachname + var validBranch bool + // display bare quick start if it is a bare repo + var displayBare bool + + if len(args) >= 1 { + validBranch = args[0] + } + + if len(args) >= 2 { + displayBare = args[1] + } var ( user *models.User @@ -71,6 +81,8 @@ func RepoAssignment(redirect bool) martini.Handler { } ctx.Repo.Repository = repo + ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare + gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) if err != nil { ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) @@ -86,50 +98,54 @@ func RepoAssignment(redirect bool) martini.Handler { ctx.Data["Owner"] = user ctx.Data["RepoLink"] = ctx.Repo.RepoLink ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner + ctx.Data["BranchName"] = "" ctx.Repo.CloneLink.SSH = fmt.Sprintf("%s@%s:%s/%s.git", base.RunUser, base.Domain, user.LowerName, repo.LowerName) ctx.Repo.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName) ctx.Data["CloneLink"] = ctx.Repo.CloneLink - if repo.IsBare { - ctx.Data["IsBareRepo"] = true - ctx.HTML(200, "repo/single_bare") - return - } - - detect: - if len(branchName) > 0 { - // TODO check tag - if models.IsBranchExist(user.Name, repoName, branchName) { - ctx.Repo.IsBranch = true - ctx.Repo.BranchName = branchName - - ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName) - if err != nil { - ctx.Handle(404, "RepoAssignment invalid branch", nil) + // when repo is bare, not valid branch + if !ctx.Repo.Repository.IsBare && validBranch { + detect: + if len(branchName) > 0 { + // TODO check tag + if models.IsBranchExist(user.Name, repoName, branchName) { + ctx.Repo.IsBranch = true + ctx.Repo.BranchName = branchName + + ctx.Repo.Commit, err = gitRepo.GetCommitOfBranch(branchName) + if err != nil { + ctx.Handle(404, "RepoAssignment invalid branch", nil) + return + } + + ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String() + + } else if len(branchName) == 40 { + ctx.Repo.IsCommit = true + ctx.Repo.CommitId = branchName + ctx.Repo.BranchName = branchName + + ctx.Repo.Commit, err = gitRepo.GetCommit(branchName) + if err != nil { + ctx.Handle(404, "RepoAssignment invalid commit", nil) + return + } + } else { + ctx.Handle(404, "RepoAssignment invalid repo", nil) return } - ctx.Repo.CommitId = ctx.Repo.Commit.Oid.String() - - } else if len(branchName) == 40 { - ctx.Repo.IsCommit = true - ctx.Repo.CommitId = branchName - ctx.Repo.BranchName = branchName - - ctx.Repo.Commit, err = gitRepo.GetCommit(branchName) - if err != nil { - ctx.Handle(404, "RepoAssignment invalid commit", nil) - return - } } else { - ctx.Handle(404, "RepoAssignment invalid repo", nil) - return + branchName = "master" + goto detect } + } - } else { - branchName = "master" - goto detect + // repo is bare and display enable + if displayBare && ctx.Repo.Repository.IsBare { + ctx.HTML(200, "repo/single_bare") + return } if ctx.IsSigned { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 27c806a3..5b0a165d 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -78,6 +78,7 @@ func Single(ctx *middleware.Context, params martini.Params) { ctx.Handle(404, "repo.Single(GetBranches)", err) return } + ctx.Data["Branches"] = brs isViewBranch := ctx.Repo.IsBranch diff --git a/templates/repo/single_bare.tmpl b/templates/repo/single_bare.tmpl index fe0e3aa3..035e78e8 100644 --- a/templates/repo/single_bare.tmpl +++ b/templates/repo/single_bare.tmpl @@ -1,6 +1,7 @@ {{template "base/head" .}} {{template "base/navbar" .}} {{template "repo/nav" .}} +{{template "repo/toolbar" .}}
@@ -23,15 +24,15 @@

Create a new repository on the command line

touch README.md
-        git init
-        git add README.md
-        git commit -m "first commit"
-        git remote add origin 
-        git push -u origin master
+git init +git add README.md +git commit -m "first commit" +git remote add origin +git push -u origin master

Push an existing repository from the command line

git remote add origin 
-        git push -u origin master
+git push -u origin master
diff --git a/templates/repo/toolbar.tmpl b/templates/repo/toolbar.tmpl index 3c7c8a50..542f8fd8 100644 --- a/templates/repo/toolbar.tmpl +++ b/templates/repo/toolbar.tmpl @@ -3,7 +3,7 @@
-{{template "base/footer" .}} \ No newline at end of file +{{template "base/footer" .}} -- cgit v1.2.3 From b1627672f9b45047976f498fe618995361e9ff35 Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 30 Mar 2014 17:09:19 +0800 Subject: fix link --- modules/middleware/repo.go | 3 +++ templates/repo/single.tmpl | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'modules/middleware') diff --git a/modules/middleware/repo.go b/modules/middleware/repo.go index 559e90c0..6912cd83 100644 --- a/modules/middleware/repo.go +++ b/modules/middleware/repo.go @@ -140,6 +140,9 @@ func RepoAssignment(redirect bool, args ...bool) martini.Handler { branchName = "master" goto detect } + + ctx.Data["IsBranch"] = ctx.Repo.IsBranch + ctx.Data["IsCommit"] = ctx.Repo.IsCommit } // repo is bare and display enable diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index 0bf7b0aa..7ee6b0ca 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -8,7 +8,7 @@ {{ $n := len .Treenames}} {{if not .IsFile}}{{end}}