git_mark_merge_base.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. def main(argv):
  17. parser = argparse.ArgumentParser(
  18. description=__doc__.strip().splitlines()[0],
  19. epilog=' '.join(__doc__.strip().splitlines()[1:]))
  20. g = parser.add_mutually_exclusive_group()
  21. g.add_argument(
  22. 'merge_base', nargs='?',
  23. help='The new hash to use as the merge base for the current branch'
  24. )
  25. g.add_argument('--delete', '-d', action='store_true',
  26. help='Remove the set mark.')
  27. opts = parser.parse_args(argv)
  28. cur = current_branch()
  29. if opts.delete:
  30. try:
  31. remove_merge_base(cur)
  32. except CalledProcessError:
  33. print('No merge base currently exists for %s.' % cur)
  34. return 0
  35. if opts.merge_base:
  36. try:
  37. opts.merge_base = hash_one(opts.merge_base)
  38. except CalledProcessError:
  39. print(
  40. 'fatal: could not resolve %s as a commit' % opts.merge_base,
  41. file=sys.stderr)
  42. return 1
  43. manual_merge_base(cur, opts.merge_base, upstream(cur))
  44. ret = 0
  45. actual = get_or_create_merge_base(cur)
  46. if opts.merge_base and opts.merge_base != actual:
  47. ret = 1
  48. print("Invalid merge_base %s" % opts.merge_base)
  49. print("merge_base(%s): %s" % (cur, actual))
  50. return ret
  51. if __name__ == '__main__':
  52. try:
  53. sys.exit(main(sys.argv[1:]))
  54. except KeyboardInterrupt:
  55. sys.stderr.write('interrupted\n')
  56. sys.exit(1)