init.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 sys
  17. from color import Coloring
  18. from command import InteractiveCommand, MirrorSafeCommand
  19. from error import ManifestParseError
  20. from project import SyncBuffer
  21. from git_command import git_require, MIN_GIT_VERSION
  22. from manifest_xml import XmlManifest
  23. from subcmds.sync import _ReloadManifest
  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. Switching Manifest Branches
  38. ---------------------------
  39. To switch to another manifest branch, `repo init -b otherbranch`
  40. may be used in an existing client. However, as this only updates the
  41. manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
  42. to update the working directory files.
  43. """
  44. def _Options(self, p):
  45. # Logging
  46. g = p.add_option_group('Logging options')
  47. g.add_option('-q', '--quiet',
  48. dest="quiet", action="store_true", default=False,
  49. help="be quiet")
  50. # Manifest
  51. g = p.add_option_group('Manifest options')
  52. g.add_option('-u', '--manifest-url',
  53. dest='manifest_url',
  54. help='manifest repository location', metavar='URL')
  55. g.add_option('-b', '--manifest-branch',
  56. dest='manifest_branch',
  57. help='manifest branch or revision', metavar='REVISION')
  58. g.add_option('-o', '--origin',
  59. dest='manifest_origin',
  60. help="use REMOTE instead of 'origin' to track upstream",
  61. metavar='REMOTE')
  62. if isinstance(self.manifest, XmlManifest) \
  63. or not self.manifest.manifestProject.Exists:
  64. g.add_option('-m', '--manifest-name',
  65. dest='manifest_name', default='default.xml',
  66. help='initial manifest file', metavar='NAME.xml')
  67. g.add_option('--mirror',
  68. dest='mirror', action='store_true',
  69. help='mirror the forrest')
  70. # Tool
  71. g = p.add_option_group('repo Version options')
  72. g.add_option('--repo-url',
  73. dest='repo_url',
  74. help='repo repository location', metavar='URL')
  75. g.add_option('--repo-branch',
  76. dest='repo_branch',
  77. help='repo branch or revision', metavar='REVISION')
  78. g.add_option('--no-repo-verify',
  79. dest='no_repo_verify', action='store_true',
  80. help='do not verify repo source code')
  81. def _ApplyOptions(self, opt, is_new):
  82. m = self.manifest.manifestProject
  83. if is_new:
  84. if opt.manifest_origin:
  85. m.remote.name = opt.manifest_origin
  86. if opt.manifest_branch:
  87. m.revisionExpr = opt.manifest_branch
  88. else:
  89. m.revisionExpr = 'refs/heads/master'
  90. else:
  91. if opt.manifest_origin:
  92. print >>sys.stderr, 'fatal: cannot change origin name'
  93. sys.exit(1)
  94. if opt.manifest_branch:
  95. m.revisionExpr = opt.manifest_branch
  96. else:
  97. m.PreSync()
  98. def _SyncManifest(self, opt):
  99. m = self.manifest.manifestProject
  100. is_new = not m.Exists
  101. if is_new:
  102. if not opt.manifest_url:
  103. print >>sys.stderr, 'fatal: manifest url (-u) is required.'
  104. sys.exit(1)
  105. if not opt.quiet:
  106. print >>sys.stderr, 'Getting manifest ...'
  107. print >>sys.stderr, ' from %s' % opt.manifest_url
  108. m._InitGitDir()
  109. self._ApplyOptions(opt, is_new)
  110. if opt.manifest_url:
  111. r = m.GetRemote(m.remote.name)
  112. r.url = opt.manifest_url
  113. r.ResetFetch()
  114. r.Save()
  115. if opt.mirror:
  116. if is_new:
  117. m.config.SetString('repo.mirror', 'true')
  118. m.config.ClearCache()
  119. else:
  120. print >>sys.stderr, 'fatal: --mirror not supported on existing client'
  121. sys.exit(1)
  122. if not m.Sync_NetworkHalf():
  123. r = m.GetRemote(m.remote.name)
  124. print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
  125. sys.exit(1)
  126. if not is_new:
  127. # Force the manifest to load if it exists, the old graph
  128. # may be needed inside of _ReloadManifest().
  129. #
  130. self.manifest.projects
  131. syncbuf = SyncBuffer(m.config)
  132. m.Sync_LocalHalf(syncbuf)
  133. syncbuf.Finish()
  134. _ReloadManifest(self)
  135. self._ApplyOptions(opt, is_new)
  136. if not self.manifest.InitBranch():
  137. print >>sys.stderr, 'fatal: cannot create branch in manifest'
  138. sys.exit(1)
  139. def _LinkManifest(self, name):
  140. if not name:
  141. print >>sys.stderr, 'fatal: manifest name (-m) is required.'
  142. sys.exit(1)
  143. try:
  144. self.manifest.Link(name)
  145. except ManifestParseError, e:
  146. print >>sys.stderr, "fatal: manifest '%s' not available" % name
  147. print >>sys.stderr, 'fatal: %s' % str(e)
  148. sys.exit(1)
  149. def _Prompt(self, prompt, value):
  150. mp = self.manifest.manifestProject
  151. sys.stdout.write('%-10s [%s]: ' % (prompt, value))
  152. a = sys.stdin.readline().strip()
  153. if a == '':
  154. return value
  155. return a
  156. def _ConfigureUser(self):
  157. mp = self.manifest.manifestProject
  158. while True:
  159. print ''
  160. name = self._Prompt('Your Name', mp.UserName)
  161. email = self._Prompt('Your Email', mp.UserEmail)
  162. print ''
  163. print 'Your identity is: %s <%s>' % (name, email)
  164. sys.stdout.write('is this correct [yes/no]? ')
  165. if 'yes' == sys.stdin.readline().strip():
  166. break
  167. if name != mp.UserName:
  168. mp.config.SetString('user.name', name)
  169. if email != mp.UserEmail:
  170. mp.config.SetString('user.email', email)
  171. def _HasColorSet(self, gc):
  172. for n in ['ui', 'diff', 'status']:
  173. if gc.Has('color.%s' % n):
  174. return True
  175. return False
  176. def _ConfigureColor(self):
  177. gc = self.manifest.globalConfig
  178. if self._HasColorSet(gc):
  179. return
  180. class _Test(Coloring):
  181. def __init__(self):
  182. Coloring.__init__(self, gc, 'test color display')
  183. self._on = True
  184. out = _Test()
  185. print ''
  186. print "Testing colorized output (for 'repo diff', 'repo status'):"
  187. for c in ['black','red','green','yellow','blue','magenta','cyan']:
  188. out.write(' ')
  189. out.printer(fg=c)(' %-6s ', c)
  190. out.write(' ')
  191. out.printer(fg='white', bg='black')(' %s ' % 'white')
  192. out.nl()
  193. for c in ['bold','dim','ul','reverse']:
  194. out.write(' ')
  195. out.printer(fg='black', attr=c)(' %-6s ', c)
  196. out.nl()
  197. sys.stdout.write('Enable color display in this user account (y/n)? ')
  198. a = sys.stdin.readline().strip().lower()
  199. if a in ('y', 'yes', 't', 'true', 'on'):
  200. gc.SetString('color.ui', 'auto')
  201. def Execute(self, opt, args):
  202. git_require(MIN_GIT_VERSION, fail=True)
  203. self._SyncManifest(opt)
  204. if isinstance(self.manifest, XmlManifest):
  205. self._LinkManifest(opt.manifest_name)
  206. if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
  207. self._ConfigureUser()
  208. self._ConfigureColor()
  209. if self.manifest.IsMirror:
  210. type = 'mirror '
  211. else:
  212. type = ''
  213. print ''
  214. print 'repo %sinitialized in %s' % (type, self.manifest.topdir)