git_upstream_diff.py 1.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import argparse
import sys

import subprocess2

11
import git_common as git
12 13

def main(args):
14
  default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
15 16
  args = default_args + args

17 18
  current_branch = git.current_branch()

19 20 21 22 23
  parser = argparse.ArgumentParser()
  parser.add_argument('--wordwise', action='store_true', default=False,
                      help=(
                        'Print a colorized wordwise diff '
                        'instead of line-wise diff'))
24 25 26 27
  parser.add_argument('--branch', default=current_branch,
                      help='Show changes from a different branch. Passing '
                           '"HEAD" is the same as omitting this option (it '
                           'diffs against the current branch)')
28 29
  opts, extra_args = parser.parse_known_args(args)

30 31 32
  if opts.branch == 'HEAD':
    opts.branch = current_branch

33
  if not opts.branch or opts.branch == 'HEAD':
34 35 36
    print 'fatal: Cannot perform git-upstream-diff while not on a branch'
    return 1

37
  par = git.upstream(opts.branch)
38
  if not par:
39
    print 'fatal: No upstream configured for branch \'%s\'' % opts.branch
40 41
    return 1

42 43
  cmd = [git.GIT_EXE, '-c', 'core.quotePath=false',
         'diff', '--patience', '-C', '-C']
44 45
  if opts.wordwise:
    cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])']
46 47 48 49 50
  cmd += [git.get_or_create_merge_base(opts.branch, par)]
  # Only specify the end commit if it is not the current branch, this lets the
  # diff include uncommitted changes when diffing the current branch.
  if opts.branch != current_branch:
    cmd += [opts.branch]
51 52 53

  cmd += extra_args

54
  return subprocess2.check_call(cmd)
55 56 57


if __name__ == '__main__':
58 59 60 61 62
  try:
    sys.exit(main(sys.argv[1:]))
  except KeyboardInterrupt:
    sys.stderr.write('interrupted\n')
    sys.exit(1)