git_mark_merge_base.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. """
  6. Explicitly set/remove/print the merge-base for the current branch.
  7. This manually set merge base will be a stand-in for `git merge-base` for the
  8. purposes of the chromium depot_tools git extensions. Passing no arguments will
  9. just print the effective merge base for the current branch.
  10. """
  11. import argparse
  12. import sys
  13. from subprocess2 import CalledProcessError
  14. from git_common import remove_merge_base, manual_merge_base, current_branch
  15. from git_common import get_or_create_merge_base, hash_one, upstream
  16. import gclient_utils
  17. def main(argv):
  18. if gclient_utils.IsEnvCog():
  19. print('mark-merge-base command is not supported in non-git '
  20. 'environment.', file=sys.stderr)
  21. return 1
  22. parser = argparse.ArgumentParser(
  23. description=__doc__.strip().splitlines()[0],
  24. epilog=' '.join(__doc__.strip().splitlines()[1:]))
  25. g = parser.add_mutually_exclusive_group()
  26. g.add_argument(
  27. 'merge_base',
  28. nargs='?',
  29. help='The new hash to use as the merge base for the current branch')
  30. g.add_argument('--delete',
  31. '-d',
  32. action='store_true',
  33. help='Remove the set mark.')
  34. opts = parser.parse_args(argv)
  35. cur = current_branch()
  36. if opts.delete:
  37. try:
  38. remove_merge_base(cur)
  39. except CalledProcessError:
  40. print('No merge base currently exists for %s.' % cur)
  41. return 0
  42. if opts.merge_base:
  43. try:
  44. opts.merge_base = hash_one(opts.merge_base)
  45. except CalledProcessError:
  46. print('fatal: could not resolve %s as a commit' % opts.merge_base,
  47. file=sys.stderr)
  48. return 1
  49. manual_merge_base(cur, opts.merge_base, upstream(cur))
  50. ret = 0
  51. actual = get_or_create_merge_base(cur)
  52. if opts.merge_base and opts.merge_base != actual:
  53. ret = 1
  54. print("Invalid merge_base %s" % opts.merge_base)
  55. print("merge_base(%s): %s" % (cur, actual))
  56. return ret
  57. if __name__ == '__main__':
  58. try:
  59. sys.exit(main(sys.argv[1:]))
  60. except KeyboardInterrupt:
  61. sys.stderr.write('interrupted\n')
  62. sys.exit(1)