mirror.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. package database
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/cockroachdb/errors"
  10. "gopkg.in/ini.v1"
  11. log "unknwon.dev/clog/v2"
  12. "xorm.io/xorm"
  13. "github.com/gogs/git-module"
  14. "gogs.io/gogs/internal/conf"
  15. "gogs.io/gogs/internal/process"
  16. "gogs.io/gogs/internal/sync"
  17. )
  18. var MirrorQueue = sync.NewUniqueQueue(1000)
  19. // MirrorNotExist represents an error when mirror does not exist.
  20. type MirrorNotExist struct {
  21. RepoID int64
  22. }
  23. // IsMirrorNotExist returns true if the error is MirrorNotExist.
  24. func IsMirrorNotExist(err error) bool {
  25. _, ok := err.(MirrorNotExist)
  26. return ok
  27. }
  28. func (err MirrorNotExist) Error() string {
  29. return fmt.Sprintf("mirror does not exist [repo_id: %d]", err.RepoID)
  30. }
  31. // Mirror represents mirror information of a repository.
  32. type Mirror struct {
  33. ID int64
  34. RepoID int64
  35. Repo *Repository `xorm:"-" json:"-" gorm:"-"`
  36. Interval int // Hour.
  37. EnablePrune bool `xorm:"NOT NULL DEFAULT true"`
  38. // Last and next sync time of Git data from upstream
  39. LastSync time.Time `xorm:"-" json:"-" gorm:"-"`
  40. LastSyncUnix int64 `xorm:"updated_unix"`
  41. NextSync time.Time `xorm:"-" json:"-" gorm:"-"`
  42. NextSyncUnix int64 `xorm:"next_update_unix"`
  43. address string `xorm:"-"`
  44. }
  45. func (m *Mirror) BeforeInsert() {
  46. m.NextSyncUnix = m.NextSync.Unix()
  47. }
  48. func (m *Mirror) BeforeUpdate() {
  49. m.LastSyncUnix = m.LastSync.Unix()
  50. m.NextSyncUnix = m.NextSync.Unix()
  51. }
  52. func (m *Mirror) AfterSet(colName string, _ xorm.Cell) {
  53. var err error
  54. switch colName {
  55. case "repo_id":
  56. m.Repo, err = GetRepositoryByID(m.RepoID)
  57. if err != nil {
  58. log.Error("GetRepositoryByID [%d]: %v", m.ID, err)
  59. }
  60. case "updated_unix":
  61. m.LastSync = time.Unix(m.LastSyncUnix, 0).Local()
  62. case "next_update_unix":
  63. m.NextSync = time.Unix(m.NextSyncUnix, 0).Local()
  64. }
  65. }
  66. // ScheduleNextSync calculates and sets next sync time based on repository mirror setting.
  67. func (m *Mirror) ScheduleNextSync() {
  68. m.NextSync = time.Now().Add(time.Duration(m.Interval) * time.Hour)
  69. }
  70. func (m *Mirror) readAddress() {
  71. if len(m.address) > 0 {
  72. return
  73. }
  74. cfg, err := ini.LoadSources(
  75. ini.LoadOptions{IgnoreInlineComment: true},
  76. m.Repo.GitConfigPath(),
  77. )
  78. if err != nil {
  79. log.Error("load config: %v", err)
  80. return
  81. }
  82. m.address = cfg.Section("remote \"origin\"").Key("url").Value()
  83. }
  84. // HandleMirrorCredentials replaces user credentials from HTTP/HTTPS URL
  85. // with placeholder <credentials>.
  86. // It returns original string if protocol is not HTTP/HTTPS.
  87. // TODO(unknwon): Use url.Parse.
  88. func HandleMirrorCredentials(url string, mosaics bool) string {
  89. i := strings.Index(url, "@")
  90. if i == -1 {
  91. return url
  92. }
  93. start := strings.Index(url, "://")
  94. if start == -1 {
  95. return url
  96. }
  97. if mosaics {
  98. return url[:start+3] + "<credentials>" + url[i:]
  99. }
  100. return url[:start+3] + url[i+1:]
  101. }
  102. // Address returns mirror address from Git repository config without credentials.
  103. func (m *Mirror) Address() string {
  104. m.readAddress()
  105. return HandleMirrorCredentials(m.address, false)
  106. }
  107. // MosaicsAddress returns mirror address from Git repository config with credentials under mosaics.
  108. func (m *Mirror) MosaicsAddress() string {
  109. m.readAddress()
  110. return HandleMirrorCredentials(m.address, true)
  111. }
  112. // RawAddress returns raw mirror address directly from Git repository config.
  113. func (m *Mirror) RawAddress() string {
  114. m.readAddress()
  115. return m.address
  116. }
  117. // SaveAddress writes new address to Git repository config.
  118. func (m *Mirror) SaveAddress(addr string) error {
  119. repoPath := m.Repo.RepoPath()
  120. err := git.RemoteRemove(repoPath, "origin")
  121. if err != nil {
  122. return errors.Newf("remove remote 'origin': %v", err)
  123. }
  124. addrURL, err := url.Parse(addr)
  125. if err != nil {
  126. return err
  127. }
  128. err = git.RemoteAdd(repoPath, "origin", addrURL.String(), git.RemoteAddOptions{MirrorFetch: true})
  129. if err != nil {
  130. return errors.Newf("add remote 'origin': %v", err)
  131. }
  132. return nil
  133. }
  134. const gitShortEmptyID = "0000000"
  135. // mirrorSyncResult contains information of a updated reference.
  136. // If the oldCommitID is "0000000", it means a new reference, the value of newCommitID is empty.
  137. // If the newCommitID is "0000000", it means the reference is deleted, the value of oldCommitID is empty.
  138. type mirrorSyncResult struct {
  139. refName string
  140. oldCommitID string
  141. newCommitID string
  142. }
  143. // parseRemoteUpdateOutput detects create, update and delete operations of references from upstream.
  144. func parseRemoteUpdateOutput(output string) []*mirrorSyncResult {
  145. results := make([]*mirrorSyncResult, 0, 3)
  146. lines := strings.Split(output, "\n")
  147. for i := range lines {
  148. // Make sure reference name is presented before continue
  149. idx := strings.Index(lines[i], "-> ")
  150. if idx == -1 {
  151. continue
  152. }
  153. refName := lines[i][idx+3:]
  154. switch {
  155. case strings.HasPrefix(lines[i], " * "): // New reference
  156. results = append(results, &mirrorSyncResult{
  157. refName: refName,
  158. oldCommitID: gitShortEmptyID,
  159. })
  160. case strings.HasPrefix(lines[i], " - "): // Delete reference
  161. results = append(results, &mirrorSyncResult{
  162. refName: refName,
  163. newCommitID: gitShortEmptyID,
  164. })
  165. case strings.HasPrefix(lines[i], " "): // New commits of a reference
  166. delimIdx := strings.Index(lines[i][3:], " ")
  167. if delimIdx == -1 {
  168. log.Error("SHA delimiter not found: %q", lines[i])
  169. continue
  170. }
  171. shas := strings.Split(lines[i][3:delimIdx+3], "..")
  172. if len(shas) != 2 {
  173. log.Error("Expect two SHAs but not what found: %q", lines[i])
  174. continue
  175. }
  176. results = append(results, &mirrorSyncResult{
  177. refName: refName,
  178. oldCommitID: shas[0],
  179. newCommitID: shas[1],
  180. })
  181. default:
  182. log.Warn("parseRemoteUpdateOutput: unexpected update line %q", lines[i])
  183. }
  184. }
  185. return results
  186. }
  187. // runSync returns true if sync finished without error.
  188. func (m *Mirror) runSync() ([]*mirrorSyncResult, bool) {
  189. repoPath := m.Repo.RepoPath()
  190. wikiPath := m.Repo.WikiPath()
  191. timeout := time.Duration(conf.Git.Timeout.Mirror) * time.Second
  192. // Do a fast-fail testing against on repository URL to ensure it is accessible under
  193. // good condition to prevent long blocking on URL resolution without syncing anything.
  194. if !git.IsURLAccessible(time.Minute, m.RawAddress()) {
  195. desc := fmt.Sprintf("Source URL of mirror repository '%s' is not accessible: %s", m.Repo.FullName(), m.MosaicsAddress())
  196. if err := Handle.Notices().Create(context.TODO(), NoticeTypeRepository, desc); err != nil {
  197. log.Error("CreateRepositoryNotice: %v", err)
  198. }
  199. return nil, false
  200. }
  201. gitArgs := []string{"remote", "update"}
  202. if m.EnablePrune {
  203. gitArgs = append(gitArgs, "--prune")
  204. }
  205. _, stderr, err := process.ExecDir(
  206. timeout, repoPath, fmt.Sprintf("Mirror.runSync: %s", repoPath),
  207. "git", gitArgs...)
  208. if err != nil {
  209. const fmtStr = "Failed to update mirror repository %q: %s"
  210. log.Error(fmtStr, repoPath, stderr)
  211. if err = Handle.Notices().Create(
  212. context.TODO(),
  213. NoticeTypeRepository,
  214. fmt.Sprintf(fmtStr, repoPath, stderr),
  215. ); err != nil {
  216. log.Error("CreateRepositoryNotice: %v", err)
  217. }
  218. return nil, false
  219. }
  220. output := stderr
  221. if err := m.Repo.UpdateSize(); err != nil {
  222. log.Error("UpdateSize [repo_id: %d]: %v", m.Repo.ID, err)
  223. }
  224. if m.Repo.HasWiki() {
  225. // Even if wiki sync failed, we still want results from the main repository
  226. if _, stderr, err := process.ExecDir(
  227. timeout, wikiPath, fmt.Sprintf("Mirror.runSync: %s", wikiPath),
  228. "git", "remote", "update", "--prune"); err != nil {
  229. const fmtStr = "Failed to update mirror wiki repository %q: %s"
  230. log.Error(fmtStr, wikiPath, stderr)
  231. if err = Handle.Notices().Create(
  232. context.TODO(),
  233. NoticeTypeRepository,
  234. fmt.Sprintf(fmtStr, wikiPath, stderr),
  235. ); err != nil {
  236. log.Error("CreateRepositoryNotice: %v", err)
  237. }
  238. }
  239. }
  240. return parseRemoteUpdateOutput(output), true
  241. }
  242. func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
  243. m := &Mirror{RepoID: repoID}
  244. has, err := e.Get(m)
  245. if err != nil {
  246. return nil, err
  247. } else if !has {
  248. return nil, MirrorNotExist{RepoID: repoID}
  249. }
  250. return m, nil
  251. }
  252. // GetMirrorByRepoID returns mirror information of a repository.
  253. func GetMirrorByRepoID(repoID int64) (*Mirror, error) {
  254. return getMirrorByRepoID(x, repoID)
  255. }
  256. func updateMirror(e Engine, m *Mirror) error {
  257. _, err := e.ID(m.ID).AllCols().Update(m)
  258. return err
  259. }
  260. func UpdateMirror(m *Mirror) error {
  261. return updateMirror(x, m)
  262. }
  263. func DeleteMirrorByRepoID(repoID int64) error {
  264. _, err := x.Delete(&Mirror{RepoID: repoID})
  265. return err
  266. }
  267. // MirrorUpdate checks and updates mirror repositories.
  268. func MirrorUpdate() {
  269. if taskStatusTable.IsRunning(taskNameMirrorUpdate) {
  270. return
  271. }
  272. taskStatusTable.Start(taskNameMirrorUpdate)
  273. defer taskStatusTable.Stop(taskNameMirrorUpdate)
  274. log.Trace("Doing: MirrorUpdate")
  275. if err := x.Where("next_update_unix<=?", time.Now().Unix()).Iterate(new(Mirror), func(idx int, bean any) error {
  276. m := bean.(*Mirror)
  277. if m.Repo == nil {
  278. log.Error("Disconnected mirror repository found: %d", m.ID)
  279. return nil
  280. }
  281. MirrorQueue.Add(m.RepoID)
  282. return nil
  283. }); err != nil {
  284. log.Error("MirrorUpdate: %v", err)
  285. }
  286. }
  287. // SyncMirrors checks and syncs mirrors.
  288. // TODO: sync more mirrors at same time.
  289. func SyncMirrors() {
  290. ctx := context.Background()
  291. // Start listening on new sync requests.
  292. for repoID := range MirrorQueue.Queue() {
  293. log.Trace("SyncMirrors [repo_id: %s]", repoID)
  294. MirrorQueue.Remove(repoID)
  295. id, _ := strconv.ParseInt(repoID, 10, 64)
  296. m, err := GetMirrorByRepoID(id)
  297. if err != nil {
  298. log.Error("GetMirrorByRepoID [%v]: %v", repoID, err)
  299. continue
  300. }
  301. results, ok := m.runSync()
  302. if !ok {
  303. continue
  304. }
  305. m.ScheduleNextSync()
  306. if err = UpdateMirror(m); err != nil {
  307. log.Error("UpdateMirror [%d]: %v", m.RepoID, err)
  308. continue
  309. }
  310. // TODO:
  311. // - Create "Mirror Sync" webhook event
  312. // - Create mirror sync (create, push and delete) events and trigger the "mirror sync" webhooks
  313. if len(results) == 0 {
  314. log.Trace("SyncMirrors [repo_id: %d]: no commits fetched", m.RepoID)
  315. }
  316. gitRepo, err := git.Open(m.Repo.RepoPath())
  317. if err != nil {
  318. log.Error("Failed to open repository [repo_id: %d]: %v", m.RepoID, err)
  319. continue
  320. }
  321. for _, result := range results {
  322. // Discard GitHub pull requests, i.e. refs/pull/*
  323. if strings.HasPrefix(result.refName, "refs/pull/") {
  324. continue
  325. }
  326. // Delete reference
  327. if result.newCommitID == gitShortEmptyID {
  328. if err = Handle.Actions().MirrorSyncDelete(ctx, m.Repo.MustOwner(), m.Repo, result.refName); err != nil {
  329. log.Error("Failed to create action for mirror sync delete [repo_id: %d]: %v", m.RepoID, err)
  330. }
  331. continue
  332. }
  333. // New reference
  334. isNewRef := false
  335. if result.oldCommitID == gitShortEmptyID {
  336. if err = Handle.Actions().MirrorSyncCreate(ctx, m.Repo.MustOwner(), m.Repo, result.refName); err != nil {
  337. log.Error("Failed to create action for mirror sync create [repo_id: %d]: %v", m.RepoID, err)
  338. continue
  339. }
  340. isNewRef = true
  341. }
  342. // Push commits
  343. var commits []*git.Commit
  344. var oldCommitID string
  345. var newCommitID string
  346. if !isNewRef {
  347. oldCommitID, err = gitRepo.RevParse(result.oldCommitID)
  348. if err != nil {
  349. log.Error("Failed to parse revision [repo_id: %d, old_commit_id: %s]: %v", m.RepoID, result.oldCommitID, err)
  350. continue
  351. }
  352. newCommitID, err = gitRepo.RevParse(result.newCommitID)
  353. if err != nil {
  354. log.Error("Failed to parse revision [repo_id: %d, new_commit_id: %s]: %v", m.RepoID, result.newCommitID, err)
  355. continue
  356. }
  357. commits, err = gitRepo.RevList([]string{oldCommitID + "..." + newCommitID})
  358. if err != nil {
  359. log.Error("Failed to list commits [repo_id: %d, old_commit_id: %s, new_commit_id: %s]: %v", m.RepoID, oldCommitID, newCommitID, err)
  360. continue
  361. }
  362. } else if gitRepo.HasBranch(result.refName) {
  363. refNewCommit, err := gitRepo.BranchCommit(result.refName)
  364. if err != nil {
  365. log.Error("Failed to get branch commit [repo_id: %d, branch: %s]: %v", m.RepoID, result.refName, err)
  366. continue
  367. }
  368. // TODO(unknwon): Get the commits for the new ref until the closest ancestor branch like GitHub does.
  369. commits, err = refNewCommit.Ancestors(git.LogOptions{MaxCount: 9})
  370. if err != nil {
  371. log.Error("Failed to get ancestors [repo_id: %d, commit_id: %s]: %v", m.RepoID, refNewCommit.ID, err)
  372. continue
  373. }
  374. // Put the latest commit in front of ancestors
  375. commits = append([]*git.Commit{refNewCommit}, commits...)
  376. oldCommitID = git.EmptyID
  377. newCommitID = refNewCommit.ID.String()
  378. }
  379. err = Handle.Actions().MirrorSyncPush(ctx,
  380. MirrorSyncPushOptions{
  381. Owner: m.Repo.MustOwner(),
  382. Repo: m.Repo,
  383. RefName: result.refName,
  384. OldCommitID: oldCommitID,
  385. NewCommitID: newCommitID,
  386. Commits: CommitsToPushCommits(commits),
  387. },
  388. )
  389. if err != nil {
  390. log.Error("Failed to create action for mirror sync push [repo_id: %d]: %v", m.RepoID, err)
  391. continue
  392. }
  393. }
  394. if _, err = x.Exec("UPDATE mirror SET updated_unix = ? WHERE repo_id = ?", time.Now().Unix(), m.RepoID); err != nil {
  395. log.Error("Update 'mirror.updated_unix' [%d]: %v", m.RepoID, err)
  396. continue
  397. }
  398. // Get latest commit date and compare to current repository updated time,
  399. // update if latest commit date is newer.
  400. latestCommitTime, err := gitRepo.LatestCommitTime()
  401. if err != nil {
  402. log.Error("GetLatestCommitDate [%d]: %v", m.RepoID, err)
  403. continue
  404. } else if !latestCommitTime.After(m.Repo.Updated) {
  405. continue
  406. }
  407. if _, err = x.Exec("UPDATE repository SET updated_unix = ? WHERE id = ?", latestCommitTime.Unix(), m.RepoID); err != nil {
  408. log.Error("Update 'repository.updated_unix' [%d]: %v", m.RepoID, err)
  409. continue
  410. }
  411. }
  412. }
  413. func InitSyncMirrors() {
  414. go SyncMirrors()
  415. }