start.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Copyright (C) 2008 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import sys
  16. from command import Command
  17. from git_config import IsImmutable
  18. from git_command import git
  19. import gitc_utils
  20. from progress import Progress
  21. from project import SyncBuffer
  22. class Start(Command):
  23. common = True
  24. helpSummary = "Start a new branch for development"
  25. helpUsage = """
  26. %prog <newbranchname> [--all | <project>...]
  27. """
  28. helpDescription = """
  29. '%prog' begins a new branch of development, starting from the
  30. revision specified in the manifest.
  31. """
  32. def _Options(self, p):
  33. p.add_option('--all',
  34. dest='all', action='store_true',
  35. help='begin branch in all projects')
  36. p.add_option('-r', '--rev', '--revision', dest='revision',
  37. help='point branch at this revision instead of upstream')
  38. p.add_option('--head', dest='revision', action='store_const', const='HEAD',
  39. help='abbreviation for --rev HEAD')
  40. def ValidateOptions(self, opt, args):
  41. if not args:
  42. self.Usage()
  43. nb = args[0]
  44. if not git.check_ref_format('heads/%s' % nb):
  45. self.OptionParser.error("'%s' is not a valid name" % nb)
  46. def Execute(self, opt, args):
  47. nb = args[0]
  48. err = []
  49. projects = []
  50. if not opt.all:
  51. projects = args[1:]
  52. if len(projects) < 1:
  53. projects = ['.'] # start it in the local project by default
  54. all_projects = self.GetProjects(projects,
  55. missing_ok=bool(self.gitc_manifest))
  56. # This must happen after we find all_projects, since GetProjects may need
  57. # the local directory, which will disappear once we save the GITC manifest.
  58. if self.gitc_manifest:
  59. gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
  60. missing_ok=True)
  61. for project in gitc_projects:
  62. if project.old_revision:
  63. project.already_synced = True
  64. else:
  65. project.already_synced = False
  66. project.old_revision = project.revisionExpr
  67. project.revisionExpr = None
  68. # Save the GITC manifest.
  69. gitc_utils.save_manifest(self.gitc_manifest)
  70. # Make sure we have a valid CWD
  71. if not os.path.exists(os.getcwd()):
  72. os.chdir(self.manifest.topdir)
  73. pm = Progress('Starting %s' % nb, len(all_projects))
  74. for project in all_projects:
  75. pm.update()
  76. if self.gitc_manifest:
  77. gitc_project = self.gitc_manifest.paths[project.relpath]
  78. # Sync projects that have not been opened.
  79. if not gitc_project.already_synced:
  80. proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
  81. project.relpath)
  82. project.worktree = proj_localdir
  83. if not os.path.exists(proj_localdir):
  84. os.makedirs(proj_localdir)
  85. project.Sync_NetworkHalf()
  86. sync_buf = SyncBuffer(self.manifest.manifestProject.config)
  87. project.Sync_LocalHalf(sync_buf)
  88. project.revisionId = gitc_project.old_revision
  89. # If the current revision is immutable, such as a SHA1, a tag or
  90. # a change, then we can't push back to it. Substitute with
  91. # dest_branch, if defined; or with manifest default revision instead.
  92. branch_merge = ''
  93. if IsImmutable(project.revisionExpr):
  94. if project.dest_branch:
  95. branch_merge = project.dest_branch
  96. else:
  97. branch_merge = self.manifest.default.revisionExpr
  98. if not project.StartBranch(
  99. nb, branch_merge=branch_merge, revision=opt.revision):
  100. err.append(project)
  101. pm.end()
  102. if err:
  103. for p in err:
  104. print("error: %s/: cannot start %s" % (p.relpath, nb),
  105. file=sys.stderr)
  106. sys.exit(1)