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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. return nil, err
  411. }
  412. commit, err = t.GetCommit(commitHash)
  413. if err != nil {
  414. return nil, err
  415. }
  416. file, err := GetFileResponseFromCommit(repo, commit, opts.NewBranch, treePath)
  417. if err != nil {
  418. return nil, err
  419. }
  420. return file, nil
  421. }
  422. // PushUpdateOptions defines the push update options
  423. type PushUpdateOptions struct {
  424. PusherID int64
  425. PusherName string
  426. RepoUserName string
  427. RepoName string
  428. RefFullName string
  429. OldCommitID string
  430. NewCommitID string
  431. }
  432. // IsNewRef return true if it's a first-time push to a branch, tag or etc.
  433. func (opts PushUpdateOptions) IsNewRef() bool {
  434. return opts.OldCommitID == git.EmptySHA
  435. }
  436. // IsDelRef return true if it's a deletion to a branch or tag
  437. func (opts PushUpdateOptions) IsDelRef() bool {
  438. return opts.NewCommitID == git.EmptySHA
  439. }
  440. // IsUpdateRef return true if it's an update operation
  441. func (opts PushUpdateOptions) IsUpdateRef() bool {
  442. return !opts.IsNewRef() && !opts.IsDelRef()
  443. }
  444. // IsTag return true if it's an operation to a tag
  445. func (opts PushUpdateOptions) IsTag() bool {
  446. return strings.HasPrefix(opts.RefFullName, git.TagPrefix)
  447. }
  448. // IsNewTag return true if it's a creation to a tag
  449. func (opts PushUpdateOptions) IsNewTag() bool {
  450. return opts.IsTag() && opts.IsNewRef()
  451. }
  452. // IsDelTag return true if it's a deletion to a tag
  453. func (opts PushUpdateOptions) IsDelTag() bool {
  454. return opts.IsTag() && opts.IsDelRef()
  455. }
  456. // IsBranch return true if it's a push to branch
  457. func (opts PushUpdateOptions) IsBranch() bool {
  458. return strings.HasPrefix(opts.RefFullName, git.BranchPrefix)
  459. }
  460. // IsNewBranch return true if it's the first-time push to a branch
  461. func (opts PushUpdateOptions) IsNewBranch() bool {
  462. return opts.IsBranch() && opts.IsNewRef()
  463. }
  464. // IsUpdateBranch return true if it's not the first push to a branch
  465. func (opts PushUpdateOptions) IsUpdateBranch() bool {
  466. return opts.IsBranch() && opts.IsUpdateRef()
  467. }
  468. // IsDelBranch return true if it's a deletion to a branch
  469. func (opts PushUpdateOptions) IsDelBranch() bool {
  470. return opts.IsBranch() && opts.IsDelRef()
  471. }
  472. // TagName returns simple tag name if it's an operation to a tag
  473. func (opts PushUpdateOptions) TagName() string {
  474. return opts.RefFullName[len(git.TagPrefix):]
  475. }
  476. // BranchName returns simple branch name if it's an operation to branch
  477. func (opts PushUpdateOptions) BranchName() string {
  478. return opts.RefFullName[len(git.BranchPrefix):]
  479. }
  480. // RepoFullName returns repo full name
  481. func (opts PushUpdateOptions) RepoFullName() string {
  482. return opts.RepoUserName + "/" + opts.RepoName
  483. }
  484. // PushUpdate must be called for any push actions in order to
  485. // generates necessary push action history feeds and other operations
  486. func PushUpdate(repo *models.Repository, branch string, opts PushUpdateOptions) error {
  487. if opts.IsNewRef() && opts.IsDelRef() {
  488. return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  489. }
  490. repoPath := models.RepoPath(opts.RepoUserName, opts.RepoName)
  491. _, err := git.NewCommand("update-server-info").RunInDir(repoPath)
  492. if err != nil {
  493. return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  494. }
  495. gitRepo, err := git.OpenRepository(repoPath)
  496. if err != nil {
  497. return fmt.Errorf("OpenRepository: %v", err)
  498. }
  499. defer gitRepo.Close()
  500. if err = repo.UpdateSize(models.DefaultDBContext()); err != nil {
  501. log.Error("Failed to update size for repository: %v", err)
  502. }
  503. var commits = &repo_module.PushCommits{}
  504. if opts.IsTag() { // If is tag reference
  505. tagName := opts.TagName()
  506. if opts.IsDelRef() {
  507. if err := models.PushUpdateDeleteTag(repo, tagName); err != nil {
  508. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  509. }
  510. } else {
  511. // Clear cache for tag commit count
  512. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  513. if err := repo_module.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
  514. return fmt.Errorf("PushUpdateAddTag: %v", err)
  515. }
  516. }
  517. } else if opts.IsBranch() { // If is branch reference
  518. pusher, err := models.GetUserByID(opts.PusherID)
  519. if err != nil {
  520. return err
  521. }
  522. if !opts.IsDelRef() {
  523. // Clear cache for branch commit count
  524. cache.Remove(repo.GetCommitsCountCacheKey(opts.BranchName(), true))
  525. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  526. if err != nil {
  527. return fmt.Errorf("gitRepo.GetCommit: %v", err)
  528. }
  529. // Push new branch.
  530. var l *list.List
  531. if opts.IsNewRef() {
  532. l, err = newCommit.CommitsBeforeLimit(10)
  533. if err != nil {
  534. return fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  535. }
  536. } else {
  537. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  538. if err != nil {
  539. return fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  540. }
  541. }
  542. commits = repo_module.ListToPushCommits(l)
  543. if err = models.RemoveDeletedBranch(repo.ID, opts.BranchName()); err != nil {
  544. log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, opts.BranchName(), err)
  545. }
  546. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  547. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  548. }
  549. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  550. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
  551. } else if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
  552. // close all related pulls
  553. log.Error("close related pull request failed: %v", err)
  554. }
  555. }
  556. if err := CommitRepoAction(&CommitRepoActionOptions{
  557. PushUpdateOptions: opts,
  558. RepoOwnerID: repo.OwnerID,
  559. Commits: commits,
  560. }); err != nil {
  561. return fmt.Errorf("CommitRepoAction: %v", err)
  562. }
  563. return nil
  564. }
  565. // PushUpdates generates push action history feeds for push updating multiple refs
  566. func PushUpdates(repo *models.Repository, optsList []*PushUpdateOptions) error {
  567. repoPath := repo.RepoPath()
  568. _, err := git.NewCommand("update-server-info").RunInDir(repoPath)
  569. if err != nil {
  570. return fmt.Errorf("Failed to call 'git update-server-info': %v", err)
  571. }
  572. gitRepo, err := git.OpenRepository(repoPath)
  573. if err != nil {
  574. return fmt.Errorf("OpenRepository: %v", err)
  575. }
  576. defer gitRepo.Close()
  577. if err = repo.UpdateSize(models.DefaultDBContext()); err != nil {
  578. log.Error("Failed to update size for repository: %v", err)
  579. }
  580. actions, err := createCommitRepoActions(repo, gitRepo, optsList)
  581. if err != nil {
  582. return err
  583. }
  584. if err := CommitRepoAction(actions...); err != nil {
  585. return fmt.Errorf("CommitRepoAction: %v", err)
  586. }
  587. var pusher *models.User
  588. for _, opts := range optsList {
  589. if !opts.IsBranch() {
  590. continue
  591. }
  592. branch := opts.BranchName()
  593. if pusher == nil || pusher.ID != opts.PusherID {
  594. var err error
  595. pusher, err = models.GetUserByID(opts.PusherID)
  596. if err != nil {
  597. return err
  598. }
  599. }
  600. if !opts.IsDelRef() {
  601. if err = models.RemoveDeletedBranch(repo.ID, branch); err != nil {
  602. log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
  603. }
  604. if err = models.WatchIfAuto(opts.PusherID, repo.ID, true); err != nil {
  605. log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
  606. }
  607. log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
  608. go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
  609. // close all related pulls
  610. } else if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
  611. log.Error("close related pull request failed: %v", err)
  612. }
  613. }
  614. return nil
  615. }
  616. func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, optsList []*PushUpdateOptions) ([]*CommitRepoActionOptions, error) {
  617. addTags := make([]string, 0, len(optsList))
  618. delTags := make([]string, 0, len(optsList))
  619. actions := make([]*CommitRepoActionOptions, 0, len(optsList))
  620. for _, opts := range optsList {
  621. if opts.IsNewRef() && opts.IsDelRef() {
  622. return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
  623. }
  624. var commits = &repo_module.PushCommits{}
  625. if opts.IsTag() {
  626. // If is tag reference
  627. tagName := opts.TagName()
  628. if opts.IsDelRef() {
  629. delTags = append(delTags, tagName)
  630. } else {
  631. cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
  632. addTags = append(addTags, tagName)
  633. }
  634. } else if !opts.IsDelRef() {
  635. // If is branch reference
  636. // Clear cache for branch commit count
  637. cache.Remove(repo.GetCommitsCountCacheKey(opts.BranchName(), true))
  638. newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
  639. if err != nil {
  640. return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
  641. }
  642. // Push new branch.
  643. var l *list.List
  644. if opts.IsNewRef() {
  645. l, err = newCommit.CommitsBeforeLimit(10)
  646. if err != nil {
  647. return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
  648. }
  649. } else {
  650. l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
  651. if err != nil {
  652. return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
  653. }
  654. }
  655. commits = repo_module.ListToPushCommits(l)
  656. }
  657. actions = append(actions, &CommitRepoActionOptions{
  658. PushUpdateOptions: *opts,
  659. RepoOwnerID: repo.OwnerID,
  660. Commits: commits,
  661. })
  662. }
  663. if err := repo_module.PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
  664. return nil, fmt.Errorf("PushUpdateAddDeleteTags: %v", err)
  665. }
  666. return actions, nil
  667. }