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