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