blob: 04589fb270deeb421abcc3831b4420df6f99aabc [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Mike Frysinger8dbc07a2021-02-18 23:37:33 -050015import functools
16import multiprocessing
Simran Basib9a1b732015-08-20 12:19:28 -070017import os
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import sys
Simran Basib9a1b732015-08-20 12:19:28 -070019
Mike Frysinger8dbc07a2021-02-18 23:37:33 -050020from command import Command, DEFAULT_LOCAL_JOBS, WORKER_BATCH_SIZE
Zac Livingston9ead97b2017-06-13 08:29:04 -060021from git_config import IsImmutable
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from git_command import git
Simran Basib9a1b732015-08-20 12:19:28 -070023import gitc_utils
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -070024from progress import Progress
Simran Basib9a1b732015-08-20 12:19:28 -070025from project import SyncBuffer
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070026
David Pursehouse819827a2020-02-12 15:20:19 +090027
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070028class Start(Command):
29 common = True
30 helpSummary = "Start a new branch for development"
31 helpUsage = """
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070032%prog <newbranchname> [--all | <project>...]
Shawn O. Pearce06e556d2009-04-18 11:19:01 -070033"""
34 helpDescription = """
35'%prog' begins a new branch of development, starting from the
36revision specified in the manifest.
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070037"""
Mike Frysinger8dbc07a2021-02-18 23:37:33 -050038 PARALLEL_JOBS = DEFAULT_LOCAL_JOBS
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070039
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070040 def _Options(self, p):
Mike Frysinger8dbc07a2021-02-18 23:37:33 -050041 super()._Options(p)
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070042 p.add_option('--all',
43 dest='all', action='store_true',
44 help='begin branch in all projects')
Theodore Dubois60fdc5c2019-07-30 12:14:25 -070045 p.add_option('-r', '--rev', '--revision', dest='revision',
46 help='point branch at this revision instead of upstream')
Mike Frysingerd1e4fa72021-03-23 20:27:29 -040047 p.add_option('--head', '--HEAD',
48 dest='revision', action='store_const', const='HEAD',
Theodore Dubois60fdc5c2019-07-30 12:14:25 -070049 help='abbreviation for --rev HEAD')
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070050
Mike Frysingerae6cb082019-08-27 01:10:59 -040051 def ValidateOptions(self, opt, args):
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070052 if not args:
53 self.Usage()
54
55 nb = args[0]
56 if not git.check_ref_format('heads/%s' % nb):
Mike Frysingerae6cb082019-08-27 01:10:59 -040057 self.OptionParser.error("'%s' is not a valid name" % nb)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070058
Mike Frysinger8dbc07a2021-02-18 23:37:33 -050059 def _ExecuteOne(self, opt, nb, project):
60 """Start one project."""
61 # If the current revision is immutable, such as a SHA1, a tag or
62 # a change, then we can't push back to it. Substitute with
63 # dest_branch, if defined; or with manifest default revision instead.
64 branch_merge = ''
65 if IsImmutable(project.revisionExpr):
66 if project.dest_branch:
67 branch_merge = project.dest_branch
68 else:
69 branch_merge = self.manifest.default.revisionExpr
70
71 try:
72 ret = project.StartBranch(
73 nb, branch_merge=branch_merge, revision=opt.revision)
74 except Exception as e:
75 print('error: unable to checkout %s: %s' % (project.name, e), file=sys.stderr)
76 ret = False
77 return (ret, project)
78
Mike Frysingerae6cb082019-08-27 01:10:59 -040079 def Execute(self, opt, args):
80 nb = args[0]
Shawn O. Pearce0a389e92009-04-10 16:21:18 -070081 err = []
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070082 projects = []
83 if not opt.all:
84 projects = args[1:]
85 if len(projects) < 1:
David Pursehouse54a4e602020-02-12 14:31:05 +090086 projects = ['.'] # start it in the local project by default
Ficus Kirkpatrick6f6cd772009-04-22 17:27:12 -070087
Dan Willemsen04197a52015-10-07 16:53:10 -070088 all_projects = self.GetProjects(projects,
89 missing_ok=bool(self.gitc_manifest))
90
91 # This must happen after we find all_projects, since GetProjects may need
92 # the local directory, which will disappear once we save the GITC manifest.
Simran Basib9a1b732015-08-20 12:19:28 -070093 if self.gitc_manifest:
Dan Willemsen04197a52015-10-07 16:53:10 -070094 gitc_projects = self.GetProjects(projects, manifest=self.gitc_manifest,
95 missing_ok=True)
96 for project in gitc_projects:
Simran Basib9a1b732015-08-20 12:19:28 -070097 if project.old_revision:
98 project.already_synced = True
99 else:
100 project.already_synced = False
101 project.old_revision = project.revisionExpr
Simran Basib9a1b732015-08-20 12:19:28 -0700102 project.revisionExpr = None
103 # Save the GITC manifest.
104 gitc_utils.save_manifest(self.gitc_manifest)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700105
Dan Willemsen04197a52015-10-07 16:53:10 -0700106 # Make sure we have a valid CWD
107 if not os.path.exists(os.getcwd()):
108 os.chdir(self.manifest.topdir)
109
Mike Frysinger8dbc07a2021-02-18 23:37:33 -0500110 pm = Progress('Syncing %s' % nb, len(all_projects))
111 for project in all_projects:
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700112 gitc_project = self.gitc_manifest.paths[project.relpath]
113 # Sync projects that have not been opened.
Simran Basib9a1b732015-08-20 12:19:28 -0700114 if not gitc_project.already_synced:
115 proj_localdir = os.path.join(self.gitc_manifest.gitc_client_dir,
116 project.relpath)
117 project.worktree = proj_localdir
118 if not os.path.exists(proj_localdir):
119 os.makedirs(proj_localdir)
120 project.Sync_NetworkHalf()
121 sync_buf = SyncBuffer(self.manifest.manifestProject.config)
122 project.Sync_LocalHalf(sync_buf)
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700123 project.revisionId = gitc_project.old_revision
Mike Frysinger8dbc07a2021-02-18 23:37:33 -0500124 pm.update()
125 pm.end()
Simran Basib9a1b732015-08-20 12:19:28 -0700126
Mike Frysinger8dbc07a2021-02-18 23:37:33 -0500127 def _ProcessResults(results):
128 for (result, project) in results:
129 if not result:
130 err.append(project)
131 pm.update()
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700132
Mike Frysinger8dbc07a2021-02-18 23:37:33 -0500133 pm = Progress('Starting %s' % nb, len(all_projects))
134 # NB: Multiprocessing is heavy, so don't spin it up for one job.
135 if len(all_projects) == 1 or opt.jobs == 1:
136 _ProcessResults(self._ExecuteOne(opt, nb, x) for x in all_projects)
137 else:
138 with multiprocessing.Pool(opt.jobs) as pool:
139 results = pool.imap_unordered(
140 functools.partial(self._ExecuteOne, opt, nb), all_projects,
141 chunksize=WORKER_BATCH_SIZE)
142 _ProcessResults(results)
Shawn O. Pearce0f0dfa32009-04-18 14:53:39 -0700143 pm.end()
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700144
145 if err:
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700146 for p in err:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700147 print("error: %s/: cannot start %s" % (p.relpath, nb),
148 file=sys.stderr)
Shawn O. Pearce0a389e92009-04-10 16:21:18 -0700149 sys.exit(1)