git_upstream_diff.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  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. import argparse
  6. import sys
  7. import subprocess2
  8. import git_common as git
  9. def main(args):
  10. default_args = git.get_config_list('depot-tools.upstream-diff.default-args')
  11. args = default_args + args
  12. current_branch = git.current_branch()
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('--wordwise', action='store_true', default=False,
  15. help=(
  16. 'Print a colorized wordwise diff '
  17. 'instead of line-wise diff'))
  18. parser.add_argument('--branch', default=current_branch,
  19. help='Show changes from a different branch. Passing '
  20. '"HEAD" is the same as omitting this option (it '
  21. 'diffs against the current branch)')
  22. opts, extra_args = parser.parse_known_args(args)
  23. if opts.branch == 'HEAD':
  24. opts.branch = current_branch
  25. if not opts.branch or opts.branch == 'HEAD':
  26. print('fatal: Cannot perform git-upstream-diff while not on a branch')
  27. return 1
  28. par = git.upstream(opts.branch)
  29. if not par:
  30. print('fatal: No upstream configured for branch \'%s\'' % opts.branch)
  31. return 1
  32. cmd = [git.GIT_EXE, '-c', 'core.quotePath=false',
  33. 'diff', '--patience', '-C', '-C']
  34. if opts.wordwise:
  35. cmd += ['--word-diff=color', r'--word-diff-regex=(\w+|[^[:space:]])']
  36. cmd += [git.get_or_create_merge_base(opts.branch, par)]
  37. # Only specify the end commit if it is not the current branch, this lets the
  38. # diff include uncommitted changes when diffing the current branch.
  39. if opts.branch != current_branch:
  40. cmd += [opts.branch]
  41. cmd += extra_args
  42. return subprocess2.check_call(cmd)
  43. if __name__ == '__main__':
  44. try:
  45. sys.exit(main(sys.argv[1:]))
  46. except KeyboardInterrupt:
  47. sys.stderr.write('interrupted\n')
  48. sys.exit(1)