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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. // Copyright 2019 The Gitea 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 repofiles
  5. import (
  6. "bytes"
  7. "container/list"
  8. "fmt"
  9. "path"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/cache"
  14. "code.gitea.io/gitea/modules/charset"
  15. "code.gitea.io/gitea/modules/git"
  16. "code.gitea.io/gitea/modules/lfs"
  17. "code.gitea.io/gitea/modules/log"
  18. repo_module "code.gitea.io/gitea/modules/repository"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/structs"
  21. pull_service "code.gitea.io/gitea/services/pull"
  22. stdcharset "golang.org/x/net/html/charset"
  23. "golang.org/x/text/transform"
  24. )
  25. // IdentityOptions for a person's identity like an author or committer
  26. type IdentityOptions struct {
  27. Name string
  28. Email string
  29. }
  30. // CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
  31. type CommitDateOptions struct {
  32. Author time.Time
  33. Committer time.Time
  34. }
  35. // UpdateRepoFileOptions holds the repository file update options
  36. type UpdateRepoFileOptions struct {
  37. LastCommitID string
  38. OldBranch string
  39. NewBranch string
  40. TreePath string
  41. FromTreePath string
  42. Message string
  43. Content string
  44. SHA string
  45. IsNewFile bool
  46. Author *IdentityOptions
  47. Committer *IdentityOptions
  48. Dates *CommitDateOptions
  49. }
  50. func detectEncodingAndBOM(entry *git.TreeEntry, repo *models.Repository) (string, bool) {
  51. reader, err := entry.Blob().DataAsync()
  52. if err != nil {
  53. // return default
  54. return "UTF-8", false
  55. }
  56. defer reader.Close()
  57. buf := make([]byte, 1024)
  58. n, err := reader.Read(buf)
  59. if err != nil {
  60. // return default
  61. return "UTF-8", false
  62. }
  63. buf = buf[:n]
  64. if setting.LFS.StartServer {
  65. meta := lfs.IsPointerFile(&buf)
  66. if meta != nil {
  67. meta, err = repo.GetLFSMetaObjectByOid(meta.Oid)
  68. if err != nil && err != models.ErrLFSObjectNotExist {
  69. // return default
  70. return "UTF-8", false
  71. }
  72. }
  73. if meta != nil {
  74. dataRc, err := lfs.ReadMetaObject(meta)
  75. if err != nil {
  76. // return default
  77. return "UTF-8", false
  78. }
  79. defer dataRc.Close()
  80. buf = make([]byte, 1024)
  81. n, err = dataRc.Read(buf)
  82. if err != nil {
  83. // return default
  84. return "UTF-8", false
  85. }
  86. buf = buf[:n]
  87. }
  88. }
  89. encoding, err := charset.DetectEncoding(buf)
  90. if err != nil {
  91. // just default to utf-8 and no bom
  92. return "UTF-8", false
  93. }
  94. if encoding == "UTF-8" {
  95. return encoding, bytes.Equal(buf[0:3], charset.UTF8BOM)
  96. }
  97. charsetEncoding, _ := stdcharset.Lookup(encoding)
  98. if charsetEncoding == nil {
  99. return "UTF-8", false
  100. }
  101. result, n, err := transform.String(charsetEncoding.NewDecoder(), string(buf))
  102. if err != nil {
  103. // return default
  104. return "UTF-8", false
  105. }
  106. if n > 2 {
  107. return encoding, bytes.Equal([]byte(result)[0:3], charset.UTF8BOM)
  108. }
  109. return encoding, false
  110. }
  111. // CreateOrUpdateRepoFile adds or updates a file in the given repository
  112. func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *UpdateRepoFileOptions) (*structs.FileResponse, error) {
  113. // If no branch name is set, assume master
  114. if opts.OldBranch == "" {
  115. opts.OldBranch = repo.DefaultBranch
  116. }
  117. if opts.NewBranch == "" {
  118. opts.NewBranch = opts.OldBranch
  119. }
  120. // oldBranch must exist for this operation
  121. if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
  122. return nil, err
  123. }
  124. // A NewBranch can be specified for the file to be created/updated in a new branch.
  125. // Check to make sure the branch does not already exist, otherwise we can't proceed.
  126. // If we aren't branching to a new branch, make sure user can commit to the given branch
  127. if opts.NewBranch != opts.OldBranch {
  128. existingBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
  129. if existingBranch != nil {
  130. return nil, models.ErrBranchAlreadyExists{
  131. BranchName: opts.NewBranch,
  132. }
  133. }
  134. if err != nil && !git.IsErrBranchNotExist(err) {
  135. return nil, err
  136. }
  137. } else {
  138. protectedBranch, err := repo.GetBranchProtection(opts.OldBranch)
  139. if err != nil {
  140. return nil, err
  141. }
  142. if protectedBranch != nil {
  143. if !protectedBranch.CanUserPush(doer.ID) {
  144. return nil, models.ErrUserCannotCommit{
  145. UserName: doer.LowerName,
  146. }
  147. }
  148. if protectedBranch.RequireSignedCommits {
  149. _, _, err := repo.SignCRUDAction(doer, repo.RepoPath(), opts.OldBranch)
  150. if err != nil {
  151. if !models.IsErrWontSign(err) {
  152. return nil, err
  153. }
  154. return nil, models.ErrUserCannotCommit{
  155. UserName: doer.LowerName,
  156. }
  157. }
  158. }
  159. patterns := protectedBranch.GetProtectedFilePatterns()
  160. for _, pat := range patterns {
  161. if pat.Match(strings.ToLower(opts.TreePath)) {
  162. return nil, models.ErrFilePathProtected{
  163. Path: opts.TreePath,
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // If FromTreePath is not set, set it to the opts.TreePath
  170. if opts.TreePath != "" && opts.FromTreePath == "" {
  171. opts.FromTreePath = opts.TreePath
  172. }
  173. // Check that the path given in opts.treePath is valid (not a git path)
  174. treePath := CleanUploadFileName(opts.TreePath)
  175. if treePath == "" {
  176. return nil, models.ErrFilenameInvalid{
  177. Path: opts.TreePath,
  178. }
  179. }
  180. // If there is a fromTreePath (we are copying it), also clean it up
  181. fromTreePath := CleanUploadFileName(opts.FromTreePath)
  182. if fromTreePath == "" && opts.FromTreePath != "" {
  183. return nil, models.ErrFilenameInvalid{
  184. Path: opts.FromTreePath,
  185. }
  186. }
  187. message := strings.TrimSpace(opts.Message)
  188. author, committer := GetAuthorAndCommitterUsers(opts.Author, opts.Committer, doer)
  189. t, err := NewTemporaryUploadRepository(repo)
  190. if err != nil {
  191. log.Error("%v", err)
  192. }
  193. defer t.Close()
  194. if err := t.Clone(opts.OldBranch); err != nil {
  195. return nil, err
  196. }
  197. if err := t.SetDefaultIndex(); err != nil {
  198. return nil, err
  199. }
  200. // Get the commit of the original branch
  201. commit, err := t.GetBranchCommit(opts.OldBranch)
  202. if err != nil {
  203. return nil, err // Couldn't get a commit for the branch
  204. }
  205. // Assigned LastCommitID in opts if it hasn't been set
  206. if opts.LastCommitID == "" {
  207. opts.LastCommitID = commit.ID.String()
  208. } else {
  209. lastCommitID, err := t.gitRepo.ConvertToSHA1(opts.LastCommitID)
  210. if err != nil {
  211. return nil, fmt.Errorf("DeleteRepoFile: Invalid last commit ID: %v", err)
  212. }
  213. opts.LastCommitID = lastCommitID.String()
  214. }
  215. encoding := "UTF-8"
  216. bom := false
  217. executable := false
  218. if !opts.IsNewFile {
  219. fromEntry, err := commit.GetTreeEntryByPath(fromTreePath)
  220. if err != nil {
  221. return nil, err
  222. }
  223. if opts.SHA != "" {
  224. // If a SHA was given and the SHA given doesn't match the SHA of the fromTreePath, throw error
  225. if opts.SHA != fromEntry.ID.String() {
  226. return nil, models.ErrSHADoesNotMatch{
  227. Path: treePath,
  228. GivenSHA: opts.SHA,
  229. CurrentSHA: fromEntry.ID.String(),
  230. }
  231. }
  232. } else if opts.LastCommitID != "" {
  233. // If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
  234. // an error, but only if we aren't creating a new branch.
  235. if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
  236. if changed, err := commit.FileChangedSinceCommit(treePath, opts.LastCommitID); err != nil {
  237. return nil, err
  238. } else if changed {
  239. return nil, models.ErrCommitIDDoesNotMatch{
  240. GivenCommitID: opts.LastCommitID,
  241. CurrentCommitID: opts.LastCommitID,
  242. }
  243. }
  244. // The file wasn't modified, so we are good to delete it
  245. }
  246. } else {
  247. // When updating a file, a lastCommitID or SHA needs to be given to make sure other commits
  248. // haven't been made. We throw an error if one wasn't provided.
  249. return nil, models.ErrSHAOrCommitIDNotProvided{}
  250. }
  251. encoding, bom = detectEncodingAndBOM(fromEntry, repo)
  252. executable = fromEntry.IsExecutable()
  253. }
  254. // For the path where this file will be created/updated, we need to make
  255. // sure no parts of the path are existing files or links except for the last
  256. // item in the path which is the file name, and that shouldn't exist IF it is
  257. // a new file OR is being moved to a new path.
  258. treePathParts := strings.Split(treePath, "/")
  259. subTreePath := ""
  260. for index, part := range treePathParts {
  261. subTreePath = path.Join(subTreePath, part)
  262. entry, err := commit.GetTreeEntryByPath(subTreePath)
  263. if err != nil {
  264. if git.IsErrNotExist(err) {
  265. // Means there is no item with that name, so we're good
  266. break
  267. }
  268. return nil, err
  269. }
  270. if index < len(treePathParts)-1 {
  271. if !entry.IsDir() {
  272. return nil, models.ErrFilePathInvalid{
  273. Message: fmt.Sprintf("a file exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  274. Path: subTreePath,
  275. Name: part,
  276. Type: git.EntryModeBlob,
  277. }
  278. }
  279. } else if entry.IsLink() {
  280. return nil, models.ErrFilePathInvalid{
  281. Message: fmt.Sprintf("a symbolic link exists where you’re trying to create a subdirectory [path: %s]", subTreePath),
  282. Path: subTreePath,
  283. Name: part,
  284. Type: git.EntryModeSymlink,
  285. }
  286. } else if entry.IsDir() {
  287. return nil, models.ErrFilePathInvalid{
  288. Message: fmt.Sprintf("a directory exists where you’re trying to create a file [path: %s]", subTreePath),
  289. Path: subTreePath,
  290. Name: part,
  291. Type: git.EntryModeTree,
  292. }
  293. } else if fromTreePath != treePath || opts.IsNewFile {
  294. // The entry shouldn't exist if we are creating new file or moving to a new path
  295. return nil, models.ErrRepoFileAlreadyExists{
  296. Path: treePath,
  297. }
  298. }
  299. }
  300. // Get the two paths (might be the same if not moving) from the index if they exist
  301. filesInIndex, err := t.LsFiles(opts.TreePath, opts.FromTreePath)
  302. if err != nil {
  303. return nil, fmt.Errorf("UpdateRepoFile: %v", err)
  304. }
  305. // If is a new file (not updating) then the given path shouldn't exist
  306. if opts.IsNewFile {
  307. for _, file := range filesInIndex {
  308. if file == opts.TreePath {
  309. return nil, models.ErrRepoFileAlreadyExists{
  310. Path: opts.TreePath,
  311. }
  312. }
  313. }
  314. }
  315. // Remove the old path from the tree
  316. if fromTreePath != treePath && len(filesInIndex) > 0 {
  317. for _, file := range filesInIndex {
  318. if file == fromTreePath {
  319. if err := t.RemoveFilesFromIndex(opts.FromTreePath); err != nil {
  320. return nil, err
  321. }
  322. }
  323. }
  324. }
  325. content := opts.Content
  326. if bom {
  327. content = string(charset.UTF8BOM) + content
  328. }
  329. if encoding != "UTF-8" {
  330. charsetEncoding, _ := stdcharset.Lookup(encoding)
  331. if charsetEncoding != nil {
  332. result, _, err := transform.String(charsetEncoding.NewEncoder(), content)
  333. if err != nil {
  334. // Look if we can't encode back in to the original we should just stick with utf-8
  335. log.Error("Error re-encoding %s (%s) as %s - will stay as UTF-8: %v", opts.TreePath, opts.FromTreePath, encoding, err)
  336. result = content
  337. }
  338. content = result
  339. } else {
  340. log.Error("Unknown encoding: %s", encoding)
  341. }
  342. }
  343. // Reset the opts.Content to our adjusted content to ensure that LFS gets the correct content
  344. opts.Content = content
  345. var lfsMetaObject *models.LFSMetaObject
  346. if setting.LFS.StartServer {
  347. // Check there is no way this can return multiple infos
  348. filename2attribute2info, err := t.CheckAttribute("filter", treePath)
  349. if err != nil {
  350. return nil, err
  351. }
  352. if filename2attribute2info[treePath] != nil && filename2attribute2info[treePath]["filter"] == "lfs" {
  353. // OK so we are supposed to LFS this data!
  354. oid, err := models.GenerateLFSOid(strings.NewReader(opts.Content))
  355. if err != nil {
  356. return nil, err
  357. }
  358. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(opts.Content)), RepositoryID: repo.ID}
  359. content = lfsMetaObject.Pointer()
  360. }
  361. }
  362. // Add the object to the database
  363. objectHash, err := t.HashObject(strings.NewReader(content))
  364. if err != nil {
  365. return nil, err
  366. }
  367. // Add the object to the index
  368. if executable {
  369. if err := t.AddObjectToIndex("100755", objectHash, treePath); err != nil {
  370. return nil, err
  371. }
  372. } else {
  373. if err := t.AddObjectToIndex("100644", objectHash, treePath); err != nil {
  374. return nil, err
  375. }
  376. }
  377. // Now write the tree
  378. treeHash, err := t.WriteTree()
  379. if err != nil {
  380. return nil, err
  381. }
  382. // Now commit the tree
  383. var commitHash string
  384. if opts.Dates != nil {
  385. commitHash, err = t.CommitTreeWithDate(author, committer, treeHash, message, opts.Dates.Author, opts.Dates.Committer)
  386. } else {
  387. commitHash, err = t.CommitTree(author, committer, treeHash, message)
  388. }
  389. if err != nil {
  390. return nil, err
  391. }
  392. if lfsMetaObject != nil {
  393. // We have an LFS object - create it
  394. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  395. if err != nil {
  396. return nil, err
  397. }
  398. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  399. if !contentStore.Exists(lfsMetaObject) {
  400. if err := contentStore.Put(lfsMetaObject, strings.NewReader(opts.Content)); err != nil {
  401. if _, err2 := repo.RemoveLFSMetaObjectByOid(lfsMetaObject.Oid); err2 != nil {
  402. return nil, fmt.Errorf("Error whilst removing failed inserted LFS object %s: %v (Prev Error: %v)", lfsMetaObject.Oid, err2, err)
  403. }
  404. return nil, err
  405. }
  406. }
  407. }
  408. // Then push this tree to NewBranch
  409. if err := t.Push(doer, commitHash, opts.NewBranch); err != nil {
  410. log.Error("%T %v", err, err)
  411. return nil, err
  412. }
  413. commit, err = t.GetCommit(commitHash)
  414. if err != nil {
  415. return nil, err
  416. }
  417. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  418. if err != nil {
  419. return nil, err
  420. }
  421. return file, nil
  422. }
  423. // PushUpdateOptions defines the push update options
  424. type PushUpdateOptions struct {
  425. PusherID int64
  426. PusherName string
  427. RepoUserName string
  428. RepoName string
  429. RefFullName string
  430. OldCommitID string
  431. NewCommitID string
  432. }
  433. // IsNewRef return true if it's a first-time push to a branch, tag or etc.
  434. func (opts PushUpdateOptions) IsNewRef() bool {
  435. return opts.OldCommitID == git.EmptySHA
  436. }
  437. // IsDelRef return true if it's a deletion to a branch or tag
  438. func (opts PushUpdateOptions) IsDelRef() bool {
  439. return opts.NewCommitID == git.EmptySHA
  440. }
  441. // IsUpdateRef return true if it's an update operation
  442. func (opts PushUpdateOptions) IsUpdateRef() bool {
  443. return !opts.IsNewRef() && !opts.IsDelRef()
  444. }
  445. // IsTag return true if it's an operation to a tag
  446. func (opts PushUpdateOptions) IsTag() bool {
  447. return strings.HasPrefix(opts.RefFullName, git.TagPrefix)
  448. }
  449. // IsNewTag return true if it's a creation to a tag
  450. func (opts PushUpdateOptions) IsNewTag() bool {
  451. return opts.IsTag() && opts.IsNewRef()
  452. }
  453. // IsDelTag return true if it's a deletion to a tag
  454. func (opts PushUpdateOptions) IsDelTag() bool {
  455. return opts.IsTag() && opts.IsDelRef()
  456. }
  457. // IsBranch return true if it's a push to branch
  458. func (opts PushUpdateOptions) IsBranch() bool {
  459. return strings.HasPrefix(opts.RefFullName, git.BranchPrefix)
  460. }
  461. // IsNewBranch return true if it's the first-time push to a branch
  462. func (opts PushUpdateOptions) IsNewBranch() bool {
  463. return opts.IsBranch() && opts.IsNewRef()
  464. }
  465. // IsUpdateBranch return true if it's not the first push to a branch
  466. func (opts PushUpdateOptions) IsUpdateBranch() bool {
  467. return opts.IsBranch() && opts.IsUpdateRef()
  468. }
  469. // IsDelBranch return true if it's a deletion to a branch
  470. func (opts PushUpdateOptions) IsDelBranch() bool {
  471. return opts.IsBranch() && opts.IsDelRef()
  472. }
  473. // TagName returns simple tag name if it's an operation to a tag
  474. func (opts PushUpdateOptions) TagName() string {
  475. return opts.RefFullName[len(git.TagPrefix):]
  476. }
  477. // BranchName returns simple branch name if it's an operation to branch
  478. func (opts PushUpdateOptions) BranchName() string {
  479. return opts.RefFullName[len(git.BranchPrefix):]
  480. }
  481. // RepoFullName returns repo full name
  482. func (opts PushUpdateOptions) RepoFullName() string {
  483. return opts.RepoUserName + "/" + opts.RepoName
  484. }
  485. // PushUpdate must be called for any push actions in order to
  486. // generates necessary push action history feeds and other operations
  487. func PushUpdate(repo *models.Repository, branch string, opts PushUpdateOptions) error {
  488. if opts.IsNewRef() && opts.IsDelRef() {
  489. return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  490. }
  491. repoPath := models.RepoPath(opts.RepoUserName, opts.RepoName)
  492. _, err := git.NewCommand("update-server-info").RunInDir(repoPath)
  493. if err != nil {
  494. return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  495. }
  496. gitRepo, err := git.OpenRepository(repoPath)
  497. if err != nil {
  498. return fmt.Errorf("OpenRepository: %v", err)
  499. }
  500. defer gitRepo.Close()
  501. if err = repo.UpdateSize(models.DefaultDBContext()); err != nil {
  502. log.Error("Failed to update size for repository: %v", err)
  503. }
  504. var commits = &repo_module.PushCommits{}
  505. if opts.IsTag() { // If is tag reference
  506. tagName := opts.TagName()
  507. if opts.IsDelRef() {
  508. if err := models.PushUpdateDeleteTag(repo, tagName); err != nil {
  509. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  510. }
  511. } else {
  512. // Clear cache for tag commit count
  513. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  514. if err := repo_module.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
  515. return fmt.Errorf("PushUpdateAddTag: %v", err)
  516. }
  517. }
  518. } else if opts.IsBranch() { // If is branch reference
  519. pusher, err := models.GetUserByID(opts.PusherID)
  520. if err != nil {
  521. return err
  522. }
  523. if !opts.IsDelRef() {
  524. // Clear cache for branch commit count
  525. cache.Remove(repo.GetCommitsCountCacheKey(opts.BranchName(), true))
  526. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  527. if err != nil {
  528. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  529. }
  530. // Push new branch.
  531. var l *list.List
  532. if opts.IsNewRef() {
  533. l, err = newCommit.CommitsBeforeLimit(10)
  534. if err != nil {
  535. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  536. }
  537. } else {
  538. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  539. if err != nil {
  540. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  541. }
  542. }
  543. commits = repo_module.ListToPushCommits(l)
  544. if err = models.RemoveDeletedBranch(repo.ID, opts.BranchName()); err != nil {
  545. log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, opts.BranchName(), err)
  546. }
  547. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  548. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  549. }
  550. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  551. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
  552. } else if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
  553. // close all related pulls
  554. log.Error("close related pull request failed: %v", err)
  555. }
  556. }
  557. if err := CommitRepoAction(&CommitRepoActionOptions{
  558. PushUpdateOptions: opts,
  559. RepoOwnerID: repo.OwnerID,
  560. Commits: commits,
  561. }); err != nil {
  562. return fmt.Errorf("CommitRepoAction: %v", err)
  563. }
  564. return nil
  565. }
  566. // PushUpdates generates push action history feeds for push updating multiple refs
  567. func PushUpdates(repo *models.Repository, optsList []*PushUpdateOptions) error {
  568. repoPath := repo.RepoPath()
  569. _, err := git.NewCommand("update-server-info").RunInDir(repoPath)
  570. if err != nil {
  571. return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  572. }
  573. gitRepo, err := git.OpenRepository(repoPath)
  574. if err != nil {
  575. return fmt.Errorf("OpenRepository: %v", err)
  576. }
  577. defer gitRepo.Close()
  578. if err = repo.UpdateSize(models.DefaultDBContext()); err != nil {
  579. log.Error("Failed to update size for repository: %v", err)
  580. }
  581. actions, err := createCommitRepoActions(repo, gitRepo, optsList)
  582. if err != nil {
  583. return err
  584. }
  585. if err := CommitRepoAction(actions...); err != nil {
  586. return fmt.Errorf("CommitRepoAction: %v", err)
  587. }
  588. var pusher *models.User
  589. for _, opts := range optsList {
  590. if !opts.IsBranch() {
  591. continue
  592. }
  593. branch := opts.BranchName()
  594. if pusher == nil || pusher.ID != opts.PusherID {
  595. var err error
  596. pusher, err = models.GetUserByID(opts.PusherID)
  597. if err != nil {
  598. return err
  599. }
  600. }
  601. if !opts.IsDelRef() {
  602. if err = models.RemoveDeletedBranch(repo.ID, branch); err != nil {
  603. log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
  604. }
  605. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  606. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  607. }
  608. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  609. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
  610. // close all related pulls
  611. } else if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
  612. log.Error("close related pull request failed: %v", err)
  613. }
  614. }
  615. return nil
  616. }
  617. func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, optsList []*PushUpdateOptions) ([]*CommitRepoActionOptions, error) {
  618. addTags := make([]string, 0, len(optsList))
  619. delTags := make([]string, 0, len(optsList))
  620. actions := make([]*CommitRepoActionOptions, 0, len(optsList))
  621. for _, opts := range optsList {
  622. if opts.IsNewRef() && opts.IsDelRef() {
  623. return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  624. }
  625. var commits = &repo_module.PushCommits{}
  626. if opts.IsTag() {
  627. // If is tag reference
  628. tagName := opts.TagName()
  629. if opts.IsDelRef() {
  630. delTags = append(delTags, tagName)
  631. } else {
  632. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  633. addTags = append(addTags, tagName)
  634. }
  635. } else if !opts.IsDelRef() {
  636. // If is branch reference
  637. // Clear cache for branch commit count
  638. cache.Remove(repo.GetCommitsCountCacheKey(opts.BranchName(), true))
  639. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  640. if err != nil {
  641. return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
  642. }
  643. // Push new branch.
  644. var l *list.List
  645. if opts.IsNewRef() {
  646. l, err = newCommit.CommitsBeforeLimit(10)
  647. if err != nil {
  648. return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  649. }
  650. } else {
  651. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  652. if err != nil {
  653. return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  654. }
  655. }
  656. commits = repo_module.ListToPushCommits(l)
  657. }
  658. actions = append(actions, &CommitRepoActionOptions{
  659. PushUpdateOptions: *opts,
  660. RepoOwnerID: repo.OwnerID,
  661. Commits: commits,
  662. })
  663. }
  664. if err := repo_module.PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
  665. return nil, fmt.Errorf("PushUpdateAddDeleteTags: %v", err)
  666. }
  667. return actions, nil
  668. }