[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): |
agable | 7aa2ddd | 2016-06-21 14:47:00 | [diff] [blame] | 14 | default_args = git.get_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 | |
Matt Mueller | 8973430 | 2017-11-03 21:37:23 | [diff] [blame] | 17 | current_branch = git.current_branch() |
| 18 | |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 19 | parser = argparse.ArgumentParser() |
| 20 | parser.add_argument('--wordwise', action='store_true', default=False, |
| 21 | help=( |
| 22 | 'Print a colorized wordwise diff ' |
| 23 | 'instead of line-wise diff')) |
Robert Iannucci | eb5f85b | 2018-06-21 19:41:31 | [diff] [blame] | 24 | parser.add_argument('--branch', default=current_branch, |
| 25 | help='Show changes from a different branch. Passing ' |
| 26 | '"HEAD" is the same as omitting this option (it ' |
| 27 | 'diffs against the current branch)') |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 28 | opts, extra_args = parser.parse_known_args(args) |
| 29 | |
Robert Iannucci | eb5f85b | 2018-06-21 19:41:31 | [diff] [blame] | 30 | if opts.branch == 'HEAD': |
| 31 | opts.branch = current_branch |
| 32 | |
Matt Mueller | 8973430 | 2017-11-03 21:37:23 | [diff] [blame] | 33 | if not opts.branch or opts.branch == 'HEAD': |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 34 | print 'fatal: Cannot perform git-upstream-diff while not on a branch' |
| 35 | return 1 |
| 36 | |
Matt Mueller | 8973430 | 2017-11-03 21:37:23 | [diff] [blame] | 37 | par = git.upstream(opts.branch) |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 38 | if not par: |
Matt Mueller | 8973430 | 2017-11-03 21:37:23 | [diff] [blame] | 39 | print 'fatal: No upstream configured for branch \'%s\'' % opts.branch |
[email protected] | de70b15 | 2014-03-26 18:55:39 | [diff] [blame] | 40 | return 1 |
| 41 | |
Aaron Gable | f4068aa | 2017-12-12 23:14:09 | [diff] [blame] | 42 | cmd = [git.GIT_EXE, '-c', 'core.quotePath=false', |
| 43 | 'diff', '--patience', '-C', '-C'] |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 44 | if opts.wordwise: |
| 45 | cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])'] |
Matt Mueller | 8973430 | 2017-11-03 21:37:23 | [diff] [blame] | 46 | cmd += [git.get_or_create_merge_base(opts.branch, par)] |
| 47 | # Only specify the end commit if it is not the current branch, this lets the |
| 48 | # diff include uncommitted changes when diffing the current branch. |
| 49 | if opts.branch != current_branch: |
| 50 | cmd += [opts.branch] |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 51 | |
| 52 | cmd += extra_args |
| 53 | |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 54 | return subprocess2.check_call(cmd) |
[email protected] | c050a5b | 2014-03-26 06:18:50 | [diff] [blame] | 55 | |
| 56 | |
| 57 | if __name__ == '__main__': |
[email protected] | 013731e | 2015-02-26 18:28:43 | [diff] [blame] | 58 | try: |
| 59 | sys.exit(main(sys.argv[1:])) |
| 60 | except KeyboardInterrupt: |
| 61 | sys.stderr.write('interrupted\n') |
| 62 | sys.exit(1) |