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

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