Josip Sokcevic | 4de5dea | 2022-03-23 21:15:14 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 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. |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 5 | """ |
| 6 | Explicitly set/remove/print the merge-base for the current branch. |
| 7 | |
| 8 | This manually set merge base will be a stand-in for `git merge-base` for the |
| 9 | purposes of the chromium depot_tools git extensions. Passing no arguments will |
| 10 | just print the effective merge base for the current branch. |
| 11 | """ |
| 12 | |
| 13 | import argparse |
| 14 | import sys |
| 15 | |
| 16 | from subprocess2 import CalledProcessError |
| 17 | |
| 18 | from git_common import remove_merge_base, manual_merge_base, current_branch |
iannucci@chromium.org | 10fbe87 | 2014-05-16 22:31:13 +0000 | [diff] [blame] | 19 | from git_common import get_or_create_merge_base, hash_one, upstream |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 20 | |
| 21 | |
| 22 | def main(argv): |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 23 | parser = argparse.ArgumentParser( |
| 24 | description=__doc__.strip().splitlines()[0], |
| 25 | epilog=' '.join(__doc__.strip().splitlines()[1:])) |
| 26 | g = parser.add_mutually_exclusive_group() |
| 27 | g.add_argument( |
| 28 | 'merge_base', |
| 29 | nargs='?', |
| 30 | help='The new hash to use as the merge base for the current branch') |
| 31 | g.add_argument('--delete', |
| 32 | '-d', |
| 33 | action='store_true', |
| 34 | help='Remove the set mark.') |
| 35 | opts = parser.parse_args(argv) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 36 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 37 | cur = current_branch() |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 38 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 39 | if opts.delete: |
| 40 | try: |
| 41 | remove_merge_base(cur) |
| 42 | except CalledProcessError: |
| 43 | print('No merge base currently exists for %s.' % cur) |
| 44 | return 0 |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 45 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 46 | if opts.merge_base: |
| 47 | try: |
| 48 | opts.merge_base = hash_one(opts.merge_base) |
| 49 | except CalledProcessError: |
| 50 | print('fatal: could not resolve %s as a commit' % opts.merge_base, |
| 51 | file=sys.stderr) |
| 52 | return 1 |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 53 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 54 | manual_merge_base(cur, opts.merge_base, upstream(cur)) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 55 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 56 | ret = 0 |
| 57 | actual = get_or_create_merge_base(cur) |
| 58 | if opts.merge_base and opts.merge_base != actual: |
| 59 | ret = 1 |
| 60 | print("Invalid merge_base %s" % opts.merge_base) |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 61 | |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 62 | print("merge_base(%s): %s" % (cur, actual)) |
| 63 | return ret |
iannucci@chromium.org | c050a5b | 2014-03-26 06:18:50 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | if __name__ == '__main__': |
Mike Frysinger | 124bb8e | 2023-09-06 05:48:55 +0000 | [diff] [blame] | 67 | try: |
| 68 | sys.exit(main(sys.argv[1:])) |
| 69 | except KeyboardInterrupt: |
| 70 | sys.stderr.write('interrupted\n') |
| 71 | sys.exit(1) |