mass-rename.py revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1#!/usr/bin/env python
2# Copyright 2013 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""
7mass-rename: update source files (gyp lists, #includes) to reflect
8a rename.  Expects "git diff --cached -M" to list a bunch of renames.
9
10To use:
11  1) git mv foo1 bar1; git mv foo2 bar2; etc.
12  2) *without committing*, ./tools/git/mass-rename.py
13  3) look at git diff (without --cached) to see what the damage is
14"""
15
16
17import os
18import subprocess
19import sys
20
21
22BASE_DIR = os.path.abspath(os.path.dirname(__file__))
23
24
25def main():
26  popen = subprocess.Popen('git diff --cached --raw -M',
27                           shell=True, stdout=subprocess.PIPE)
28  out, _ = popen.communicate()
29  if popen.returncode != 0:
30    return 1
31  for line in out.splitlines():
32    attrs, fro, to = line.split('\t')
33    if attrs.split()[4].startswith('R'):
34      subprocess.check_call([
35        sys.executable,
36        os.path.join(BASE_DIR, 'move_source_file.py'),
37        '--already_moved',
38        '--no_error_for_non_source_file',
39        fro, to])
40    else:
41      print "Skipping: %s -- not a rename?" % fro
42  return 0
43
44
45if __name__ == '__main__':
46  sys.exit(main())
47