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.

update.go 2.4KB

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