You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.0 KiB

  1. package models
  2. import (
  3. "container/list"
  4. "os/exec"
  5. "strings"
  6. "github.com/gogits/git"
  7. "github.com/gogits/gogs/modules/base"
  8. qlog "github.com/qiniu/log"
  9. )
  10. func Update(refName, oldCommitId, newCommitId, userName, repoName string, userId int64) {
  11. isNew := strings.HasPrefix(oldCommitId, "0000000")
  12. if isNew &&
  13. strings.HasPrefix(newCommitId, "0000000") {
  14. qlog.Fatal("old rev and new rev both 000000")
  15. }
  16. f := RepoPath(userName, repoName)
  17. gitUpdate := exec.Command("git", "update-server-info")
  18. gitUpdate.Dir = f
  19. gitUpdate.Run()
  20. repo, err := git.OpenRepository(f)
  21. if err != nil {
  22. qlog.Fatalf("runUpdate.Open repoId: %v", err)
  23. }
  24. newCommit, err := repo.GetCommit(newCommitId)
  25. if err != nil {
  26. qlog.Fatalf("runUpdate GetCommit of newCommitId: %v", err)
  27. return
  28. }
  29. var l *list.List
  30. // if a new branch
  31. if isNew {
  32. l, err = newCommit.CommitsBefore()
  33. if err != nil {
  34. qlog.Fatalf("Find CommitsBefore erro:", err)
  35. }
  36. } else {
  37. l, err = newCommit.CommitsBeforeUntil(oldCommitId)
  38. if err != nil {
  39. qlog.Fatalf("Find CommitsBeforeUntil erro:", err)
  40. return
  41. }
  42. }
  43. if err != nil {
  44. qlog.Fatalf("runUpdate.Commit repoId: %v", err)
  45. }
  46. repos, err := GetRepositoryByName(userId, repoName)
  47. if err != nil {
  48. qlog.Fatalf("runUpdate.GetRepositoryByName userId: %v", err)
  49. }
  50. commits := make([]*base.PushCommit, 0)
  51. var maxCommits = 3
  52. var actEmail string
  53. for e := l.Front(); e != nil; e = e.Next() {
  54. commit := e.Value.(*git.Commit)
  55. if actEmail == "" {
  56. actEmail = commit.Committer.Email
  57. }
  58. commits = append(commits,
  59. &base.PushCommit{commit.Id.String(),
  60. commit.Message(),
  61. commit.Author.Email,
  62. commit.Author.Name})
  63. if len(commits) >= maxCommits {
  64. break
  65. }
  66. }
  67. //commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
  68. if err = CommitRepoAction(userId, userName, actEmail,
  69. repos.Id, repoName, git.RefEndName(refName), &base.PushCommits{l.Len(), commits}); err != nil {
  70. qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
  71. }
  72. }