start.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. def ValidateOptions(self, opt, args):
  40. if not args:
  41. self.Usage()
  42. nb = args[0]
  43. if not git.check_ref_format('heads/%s' % nb):
  44. self.OptionParser.error("'%s' is not a valid name" % nb)
  45. def Execute(self, opt, args):
  46. nb = args[0]
  47. err = []
  48. projects = []
  49. if not opt.all:
  50. projects = args[1:]
  51. if len(projects) < 1:
  52. projects = ['.',] # start it in the local project by default
  53. all_projects = self.GetProjects(projects,
  54. missing_ok=bool(self.gitc_manifest))
  55. # This must happen after we find all_projects, since GetProjects may need
  56. # the local directory, which will disappear once we save the GITC manifest.
  57. if self.gitc_manifest:
  58. gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
  59. missing_ok=True)
  60. for project in gitc_projects:
  61. if project.old_revision:
  62. project.already_synced = True
  63. else:
  64. project.already_synced = False
  65. project.old_revision = project.revisionExpr
  66. project.revisionExpr = None
  67. # Save the GITC manifest.
  68. gitc_utils.save_manifest(self.gitc_manifest)
  69. # Make sure we have a valid CWD
  70. if not os.path.exists(os.getcwd()):
  71. os.chdir(self.manifest.topdir)
  72. pm = Progress('Starting %s' % nb, len(all_projects))
  73. for project in all_projects:
  74. pm.update()
  75. if self.gitc_manifest:
  76. gitc_project = self.gitc_manifest.paths[project.relpath]
  77. # Sync projects that have not been opened.
  78. if not gitc_project.already_synced:
  79. proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
  80. project.relpath)
  81. project.worktree = proj_localdir
  82. if not os.path.exists(proj_localdir):
  83. os.makedirs(proj_localdir)
  84. project.Sync_NetworkHalf()
  85. sync_buf = SyncBuffer(self.manifest.manifestProject.config)
  86. project.Sync_LocalHalf(sync_buf)
  87. project.revisionId = gitc_project.old_revision
  88. # If the current revision is immutable, such as a SHA1, a tag or
  89. # a change, then we can't push back to it. Substitute with
  90. # dest_branch, if defined; or with manifest default revision instead.
  91. branch_merge = ''
  92. if IsImmutable(project.revisionExpr):
  93. if project.dest_branch:
  94. branch_merge = project.dest_branch
  95. else:
  96. branch_merge = self.manifest.default.revisionExpr
  97. if not project.StartBranch(nb, branch_merge=branch_merge):
  98. err.append(project)
  99. pm.end()
  100. if err:
  101. for p in err:
  102. print("error: %s/: cannot start %s" % (p.relpath, nb),
  103. file=sys.stderr)
  104. sys.exit(1)