本站源代码
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

396 行
11KB

  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. "fmt"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/cache"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/process"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/sync"
  17. "code.gitea.io/gitea/modules/timeutil"
  18. "code.gitea.io/gitea/modules/util"
  19. "github.com/mcuadros/go-version"
  20. "github.com/unknwon/com"
  21. )
  22. // mirrorQueue holds an UniqueQueue object of the mirror
  23. var mirrorQueue = sync.NewUniqueQueue(setting.Repository.MirrorQueueLength)
  24. func readAddress(m *models.Mirror) {
  25. if len(m.Address) > 0 {
  26. return
  27. }
  28. var err error
  29. m.Address, err = remoteAddress(m.Repo.RepoPath())
  30. if err != nil {
  31. log.Error("remoteAddress: %v", err)
  32. }
  33. }
  34. func remoteAddress(repoPath string) (string, error) {
  35. var cmd *git.Command
  36. binVersion, err := git.BinVersion()
  37. if err != nil {
  38. return "", err
  39. }
  40. if version.Compare(binVersion, "2.7", ">=") {
  41. cmd = git.NewCommand("remote", "get-url", "origin")
  42. } else {
  43. cmd = git.NewCommand("config", "--get", "remote.origin.url")
  44. }
  45. result, err := cmd.RunInDir(repoPath)
  46. if err != nil {
  47. if strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  48. return "", nil
  49. }
  50. return "", err
  51. }
  52. if len(result) > 0 {
  53. return result[:len(result)-1], nil
  54. }
  55. return "", nil
  56. }
  57. // sanitizeOutput sanitizes output of a command, replacing occurrences of the
  58. // repository's remote address with a sanitized version.
  59. func sanitizeOutput(output, repoPath string) (string, error) {
  60. remoteAddr, err := remoteAddress(repoPath)
  61. if err != nil {
  62. // if we're unable to load the remote address, then we're unable to
  63. // sanitize.
  64. return "", err
  65. }
  66. return util.SanitizeMessage(output, remoteAddr), nil
  67. }
  68. // AddressNoCredentials returns mirror address from Git repository config without credentials.
  69. func AddressNoCredentials(m *models.Mirror) string {
  70. readAddress(m)
  71. u, err := url.Parse(m.Address)
  72. if err != nil {
  73. // this shouldn't happen but just return it unsanitised
  74. return m.Address
  75. }
  76. u.User = nil
  77. return u.String()
  78. }
  79. // SaveAddress writes new address to Git repository config.
  80. func SaveAddress(m *models.Mirror, addr string) error {
  81. repoPath := m.Repo.RepoPath()
  82. // Remove old origin
  83. _, err := git.NewCommand("remote", "rm", "origin").RunInDir(repoPath)
  84. if err != nil && !strings.HasPrefix(err.Error(), "exit status 128 - fatal: No such remote ") {
  85. return err
  86. }
  87. _, err = git.NewCommand("remote", "add", "origin", "--mirror=fetch", addr).RunInDir(repoPath)
  88. return err
  89. }
  90. // gitShortEmptySha Git short empty SHA
  91. const gitShortEmptySha = "0000000"
  92. // mirrorSyncResult contains information of a updated reference.
  93. // If the oldCommitID is "0000000", it means a new reference, the value of newCommitID is empty.
  94. // If the newCommitID is "0000000", it means the reference is deleted, the value of oldCommitID is empty.
  95. type mirrorSyncResult struct {
  96. refName string
  97. oldCommitID string
  98. newCommitID string
  99. }
  100. // parseRemoteUpdateOutput detects create, update and delete operations of references from upstream.
  101. func parseRemoteUpdateOutput(output string) []*mirrorSyncResult {
  102. results := make([]*mirrorSyncResult, 0, 3)
  103. lines := strings.Split(output, "\n")
  104. for i := range lines {
  105. // Make sure reference name is presented before continue
  106. idx := strings.Index(lines[i], "-> ")
  107. if idx == -1 {
  108. continue
  109. }
  110. refName := lines[i][idx+3:]
  111. switch {
  112. case strings.HasPrefix(lines[i], " * "): // New reference
  113. results = append(results, &mirrorSyncResult{
  114. refName: refName,
  115. oldCommitID: gitShortEmptySha,
  116. })
  117. case strings.HasPrefix(lines[i], " - "): // Delete reference
  118. results = append(results, &mirrorSyncResult{
  119. refName: refName,
  120. newCommitID: gitShortEmptySha,
  121. })
  122. case strings.HasPrefix(lines[i], " "): // New commits of a reference
  123. delimIdx := strings.Index(lines[i][3:], " ")
  124. if delimIdx == -1 {
  125. log.Error("SHA delimiter not found: %q", lines[i])
  126. continue
  127. }
  128. shas := strings.Split(lines[i][3:delimIdx+3], "..")
  129. if len(shas) != 2 {
  130. log.Error("Expect two SHAs but not what found: %q", lines[i])
  131. continue
  132. }
  133. results = append(results, &mirrorSyncResult{
  134. refName: refName,
  135. oldCommitID: shas[0],
  136. newCommitID: shas[1],
  137. })
  138. default:
  139. log.Warn("parseRemoteUpdateOutput: unexpected update line %q", lines[i])
  140. }
  141. }
  142. return results
  143. }
  144. // runSync returns true if sync finished without error.
  145. func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
  146. repoPath := m.Repo.RepoPath()
  147. wikiPath := m.Repo.WikiPath()
  148. timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
  149. gitArgs := []string{"remote", "update"}
  150. if m.EnablePrune {
  151. gitArgs = append(gitArgs, "--prune")
  152. }
  153. _, stderr, err := process.GetManager().ExecDir(
  154. timeout, repoPath, fmt.Sprintf("Mirror.runSync: %s", repoPath),
  155. git.GitExecutable, gitArgs...)
  156. if err != nil {
  157. // sanitize the output, since it may contain the remote address, which may
  158. // contain a password
  159. message, err := sanitizeOutput(stderr, repoPath)
  160. if err != nil {
  161. log.Error("sanitizeOutput: %v", err)
  162. return nil, false
  163. }
  164. desc := fmt.Sprintf("Failed to update mirror repository '%s': %s", repoPath, message)
  165. log.Error(desc)
  166. if err = models.CreateRepositoryNotice(desc); err != nil {
  167. log.Error("CreateRepositoryNotice: %v", err)
  168. }
  169. return nil, false
  170. }
  171. output := stderr
  172. gitRepo, err := git.OpenRepository(repoPath)
  173. if err != nil {
  174. log.Error("OpenRepository: %v", err)
  175. return nil, false
  176. }
  177. if err = models.SyncReleasesWithTags(m.Repo, gitRepo); err != nil {
  178. log.Error("Failed to synchronize tags to releases for repository: %v", err)
  179. }
  180. if err := m.Repo.UpdateSize(); err != nil {
  181. log.Error("Failed to update size for mirror repository: %v", err)
  182. }
  183. if m.Repo.HasWiki() {
  184. if _, stderr, err := process.GetManager().ExecDir(
  185. timeout, wikiPath, fmt.Sprintf("Mirror.runSync: %s", wikiPath),
  186. git.GitExecutable, "remote", "update", "--prune"); err != nil {
  187. // sanitize the output, since it may contain the remote address, which may
  188. // contain a password
  189. message, err := sanitizeOutput(stderr, wikiPath)
  190. if err != nil {
  191. log.Error("sanitizeOutput: %v", err)
  192. return nil, false
  193. }
  194. desc := fmt.Sprintf("Failed to update mirror wiki repository '%s': %s", wikiPath, message)
  195. log.Error(desc)
  196. if err = models.CreateRepositoryNotice(desc); err != nil {
  197. log.Error("CreateRepositoryNotice: %v", err)
  198. }
  199. return nil, false
  200. }
  201. }
  202. branches, err := m.Repo.GetBranches()
  203. if err != nil {
  204. log.Error("GetBranches: %v", err)
  205. return nil, false
  206. }
  207. for i := range branches {
  208. cache.Remove(m.Repo.GetCommitsCountCacheKey(branches[i].Name, true))
  209. }
  210. m.UpdatedUnix = timeutil.TimeStampNow()
  211. return parseRemoteUpdateOutput(output), true
  212. }
  213. // Address returns mirror address from Git repository config without credentials.
  214. func Address(m *models.Mirror) string {
  215. readAddress(m)
  216. return util.SanitizeURLCredentials(m.Address, false)
  217. }
  218. // Username returns the mirror address username
  219. func Username(m *models.Mirror) string {
  220. readAddress(m)
  221. u, err := url.Parse(m.Address)
  222. if err != nil {
  223. // this shouldn't happen but if it does return ""
  224. return ""
  225. }
  226. return u.User.Username()
  227. }
  228. // Password returns the mirror address password
  229. func Password(m *models.Mirror) string {
  230. readAddress(m)
  231. u, err := url.Parse(m.Address)
  232. if err != nil {
  233. // this shouldn't happen but if it does return ""
  234. return ""
  235. }
  236. password, _ := u.User.Password()
  237. return password
  238. }
  239. // Update checks and updates mirror repositories.
  240. func Update() {
  241. log.Trace("Doing: Update")
  242. if err := models.MirrorsIterate(func(idx int, bean interface{}) error {
  243. m := bean.(*models.Mirror)
  244. if m.Repo == nil {
  245. log.Error("Disconnected mirror repository found: %d", m.ID)
  246. return nil
  247. }
  248. mirrorQueue.Add(m.RepoID)
  249. return nil
  250. }); err != nil {
  251. log.Error("Update: %v", err)
  252. }
  253. }
  254. // SyncMirrors checks and syncs mirrors.
  255. // TODO: sync more mirrors at same time.
  256. func SyncMirrors() {
  257. // Start listening on new sync requests.
  258. for repoID := range mirrorQueue.Queue() {
  259. log.Trace("SyncMirrors [repo_id: %v]", repoID)
  260. mirrorQueue.Remove(repoID)
  261. m, err := models.GetMirrorByRepoID(com.StrTo(repoID).MustInt64())
  262. if err != nil {
  263. log.Error("GetMirrorByRepoID [%s]: %v", repoID, err)
  264. continue
  265. }
  266. results, ok := runSync(m)
  267. if !ok {
  268. continue
  269. }
  270. m.ScheduleNextUpdate()
  271. if err = models.UpdateMirror(m); err != nil {
  272. log.Error("UpdateMirror [%s]: %v", repoID, err)
  273. continue
  274. }
  275. var gitRepo *git.Repository
  276. if len(results) == 0 {
  277. log.Trace("SyncMirrors [repo_id: %d]: no commits fetched", m.RepoID)
  278. } else {
  279. gitRepo, err = git.OpenRepository(m.Repo.RepoPath())
  280. if err != nil {
  281. log.Error("OpenRepository [%d]: %v", m.RepoID, err)
  282. continue
  283. }
  284. }
  285. for _, result := range results {
  286. // Discard GitHub pull requests, i.e. refs/pull/*
  287. if strings.HasPrefix(result.refName, "refs/pull/") {
  288. continue
  289. }
  290. // Create reference
  291. if result.oldCommitID == gitShortEmptySha {
  292. if err = SyncCreateAction(m.Repo, result.refName); err != nil {
  293. log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err)
  294. }
  295. continue
  296. }
  297. // Delete reference
  298. if result.newCommitID == gitShortEmptySha {
  299. if err = SyncDeleteAction(m.Repo, result.refName); err != nil {
  300. log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err)
  301. }
  302. continue
  303. }
  304. // Push commits
  305. oldCommitID, err := git.GetFullCommitID(gitRepo.Path, result.oldCommitID)
  306. if err != nil {
  307. log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
  308. continue
  309. }
  310. newCommitID, err := git.GetFullCommitID(gitRepo.Path, result.newCommitID)
  311. if err != nil {
  312. log.Error("GetFullCommitID [%d]: %v", m.RepoID, err)
  313. continue
  314. }
  315. commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID)
  316. if err != nil {
  317. log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err)
  318. continue
  319. }
  320. if err = SyncPushAction(m.Repo, SyncPushActionOptions{
  321. RefName: result.refName,
  322. OldCommitID: oldCommitID,
  323. NewCommitID: newCommitID,
  324. Commits: models.ListToPushCommits(commits),
  325. }); err != nil {
  326. log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err)
  327. continue
  328. }
  329. }
  330. // Get latest commit date and update to current repository updated time
  331. commitDate, err := git.GetLatestCommitTime(m.Repo.RepoPath())
  332. if err != nil {
  333. log.Error("GetLatestCommitDate [%d]: %v", m.RepoID, err)
  334. continue
  335. }
  336. if err = models.UpdateRepositoryUpdatedTime(m.RepoID, commitDate); err != nil {
  337. log.Error("Update repository 'updated_unix' [%d]: %v", m.RepoID, err)
  338. continue
  339. }
  340. }
  341. }
  342. // InitSyncMirrors initializes a go routine to sync the mirrors
  343. func InitSyncMirrors() {
  344. go SyncMirrors()
  345. }
  346. // StartToMirror adds repoID to mirror queue
  347. func StartToMirror(repoID int64) {
  348. go mirrorQueue.Add(repoID)
  349. }
上海开阖软件有限公司 沪ICP备12045867号-1