git_upstream_diff.py 2.1 KB

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