From b27c34f39acee3bf7b6594a1f0db2183b343326c Mon Sep 17 00:00:00 2001 From: slene Date: Sun, 30 Mar 2014 10:13:02 +0800 Subject: [PATCH] update git api. fix link... and so on --- models/git.go | 26 ++++++++++++++----- models/repo.go | 2 +- routers/repo/branch.go | 10 ++------ routers/repo/commit.go | 18 +++++-------- routers/repo/repo.go | 51 +++++++++---------------------------- templates/issue/create.tmpl | 4 +-- templates/issue/list.tmpl | 12 ++++----- templates/issue/view.tmpl | 4 +-- templates/repo/diff.tmpl | 3 +-- templates/repo/single.tmpl | 4 +-- templates/repo/toolbar.tmpl | 20 +++++++-------- 11 files changed, 63 insertions(+), 91 deletions(-) diff --git a/models/git.go b/models/git.go index 34f0267f65..d3bad6e0ce 100644 --- a/models/git.go +++ b/models/git.go @@ -70,9 +70,12 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep return nil, err } - commit, err := repo.GetCommit(branchName, commitId) + commit, err := repo.GetCommitOfBranch(branchName) if err != nil { - return nil, err + commit, err = repo.GetCommit(commitId) + if err != nil { + return nil, err + } } parts := strings.Split(path.Clean(rpath), "/") @@ -110,13 +113,22 @@ func GetTargetFile(userName, repoName, branchName, commitId, rpath string) (*Rep } // GetReposFiles returns a list of file object in given directory of repository. -func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*RepoFile, error) { +// func GetReposFilesOfBranch(userName, repoName, branchName, rpath string) ([]*RepoFile, error) { +// return getReposFiles(userName, repoName, commitId, rpath) +// } + +// GetReposFiles returns a list of file object in given directory of repository. +func GetReposFiles(userName, repoName, commitId, rpath string) ([]*RepoFile, error) { + return getReposFiles(userName, repoName, commitId, rpath) +} + +func getReposFiles(userName, repoName, commitId string, rpath string) ([]*RepoFile, error) { repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } - commit, err := repo.GetCommit(branchName, commitId) + commit, err := repo.GetCommit(commitId) if err != nil { return nil, err } @@ -216,13 +228,13 @@ func GetReposFiles(userName, repoName, branchName, commitId, rpath string) ([]*R return append(repodirs, repofiles...), nil } -func GetCommit(userName, repoName, branchname, commitid string) (*git.Commit, error) { +func GetCommit(userName, repoName, commitId string) (*git.Commit, error) { repo, err := git.OpenRepository(RepoPath(userName, repoName)) if err != nil { return nil, err } - return repo.GetCommit(branchname, commitid) + return repo.GetCommit(commitId) } // GetCommitsByBranch returns all commits of given branch of repository. @@ -397,7 +409,7 @@ func GetDiff(repoPath, commitid string) (*Diff, error) { return nil, err } - commit, err := repo.GetCommit("", commitid) + commit, err := repo.GetCommit(commitid) if err != nil { return nil, err } diff --git a/models/repo.go b/models/repo.go index 0b2bbe4862..4110548362 100644 --- a/models/repo.go +++ b/models/repo.go @@ -364,7 +364,7 @@ func GetRepos(num, offset int) ([]UserRepo, error) { } func RepoPath(userName, repoName string) string { - return filepath.Join(UserPath(userName), repoName+".git") + return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git") } func UpdateRepository(repo *Repository) error { diff --git a/routers/repo/branch.go b/routers/repo/branch.go index aed77cfaa5..c598db436c 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -11,21 +11,15 @@ import ( ) func Branches(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsValid { - return - } - - brs, err := models.GetBranches(params["username"], params["reponame"]) + brs, err := models.GetBranches(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) if err != nil { - ctx.Handle(200, "repo.Branches", err) + ctx.Handle(404, "repo.Branches", err) return } else if len(brs) == 0 { ctx.Handle(404, "repo.Branches", nil) return } - ctx.Data["Username"] = params["username"] - ctx.Data["Reponame"] = params["reponame"] ctx.Data["Branches"] = brs ctx.Data["IsRepoToolbarBranches"] = true diff --git a/routers/repo/commit.go b/routers/repo/commit.go index afc1ffda29..449f644391 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -50,16 +50,12 @@ func Commits(ctx *middleware.Context, params martini.Params) { } func Diff(ctx *middleware.Context, params martini.Params) { - userName := params["username"] - repoName := params["reponame"] - branchName := params["branchname"] - commitId := params["commitid"] + userName := ctx.Repo.Owner.Name + repoName := ctx.Repo.Repository.Name + branchName := ctx.Repo.BranchName + commitId := ctx.Repo.CommitId - commit, err := models.GetCommit(userName, repoName, branchName, commitId) - if err != nil { - ctx.Handle(404, "repo.Diff", err) - return - } + commit := ctx.Repo.Commit diff, err := models.GetDiff(models.RepoPath(userName, repoName), commitId) if err != nil { @@ -85,11 +81,9 @@ func Diff(ctx *middleware.Context, params martini.Params) { return isImage } - shortSha := params["commitid"][:10] ctx.Data["IsImageFile"] = isImageFile - ctx.Data["Title"] = commit.Message() + " · " + shortSha + ctx.Data["Title"] = commit.Message() + " · " + base.ShortSha(commitId) ctx.Data["Commit"] = commit - ctx.Data["ShortSha"] = shortSha ctx.Data["Diff"] = diff ctx.Data["IsRepoToolbarCommits"] = true ctx.Data["SourcePath"] = "/" + path.Join(userName, repoName, "src", commitId) diff --git a/routers/repo/repo.go b/routers/repo/repo.go index b9ac1f1c42..c9c9af1e41 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -53,20 +53,20 @@ func Create(ctx *middleware.Context, form auth.CreateRepoForm) { } func Single(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsValid { - return - } + branchName := ctx.Repo.BranchName + commitId := ctx.Repo.CommitId + userName := ctx.Repo.Owner.Name + repoName := ctx.Repo.Repository.Name - branchName := params["branchname"] - userName := params["username"] - repoName := params["reponame"] + repoLink := ctx.Repo.RepoLink + branchLink := ctx.Repo.RepoLink + "/src/" + branchName + rawLink := ctx.Repo.RepoLink + "/raw/" + branchName // Get tree path treename := params["_1"] if len(treename) > 0 && treename[len(treename)-1] == '/' { - ctx.Redirect("/" + ctx.Repo.Owner.LowerName + "/" + - ctx.Repo.Repository.Name + "/src/" + branchName + "/" + treename[:len(treename)-1]) + ctx.Redirect(repoLink + "/src/" + branchName + "/" + treename[:len(treename)-1]) return } @@ -84,23 +84,17 @@ func Single(ctx *middleware.Context, params martini.Params) { } ctx.Data["Branches"] = brs - var commitId string - isViewBranch := models.IsBranchExist(userName, repoName, branchName) - if !isViewBranch { - commitId = branchName - } + isViewBranch := ctx.Repo.IsBranch ctx.Data["IsViewBranch"] = isViewBranch repoFile, err := models.GetTargetFile(userName, repoName, branchName, commitId, treename) + if err != nil && err != models.ErrRepoFileNotExist { ctx.Handle(404, "repo.Single(GetTargetFile)", err) return } - branchLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/src/" + branchName - rawLink := "/" + ctx.Repo.Owner.LowerName + "/" + ctx.Repo.Repository.Name + "/raw/" + branchName - if len(treename) != 0 && repoFile == nil { ctx.Handle(404, "repo.Single", nil) return @@ -142,8 +136,7 @@ func Single(ctx *middleware.Context, params martini.Params) { } else { // Directory and file list. - files, err := models.GetReposFiles(userName, repoName, - branchName, commitId, treename) + files, err := models.GetReposFiles(userName, repoName, ctx.Repo.CommitId, treename) if err != nil { ctx.Handle(404, "repo.Single(GetReposFiles)", err) return @@ -200,18 +193,7 @@ func Single(ctx *middleware.Context, params martini.Params) { } } - // Get latest commit according username and repo name. - commit, err := models.GetCommit(userName, repoName, - branchName, commitId) - if err != nil { - log.Error("repo.Single(GetCommit): %v", err) - ctx.Handle(404, "repo.Single(GetCommit)", err) - return - } - ctx.Data["LastCommit"] = commit - - ctx.Data["CommitId"] = commitId - + ctx.Data["LastCommit"] = ctx.Repo.Commit ctx.Data["Paths"] = Paths ctx.Data["Treenames"] = treenames ctx.Data["BranchLink"] = branchLink @@ -219,11 +201,6 @@ func Single(ctx *middleware.Context, params martini.Params) { } func SingleDownload(ctx *middleware.Context, params martini.Params) { - if !ctx.Repo.IsValid { - ctx.Handle(404, "repo.SingleDownload", nil) - return - } - // Get tree path treename := params["_1"] @@ -263,10 +240,6 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) { } func Http(ctx *middleware.Context, params martini.Params) { - /*if !ctx.Repo.IsValid { - return - }*/ - // TODO: access check username := params["username"] diff --git a/templates/issue/create.tmpl b/templates/issue/create.tmpl index f5cec0c05a..01784cd21e 100644 --- a/templates/issue/create.tmpl +++ b/templates/issue/create.tmpl @@ -4,7 +4,7 @@ {{template "repo/toolbar" .}}
-
+ {{.CsrfTokenHtml}}
@@ -40,4 +40,4 @@
-{{template "base/footer" .}} \ No newline at end of file +{{template "base/footer" .}} diff --git a/templates/issue/list.tmpl b/templates/issue/list.tmpl index 7622a4c172..de25b0e37f 100644 --- a/templates/issue/list.tmpl +++ b/templates/issue/list.tmpl @@ -6,24 +6,24 @@
-{{template "base/footer" .}} \ No newline at end of file +{{template "base/footer" .}} diff --git a/templates/issue/view.tmpl b/templates/issue/view.tmpl index 91e5250ca3..df8b660769 100644 --- a/templates/issue/view.tmpl +++ b/templates/issue/view.tmpl @@ -64,7 +64,7 @@
{{if .SignedUser}}
-
+ {{.CsrfTokenHtml}}
@@ -102,4 +102,4 @@
-{{template "base/footer" .}} \ No newline at end of file +{{template "base/footer" .}} diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index e58f2d664e..5c95ddefde 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -1,7 +1,6 @@ {{template "base/head" .}} {{template "base/navbar" .}} {{template "repo/nav" .}} -{{template "repo/toolbar" .}}
@@ -11,7 +10,7 @@
- commit {{.ShortSha}} + commit {{ShortSha .CommitId}}

diff --git a/templates/repo/single.tmpl b/templates/repo/single.tmpl index 4c9406762c..abaa4e8936 100644 --- a/templates/repo/single.tmpl +++ b/templates/repo/single.tmpl @@ -11,11 +11,11 @@ {{ $n := len .Treenames}} {{if not .IsFile}}{{end}}

diff --git a/templates/repo/toolbar.tmpl b/templates/repo/toolbar.tmpl index 75f1efdc8e..3c7c8a50e1 100644 --- a/templates/repo/toolbar.tmpl +++ b/templates/repo/toolbar.tmpl @@ -3,24 +3,24 @@