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