blob: 5ec6419198e02f87e560ee65bc10128cc749f8f9 [file] [log] [blame]
Josip Sokcevic4de5dea2022-03-23 21:15:14 +00001#!/usr/bin/env python3
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +00002# 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.orgc050a5b2014-03-26 06:18:50 +00005"""
6Explicitly set/remove/print the merge-base for the current branch.
7
8This manually set merge base will be a stand-in for `git merge-base` for the
9purposes of the chromium depot_tools git extensions. Passing no arguments will
10just print the effective merge base for the current branch.
11"""
12
13import argparse
14import sys
15
16from subprocess2 import CalledProcessError
17
18from git_common import remove_merge_base, manual_merge_base, current_branch
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000019from git_common import get_or_create_merge_base, hash_one, upstream
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000020
21
22def main(argv):
Mike Frysinger124bb8e2023-09-06 05:48:55 +000023 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.orgc050a5b2014-03-26 06:18:50 +000036
Mike Frysinger124bb8e2023-09-06 05:48:55 +000037 cur = current_branch()
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000038
Mike Frysinger124bb8e2023-09-06 05:48:55 +000039 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.orgc050a5b2014-03-26 06:18:50 +000045
Mike Frysinger124bb8e2023-09-06 05:48:55 +000046 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.orgc050a5b2014-03-26 06:18:50 +000053
Mike Frysinger124bb8e2023-09-06 05:48:55 +000054 manual_merge_base(cur, opts.merge_base, upstream(cur))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000055
Mike Frysinger124bb8e2023-09-06 05:48:55 +000056 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.orgc050a5b2014-03-26 06:18:50 +000061
Mike Frysinger124bb8e2023-09-06 05:48:55 +000062 print("merge_base(%s): %s" % (cur, actual))
63 return ret
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000064
65
66if __name__ == '__main__':
Mike Frysinger124bb8e2023-09-06 05:48:55 +000067 try:
68 sys.exit(main(sys.argv[1:]))
69 except KeyboardInterrupt:
70 sys.stderr.write('interrupted\n')
71 sys.exit(1)