blob: 8074e38c9eb80b515d0df51033394bfffbc8e8b0 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00002# 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
6import argparse
7import sys
8
9import subprocess2
10
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000011import git_common as git
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000012
Mike Frysinger124bb8e2023-09-06 05:48:55 +000013
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000014def main(args):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000015 default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
16 args = default_args + args
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000017
Mike Frysinger124bb8e2023-09-06 05:48:55 +000018 current_branch = git.current_branch()
Matt Mueller89734302017-11-03 14:37:23 -070019
Mike Frysinger124bb8e2023-09-06 05:48:55 +000020 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.orgc050a5b2014-03-26 06:18:50 +000032
Mike Frysinger124bb8e2023-09-06 05:48:55 +000033 if opts.branch == 'HEAD':
34 opts.branch = current_branch
Robert Iannuccieb5f85b2018-06-21 19:41:31 +000035
Mike Frysinger124bb8e2023-09-06 05:48:55 +000036 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.orgde70b152014-03-26 18:55:39 +000039
Mike Frysinger124bb8e2023-09-06 05:48:55 +000040 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.orgde70b152014-03-26 18:55:39 +000044
Mike Frysinger124bb8e2023-09-06 05:48:55 +000045 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.orgc050a5b2014-03-26 06:18:50 +000056
Mike Frysinger124bb8e2023-09-06 05:48:55 +000057 cmd += extra_args
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000058
Mike Frysinger124bb8e2023-09-06 05:48:55 +000059 return subprocess2.check_call(cmd)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000060
61
62if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000063 try:
64 sys.exit(main(sys.argv[1:]))
65 except KeyboardInterrupt:
66 sys.stderr.write('interrupted\n')
67 sys.exit(1)