sync.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. from progress import Progress
  25. class Sync(Command, MirrorSafeCommand):
  26. common = True
  27. helpSummary = "Update working tree to the latest revision"
  28. helpUsage = """
  29. %prog [<project>...]
  30. """
  31. helpDescription = """
  32. The '%prog' command synchronizes local project directories
  33. with the remote repositories specified in the manifest. If a local
  34. project does not yet exist, it will clone a new local directory from
  35. the remote repository and set up tracking branches as specified in
  36. the manifest. If the local project already exists, '%prog'
  37. will update the remote branches and rebase any new local changes
  38. on top of the new remote changes.
  39. '%prog' will synchronize all projects listed at the command
  40. line. Projects can be specified either by name, or by a relative
  41. or absolute path to the project's local directory. If no projects
  42. are specified, '%prog' will synchronize all projects listed in
  43. the manifest.
  44. The -d/--detach option can be used to switch specified projects
  45. back to the manifest revision. This option is especially helpful
  46. if the project is currently on a topic branch, but the manifest
  47. revision is temporarily needed.
  48. """
  49. def _Options(self, p):
  50. p.add_option('-l','--local-only',
  51. dest='local_only', action='store_true',
  52. help="only update working tree, don't fetch")
  53. p.add_option('-n','--network-only',
  54. dest='network_only', action='store_true',
  55. help="fetch only, don't update working tree")
  56. p.add_option('-d','--detach',
  57. dest='detach_head', action='store_true',
  58. help='detach projects back to manifest revision')
  59. p.add_option('--no-repo-verify',
  60. dest='no_repo_verify', action='store_true',
  61. help='do not verify repo source code')
  62. p.add_option('--repo-upgraded',
  63. dest='repo_upgraded', action='store_true',
  64. help=SUPPRESS_HELP)
  65. def _Fetch(self, *projects):
  66. fetched = set()
  67. pm = Progress('Fetching projects', len(projects))
  68. for project in projects:
  69. pm.update()
  70. if project.Sync_NetworkHalf():
  71. fetched.add(project.gitdir)
  72. else:
  73. print >>sys.stderr, 'error: Cannot fetch %s' % project.name
  74. sys.exit(1)
  75. pm.end()
  76. return fetched
  77. def Execute(self, opt, args):
  78. if opt.network_only and opt.detach_head:
  79. print >>sys.stderr, 'error: cannot combine -n and -d'
  80. sys.exit(1)
  81. if opt.network_only and opt.local_only:
  82. print >>sys.stderr, 'error: cannot combine -n and -l'
  83. sys.exit(1)
  84. rp = self.manifest.repoProject
  85. rp.PreSync()
  86. mp = self.manifest.manifestProject
  87. mp.PreSync()
  88. if opt.repo_upgraded:
  89. for project in self.manifest.projects.values():
  90. if project.Exists:
  91. project.PostRepoUpgrade()
  92. all = self.GetProjects(args, missing_ok=True)
  93. if not opt.local_only:
  94. fetched = self._Fetch(rp, mp, *all)
  95. if rp.HasChanges:
  96. print >>sys.stderr, 'info: A new version of repo is available'
  97. print >>sys.stderr, ''
  98. if opt.no_repo_verify or _VerifyTag(rp):
  99. if not rp.Sync_LocalHalf():
  100. sys.exit(1)
  101. print >>sys.stderr, 'info: Restarting repo with latest version'
  102. raise RepoChangedException(['--repo-upgraded'])
  103. else:
  104. print >>sys.stderr, 'warning: Skipped upgrade to unverified version'
  105. if opt.network_only:
  106. # bail out now; the rest touches the working tree
  107. return
  108. if mp.HasChanges:
  109. if not mp.Sync_LocalHalf():
  110. sys.exit(1)
  111. self.manifest._Unload()
  112. all = self.GetProjects(args, missing_ok=True)
  113. missing = []
  114. for project in all:
  115. if project.gitdir not in fetched:
  116. missing.append(project)
  117. self._Fetch(*missing)
  118. pm = Progress('Syncing work tree', len(all))
  119. for project in all:
  120. pm.update()
  121. if project.worktree:
  122. if not project.Sync_LocalHalf(
  123. detach_head=opt.detach_head):
  124. sys.exit(1)
  125. pm.end()
  126. def _VerifyTag(project):
  127. gpg_dir = os.path.expanduser('~/.repoconfig/gnupg')
  128. if not os.path.exists(gpg_dir):
  129. print >>sys.stderr,\
  130. """warning: GnuPG was not available during last "repo init"
  131. warning: Cannot automatically authenticate repo."""
  132. return True
  133. remote = project.GetRemote(project.remote.name)
  134. ref = remote.ToLocal(project.revision)
  135. try:
  136. cur = project.bare_git.describe(ref)
  137. except GitError:
  138. cur = None
  139. if not cur \
  140. or re.compile(r'^.*-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur):
  141. rev = project.revision
  142. if rev.startswith(R_HEADS):
  143. rev = rev[len(R_HEADS):]
  144. print >>sys.stderr
  145. print >>sys.stderr,\
  146. "warning: project '%s' branch '%s' is not signed" \
  147. % (project.name, rev)
  148. return False
  149. env = dict(os.environ)
  150. env['GIT_DIR'] = project.gitdir
  151. env['GNUPGHOME'] = gpg_dir
  152. cmd = [GIT, 'tag', '-v', cur]
  153. proc = subprocess.Popen(cmd,
  154. stdout = subprocess.PIPE,
  155. stderr = subprocess.PIPE,
  156. env = env)
  157. out = proc.stdout.read()
  158. proc.stdout.close()
  159. err = proc.stderr.read()
  160. proc.stderr.close()
  161. if proc.wait() != 0:
  162. print >>sys.stderr
  163. print >>sys.stderr, out
  164. print >>sys.stderr, err
  165. print >>sys.stderr
  166. return False
  167. return True