git_mark_merge_base.py 2.0 KB

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