commit.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "container/list"
  7. "fmt"
  8. "path"
  9. "github.com/codegangsta/martini"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/middleware"
  12. )
  13. func Commits(ctx *middleware.Context, params martini.Params) {
  14. userName := params["username"]
  15. repoName := params["reponame"]
  16. branchName := params["branchname"]
  17. brs, err := models.GetBranches(userName, repoName)
  18. if err != nil {
  19. ctx.Handle(200, "repo.Commits", err)
  20. return
  21. } else if len(brs) == 0 {
  22. ctx.Handle(404, "repo.Commits", nil)
  23. return
  24. }
  25. var commits *list.List
  26. if models.IsBranchExist(userName, repoName, branchName) {
  27. commits, err = models.GetCommitsByBranch(userName, repoName, branchName)
  28. } else {
  29. commits, err = models.GetCommitsByCommitId(userName, repoName, branchName)
  30. }
  31. if err != nil {
  32. ctx.Handle(404, "repo.Commits", err)
  33. return
  34. }
  35. ctx.Data["Username"] = userName
  36. ctx.Data["Reponame"] = repoName
  37. ctx.Data["CommitCount"] = commits.Len()
  38. ctx.Data["Commits"] = commits
  39. ctx.Data["IsRepoToolbarCommits"] = true
  40. ctx.HTML(200, "repo/commits")
  41. }
  42. func Diff(ctx *middleware.Context, params martini.Params) {
  43. fmt.Println(params["branchname"])
  44. commit, err := models.GetCommit(params["username"], params["reponame"], params["branchname"], params["commitid"])
  45. if err != nil {
  46. ctx.Handle(404, "repo.Diff", err)
  47. return
  48. }
  49. diff, err := models.GetDiff(models.RepoPath(params["username"], params["reponame"]), params["commitid"])
  50. if err != nil {
  51. ctx.Handle(404, "repo.Diff", err)
  52. return
  53. }
  54. shortSha := params["commitid"][:10]
  55. ctx.Data["Title"] = commit.Message() + " · " + shortSha
  56. ctx.Data["Commit"] = commit
  57. ctx.Data["ShortSha"] = shortSha
  58. ctx.Data["Diff"] = diff
  59. ctx.Data["IsRepoToolbarCommits"] = true
  60. ctx.Data["SourcePath"] = "/" + path.Join(params["username"], params["reponame"], "src", params["commitid"])
  61. ctx.HTML(200, "repo/diff")
  62. }