blob: f1e712ea88d3fd039a9e1927a600c92419e016c8 [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
Raul Tambre80ee78e2019-05-06 22:41:05 +00006from __future__ import print_function
7
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00008import argparse
9import sys
10
11import subprocess2
12
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000013import git_common as git
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000014
15def main(args):
agable7aa2ddd2016-06-21 07:47:00 -070016 default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000017 args = default_args + args
18
Matt Mueller89734302017-11-03 14:37:23 -070019 current_branch = git.current_branch()
20
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000021 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 Iannuccieb5f85b2018-06-21 19:41:31 +000026 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.orgc050a5b2014-03-26 06:18:50 +000030 opts, extra_args = parser.parse_known_args(args)
31
Robert Iannuccieb5f85b2018-06-21 19:41:31 +000032 if opts.branch == 'HEAD':
33 opts.branch = current_branch
34
Matt Mueller89734302017-11-03 14:37:23 -070035 if not opts.branch or opts.branch == 'HEAD':
Raul Tambre80ee78e2019-05-06 22:41:05 +000036 print('fatal: Cannot perform git-upstream-diff while not on a branch')
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000037 return 1
38
Matt Mueller89734302017-11-03 14:37:23 -070039 par = git.upstream(opts.branch)
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000040 if not par:
Raul Tambre80ee78e2019-05-06 22:41:05 +000041 print('fatal: No upstream configured for branch \'%s\'' % opts.branch)
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000042 return 1
43
Aaron Gablef4068aa2017-12-12 15:14:09 -080044 cmd = [git.GIT_EXE, '-c', 'core.quotePath=false',
45 'diff', '--patience', '-C', '-C']
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000046 if opts.wordwise:
47 cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])']
Matt Mueller89734302017-11-03 14:37:23 -070048 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.orgc050a5b2014-03-26 06:18:50 +000053
54 cmd += extra_args
55
sbc@chromium.org013731e2015-02-26 18:28:43 +000056 return subprocess2.check_call(cmd)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000057
58
59if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000060 try:
61 sys.exit(main(sys.argv[1:]))
62 except KeyboardInterrupt:
63 sys.stderr.write('interrupted\n')
64 sys.exit(1)