[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2014 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 | import argparse |
| 7 | import sys |
| 8 | |
| 9 | import subprocess2 |
| 10 | |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 11 | import git_common as git |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 12 | |
| 13 | def main(args): |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 14 | default_args = git.config_list('depot-tools.upstream-diff.default-args') |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 15 | args = default_args + args |
| 16 | |
| 17 | parser = argparse.ArgumentParser() |
| 18 | parser.add_argument('--wordwise', action='store_true', default=False, |
| 19 | help=( |
| 20 | 'Print a colorized wordwise diff ' |
| 21 | 'instead of line-wise diff')) |
| 22 | opts, extra_args = parser.parse_known_args(args) |
| 23 | |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 24 | cur = git.current_branch() |
| 25 | if not cur or cur == 'HEAD': |
| 26 | print 'fatal: Cannot perform git-upstream-diff while not on a branch' |
| 27 | return 1 |
| 28 | |
| 29 | par = git.upstream(cur) |
| 30 | if not par: |
| 31 | print 'fatal: No upstream configured for branch \'%s\'' % cur |
| 32 | return 1 |
| 33 | |
| 34 | cmd = [git.GIT_EXE, 'diff', '--patience', '-C', '-C'] |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 35 | if opts.wordwise: |
| 36 | cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])'] |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 37 | cmd += [git.get_or_create_merge_base(cur, par)] |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 38 | |
| 39 | cmd += extra_args |
| 40 | |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 41 | return subprocess2.check_call(cmd) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 42 | |
| 43 | |
| 44 | if __name__ == '__main__': |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 45 | try: |
| 46 | sys.exit(main(sys.argv[1:])) |
| 47 | except KeyboardInterrupt: |
| 48 | sys.stderr.write('interrupted\n') |
| 49 | sys.exit(1) |