diff options
author | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-21 00:19:57 +0000 |
---|---|---|
committer | joi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-21 00:19:57 +0000 |
commit | 30d0788c2c66df46623783f7c19d1e32c6338e15 (patch) | |
tree | 3575fd8bf6cdfeff787efb6933d5dff92ab0cdce /tools/git | |
parent | 602104b6761c08e11879e2e5ae1dc0b55f2f72e2 (diff) | |
download | chromium_src-30d0788c2c66df46623783f7c19d1e32c6338e15.zip chromium_src-30d0788c2c66df46623783f7c19d1e32c6338e15.tar.gz chromium_src-30d0788c2c66df46623783f7c19d1e32c6338e15.tar.bz2 |
Consolidate mass-rename.sh and move_source_file.py
BUG=none
Review URL: https://chromiumcodereview.appspot.com/11358216
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168926 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/git')
-rwxr-xr-x | tools/git/mass-rename.sh | 33 | ||||
-rwxr-xr-x | tools/git/move_source_file.py | 192 |
2 files changed, 194 insertions, 31 deletions
diff --git a/tools/git/mass-rename.sh b/tools/git/mass-rename.sh index bc184be..8e9b378 100755 --- a/tools/git/mass-rename.sh +++ b/tools/git/mass-rename.sh @@ -13,34 +13,7 @@ # 4) commit, then use tools/sort-headers.py to fix #include ordering: # for f in $(git diff --name-only origin); do ./tools/sort-headers.py $f; done -# rename: -# update all uses of |from| (first argument) to |to| (second argument). -rename() { - from="$1" - to="$2" - - # Fix references to the files in headers/gyp files. - echo "Processing: $from -> $to" - git grep -l "$from" -- '*.cc' '*.h' '*.m' '*.mm' '*.gyp*' | \ - xargs sed -i -e "s|$from|$to|" - - # Fix header guards. - if [ "${from##*.}" = "h" ]; then - hfrom=$(echo "$from" | tr 'a-z/' 'A-Z_' | sed -e 's|\..*$||') - hto=$(echo "$to" | tr 'a-z/' 'A-Z_' | sed -e 's|\..*$||') - echo "Processing: $hfrom -> $hto" - git grep -l "$hfrom" -- '*.cc' '*.h' '*.m' '*.mm' | \ - xargs sed -i -e "s|$hfrom|$hto|" - fi - - # Try again, stripping the first directory component -- helps with - # gyp files that rely on paths from the directory they're in. - from=$(echo "$from" | sed -e 's|^[^/]*/||') - to=$(echo "$to" | sed -e 's|^[^/]*/||') - echo "Processing: $from -> $to" - git grep -l "$from" -- '*.cc' '*.h' '*.m' '*.mm' '*.gyp*' | \ - xargs sed -i -e "s|$from|$to|" -} +DIR="$( cd "$( dirname "$0" )" && pwd )" # Make the 'read' used in the while loop split only on tabs/newlines. IFS=$'\t\n' @@ -48,10 +21,8 @@ IFS=$'\t\n' git diff --cached --raw -M | while read attrs from to; do type=$(echo "$attrs" | cut -d' ' -f5) if echo "$type" | grep -q "^R"; then - # It's a rename. - rename "$from" "$to" + python $DIR/move_source_file.py --already-moved "$from" "$to" else echo "Skipping: $from -- not a rename?" fi done - diff --git a/tools/git/move_source_file.py b/tools/git/move_source_file.py new file mode 100755 index 0000000..148cc0d --- /dev/null +++ b/tools/git/move_source_file.py @@ -0,0 +1,192 @@ +#!/usr/bin/env python +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Moves a C++ file to a new location, updating any include paths that +point to it. Updates include guards in moved header files. Assumes +Chromium coding style. + +Does not reorder headers; instead, use this after committing all of +your moves: + ./tools/git/for-all-touched-files.py -c "tools/sort-headers.py [[FILENAME]]" + +Updates paths used in .gyp(i) files, but does not reorder or +restructure .gyp(i) files in any way. + +Must run in a git checkout, as it relies on git for a fast way to find +files that reference the moved file. +""" + + +import os +import re +import subprocess +import sys + +HANDLED_EXTENSIONS = ['.cc', '.mm', '.h', '.hh'] + + +def MakeDestinationPath(from_path, to_path): + """Given the from and to paths, return a correct destination path. + + The initial destination path may either a full path or a directory, + in which case the path must end with /. Also does basic sanity + checks. + """ + if os.path.splitext(from_path)[1] not in HANDLED_EXTENSIONS: + raise Exception('Only intended to move individual source files.') + dest_extension = os.path.splitext(to_path)[1] + if dest_extension not in HANDLED_EXTENSIONS: + if to_path.endswith('/') or to_path.endswith('\\'): + to_path += os.path.basename(from_path) + else: + raise Exception('Destination must be either full path or end with /.') + return to_path + + +def MoveFile(from_path, to_path): + """Performs a git mv command to move a file from |from_path| to |to_path|. + """ + if not os.system('git mv %s %s' % (from_path, to_path)) == 0: + raise Exception('Fatal: Failed to run git mv command.') + + +def MultiFileFindReplace(original, + replacement, + grep_pattern, + file_globs, + guard_formats): + """Implements fast multi-file find and replace with optional guards. + + Given an |original| string and a |replacement| string, search for + them by formatting |grep_pattern| with |original| and running + git grep on the result, for files matching any of |file_globs|. + + Once files are found, the function searches for any of + |guard_formats| formatted with |original| and replaces each match + with the same guard format as matched, formatted with |replacement|. + + Args: + original: 'chrome/browser/ui/browser.h' + replacement: 'chrome/browser/ui/browser/browser.h' + grep_pattern: r'#(include|import)\s*["<]%s[>"]' + file_globs: ['*.cc', '*.h', '*.m', '*.mm'] + guard_formats: None or ('"%s"', '<%s>') + + Raises an exception on error. + """ + out, err = subprocess.Popen( + ['git', 'grep', '-E', '--name-only', + grep_pattern % re.escape(original), '--'] + file_globs, + stdout=subprocess.PIPE).communicate() + referees = out.splitlines() + + for referee in referees: + with open(referee) as f: + original_contents = f.read() + contents = original_contents + for guard_format in guard_formats or []: + contents = contents.replace(guard_format % original, + guard_format % replacement) + if contents == original_contents: + raise Exception('No change in file %s although matched in grep' % + referee) + with open(referee, 'w') as f: + f.write(contents) + + +def UpdatePostMove(from_path, to_path): + """Given a file that has moved from |from_path| to |to_path|, + updates the moved file's include guard to match the new path and + updates all references to the file in other source files. Also tries + to update references in .gyp(i) files using a heuristic. + """ + # Include paths always use forward slashes. + from_path = from_path.replace('\\', '/') + to_path = to_path.replace('\\', '/') + + if os.path.splitext(from_path)[1] in ['.h', '.hh']: + UpdateIncludeGuard(from_path, to_path) + + # Update include/import references. + MultiFileFindReplace( + from_path, + to_path, + r'#(include|import)\s*["<]%s[>"]', + ['*.cc', '*.h', '*.m', '*.mm'], + ['"%s"', '<%s>']) + + # Update references in .gyp(i) files. + def PathMinusFirstComponent(path): + """foo/bar/baz -> bar/baz""" + parts = re.split(r"[/\\]", path, 1) + if len(parts) == 2: + return parts[1] + else: + return parts[0] + MultiFileFindReplace(PathMinusFirstComponent(from_path), + PathMinusFirstComponent(to_path), + r'[\'"]%s[\'"]', + ['*.gyp*'], + ["'%s'", '"%s"']) + + +def MakeIncludeGuardName(path_from_root): + """Returns an include guard name given a path from root.""" + guard = path_from_root.replace('/', '_') + guard = guard.replace('\\', '_') + guard = guard.replace('.', '_') + guard += '_' + return guard.upper() + + +def UpdateIncludeGuard(old_path, new_path): + """Updates the include guard in a file now residing at |new_path|, + previously residing at |old_path|, with an up-to-date include guard. + + Errors out if an include guard per Chromium style guide cannot be + found for the old path. + """ + old_guard = MakeIncludeGuardName(old_path) + new_guard = MakeIncludeGuardName(new_path) + + with open(new_path) as f: + contents = f.read() + + new_contents = contents.replace(old_guard, new_guard) + if new_contents == contents: + raise Exception( + 'Error updating include guard; perhaps old guard is not per style guide?') + + with open(new_path, 'w') as f: + f.write(new_contents) + + +def main(): + if not os.path.isdir('.git'): + print 'Fatal: You must run from the root of a git checkout.' + return 1 + args = sys.argv[1:] + if not len(args) in [2, 3]: + print ('Usage: move_source_file.py [--already-moved] FROM_PATH TO_PATH' + '\n\n%s' % __doc__) + return 1 + + already_moved = False + if args[0] == '--already-moved': + args = args[1:] + already_moved = True + + from_path = args[0] + to_path = args[1] + + to_path = MakeDestinationPath(from_path, to_path) + if not already_moved: + MoveFile(from_path, to_path) + UpdatePostMove(from_path, to_path) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) |