git_upstream_diff.py 2.4 KB

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