diff --git a/llvm/utils/git-svn/git-llvm b/llvm/utils/git-svn/git-llvm --- a/llvm/utils/git-svn/git-llvm +++ b/llvm/utils/git-svn/git-llvm @@ -40,6 +40,13 @@ def iteritems(d): return d.iteritems() +try: + # Python 3 + from shlex import quote +except ImportError: + # Python 2 + from pipes import quote + # It's *almost* a straightforward mapping from the monorepo to svn... GIT_TO_SVN_DIR = { d: (d + '/trunk') @@ -110,7 +117,9 @@ def shell(cmd, strip=True, cwd=None, stdin=None, die_on_failure=True, ignore_errors=False, text=True): - log_verbose('Running in %s: %s' % (cwd, ' '.join(cmd))) + # Escape args when logging for easy repro. + quoted_cmd = [quote(arg) for arg in cmd] + log_verbose('Running in %s: %s' % (cwd, ' '.join(quoted_cmd))) err_pipe = subprocess.PIPE if ignore_errors: @@ -128,7 +137,7 @@ if p.returncode == 0 or ignore_errors: if stderr and not ignore_errors: - eprint('`%s` printed to stderr:' % ' '.join(cmd)) + eprint('`%s` printed to stderr:' % ' '.join(quoted_cmd)) eprint(stderr.rstrip()) if strip: if text: @@ -139,7 +148,7 @@ for l in stdout.splitlines(): log_verbose("STDOUT: %s" % l) return stdout - err_msg = '`%s` returned %s' % (' '.join(cmd), p.returncode) + err_msg = '`%s` returned %s' % (' '.join(quoted_cmd), p.returncode) eprint(err_msg) if stderr: eprint(stderr.rstrip()) @@ -398,6 +407,59 @@ svn_push_one_rev(svn_root, r, dry_run) +def cmd_revert(args): + '''Revert a commit by either SVN id (rNNNNNN) or git hash.''' + # Get the git root + git_root = git('rev-parse', '--show-toplevel') + if not os.path.isdir(git_root): + die("Can't find git root dir") + + # Run commands from the root + os.chdir(git_root) + + # Check for a client branch first. + open_files = git('status', '-uno', '-s', '--porcelain') + if len(open_files) > 0: + die("Found open files. Please stash and then revert.\n" + open_files) + + # Search by svn id if it looks like rNNNNNN or NNNNNN, otherwise search by + # git commit. + svn_match = re.match('^r?(\d{5,7})$', args.revision) + if svn_match: + svn_rev = svn_match.group(1) + log_verbose('Searching by svn id for revision r' + svn_rev) + oneline = git('log', '--all', '-1', '--grep', 'llvm-svn: ' + svn_rev, + '--oneline') + if len(oneline) == 0: + die("Can't find svn revision r" + svn_rev) + else: + log_verbose('Searching by git hash for commit ' + args.revision) + # Ignore errors so we can print a friendlier message. Also, use a format + # that looks line --oneline for the first line so we can share parsing + # logic. + full_msg = git('log', '-1', '--format=%h %s%n%b', args.revision, + ignore_errors=True) + if len(full_msg) == 0: + die("Can't find git commit " + args.revision) + (oneline, rest) = full_msg.split('\n', 1) + svn_match = re.search('llvm-svn: (\d{5,7})$', rest) + if svn_match: + svn_rev = svn_match.group(1) + else: + die("Can't find svn revision in git commit " + args.revision) + (git_hash, msg) = oneline.split(' ', 1) + + log_verbose('Ready to revert r%s/%s: "%s"' % (svn_rev, git_hash, msg)) + git('revert', '--no-commit', git_hash) + # TODO: Running --edit doesn't seem to work, with errors that stdin is not + # a tty. + commit_log = git( + 'commit', '-m', 'Revert ' + msg, + '-m', 'This reverts r%s (git commit %s)' % (svn_rev, git_hash)) + log('Created revert of r%s: %s' % (svn_rev, commit_log)) + log("Run 'git llvm push -n' to inspect your changes and " + "run 'git llvm push' when ready") + if __name__ == '__main__': if not program_exists('svn'): die('error: git-llvm needs svn command, but svn is not installed.') @@ -435,6 +497,17 @@ 'upstream, or not in origin/master if the branch lacks ' 'an explicit upstream)') parser_push.set_defaults(func=cmd_push) + + parser_revert = subcommands.add_parser( + 'revert', description=cmd_revert.__doc__, + help='Revert a commit locally.') + parser_revert.add_argument( + 'revision', + help='Revision to revert. Can either be an SVN revision number ' + "(rNNNNNN or NNNNNN) or a git commit hash (anything that doesn't look " + 'like an SVN revision number).') + parser_revert.set_defaults(func=cmd_revert) + args = p.parse_args(argv) VERBOSE = args.verbose QUIET = args.quiet