start.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from __future__ import print_function
  16. import os
  17. import sys
  18. from command import Command
  19. from git_config import IsImmutable
  20. from git_command import git
  21. import gitc_utils
  22. from progress import Progress
  23. from project import SyncBuffer
  24. class Start(Command):
  25. common = True
  26. helpSummary = "Start a new branch for development"
  27. helpUsage = """
  28. %prog <newbranchname> [--all | <project>...]
  29. """
  30. helpDescription = """
  31. '%prog' begins a new branch of development, starting from the
  32. revision specified in the manifest.
  33. """
  34. def _Options(self, p):
  35. p.add_option('--all',
  36. dest='all', action='store_true',
  37. help='begin branch in all projects')
  38. def Execute(self, opt, args):
  39. if not args:
  40. self.Usage()
  41. nb = args[0]
  42. if not git.check_ref_format('heads/%s' % nb):
  43. print("error: '%s' is not a valid name" % nb, file=sys.stderr)
  44. sys.exit(1)
  45. err = []
  46. projects = []
  47. if not opt.all:
  48. projects = args[1:]
  49. if len(projects) < 1:
  50. projects = ['.',] # start it in the local project by default
  51. all_projects = self.GetProjects(projects,
  52. missing_ok=bool(self.gitc_manifest))
  53. # This must happen after we find all_projects, since GetProjects may need
  54. # the local directory, which will disappear once we save the GITC manifest.
  55. if self.gitc_manifest:
  56. gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
  57. missing_ok=True)
  58. for project in gitc_projects:
  59. if project.old_revision:
  60. project.already_synced = True
  61. else:
  62. project.already_synced = False
  63. project.old_revision = project.revisionExpr
  64. project.revisionExpr = None
  65. # Save the GITC manifest.
  66. gitc_utils.save_manifest(self.gitc_manifest)
  67. # Make sure we have a valid CWD
  68. if not os.path.exists(os.getcwd()):
  69. os.chdir(self.manifest.topdir)
  70. pm = Progress('Starting %s' % nb, len(all_projects))
  71. for project in all_projects:
  72. pm.update()
  73. if self.gitc_manifest:
  74. gitc_project = self.gitc_manifest.paths[project.relpath]
  75. # Sync projects that have not been opened.
  76. if not gitc_project.already_synced:
  77. proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
  78. project.relpath)
  79. project.worktree = proj_localdir
  80. if not os.path.exists(proj_localdir):
  81. os.makedirs(proj_localdir)
  82. project.Sync_NetworkHalf()
  83. sync_buf = SyncBuffer(self.manifest.manifestProject.config)
  84. project.Sync_LocalHalf(sync_buf)
  85. project.revisionId = gitc_project.old_revision
  86. # If the current revision is immutable, such as a SHA1, a tag or
  87. # a change, then we can't push back to it. Substitute with
  88. # dest_branch, if defined; or with manifest default revision instead.
  89. branch_merge = ''
  90. if IsImmutable(project.revisionExpr):
  91. if project.dest_branch:
  92. branch_merge = project.dest_branch
  93. else:
  94. branch_merge = self.manifest.default.revisionExpr
  95. if not project.StartBranch(nb, branch_merge=branch_merge):
  96. err.append(project)
  97. pm.end()
  98. if err:
  99. for p in err:
  100. print("error: %s/: cannot start %s" % (p.relpath, nb),
  101. file=sys.stderr)
  102. sys.exit(1)