init.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. import os
  16. import shutil
  17. import sys
  18. from color import Coloring
  19. from command import InteractiveCommand, MirrorSafeCommand
  20. from error import ManifestParseError
  21. from project import SyncBuffer
  22. from git_config import GitConfig
  23. from git_command import git_require, MIN_GIT_VERSION
  24. class Init(InteractiveCommand, MirrorSafeCommand):
  25. common = True
  26. helpSummary = "Initialize repo in the current directory"
  27. helpUsage = """
  28. %prog [options]
  29. """
  30. helpDescription = """
  31. The '%prog' command is run once to install and initialize repo.
  32. The latest repo source code and manifest collection is downloaded
  33. from the server and is installed in the .repo/ directory in the
  34. current working directory.
  35. The optional -b argument can be used to select the manifest branch
  36. to checkout and use. If no branch is specified, master is assumed.
  37. The optional -m argument can be used to specify an alternate manifest
  38. to be used. If no manifest is specified, the manifest default.xml
  39. will be used.
  40. The --reference option can be used to point to a directory that
  41. has the content of a --mirror sync. This will make the working
  42. directory use as much data as possible from the local reference
  43. directory when fetching from the server. This will make the sync
  44. go a lot faster by reducing data traffic on the network.
  45. Switching Manifest Branches
  46. ---------------------------
  47. To switch to another manifest branch, `repo init -b otherbranch`
  48. may be used in an existing client. However, as this only updates the
  49. manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
  50. to update the working directory files.
  51. """
  52. def _Options(self, p):
  53. # Logging
  54. g = p.add_option_group('Logging options')
  55. g.add_option('-q', '--quiet',
  56. dest="quiet", action="store_true", default=False,
  57. help="be quiet")
  58. # Manifest
  59. g = p.add_option_group('Manifest options')
  60. g.add_option('-u', '--manifest-url',
  61. dest='manifest_url',
  62. help='manifest repository location', metavar='URL')
  63. g.add_option('-b', '--manifest-branch',
  64. dest='manifest_branch',
  65. help='manifest branch or revision', metavar='REVISION')
  66. g.add_option('-m', '--manifest-name',
  67. dest='manifest_name', default='default.xml',
  68. help='initial manifest file', metavar='NAME.xml')
  69. g.add_option('--mirror',
  70. dest='mirror', action='store_true',
  71. help='mirror the forrest')
  72. g.add_option('--reference',
  73. dest='reference',
  74. help='location of mirror directory', metavar='DIR')
  75. g.add_option('--depth', type='int', default=None,
  76. dest='depth',
  77. help='create a shallow clone with given depth; see git clone')
  78. # Tool
  79. g = p.add_option_group('repo Version options')
  80. g.add_option('--repo-url',
  81. dest='repo_url',
  82. help='repo repository location', metavar='URL')
  83. g.add_option('--repo-branch',
  84. dest='repo_branch',
  85. help='repo branch or revision', metavar='REVISION')
  86. g.add_option('--no-repo-verify',
  87. dest='no_repo_verify', action='store_true',
  88. help='do not verify repo source code')
  89. def _SyncManifest(self, opt):
  90. m = self.manifest.manifestProject
  91. is_new = not m.Exists
  92. if is_new:
  93. if not opt.manifest_url:
  94. print >>sys.stderr, 'fatal: manifest url (-u) is required.'
  95. sys.exit(1)
  96. if not opt.quiet:
  97. print >>sys.stderr, 'Get %s' \
  98. % GitConfig.ForUser().UrlInsteadOf(opt.manifest_url)
  99. m._InitGitDir()
  100. if opt.manifest_branch:
  101. m.revisionExpr = opt.manifest_branch
  102. else:
  103. m.revisionExpr = 'refs/heads/master'
  104. else:
  105. if opt.manifest_branch:
  106. m.revisionExpr = opt.manifest_branch
  107. else:
  108. m.PreSync()
  109. if opt.manifest_url:
  110. r = m.GetRemote(m.remote.name)
  111. r.url = opt.manifest_url
  112. r.ResetFetch()
  113. r.Save()
  114. if opt.reference:
  115. m.config.SetString('repo.reference', opt.reference)
  116. if opt.mirror:
  117. if is_new:
  118. m.config.SetString('repo.mirror', 'true')
  119. else:
  120. print >>sys.stderr, 'fatal: --mirror not supported on existing client'
  121. sys.exit(1)
  122. if not m.Sync_NetworkHalf(is_new=is_new):
  123. r = m.GetRemote(m.remote.name)
  124. print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
  125. # Better delete the manifest git dir if we created it; otherwise next
  126. # time (when user fixes problems) we won't go through the "is_new" logic.
  127. if is_new:
  128. shutil.rmtree(m.gitdir)
  129. sys.exit(1)
  130. syncbuf = SyncBuffer(m.config)
  131. m.Sync_LocalHalf(syncbuf)
  132. syncbuf.Finish()
  133. if is_new or m.CurrentBranch is None:
  134. if not m.StartBranch('default'):
  135. print >>sys.stderr, 'fatal: cannot create default in manifest'
  136. sys.exit(1)
  137. def _LinkManifest(self, name):
  138. if not name:
  139. print >>sys.stderr, 'fatal: manifest name (-m) is required.'
  140. sys.exit(1)
  141. try:
  142. self.manifest.Link(name)
  143. except ManifestParseError, e:
  144. print >>sys.stderr, "fatal: manifest '%s' not available" % name
  145. print >>sys.stderr, 'fatal: %s' % str(e)
  146. sys.exit(1)
  147. def _Prompt(self, prompt, value):
  148. mp = self.manifest.manifestProject
  149. sys.stdout.write('%-10s [%s]: ' % (prompt, value))
  150. a = sys.stdin.readline().strip()
  151. if a == '':
  152. return value
  153. return a
  154. def _ConfigureUser(self):
  155. mp = self.manifest.manifestProject
  156. while True:
  157. print ''
  158. name = self._Prompt('Your Name', mp.UserName)
  159. email = self._Prompt('Your Email', mp.UserEmail)
  160. print ''
  161. print 'Your identity is: %s <%s>' % (name, email)
  162. sys.stdout.write('is this correct [y/n]? ')
  163. a = sys.stdin.readline().strip()
  164. if a in ('yes', 'y', 't', 'true'):
  165. break
  166. if name != mp.UserName:
  167. mp.config.SetString('user.name', name)
  168. if email != mp.UserEmail:
  169. mp.config.SetString('user.email', email)
  170. def _HasColorSet(self, gc):
  171. for n in ['ui', 'diff', 'status']:
  172. if gc.Has('color.%s' % n):
  173. return True
  174. return False
  175. def _ConfigureColor(self):
  176. gc = self.manifest.globalConfig
  177. if self._HasColorSet(gc):
  178. return
  179. class _Test(Coloring):
  180. def __init__(self):
  181. Coloring.__init__(self, gc, 'test color display')
  182. self._on = True
  183. out = _Test()
  184. print ''
  185. print "Testing colorized output (for 'repo diff', 'repo status'):"
  186. for c in ['black','red','green','yellow','blue','magenta','cyan']:
  187. out.write(' ')
  188. out.printer(fg=c)(' %-6s ', c)
  189. out.write(' ')
  190. out.printer(fg='white', bg='black')(' %s ' % 'white')
  191. out.nl()
  192. for c in ['bold','dim','ul','reverse']:
  193. out.write(' ')
  194. out.printer(fg='black', attr=c)(' %-6s ', c)
  195. out.nl()
  196. sys.stdout.write('Enable color display in this user account (y/n)? ')
  197. a = sys.stdin.readline().strip().lower()
  198. if a in ('y', 'yes', 't', 'true', 'on'):
  199. gc.SetString('color.ui', 'auto')
  200. def _ConfigureDepth(self, opt):
  201. """Configure the depth we'll sync down.
  202. Args:
  203. opt: Options from optparse. We care about opt.depth.
  204. """
  205. # Opt.depth will be non-None if user actually passed --depth to repo init.
  206. if opt.depth is not None:
  207. if opt.depth > 0:
  208. # Positive values will set the depth.
  209. depth = str(opt.depth)
  210. else:
  211. # Negative numbers will clear the depth; passing None to SetString
  212. # will do that.
  213. depth = None
  214. # We store the depth in the main manifest project.
  215. self.manifest.manifestProject.config.SetString('repo.depth', depth)
  216. def Execute(self, opt, args):
  217. git_require(MIN_GIT_VERSION, fail=True)
  218. self._SyncManifest(opt)
  219. self._LinkManifest(opt.manifest_name)
  220. if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
  221. self._ConfigureUser()
  222. self._ConfigureColor()
  223. self._ConfigureDepth(opt)
  224. if self.manifest.IsMirror:
  225. type = 'mirror '
  226. else:
  227. type = ''
  228. print ''
  229. print 'repo %sinitialized in %s' % (type, self.manifest.topdir)