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.

branch.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. actions_model "code.gitea.io/gitea/models/actions"
  11. "code.gitea.io/gitea/models/db"
  12. git_model "code.gitea.io/gitea/models/git"
  13. issues_model "code.gitea.io/gitea/models/issues"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. user_model "code.gitea.io/gitea/models/user"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/gitrepo"
  18. "code.gitea.io/gitea/modules/graceful"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/queue"
  21. repo_module "code.gitea.io/gitea/modules/repository"
  22. "code.gitea.io/gitea/modules/timeutil"
  23. "code.gitea.io/gitea/modules/util"
  24. webhook_module "code.gitea.io/gitea/modules/webhook"
  25. notify_service "code.gitea.io/gitea/services/notify"
  26. files_service "code.gitea.io/gitea/services/repository/files"
  27. "xorm.io/builder"
  28. )
  29. // CreateNewBranch creates a new repository branch
  30. func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, oldBranchName, branchName string) (err error) {
  31. branch, err := git_model.GetBranch(ctx, repo.ID, oldBranchName)
  32. if err != nil {
  33. return err
  34. }
  35. return CreateNewBranchFromCommit(ctx, doer, repo, gitRepo, branch.CommitID, branchName)
  36. }
  37. // Branch contains the branch information
  38. type Branch struct {
  39. DBBranch *git_model.Branch
  40. IsProtected bool
  41. IsIncluded bool
  42. CommitsAhead int
  43. CommitsBehind int
  44. LatestPullRequest *issues_model.PullRequest
  45. MergeMovedOn bool
  46. }
  47. // LoadBranches loads branches from the repository limited by page & pageSize.
  48. func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, isDeletedBranch util.OptionalBool, keyword string, page, pageSize int) (*Branch, []*Branch, int64, error) {
  49. defaultDBBranch, err := git_model.GetBranch(ctx, repo.ID, repo.DefaultBranch)
  50. if err != nil {
  51. return nil, nil, 0, err
  52. }
  53. branchOpts := git_model.FindBranchOptions{
  54. RepoID: repo.ID,
  55. IsDeletedBranch: isDeletedBranch,
  56. ListOptions: db.ListOptions{
  57. Page: page,
  58. PageSize: pageSize,
  59. },
  60. Keyword: keyword,
  61. ExcludeBranchNames: []string{repo.DefaultBranch},
  62. }
  63. dbBranches, totalNumOfBranches, err := db.FindAndCount[git_model.Branch](ctx, branchOpts)
  64. if err != nil {
  65. return nil, nil, 0, err
  66. }
  67. if err := git_model.BranchList(dbBranches).LoadDeletedBy(ctx); err != nil {
  68. return nil, nil, 0, err
  69. }
  70. if err := git_model.BranchList(dbBranches).LoadPusher(ctx); err != nil {
  71. return nil, nil, 0, err
  72. }
  73. rules, err := git_model.FindRepoProtectedBranchRules(ctx, repo.ID)
  74. if err != nil {
  75. return nil, nil, 0, err
  76. }
  77. repoIDToRepo := map[int64]*repo_model.Repository{}
  78. repoIDToRepo[repo.ID] = repo
  79. repoIDToGitRepo := map[int64]*git.Repository{}
  80. repoIDToGitRepo[repo.ID] = gitRepo
  81. branches := make([]*Branch, 0, len(dbBranches))
  82. for i := range dbBranches {
  83. branch, err := loadOneBranch(ctx, repo, dbBranches[i], &rules, repoIDToRepo, repoIDToGitRepo)
  84. if err != nil {
  85. return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
  86. }
  87. branches = append(branches, branch)
  88. }
  89. // Always add the default branch
  90. log.Debug("loadOneBranch: load default: '%s'", defaultDBBranch.Name)
  91. defaultBranch, err := loadOneBranch(ctx, repo, defaultDBBranch, &rules, repoIDToRepo, repoIDToGitRepo)
  92. if err != nil {
  93. return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
  94. }
  95. return defaultBranch, branches, totalNumOfBranches, nil
  96. }
  97. func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *git_model.Branch, protectedBranches *git_model.ProtectedBranchRules,
  98. repoIDToRepo map[int64]*repo_model.Repository,
  99. repoIDToGitRepo map[int64]*git.Repository,
  100. ) (*Branch, error) {
  101. log.Trace("loadOneBranch: '%s'", dbBranch.Name)
  102. branchName := dbBranch.Name
  103. p := protectedBranches.GetFirstMatched(branchName)
  104. isProtected := p != nil
  105. divergence := &git.DivergeObject{
  106. Ahead: -1,
  107. Behind: -1,
  108. }
  109. // it's not default branch
  110. if repo.DefaultBranch != dbBranch.Name && !dbBranch.IsDeleted {
  111. var err error
  112. divergence, err = files_service.CountDivergingCommits(ctx, repo, git.BranchPrefix+branchName)
  113. if err != nil {
  114. log.Error("CountDivergingCommits: %v", err)
  115. }
  116. }
  117. pr, err := issues_model.GetLatestPullRequestByHeadInfo(ctx, repo.ID, branchName)
  118. if err != nil {
  119. return nil, fmt.Errorf("GetLatestPullRequestByHeadInfo: %v", err)
  120. }
  121. headCommit := dbBranch.CommitID
  122. mergeMovedOn := false
  123. if pr != nil {
  124. pr.HeadRepo = repo
  125. if err := pr.LoadIssue(ctx); err != nil {
  126. return nil, fmt.Errorf("LoadIssue: %v", err)
  127. }
  128. if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
  129. pr.BaseRepo = repo
  130. } else if err := pr.LoadBaseRepo(ctx); err != nil {
  131. return nil, fmt.Errorf("LoadBaseRepo: %v", err)
  132. } else {
  133. repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
  134. }
  135. pr.Issue.Repo = pr.BaseRepo
  136. if pr.HasMerged {
  137. baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
  138. if !ok {
  139. baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo)
  140. if err != nil {
  141. return nil, fmt.Errorf("OpenRepository: %v", err)
  142. }
  143. defer baseGitRepo.Close()
  144. repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
  145. }
  146. pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
  147. if err != nil && !git.IsErrNotExist(err) {
  148. return nil, fmt.Errorf("GetBranchCommitID: %v", err)
  149. }
  150. if err == nil && headCommit != pullCommit {
  151. // the head has moved on from the merge - we shouldn't delete
  152. mergeMovedOn = true
  153. }
  154. }
  155. }
  156. isIncluded := divergence.Ahead == 0 && repo.DefaultBranch != branchName
  157. return &Branch{
  158. DBBranch: dbBranch,
  159. IsProtected: isProtected,
  160. IsIncluded: isIncluded,
  161. CommitsAhead: divergence.Ahead,
  162. CommitsBehind: divergence.Behind,
  163. LatestPullRequest: pr,
  164. MergeMovedOn: mergeMovedOn,
  165. }, nil
  166. }
  167. // checkBranchName validates branch name with existing repository branches
  168. func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error {
  169. _, err := gitrepo.WalkReferences(ctx, repo, func(_, refName string) error {
  170. branchRefName := strings.TrimPrefix(refName, git.BranchPrefix)
  171. switch {
  172. case branchRefName == name:
  173. return git_model.ErrBranchAlreadyExists{
  174. BranchName: name,
  175. }
  176. // If branchRefName like a/b but we want to create a branch named a then we have a conflict
  177. case strings.HasPrefix(branchRefName, name+"/"):
  178. return git_model.ErrBranchNameConflict{
  179. BranchName: branchRefName,
  180. }
  181. // Conversely if branchRefName like a but we want to create a branch named a/b then we also have a conflict
  182. case strings.HasPrefix(name, branchRefName+"/"):
  183. return git_model.ErrBranchNameConflict{
  184. BranchName: branchRefName,
  185. }
  186. case refName == git.TagPrefix+name:
  187. return models.ErrTagAlreadyExists{
  188. TagName: name,
  189. }
  190. }
  191. return nil
  192. })
  193. return err
  194. }
  195. // syncBranchToDB sync the branch information in the database. It will try to update the branch first,
  196. // if updated success with affect records > 0, then all are done. Because that means the branch has been in the database.
  197. // If no record is affected, that means the branch does not exist in database. So there are two possibilities.
  198. // One is this is a new branch, then we just need to insert the record. Another is the branches haven't been synced,
  199. // then we need to sync all the branches into database.
  200. func syncBranchToDB(ctx context.Context, repoID, pusherID int64, branchName string, commit *git.Commit) error {
  201. cnt, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit)
  202. if err != nil {
  203. return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
  204. }
  205. if cnt > 0 { // This means branch does exist, so it's a normal update. It also means the branch has been synced.
  206. return nil
  207. }
  208. // if user haven't visit UI but directly push to a branch after upgrading from 1.20 -> 1.21,
  209. // we cannot simply insert the branch but need to check we have branches or not
  210. hasBranch, err := db.Exist[git_model.Branch](ctx, git_model.FindBranchOptions{
  211. RepoID: repoID,
  212. IsDeletedBranch: util.OptionalBoolFalse,
  213. }.ToConds())
  214. if err != nil {
  215. return err
  216. }
  217. if !hasBranch {
  218. if _, err = repo_module.SyncRepoBranches(ctx, repoID, pusherID); err != nil {
  219. return fmt.Errorf("repo_module.SyncRepoBranches %d:%s failed: %v", repoID, branchName, err)
  220. }
  221. return nil
  222. }
  223. // if database have branches but not this branch, it means this is a new branch
  224. return db.Insert(ctx, &git_model.Branch{
  225. RepoID: repoID,
  226. Name: branchName,
  227. CommitID: commit.ID.String(),
  228. CommitMessage: commit.Summary(),
  229. PusherID: pusherID,
  230. CommitTime: timeutil.TimeStamp(commit.Committer.When.Unix()),
  231. })
  232. }
  233. // CreateNewBranchFromCommit creates a new repository branch
  234. func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, commitID, branchName string) (err error) {
  235. err = repo.MustNotBeArchived()
  236. if err != nil {
  237. return err
  238. }
  239. // Check if branch name can be used
  240. if err := checkBranchName(ctx, repo, branchName); err != nil {
  241. return err
  242. }
  243. if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
  244. Remote: repo.RepoPath(),
  245. Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
  246. Env: repo_module.PushingEnvironment(doer, repo),
  247. }); err != nil {
  248. if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
  249. return err
  250. }
  251. return fmt.Errorf("push: %w", err)
  252. }
  253. return nil
  254. }
  255. // RenameBranch rename a branch
  256. func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, gitRepo *git.Repository, from, to string) (string, error) {
  257. err := repo.MustNotBeArchived()
  258. if err != nil {
  259. return "", err
  260. }
  261. if from == to {
  262. return "target_exist", nil
  263. }
  264. if gitRepo.IsBranchExist(to) {
  265. return "target_exist", nil
  266. }
  267. if !gitRepo.IsBranchExist(from) {
  268. return "from_not_exist", nil
  269. }
  270. if err := git_model.RenameBranch(ctx, repo, from, to, func(ctx context.Context, isDefault bool) error {
  271. err2 := gitRepo.RenameBranch(from, to)
  272. if err2 != nil {
  273. return err2
  274. }
  275. if isDefault {
  276. // if default branch changed, we need to delete all schedules and cron jobs
  277. if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
  278. log.Error("DeleteCronTaskByRepo: %v", err)
  279. }
  280. // cancel running cron jobs of this repository and delete old schedules
  281. if err := actions_model.CancelRunningJobs(
  282. ctx,
  283. repo.ID,
  284. from,
  285. "",
  286. webhook_module.HookEventSchedule,
  287. ); err != nil {
  288. log.Error("CancelRunningJobs: %v", err)
  289. }
  290. err2 = gitRepo.SetDefaultBranch(to)
  291. if err2 != nil {
  292. return err2
  293. }
  294. }
  295. return nil
  296. }); err != nil {
  297. return "", err
  298. }
  299. refNameTo := git.RefNameFromBranch(to)
  300. refID, err := gitRepo.GetRefCommitID(refNameTo.String())
  301. if err != nil {
  302. return "", err
  303. }
  304. notify_service.DeleteRef(ctx, doer, repo, git.RefNameFromBranch(from))
  305. notify_service.CreateRef(ctx, doer, repo, refNameTo, refID)
  306. return "", nil
  307. }
  308. // enmuerates all branch related errors
  309. var (
  310. ErrBranchIsDefault = errors.New("branch is default")
  311. )
  312. // DeleteBranch delete branch
  313. func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, branchName string) error {
  314. err := repo.MustNotBeArchived()
  315. if err != nil {
  316. return err
  317. }
  318. if branchName == repo.DefaultBranch {
  319. return ErrBranchIsDefault
  320. }
  321. isProtected, err := git_model.IsBranchProtected(ctx, repo.ID, branchName)
  322. if err != nil {
  323. return err
  324. }
  325. if isProtected {
  326. return git_model.ErrBranchIsProtected
  327. }
  328. rawBranch, err := git_model.GetBranch(ctx, repo.ID, branchName)
  329. if err != nil {
  330. return fmt.Errorf("GetBranch: %vc", err)
  331. }
  332. objectFormat, err := gitRepo.GetObjectFormat()
  333. if err != nil {
  334. return err
  335. }
  336. if rawBranch.IsDeleted {
  337. return nil
  338. }
  339. commit, err := gitRepo.GetBranchCommit(branchName)
  340. if err != nil {
  341. return err
  342. }
  343. if err := db.WithTx(ctx, func(ctx context.Context) error {
  344. if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil {
  345. return err
  346. }
  347. return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
  348. Force: true,
  349. })
  350. }); err != nil {
  351. return err
  352. }
  353. // Don't return error below this
  354. if err := PushUpdate(
  355. &repo_module.PushUpdateOptions{
  356. RefFullName: git.RefNameFromBranch(branchName),
  357. OldCommitID: commit.ID.String(),
  358. NewCommitID: objectFormat.EmptyObjectID().String(),
  359. PusherID: doer.ID,
  360. PusherName: doer.Name,
  361. RepoUserName: repo.OwnerName,
  362. RepoName: repo.Name,
  363. }); err != nil {
  364. log.Error("Update: %v", err)
  365. }
  366. return nil
  367. }
  368. type BranchSyncOptions struct {
  369. RepoID int64
  370. }
  371. // branchSyncQueue represents a queue to handle branch sync jobs.
  372. var branchSyncQueue *queue.WorkerPoolQueue[*BranchSyncOptions]
  373. func handlerBranchSync(items ...*BranchSyncOptions) []*BranchSyncOptions {
  374. for _, opts := range items {
  375. _, err := repo_module.SyncRepoBranches(graceful.GetManager().ShutdownContext(), opts.RepoID, 0)
  376. if err != nil {
  377. log.Error("syncRepoBranches [%d] failed: %v", opts.RepoID, err)
  378. }
  379. }
  380. return nil
  381. }
  382. func addRepoToBranchSyncQueue(repoID, doerID int64) error {
  383. return branchSyncQueue.Push(&BranchSyncOptions{
  384. RepoID: repoID,
  385. })
  386. }
  387. func initBranchSyncQueue(ctx context.Context) error {
  388. branchSyncQueue = queue.CreateUniqueQueue(ctx, "branch_sync", handlerBranchSync)
  389. if branchSyncQueue == nil {
  390. return errors.New("unable to create branch_sync queue")
  391. }
  392. go graceful.GetManager().RunWithCancel(branchSyncQueue)
  393. return nil
  394. }
  395. func AddAllRepoBranchesToSyncQueue(ctx context.Context, doerID int64) error {
  396. if err := db.Iterate(ctx, builder.Eq{"is_empty": false}, func(ctx context.Context, repo *repo_model.Repository) error {
  397. return addRepoToBranchSyncQueue(repo.ID, doerID)
  398. }); err != nil {
  399. return fmt.Errorf("run sync all branches failed: %v", err)
  400. }
  401. return nil
  402. }
  403. func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, newBranchName string) error {
  404. if repo.DefaultBranch == newBranchName {
  405. return nil
  406. }
  407. if !gitRepo.IsBranchExist(newBranchName) {
  408. return git_model.ErrBranchNotExist{
  409. BranchName: newBranchName,
  410. }
  411. }
  412. oldDefaultBranchName := repo.DefaultBranch
  413. repo.DefaultBranch = newBranchName
  414. if err := db.WithTx(ctx, func(ctx context.Context) error {
  415. if err := repo_model.UpdateDefaultBranch(ctx, repo); err != nil {
  416. return err
  417. }
  418. if err := actions_model.DeleteScheduleTaskByRepo(ctx, repo.ID); err != nil {
  419. log.Error("DeleteCronTaskByRepo: %v", err)
  420. }
  421. // cancel running cron jobs of this repository and delete old schedules
  422. if err := actions_model.CancelRunningJobs(
  423. ctx,
  424. repo.ID,
  425. oldDefaultBranchName,
  426. "",
  427. webhook_module.HookEventSchedule,
  428. ); err != nil {
  429. log.Error("CancelRunningJobs: %v", err)
  430. }
  431. if err := gitRepo.SetDefaultBranch(newBranchName); err != nil {
  432. if !git.IsErrUnsupportedVersion(err) {
  433. return err
  434. }
  435. }
  436. return nil
  437. }); err != nil {
  438. return err
  439. }
  440. notify_service.ChangeDefaultBranch(ctx, repo)
  441. return nil
  442. }