2019-06-13 06:30:51 +00:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-10-23 18:58:52 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2012-11-02 05:59:27 +00:00
|
|
|
from __future__ import print_function
|
2008-10-23 18:58:52 +00:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from command import Command
|
Add error message for download -c conflicts
Currently if you run repo download -c on a change and the cherry-pick
runs into a merge conflict a Traceback is produced:
rob@rob-i5-lm ~/Programming/repo_test/repo1 $ repo download -c repo1 3/1
From ssh://rob-i5-lm:29418/repo1
* branch refs/changes/03/3/1 -> FETCH_HEAD
error: could not apply 0c8b474... 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Traceback (most recent call last):
File "/home/rob/Programming/git-repo/main.py", line 408, in <module>
_Main(sys.argv[1:])
File "/home/rob/Programming/git-repo/main.py", line 384, in _Main
result = repo._Run(argv) or 0
File "/home/rob/Programming/git-repo/main.py", line 143, in _Run
result = cmd.Execute(copts, cargs)
File "/home/rob/Programming/git-repo/subcmds/download.py", line 90, in Execute
project._CherryPick(dl.commit)
File "/home/rob/Programming/git-repo/project.py", line 1943, in _CherryPick
raise GitError('%s cherry-pick %s ' % (self.name, rev))
error.GitError: repo1 cherry-pick 0c8b4740f876f8f8372bbaed430f02b6ba8b1898
This amount of error message is confusing to users and has the side effect
of the git message telling you the actual issue being ignored.
This change introduces a message stating that the cherry-pick couldn't
be completed removing the Traceback.
To reproduce the issue create a change that causes a conflict with one currently
in review and use repo download -c to cherry-pick the conflicting change.
Change-Id: I8ddf4e0c8ad9bd04b1af5360313f67cc053f7d6a
2014-02-02 11:42:05 +00:00
|
|
|
from error import GitError
|
2008-10-23 18:58:52 +00:00
|
|
|
|
|
|
|
CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$')
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2008-10-23 18:58:52 +00:00
|
|
|
class Download(Command):
|
|
|
|
common = True
|
|
|
|
helpSummary = "Download and checkout a change"
|
|
|
|
helpUsage = """
|
2017-06-29 07:15:54 +00:00
|
|
|
%prog {[project] change[/patchset]}...
|
2008-10-23 18:58:52 +00:00
|
|
|
"""
|
|
|
|
helpDescription = """
|
|
|
|
The '%prog' command downloads a change from the review system and
|
|
|
|
makes it available in your project's local working directory.
|
2017-06-29 07:15:54 +00:00
|
|
|
If no project is specified try to use current directory as a project.
|
2008-10-23 18:58:52 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def _Options(self, p):
|
2020-03-22 17:54:55 +00:00
|
|
|
p.add_option('-b', '--branch',
|
|
|
|
help='create a new branch first')
|
2012-11-14 03:09:38 +00:00
|
|
|
p.add_option('-c', '--cherry-pick',
|
2011-03-24 15:28:18 +00:00
|
|
|
dest='cherrypick', action='store_true',
|
|
|
|
help="cherry-pick instead of checkout")
|
2020-03-22 16:15:20 +00:00
|
|
|
p.add_option('-x', '--record-origin', action='store_true',
|
|
|
|
help='pass -x when cherry-picking')
|
2012-11-14 03:09:38 +00:00
|
|
|
p.add_option('-r', '--revert',
|
2011-08-19 11:56:09 +00:00
|
|
|
dest='revert', action='store_true',
|
|
|
|
help="revert instead of checkout")
|
2012-11-14 03:09:38 +00:00
|
|
|
p.add_option('-f', '--ff-only',
|
2012-05-04 10:18:12 +00:00
|
|
|
dest='ffonly', action='store_true',
|
|
|
|
help="force fast-forward merge")
|
2008-10-23 18:58:52 +00:00
|
|
|
|
|
|
|
def _ParseChangeIds(self, args):
|
2009-09-09 04:41:34 +00:00
|
|
|
if not args:
|
|
|
|
self.Usage()
|
|
|
|
|
2008-10-23 18:58:52 +00:00
|
|
|
to_get = []
|
|
|
|
project = None
|
|
|
|
|
|
|
|
for a in args:
|
|
|
|
m = CHANGE_RE.match(a)
|
|
|
|
if m:
|
|
|
|
if not project:
|
2017-06-29 07:15:54 +00:00
|
|
|
project = self.GetProjects(".")[0]
|
2008-10-23 18:58:52 +00:00
|
|
|
chg_id = int(m.group(1))
|
|
|
|
if m.group(2):
|
|
|
|
ps_id = int(m.group(2))
|
|
|
|
else:
|
|
|
|
ps_id = 1
|
2018-03-24 06:57:05 +00:00
|
|
|
refs = 'refs/changes/%2.2d/%d/' % (chg_id % 100, chg_id)
|
|
|
|
output = project._LsRemote(refs + '*')
|
2018-03-15 16:26:30 +00:00
|
|
|
if output:
|
2018-03-24 06:57:05 +00:00
|
|
|
regex = refs + r'(\d+)'
|
2018-03-15 16:26:30 +00:00
|
|
|
rcomp = re.compile(regex, re.I)
|
|
|
|
for line in output.splitlines():
|
|
|
|
match = rcomp.search(line)
|
|
|
|
if match:
|
|
|
|
ps_id = max(int(match.group(1)), ps_id)
|
2008-10-23 18:58:52 +00:00
|
|
|
to_get.append((project, chg_id, ps_id))
|
|
|
|
else:
|
|
|
|
project = self.GetProjects([a])[0]
|
|
|
|
return to_get
|
|
|
|
|
2020-03-22 16:15:20 +00:00
|
|
|
def ValidateOptions(self, opt, args):
|
|
|
|
if opt.record_origin:
|
|
|
|
if not opt.cherrypick:
|
|
|
|
self.OptionParser.error('-x only makes sense with --cherry-pick')
|
|
|
|
|
|
|
|
if opt.ffonly:
|
|
|
|
self.OptionParser.error('-x and --ff are mutually exclusive options')
|
|
|
|
|
2008-10-23 18:58:52 +00:00
|
|
|
def Execute(self, opt, args):
|
|
|
|
for project, change_id, ps_id in self._ParseChangeIds(args):
|
|
|
|
dl = project.DownloadPatchSet(change_id, ps_id)
|
|
|
|
if not dl:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('[%s] change %d/%d not found'
|
|
|
|
% (project.name, change_id, ps_id),
|
|
|
|
file=sys.stderr)
|
2008-10-23 18:58:52 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2011-08-19 11:56:09 +00:00
|
|
|
if not opt.revert and not dl.commits:
|
2012-11-02 05:59:27 +00:00
|
|
|
print('[%s] change %d/%d has already been merged'
|
|
|
|
% (project.name, change_id, ps_id),
|
|
|
|
file=sys.stderr)
|
2008-10-23 18:58:52 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
if len(dl.commits) > 1:
|
2020-02-12 05:37:15 +00:00
|
|
|
print('[%s] %d/%d depends on %d unmerged changes:'
|
2012-11-02 05:59:27 +00:00
|
|
|
% (project.name, change_id, ps_id, len(dl.commits)),
|
|
|
|
file=sys.stderr)
|
2008-10-23 18:58:52 +00:00
|
|
|
for c in dl.commits:
|
2012-11-02 05:59:27 +00:00
|
|
|
print(' %s' % (c), file=sys.stderr)
|
Add error message for download -c conflicts
Currently if you run repo download -c on a change and the cherry-pick
runs into a merge conflict a Traceback is produced:
rob@rob-i5-lm ~/Programming/repo_test/repo1 $ repo download -c repo1 3/1
From ssh://rob-i5-lm:29418/repo1
* branch refs/changes/03/3/1 -> FETCH_HEAD
error: could not apply 0c8b474... 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Traceback (most recent call last):
File "/home/rob/Programming/git-repo/main.py", line 408, in <module>
_Main(sys.argv[1:])
File "/home/rob/Programming/git-repo/main.py", line 384, in _Main
result = repo._Run(argv) or 0
File "/home/rob/Programming/git-repo/main.py", line 143, in _Run
result = cmd.Execute(copts, cargs)
File "/home/rob/Programming/git-repo/subcmds/download.py", line 90, in Execute
project._CherryPick(dl.commit)
File "/home/rob/Programming/git-repo/project.py", line 1943, in _CherryPick
raise GitError('%s cherry-pick %s ' % (self.name, rev))
error.GitError: repo1 cherry-pick 0c8b4740f876f8f8372bbaed430f02b6ba8b1898
This amount of error message is confusing to users and has the side effect
of the git message telling you the actual issue being ignored.
This change introduces a message stating that the cherry-pick couldn't
be completed removing the Traceback.
To reproduce the issue create a change that causes a conflict with one currently
in review and use repo download -c to cherry-pick the conflicting change.
Change-Id: I8ddf4e0c8ad9bd04b1af5360313f67cc053f7d6a
2014-02-02 11:42:05 +00:00
|
|
|
|
2020-03-22 17:36:27 +00:00
|
|
|
if opt.cherrypick:
|
|
|
|
mode = 'cherry-pick'
|
2011-08-19 11:56:09 +00:00
|
|
|
elif opt.revert:
|
2020-03-22 17:36:27 +00:00
|
|
|
mode = 'revert'
|
2012-05-04 10:18:12 +00:00
|
|
|
elif opt.ffonly:
|
2020-03-22 17:36:27 +00:00
|
|
|
mode = 'fast-forward merge'
|
2011-03-24 15:28:18 +00:00
|
|
|
else:
|
2020-03-22 17:36:27 +00:00
|
|
|
mode = 'checkout'
|
|
|
|
|
2020-03-22 17:54:55 +00:00
|
|
|
# We'll combine the branch+checkout operation, but all the rest need a
|
|
|
|
# dedicated branch start.
|
|
|
|
if opt.branch and mode != 'checkout':
|
|
|
|
project.StartBranch(opt.branch)
|
|
|
|
|
2020-03-22 17:36:27 +00:00
|
|
|
try:
|
|
|
|
if opt.cherrypick:
|
|
|
|
project._CherryPick(dl.commit, ffonly=opt.ffonly,
|
|
|
|
record_origin=opt.record_origin)
|
|
|
|
elif opt.revert:
|
|
|
|
project._Revert(dl.commit)
|
|
|
|
elif opt.ffonly:
|
|
|
|
project._FastForward(dl.commit, ffonly=True)
|
|
|
|
else:
|
2020-03-22 17:54:55 +00:00
|
|
|
if opt.branch:
|
|
|
|
project.StartBranch(opt.branch, revision=dl.commit)
|
|
|
|
else:
|
|
|
|
project._Checkout(dl.commit)
|
2020-03-22 17:36:27 +00:00
|
|
|
|
|
|
|
except GitError:
|
|
|
|
print('[%s] Could not complete the %s of %s'
|
|
|
|
% (project.name, mode, dl.commit), file=sys.stderr)
|
|
|
|
sys.exit(1)
|