start.py 3.9 KB

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