fix display bare repo

This commit is contained in:
slene 2014-03-30 13:30:17 +08:00
parent e2f63d81d3
commit d6c9e3413a
5 changed files with 72 additions and 53 deletions

View File

@ -17,10 +17,20 @@ import (
"github.com/gogits/gogs/modules/base" "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) { return func(ctx *Context, params martini.Params) {
// assign false first // valid brachname
ctx.Data["IsRepositoryValid"] = false 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 ( var (
user *models.User user *models.User
@ -71,6 +81,8 @@ func RepoAssignment(redirect bool) martini.Handler {
} }
ctx.Repo.Repository = repo ctx.Repo.Repository = repo
ctx.Data["IsBareRepo"] = ctx.Repo.Repository.IsBare
gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName)) gitRepo, err := git.OpenRepository(models.RepoPath(userName, repoName))
if err != nil { if err != nil {
ctx.Handle(404, "RepoAssignment Invalid repo "+models.RepoPath(userName, repoName), err) 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["Owner"] = user
ctx.Data["RepoLink"] = ctx.Repo.RepoLink ctx.Data["RepoLink"] = ctx.Repo.RepoLink
ctx.Data["IsRepositoryOwner"] = ctx.Repo.IsOwner 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.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.CloneLink.HTTPS = fmt.Sprintf("%s%s/%s.git", base.AppUrl, user.LowerName, repo.LowerName)
ctx.Data["CloneLink"] = ctx.Repo.CloneLink ctx.Data["CloneLink"] = ctx.Repo.CloneLink
if repo.IsBare { // when repo is bare, not valid branch
ctx.Data["IsBareRepo"] = true if !ctx.Repo.Repository.IsBare && validBranch {
ctx.HTML(200, "repo/single_bare") detect:
return 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
}
} }
detect: // repo is bare and display enable
if len(branchName) > 0 { if displayBare && ctx.Repo.Repository.IsBare {
// TODO check tag ctx.HTML(200, "repo/single_bare")
if models.IsBranchExist(user.Name, repoName, branchName) { return
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
} }
if ctx.IsSigned { if ctx.IsSigned {

View File

@ -78,6 +78,7 @@ func Single(ctx *middleware.Context, params martini.Params) {
ctx.Handle(404, "repo.Single(GetBranches)", err) ctx.Handle(404, "repo.Single(GetBranches)", err)
return return
} }
ctx.Data["Branches"] = brs ctx.Data["Branches"] = brs
isViewBranch := ctx.Repo.IsBranch isViewBranch := ctx.Repo.IsBranch

View File

@ -1,6 +1,7 @@
{{template "base/head" .}} {{template "base/head" .}}
{{template "base/navbar" .}} {{template "base/navbar" .}}
{{template "repo/nav" .}} {{template "repo/nav" .}}
{{template "repo/toolbar" .}}
<div id="body" class="container"> <div id="body" class="container">
<div id="source"> <div id="source">
<div class="panel panel-default guide-box clone-group-btn"> <div class="panel panel-default guide-box clone-group-btn">
@ -23,15 +24,15 @@
<hr/> <hr/>
<h3>Create a new repository on the command line</h3> <h3>Create a new repository on the command line</h3>
<pre class="text-left"><code>touch README.md <pre class="text-left"><code>touch README.md
git init git init
git add README.md git add README.md
git commit -m "first commit" git commit -m "first commit"
git remote add origin <span class="clone-url"></span> git remote add origin <span class="clone-url"></span>
git push -u origin master</code></pre> git push -u origin master</code></pre>
<hr/> <hr/>
<h3>Push an existing repository from the command line</h3> <h3>Push an existing repository from the command line</h3>
<pre class="text-left"><code>git remote add origin <span class="clone-url"></span> <pre class="text-left"><code>git remote add origin <span class="clone-url"></span>
git push -u origin master</code></pre> git push -u origin master</code></pre>
</div> </div>
</div> </div>
</div> </div>

View File

@ -3,7 +3,7 @@
<nav class="navbar navbar-toolbar navbar-default" role="navigation"> <nav class="navbar navbar-toolbar navbar-default" role="navigation">
<div class="collapse navbar-collapse"> <div class="collapse navbar-collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li class="{{if .IsRepoToolbarSource}}active{{end}}"><a href="{{.RepoLink}}{{if ne .BranchName `master`}}/src/{{.BranchName}}{{end}}">Source</a></li> <li class="{{if .IsRepoToolbarSource}}active{{end}}"><a href="{{.RepoLink}}{{if .BranchName}}{{if ne .BranchName `master`}}/src/{{.BranchName}}{{end}}{{end}}">Source</a></li>
{{if not .IsBareRepo}} {{if not .IsBareRepo}}
<li class="{{if .IsRepoToolbarCommits}}active{{end}}"><a href="{{.RepoLink}}/commits/{{.BranchName}}">Commits</a></li> <li class="{{if .IsRepoToolbarCommits}}active{{end}}"><a href="{{.RepoLink}}/commits/{{.BranchName}}">Commits</a></li>
<!-- <li class="{{if .IsRepoToolbarBranches}}active{{end}}"><a href="{{.RepoLink}}/branches">Branches</a></li> --> <!-- <li class="{{if .IsRepoToolbarBranches}}active{{end}}"><a href="{{.RepoLink}}/branches">Branches</a></li> -->

13
web.go
View File

@ -136,19 +136,20 @@ func runWeb(*cli.Context) {
r.Get("/issues/:index", repo.ViewIssue) r.Get("/issues/:index", repo.ViewIssue)
r.Get("/pulls", repo.Pulls) r.Get("/pulls", repo.Pulls)
r.Get("/branches", repo.Branches) r.Get("/branches", repo.Branches)
}, ignSignIn, middleware.RepoAssignment(true))
m.Group("/:username/:reponame", func(r martini.Router) {
r.Get("/src/:branchname", repo.Single) r.Get("/src/:branchname", repo.Single)
r.Get("/src/:branchname/**", repo.Single) r.Get("/src/:branchname/**", repo.Single)
r.Get("/raw/:branchname/**", repo.SingleDownload) r.Get("/raw/:branchname/**", repo.SingleDownload)
r.Get("/commits/:branchname", repo.Commits) r.Get("/commits/:branchname", repo.Commits)
}, ignSignIn, middleware.RepoAssignment(true)) r.Get("/commit/:branchname", repo.Diff)
r.Get("/commit/:branchname/**", repo.Diff)
m.Get("/:username/:reponame/commit/:branchname/**", ignSignIn, middleware.RepoAssignment(true), repo.Diff) }, ignSignIn, middleware.RepoAssignment(true, true))
m.Get("/:username/:reponame/commit/:branchname", ignSignIn, middleware.RepoAssignment(true), repo.Diff)
m.Group("/:username", func(r martini.Router) { m.Group("/:username", func(r martini.Router) {
r.Get("/:reponame", middleware.RepoAssignment(true), repo.Single)
r.Get("/:reponame", middleware.RepoAssignment(true), repo.Single)
r.Any("/:reponame/**", repo.Http) r.Any("/:reponame/**", repo.Http)
r.Get("/:reponame", middleware.RepoAssignment(true, true, true), repo.Single)
}, ignSignIn) }, ignSignIn)
// Not found handler. // Not found handler.