init.py 8.6 KB

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