sync.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. # Copyright (C) 2008 The Android Open Source Project
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from optparse import SUPPRESS_HELP
  16. import os
  17. import re
  18. import subprocess
  19. import sys
  20. from git_command import GIT
  21. from command import Command, MirrorSafeCommand
  22. from error import RepoChangedException, GitError
  23. from project import R_HEADS
  24. class Sync(Command, MirrorSafeCommand):
  25. common = True
  26. helpSummary = "Update working tree to the latest revision"
  27. helpUsage = """
  28. %prog [<project>...]
  29. """
  30. helpDescription = """
  31. The '%prog' command synchronizes local project directories
  32. with the remote repositories specified in the manifest. If a local
  33. project does not yet exist, it will clone a new local directory from
  34. the remote repository and set up tracking branches as specified in
  35. the manifest. If the local project already exists, '%prog'
  36. will update the remote branches and rebase any new local changes
  37. on top of the new remote changes.
  38. '%prog' will synchronize all projects listed at the command
  39. line. Projects can be specified either by name, or by a relative
  40. or absolute path to the project's local directory. If no projects
  41. are specified, '%prog' will synchronize all projects listed in
  42. the manifest.
  43. The -d/--detach option can be used to switch specified projects
  44. back to the manifest revision. This option is especially helpful
  45. if the project is currently on a topic branch, but the manifest
  46. revision is temporarily needed.
  47. """
  48. def _Options(self, p):
  49. p.add_option('-n','--network-only',
  50. dest='network_only', action='store_true',
  51. help="fetch only, don't update working tree")
  52. p.add_option('-d','--detach',
  53. dest='detach_head', action='store_true',
  54. help='detach projects back to manifest revision')
  55. p.add_option('--no-repo-verify',
  56. dest='no_repo_verify', action='store_true',
  57. help='do not verify repo source code')
  58. p.add_option('--repo-upgraded',
  59. dest='repo_upgraded', action='store_true',
  60. help=SUPPRESS_HELP)
  61. def _Fetch(self, *projects):
  62. fetched = set()
  63. for project in projects:
  64. if project.Sync_NetworkHalf():
  65. fetched.add(project.gitdir)
  66. else:
  67. print >>sys.stderr, 'error: Cannot fetch %s' % project.name
  68. sys.exit(1)
  69. return fetched
  70. def Execute(self, opt, args):
  71. if opt.network_only and opt.detach_head:
  72. print >>sys.stderr, 'error: cannot combine -n and -d'
  73. sys.exit(1)
  74. rp = self.manifest.repoProject
  75. rp.PreSync()
  76. mp = self.manifest.manifestProject
  77. mp.PreSync()
  78. if opt.repo_upgraded:
  79. for project in self.manifest.projects.values():
  80. if project.Exists:
  81. project.PostRepoUpgrade()
  82. all = self.GetProjects(args, missing_ok=True)
  83. fetched = self._Fetch(rp, mp, *all)
  84. if rp.HasChanges:
  85. print >>sys.stderr, 'info: A new version of repo is available'
  86. print >>sys.stderr, ''
  87. if opt.no_repo_verify or _VerifyTag(rp):
  88. if not rp.Sync_LocalHalf():
  89. sys.exit(1)
  90. print >>sys.stderr, 'info: Restarting repo with latest version'
  91. raise RepoChangedException(['--repo-upgraded'])
  92. else:
  93. print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
  94. if opt.network_only:
  95. # bail out now; the rest touches the working tree
  96. return
  97. if mp.HasChanges:
  98. if not mp.Sync_LocalHalf():
  99. sys.exit(1)
  100. self.manifest._Unload()
  101. all = self.GetProjects(args, missing_ok=True)
  102. missing = []
  103. for project in all:
  104. if project.gitdir not in fetched:
  105. missing.append(project)
  106. self._Fetch(*missing)
  107. for project in all:
  108. if project.worktree:
  109. if not project.Sync_LocalHalf(
  110. detach_head=opt.detach_head):
  111. sys.exit(1)
  112. def _VerifyTag(project):
  113. gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
  114. if not os.path.exists(gpg_dir):
  115. print >>sys.stderr,\
  116. """warning: GnuPG was not available during last "repo init"
  117. warning: Cannot automatically authenticate repo."""
  118. return True
  119. remote = project.GetRemote(project.remote.name)
  120. ref = remote.ToLocal(project.revision)
  121. try:
  122. cur = project.bare_git.describe(ref)
  123. except GitError:
  124. cur = None
  125. if not cur \
  126. or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
  127. rev = project.revision
  128. if rev.startswith(R_HEADS):
  129. rev = rev[len(R_HEADS):]
  130. print >>sys.stderr
  131. print >>sys.stderr,\
  132. "warning: project '%s' branch '%s' is not signed" \
  133. % (project.name, rev)
  134. return False
  135. env = dict(os.environ)
  136. env['GIT_DIR'] = project.gitdir
  137. env['GNUPGHOME'] = gpg_dir
  138. cmd = [GIT, 'tag', '-v', cur]
  139. proc = subprocess.Popen(cmd,
  140. stdout = subprocess.PIPE,
  141. stderr = subprocess.PIPE,
  142. env = env)
  143. out = proc.stdout.read()
  144. proc.stdout.close()
  145. err = proc.stderr.read()
  146. proc.stderr.close()
  147. if proc.wait() != 0:
  148. print >>sys.stderr
  149. print >>sys.stderr, out
  150. print >>sys.stderr, err
  151. print >>sys.stderr
  152. return False
  153. return True