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.

mirror.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 mirror
  5. import (
  6. "context"
  7. "fmt"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/cache"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/graceful"
  16. "code.gitea.io/gitea/modules/lfs"
  17. "code.gitea.io/gitea/modules/log"
  18. "code.gitea.io/gitea/modules/notification"
  19. repo_module "code.gitea.io/gitea/modules/repository"
  20. "code.gitea.io/gitea/modules/setting"
  21. "code.gitea.io/gitea/modules/sync"
  22. "code.gitea.io/gitea/modules/timeutil"
  23. "code.gitea.io/gitea/modules/util"
  24. )
  25. // mirrorQueue holds an UniqueQueue object of the mirror
  26. var mirrorQueue = sync.NewUniqueQueue(setting.Repository.MirrorQueueLength)
  27. func readAddress(m *models.Mirror) {
  28. if len(m.Address) > 0 {
  29. return
  30. }
  31. var err error
  32. m.Address, err = remoteAddress(m.Repo.RepoPath())
  33. if err != nil {
  34. log.Error("remoteAddress: %v", err)
  35. }
  36. }
  37. func remoteAddress(repoPath string) (string, error) {
  38. var cmd *git.Command
  39. err := git.LoadGitVersion()
  40. if err != nil {
  41. return "", err
  42. }
  43. if git.CheckGitVersionAtLeast("2.7") == nil {
  44. cmd = git.NewCommand("remote", "get-url", "origin")
  45. } else {
  46. cmd = git.NewCommand("config", "--get", "remote.origin.url")
  47. }
  48. result, err := cmd.RunInDir(repoPath)
  49. if err != nil {
  50. if strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  51. return "", nil
  52. }
  53. return "", err
  54. }
  55. if len(result) > 0 {
  56. return result[:len(result)-1], nil
  57. }
  58. return "", nil
  59. }
  60. // sanitizeOutput sanitizes output of a command, replacing occurrences of the
  61. // repository's remote address with a sanitized version.
  62. func sanitizeOutput(output, repoPath string) (string, error) {
  63. remoteAddr, err := remoteAddress(repoPath)
  64. if err != nil {
  65. // if we're unable to load the remote address, then we're unable to
  66. // sanitize.
  67. return "", err
  68. }
  69. return util.SanitizeMessage(output, remoteAddr), nil
  70. }
  71. // AddressNoCredentials returns mirror address from Git repository config without credentials.
  72. func AddressNoCredentials(m *models.Mirror) string {
  73. readAddress(m)
  74. u, err := url.Parse(m.Address)
  75. if err != nil {
  76. // this shouldn't happen but just return it unsanitised
  77. return m.Address
  78. }
  79. u.User = nil
  80. return u.String()
  81. }
  82. // UpdateAddress writes new address to Git repository and database
  83. func UpdateAddress(m *models.Mirror, addr string) error {
  84. repoPath := m.Repo.RepoPath()
  85. // Remove old origin
  86. _, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
  87. if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  88. return err
  89. }
  90. _, err = git.NewCommand("remote", "add", "origin", "--mirror=fetch", addr).RunInDir(repoPath)
  91. if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  92. return err
  93. }
  94. if m.Repo.HasWiki() {
  95. wikiPath := m.Repo.WikiPath()
  96. wikiRemotePath := repo_module.WikiRemoteURL(addr)
  97. // Remove old origin of wiki
  98. _, err := git.NewCommand("remote", "rm", "origin").RunInDir(wikiPath)
  99. if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  100. return err
  101. }
  102. _, err = git.NewCommand("remote", "add", "origin", "--mirror=fetch", wikiRemotePath).RunInDir(wikiPath)
  103. if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  104. return err
  105. }
  106. }
  107. m.Repo.OriginalURL = addr
  108. return models.UpdateRepositoryCols(m.Repo, "original_url")
  109. }
  110. // gitShortEmptySha Git short empty SHA
  111. const gitShortEmptySha = "0000000"
  112. // mirrorSyncResult contains information of a updated reference.
  113. // If the oldCommitID is "0000000", it means a new reference, the value of newCommitID is empty.
  114. // If the newCommitID is "0000000", it means the reference is deleted, the value of oldCommitID is empty.
  115. type mirrorSyncResult struct {
  116. refName string
  117. oldCommitID string
  118. newCommitID string
  119. }
  120. // parseRemoteUpdateOutput detects create, update and delete operations of references from upstream.
  121. func parseRemoteUpdateOutput(output string) []*mirrorSyncResult {
  122. results := make([]*mirrorSyncResult, 0, 3)
  123. lines := strings.Split(output, "\n")
  124. for i := range lines {
  125. // Make sure reference name is presented before continue
  126. idx := strings.Index(lines[i], "-> ")
  127. if idx == -1 {
  128. continue
  129. }
  130. refName := lines[i][idx+3:]
  131. switch {
  132. case strings.HasPrefix(lines[i], " * "): // New reference
  133. if strings.HasPrefix(lines[i], " * [new tag]") {
  134. refName = git.TagPrefix + refName
  135. } else if strings.HasPrefix(lines[i], " * [new branch]") {
  136. refName = git.BranchPrefix + refName
  137. }
  138. results = append(results, &mirrorSyncResult{
  139. refName: refName,
  140. oldCommitID: gitShortEmptySha,
  141. })
  142. case strings.HasPrefix(lines[i], " - "): // Delete reference
  143. results = append(results, &mirrorSyncResult{
  144. refName: refName,
  145. newCommitID: gitShortEmptySha,
  146. })
  147. case strings.HasPrefix(lines[i], " + "): // Force update
  148. if idx := strings.Index(refName, " "); idx > -1 {
  149. refName = refName[:idx]
  150. }
  151. delimIdx := strings.Index(lines[i][3:], " ")
  152. if delimIdx == -1 {
  153. log.Error("SHA delimiter not found: %q", lines[i])
  154. continue
  155. }
  156. shas := strings.Split(lines[i][3:delimIdx+3], "...")
  157. if len(shas) != 2 {
  158. log.Error("Expect two SHAs but not what found: %q", lines[i])
  159. continue
  160. }
  161. results = append(results, &mirrorSyncResult{
  162. refName: refName,
  163. oldCommitID: shas[0],
  164. newCommitID: shas[1],
  165. })
  166. case strings.HasPrefix(lines[i], " "): // New commits of a reference
  167. delimIdx := strings.Index(lines[i][3:], " ")
  168. if delimIdx == -1 {
  169. log.Error("SHA delimiter not found: %q", lines[i])
  170. continue
  171. }
  172. shas := strings.Split(lines[i][3:delimIdx+3], "..")
  173. if len(shas) != 2 {
  174. log.Error("Expect two SHAs but not what found: %q", lines[i])
  175. continue
  176. }
  177. results = append(results, &mirrorSyncResult{
  178. refName: refName,
  179. oldCommitID: shas[0],
  180. newCommitID: shas[1],
  181. })
  182. default:
  183. log.Warn("parseRemoteUpdateOutput: unexpected update line %q", lines[i])
  184. }
  185. }
  186. return results
  187. }
  188. // runSync returns true if sync finished without error.
  189. func runSync(ctx context.Context, m *models.Mirror) ([]*mirrorSyncResult, bool) {
  190. repoPath := m.Repo.RepoPath()
  191. wikiPath := m.Repo.WikiPath()
  192. timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
  193. log.Trace("SyncMirrors [repo: %-v]: running git remote update...", m.Repo)
  194. gitArgs := []string{"remote", "update"}
  195. if m.EnablePrune {
  196. gitArgs = append(gitArgs, "--prune")
  197. }
  198. stdoutBuilder := strings.Builder{}
  199. stderrBuilder := strings.Builder{}
  200. if err := git.NewCommand(gitArgs...).
  201. SetDescription(fmt.Sprintf("Mirror.runSync: %s", m.Repo.FullName())).
  202. RunInDirTimeoutPipeline(timeout, repoPath, &stdoutBuilder, &stderrBuilder); err != nil {
  203. stdout := stdoutBuilder.String()
  204. stderr := stderrBuilder.String()
  205. // sanitize the output, since it may contain the remote address, which may
  206. // contain a password
  207. stderrMessage, sanitizeErr := sanitizeOutput(stderr, repoPath)
  208. if sanitizeErr != nil {
  209. log.Error("sanitizeOutput failed on stderr: %v", sanitizeErr)
  210. log.Error("Failed to update mirror repository %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdout, stderr, err)
  211. return nil, false
  212. }
  213. stdoutMessage, err := sanitizeOutput(stdout, repoPath)
  214. if err != nil {
  215. log.Error("sanitizeOutput failed: %v", sanitizeErr)
  216. log.Error("Failed to update mirror repository %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdout, stderrMessage, err)
  217. return nil, false
  218. }
  219. log.Error("Failed to update mirror repository %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdoutMessage, stderrMessage, err)
  220. desc := fmt.Sprintf("Failed to update mirror repository '%s': %s", repoPath, stderrMessage)
  221. if err = models.CreateRepositoryNotice(desc); err != nil {
  222. log.Error("CreateRepositoryNotice: %v", err)
  223. }
  224. return nil, false
  225. }
  226. output := stderrBuilder.String()
  227. gitRepo, err := git.OpenRepository(repoPath)
  228. if err != nil {
  229. log.Error("OpenRepository: %v", err)
  230. return nil, false
  231. }
  232. defer gitRepo.Close()
  233. log.Trace("SyncMirrors [repo: %-v]: syncing releases with tags...", m.Repo)
  234. if err = repo_module.SyncReleasesWithTags(m.Repo, gitRepo); err != nil {
  235. log.Error("Failed to synchronize tags to releases for repository: %v", err)
  236. }
  237. if m.LFS && setting.LFS.StartServer {
  238. log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo)
  239. readAddress(m)
  240. ep := lfs.DetermineEndpoint(m.Address, m.LFSEndpoint)
  241. if err = repo_module.StoreMissingLfsObjectsInRepository(ctx, m.Repo, gitRepo, ep); err != nil {
  242. log.Error("Failed to synchronize LFS objects for repository: %v", err)
  243. }
  244. }
  245. log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo)
  246. if err := m.Repo.UpdateSize(models.DefaultDBContext()); err != nil {
  247. log.Error("Failed to update size for mirror repository: %v", err)
  248. }
  249. if m.Repo.HasWiki() {
  250. log.Trace("SyncMirrors [repo: %-v Wiki]: running git remote update...", m.Repo)
  251. stderrBuilder.Reset()
  252. stdoutBuilder.Reset()
  253. if err := git.NewCommand("remote", "update", "--prune").
  254. SetDescription(fmt.Sprintf("Mirror.runSync Wiki: %s ", m.Repo.FullName())).
  255. RunInDirTimeoutPipeline(timeout, wikiPath, &stdoutBuilder, &stderrBuilder); err != nil {
  256. stdout := stdoutBuilder.String()
  257. stderr := stderrBuilder.String()
  258. // sanitize the output, since it may contain the remote address, which may
  259. // contain a password
  260. stderrMessage, sanitizeErr := sanitizeOutput(stderr, repoPath)
  261. if sanitizeErr != nil {
  262. log.Error("sanitizeOutput failed on stderr: %v", sanitizeErr)
  263. log.Error("Failed to update mirror repository wiki %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdout, stderr, err)
  264. return nil, false
  265. }
  266. stdoutMessage, err := sanitizeOutput(stdout, repoPath)
  267. if err != nil {
  268. log.Error("sanitizeOutput failed: %v", sanitizeErr)
  269. log.Error("Failed to update mirror repository wiki %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdout, stderrMessage, err)
  270. return nil, false
  271. }
  272. log.Error("Failed to update mirror repository wiki %v:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdoutMessage, stderrMessage, err)
  273. desc := fmt.Sprintf("Failed to update mirror repository wiki '%s': %s", wikiPath, stderrMessage)
  274. if err = models.CreateRepositoryNotice(desc); err != nil {
  275. log.Error("CreateRepositoryNotice: %v", err)
  276. }
  277. return nil, false
  278. }
  279. log.Trace("SyncMirrors [repo: %-v Wiki]: git remote update complete", m.Repo)
  280. }
  281. log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
  282. branches, _, err := repo_module.GetBranches(m.Repo, 0, 0)
  283. if err != nil {
  284. log.Error("GetBranches: %v", err)
  285. return nil, false
  286. }
  287. for _, branch := range branches {
  288. cache.Remove(m.Repo.GetCommitsCountCacheKey(branch.Name, true))
  289. }
  290. m.UpdatedUnix = timeutil.TimeStampNow()
  291. return parseRemoteUpdateOutput(output), true
  292. }
  293. // Address returns mirror address from Git repository config without credentials.
  294. func Address(m *models.Mirror) string {
  295. readAddress(m)
  296. return util.SanitizeURLCredentials(m.Address, false)
  297. }
  298. // Username returns the mirror address username
  299. func Username(m *models.Mirror) string {
  300. readAddress(m)
  301. u, err := url.Parse(m.Address)
  302. if err != nil {
  303. // this shouldn't happen but if it does return ""
  304. return ""
  305. }
  306. return u.User.Username()
  307. }
  308. // Password returns the mirror address password
  309. func Password(m *models.Mirror) string {
  310. readAddress(m)
  311. u, err := url.Parse(m.Address)
  312. if err != nil {
  313. // this shouldn't happen but if it does return ""
  314. return ""
  315. }
  316. password, _ := u.User.Password()
  317. return password
  318. }
  319. // Update checks and updates mirror repositories.
  320. func Update(ctx context.Context) error {
  321. log.Trace("Doing: Update")
  322. if err := models.MirrorsIterate(func(idx int, bean interface{}) error {
  323. m := bean.(*models.Mirror)
  324. if m.Repo == nil {
  325. log.Error("Disconnected mirror repository found: %d", m.ID)
  326. return nil
  327. }
  328. select {
  329. case <-ctx.Done():
  330. return fmt.Errorf("Aborted")
  331. default:
  332. mirrorQueue.Add(m.RepoID)
  333. return nil
  334. }
  335. }); err != nil {
  336. log.Trace("Update: %v", err)
  337. return err
  338. }
  339. log.Trace("Finished: Update")
  340. return nil
  341. }
  342. // SyncMirrors checks and syncs mirrors.
  343. // FIXME: graceful: this should be a persistable queue
  344. func SyncMirrors(ctx context.Context) {
  345. // Start listening on new sync requests.
  346. for {
  347. select {
  348. case <-ctx.Done():
  349. mirrorQueue.Close()
  350. return
  351. case repoID := <-mirrorQueue.Queue():
  352. syncMirror(ctx, repoID)
  353. }
  354. }
  355. }
  356. func syncMirror(ctx context.Context, repoID string) {
  357. log.Trace("SyncMirrors [repo_id: %v]", repoID)
  358. defer func() {
  359. err := recover()
  360. if err == nil {
  361. return
  362. }
  363. // There was a panic whilst syncMirrors...
  364. log.Error("PANIC whilst syncMirrors[%s] Panic: %v\nStacktrace: %s", repoID, err, log.Stack(2))
  365. }()
  366. mirrorQueue.Remove(repoID)
  367. id, _ := strconv.ParseInt(repoID, 10, 64)
  368. m, err := models.GetMirrorByRepoID(id)
  369. if err != nil {
  370. log.Error("GetMirrorByRepoID [%s]: %v", repoID, err)
  371. return
  372. }
  373. log.Trace("SyncMirrors [repo: %-v]: Running Sync", m.Repo)
  374. results, ok := runSync(ctx, m)
  375. if !ok {
  376. return
  377. }
  378. log.Trace("SyncMirrors [repo: %-v]: Scheduling next update", m.Repo)
  379. m.ScheduleNextUpdate()
  380. if err = models.UpdateMirror(m); err != nil {
  381. log.Error("UpdateMirror [%s]: %v", repoID, err)
  382. return
  383. }
  384. var gitRepo *git.Repository
  385. if len(results) == 0 {
  386. log.Trace("SyncMirrors [repo: %-v]: no branches updated", m.Repo)
  387. } else {
  388. log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
  389. gitRepo, err = git.OpenRepository(m.Repo.RepoPath())
  390. if err != nil {
  391. log.Error("OpenRepository [%d]: %v", m.RepoID, err)
  392. return
  393. }
  394. defer gitRepo.Close()
  395. if ok := checkAndUpdateEmptyRepository(m, gitRepo, results); !ok {
  396. return
  397. }
  398. }
  399. for _, result := range results {
  400. // Discard GitHub pull requests, i.e. refs/pull/*
  401. if strings.HasPrefix(result.refName, "refs/pull/") {
  402. continue
  403. }
  404. tp, _ := git.SplitRefName(result.refName)
  405. // Create reference
  406. if result.oldCommitID == gitShortEmptySha {
  407. if tp == git.TagPrefix {
  408. tp = "tag"
  409. } else if tp == git.BranchPrefix {
  410. tp = "branch"
  411. }
  412. commitID, err := gitRepo.GetRefCommitID(result.refName)
  413. if err != nil {
  414. log.Error("gitRepo.GetRefCommitID [repo_id: %s, ref_name: %s]: %v", m.RepoID, result.refName, err)
  415. continue
  416. }
  417. notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, &repo_module.PushUpdateOptions{
  418. RefFullName: result.refName,
  419. OldCommitID: git.EmptySHA,
  420. NewCommitID: commitID,
  421. }, repo_module.NewPushCommits())
  422. notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
  423. continue
  424. }
  425. // Delete reference
  426. if result.newCommitID == gitShortEmptySha {
  427. notification.NotifySyncDeleteRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
  428. continue
  429. }
  430. // Push commits
  431. oldCommitID, err := git.GetFullCommitID(gitRepo.Path, result.oldCommitID)
  432. if err != nil {
  433. log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
  434. continue
  435. }
  436. newCommitID, err := git.GetFullCommitID(gitRepo.Path, result.newCommitID)
  437. if err != nil {
  438. log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
  439. continue
  440. }
  441. commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID)
  442. if err != nil {
  443. log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err)
  444. continue
  445. }
  446. theCommits := repo_module.ListToPushCommits(commits)
  447. if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
  448. theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
  449. }
  450. theCommits.CompareURL = m.Repo.ComposeCompareURL(oldCommitID, newCommitID)
  451. notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, &repo_module.PushUpdateOptions{
  452. RefFullName: result.refName,
  453. OldCommitID: oldCommitID,
  454. NewCommitID: newCommitID,
  455. }, theCommits)
  456. }
  457. log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
  458. // Get latest commit date and update to current repository updated time
  459. commitDate, err := git.GetLatestCommitTime(m.Repo.RepoPath())
  460. if err != nil {
  461. log.Error("GetLatestCommitDate [%d]: %v", m.RepoID, err)
  462. return
  463. }
  464. if err = models.UpdateRepositoryUpdatedTime(m.RepoID, commitDate); err != nil {
  465. log.Error("Update repository 'updated_unix' [%d]: %v", m.RepoID, err)
  466. return
  467. }
  468. log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)
  469. }
  470. func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, results []*mirrorSyncResult) bool {
  471. if !m.Repo.IsEmpty {
  472. return true
  473. }
  474. hasDefault := false
  475. hasMaster := false
  476. hasMain := false
  477. defaultBranchName := m.Repo.DefaultBranch
  478. if len(defaultBranchName) == 0 {
  479. defaultBranchName = setting.Repository.DefaultBranch
  480. }
  481. firstName := ""
  482. for _, result := range results {
  483. if strings.HasPrefix(result.refName, "refs/pull/") {
  484. continue
  485. }
  486. tp, name := git.SplitRefName(result.refName)
  487. if len(tp) > 0 && tp != git.BranchPrefix {
  488. continue
  489. }
  490. if len(firstName) == 0 {
  491. firstName = name
  492. }
  493. hasDefault = hasDefault || name == defaultBranchName
  494. hasMaster = hasMaster || name == "master"
  495. hasMain = hasMain || name == "main"
  496. }
  497. if len(firstName) > 0 {
  498. if hasDefault {
  499. m.Repo.DefaultBranch = defaultBranchName
  500. } else if hasMaster {
  501. m.Repo.DefaultBranch = "master"
  502. } else if hasMain {
  503. m.Repo.DefaultBranch = "main"
  504. } else {
  505. m.Repo.DefaultBranch = firstName
  506. }
  507. // Update the git repository default branch
  508. if err := gitRepo.SetDefaultBranch(m.Repo.DefaultBranch); err != nil {
  509. if !git.IsErrUnsupportedVersion(err) {
  510. log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
  511. desc := fmt.Sprintf("Failed to uupdate default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)
  512. if err = models.CreateRepositoryNotice(desc); err != nil {
  513. log.Error("CreateRepositoryNotice: %v", err)
  514. }
  515. return false
  516. }
  517. }
  518. m.Repo.IsEmpty = false
  519. // Update the is empty and default_branch columns
  520. if err := models.UpdateRepositoryCols(m.Repo, "default_branch", "is_empty"); err != nil {
  521. log.Error("Failed to update default branch of repository %-v. Error: %v", m.Repo, err)
  522. desc := fmt.Sprintf("Failed to uupdate default branch of repository '%s': %v", m.Repo.RepoPath(), err)
  523. if err = models.CreateRepositoryNotice(desc); err != nil {
  524. log.Error("CreateRepositoryNotice: %v", err)
  525. }
  526. return false
  527. }
  528. }
  529. return true
  530. }
  531. // InitSyncMirrors initializes a go routine to sync the mirrors
  532. func InitSyncMirrors() {
  533. go graceful.GetManager().RunWithShutdownContext(SyncMirrors)
  534. }
  535. // StartToMirror adds repoID to mirror queue
  536. func StartToMirror(repoID int64) {
  537. go mirrorQueue.Add(repoID)
  538. }