start.py 4.3 KB

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