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