start.py 3.6 KB

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