update.go 2.5 KB

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