git_upstream_diff.py 2.0 KB

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