blob: 6e07c6d33947f31cad9c7e901cb8d9d59311209d [file] [log] [blame]
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00001#!/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
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
13def main(args):
agable7aa2ddd2016-06-21 07:47:00 -070014 default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000015 args = default_args + args
16
Matt Mueller89734302017-11-03 14:37:23 -070017 current_branch = git.current_branch()
18
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000019 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'))
Matt Mueller89734302017-11-03 14:37:23 -070024 parser.add_argument('branch', nargs='?', default=current_branch,
25 help='Show changes from a different branch')
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000026 opts, extra_args = parser.parse_known_args(args)
27
Matt Mueller89734302017-11-03 14:37:23 -070028 if not opts.branch or opts.branch == 'HEAD':
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000029 print 'fatal: Cannot perform git-upstream-diff while not on a branch'
30 return 1
31
Matt Mueller89734302017-11-03 14:37:23 -070032 par = git.upstream(opts.branch)
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000033 if not par:
Matt Mueller89734302017-11-03 14:37:23 -070034 print 'fatal: No upstream configured for branch \'%s\'' % opts.branch
iannucci@chromium.orgde70b152014-03-26 18:55:39 +000035 return 1
36
Aaron Gablef4068aa2017-12-12 15:14:09 -080037 cmd = [git.GIT_EXE, '-c', 'core.quotePath=false',
38 'diff', '--patience', '-C', '-C']
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000039 if opts.wordwise:
40 cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])']
Matt Mueller89734302017-11-03 14:37:23 -070041 cmd += [git.get_or_create_merge_base(opts.branch, par)]
42 # Only specify the end commit if it is not the current branch, this lets the
43 # diff include uncommitted changes when diffing the current branch.
44 if opts.branch != current_branch:
45 cmd += [opts.branch]
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000046
47 cmd += extra_args
48
sbc@chromium.org013731e2015-02-26 18:28:43 +000049 return subprocess2.check_call(cmd)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000050
51
52if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000053 try:
54 sys.exit(main(sys.argv[1:]))
55 except KeyboardInterrupt:
56 sys.stderr.write('interrupted\n')
57 sys.exit(1)