From 443f7255d2dba0da1976fbaef6d7c3aa59df225d Mon Sep 17 00:00:00 2001 From: James Y Knight Date: Fri, 12 Jul 2019 16:41:28 +0000 Subject: [PATCH] Fix some minor coding-style issues in git-llvm. llvm-svn: 365918 --- utils/git-svn/git-llvm | 71 +++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/utils/git-svn/git-llvm b/utils/git-svn/git-llvm index 2ee4723f568..399e55247b5 100755 --- a/utils/git-svn/git-llvm +++ b/utils/git-svn/git-llvm @@ -18,14 +18,11 @@ This file provides integration for git. from __future__ import print_function import argparse import collections -import contextlib -import errno import os import re import shutil import subprocess import sys -import tempfile import time assert sys.version_info >= (2, 7) @@ -73,7 +70,7 @@ LLVM_MONOREPO_SVN_MAPPING = { LLVM_MONOREPO_SVN_MAPPING.update({'clang': 'cfe/trunk'}) LLVM_MONOREPO_SVN_MAPPING.update({'': 'monorepo-root/trunk'}) -SPLIT_REPO_NAMES = {'llvm-' + d : d + '/trunk' +SPLIT_REPO_NAMES = {'llvm-' + d: d + '/trunk' for d in ['www', 'zorg', 'test-suite', 'lnt']} VERBOSE = False @@ -167,6 +164,7 @@ def git(*cmd, **kwargs): def svn(cwd, *cmd, **kwargs): return shell(['svn'] + list(cmd), cwd=cwd, **kwargs) + def program_exists(cmd): if sys.platform == 'win32' and not cmd.endswith('.exe'): cmd += '.exe' @@ -175,6 +173,7 @@ def program_exists(cmd): return True return False + def get_default_rev_range(): # Get the branch tracked by the current branch, as set by # git branch --set-upstream-to See http://serverfault.com/a/352236/38694. @@ -277,6 +276,7 @@ def fix_eol_style_native(rev, svn_sr_path, files): # just the diff, and not a mass line ending change. shell(['dos2unix'] + crlf_files, ignore_errors=True, cwd=svn_sr_path) + def split_subrepo(f, git_to_svn_mapping): # Given a path, splits it into (subproject, rest-of-path). If the path is # not in a subproject, returns ('', full-path). @@ -288,6 +288,7 @@ def split_subrepo(f, git_to_svn_mapping): else: return '', f + def get_all_parent_dirs(name): parts = [] head, tail = os.path.split(name) @@ -296,6 +297,7 @@ def get_all_parent_dirs(name): head, tail = os.path.split(head) return parts + def svn_push_one_rev(svn_repo, rev, git_to_svn_mapping, dry_run): files = git('diff-tree', '--no-commit-id', '--name-only', '-r', rev).split('\n') @@ -320,7 +322,8 @@ def svn_push_one_rev(svn_repo, rev, git_to_svn_mapping, dry_run): svn_dirs_to_update.add( os.path.dirname(os.path.join(svn_sr_path, f))) - # We also need to svn update any parent directories which are not yet present + # We also need to svn update any parent directories which are not yet + # present parent_dirs = set() for dir in svn_dirs_to_update: parent_dirs.update(get_all_parent_dirs(dir)) @@ -399,17 +402,17 @@ def cmd_push(args): git_remote_repo_name = git_remote_url.rsplit('/', 1)[-1] split_repo_path = SPLIT_REPO_NAMES.get(git_remote_repo_name) if split_repo_path: - git_to_svn_mapping = {'': split_repo_path} + git_to_svn_mapping = {'': split_repo_path} else: - # Default to the monorepo mapping - git_to_svn_mapping = LLVM_MONOREPO_SVN_MAPPING + # Default to the monorepo mapping + git_to_svn_mapping = LLVM_MONOREPO_SVN_MAPPING # We need a staging area for SVN, let's hide it in the .git directory. dot_git_dir = git('rev-parse', '--git-common-dir') # Not all versions of git support --git-common-dir and just print the # unknown command back. If this happens, fall back to --git-dir if dot_git_dir == '--git-common-dir': - dot_git_dir = git('rev-parse', '--git-dir') + dot_git_dir = git('rev-parse', '--git-dir') svn_root = os.path.join(dot_git_dir, 'llvm-upstream-svn') svn_init(svn_root) @@ -419,7 +422,8 @@ def cmd_push(args): revs = get_revs_to_push(rev_range) log('Pushing %d %s commit%s:\n%s' % (len(revs), - 'split-repo (%s)' % split_repo_path if split_repo_path else 'monorepo', + 'split-repo (%s)' % split_repo_path + if split_repo_path else 'monorepo', 's' if len(revs) != 1 else '', '\n'.join(' ' + git('show', '--oneline', '--quiet', c) for c in revs))) @@ -434,7 +438,7 @@ def lookup_llvm_svn_id(git_commit_hash): commit_msg = git('log', '-1', '--format=%b', git_commit_hash, ignore_errors=True) if len(commit_msg) == 0: - die("Can't find git commit " + git_commit_hash) + die("Can't find git commit " + git_commit_hash) # If a commit has multiple "llvm-svn:" lines (e.g. if the commit is # reverting/quoting a previous commit), choose the last one, which should # be the authoritative one. @@ -442,9 +446,9 @@ def lookup_llvm_svn_id(git_commit_hash): re.MULTILINE) svn_match = None for m in svn_match_iter: - svn_match = m.group(1) + svn_match = m.group(1) if svn_match: - return int(svn_match) + return int(svn_match) die("Can't find svn revision in git commit " + git_commit_hash) @@ -479,12 +483,13 @@ def git_hash_by_svn_rev(svn_rev): matching_hashes = [h for h in possible_hashes if lookup_llvm_svn_id(h) == svn_rev] if len(matching_hashes) > 1: - die("svn revision r%d has ambiguous commits: %s" % ( - svn_rev, ', '.join(matching_hashes))) + die("svn revision r%d has ambiguous commits: %s" % ( + svn_rev, ', '.join(matching_hashes))) elif len(matching_hashes) < 1: - die("svn revision r%d matches no commits" % svn_rev) + die("svn revision r%d matches no commits" % svn_rev) return matching_hashes[0] + def cmd_revert(args): '''Revert a commit by either SVN id (rNNNNNN) or git hash. This also populates the git commit message with both the SVN revision and git hash of @@ -501,23 +506,23 @@ def cmd_revert(args): # 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) + die("Found open files. Please stash and then revert.\n" + open_files) # If the revision looks like rNNNNNN, use that. Otherwise, look for it in # the git commit. svn_match = re.match('^r(\d{5,7})$', args.revision) if svn_match: - # If the revision looks like rNNNNNN, use that as the svn revision, and - # grep through git commits to find which one corresponds to that svn - # revision. - svn_rev = int(svn_match.group(1)) - git_hash = git_hash_by_svn_rev(svn_rev) + # If the revision looks like rNNNNNN, use that as the svn revision, and + # grep through git commits to find which one corresponds to that svn + # revision. + svn_rev = int(svn_match.group(1)) + git_hash = git_hash_by_svn_rev(svn_rev) else: - # Otherwise, this looks like a git hash, so we just need to grab the svn - # revision from the end of the commit message. - # Get the actual git hash in case the revision is something like "HEAD~1" - git_hash = git('rev-parse', '--verify', args.revision + '^{commit}') - svn_rev = lookup_llvm_svn_id(git_hash) + # Otherwise, this looks like a git hash, so we just need to grab the + # svn revision from the end of the commit message. Get the actual git + # hash in case the revision is something like "HEAD~1" + git_hash = git('rev-parse', '--verify', args.revision + '^{commit}') + svn_rev = lookup_llvm_svn_id(git_hash) msg = git('log', '-1', '--format=%s', git_hash) @@ -530,11 +535,12 @@ def cmd_revert(args): 'commit', '-m', 'Revert ' + msg, '-m', 'This reverts r%d (git commit %s)' % (svn_rev, git_hash)] if args.dry_run: - log("Would have run the following commands, if this weren't a dry run:\n" - '1) git %s\n2) git %s' % ( - ' '.join(quote(arg) for arg in revert_args), - ' '.join(quote(arg) for arg in commit_args))) - return + log("Would have run the following commands, if this weren't a" + "dry run:\n" + '1) git %s\n2) git %s' % ( + ' '.join(quote(arg) for arg in revert_args), + ' '.join(quote(arg) for arg in commit_args))) + return git(*revert_args) commit_log = git(*commit_args) @@ -543,6 +549,7 @@ def cmd_revert(args): 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.')