blob: 2a45bae139bc7c59af0067969eed11c4d9029414 [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.
5
6"""
7Explicitly set/remove/print the merge-base for the current branch.
8
9This manually set merge base will be a stand-in for `git merge-base` for the
10purposes of the chromium depot_tools git extensions. Passing no arguments will
11just print the effective merge base for the current branch.
12"""
13
14import argparse
15import sys
16
17from subprocess2 import CalledProcessError
18
19from git_common import remove_merge_base, manual_merge_base, current_branch
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000020from git_common import get_or_create_merge_base, hash_one, upstream
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000021
22
23def main(argv):
24 parser = argparse.ArgumentParser(
25 description=__doc__.strip().splitlines()[0],
26 epilog=' '.join(__doc__.strip().splitlines()[1:]))
27 g = parser.add_mutually_exclusive_group()
28 g.add_argument(
29 'merge_base', nargs='?',
30 help='The new hash to use as the merge base for the current branch'
31 )
32 g.add_argument('--delete', '-d', action='store_true',
33 help='Remove the set mark.')
34 opts = parser.parse_args(argv)
35
36 cur = current_branch()
37
38 if opts.delete:
39 try:
40 remove_merge_base(cur)
41 except CalledProcessError:
Raul Tambre80ee78e2019-05-06 22:41:05 +000042 print('No merge base currently exists for %s.' % cur)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000043 return 0
44
45 if opts.merge_base:
46 try:
47 opts.merge_base = hash_one(opts.merge_base)
48 except CalledProcessError:
Raul Tambre80ee78e2019-05-06 22:41:05 +000049 print(
50 'fatal: could not resolve %s as a commit' % opts.merge_base,
51 file=sys.stderr)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000052 return 1
53
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000054 manual_merge_base(cur, opts.merge_base, upstream(cur))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000055
56 ret = 0
57 actual = get_or_create_merge_base(cur)
58 if opts.merge_base and opts.merge_base != actual:
59 ret = 1
Raul Tambre80ee78e2019-05-06 22:41:05 +000060 print("Invalid merge_base %s" % opts.merge_base)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000061
Raul Tambre80ee78e2019-05-06 22:41:05 +000062 print("merge_base(%s): %s" % (cur, actual))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000063 return ret
64
65
66if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000067 try:
68 sys.exit(main(sys.argv[1:]))
69 except KeyboardInterrupt:
70 sys.stderr.write('interrupted\n')
71 sys.exit(1)