blob: 3b87471bfaaa76b47968c6c2ed186e7095a75a47 [file] [log] [blame]
Edward Lemurd6186f92019-08-12 17:56:58 +00001#!/usr/bin/env vpython
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
Raul Tambre80ee78e2019-05-06 22:41:05 +000014from __future__ import print_function
15
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000016import argparse
17import sys
18
19from subprocess2 import CalledProcessError
20
21from git_common import remove_merge_base, manual_merge_base, current_branch
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000022from git_common import get_or_create_merge_base, hash_one, upstream
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000023
24
25def 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 Tambre80ee78e2019-05-06 22:41:05 +000044 print('No merge base currently exists for %s.' % cur)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000045 return 0
46
47 if opts.merge_base:
48 try:
49 opts.merge_base = hash_one(opts.merge_base)
50 except CalledProcessError:
Raul Tambre80ee78e2019-05-06 22:41:05 +000051 print(
52 'fatal: could not resolve %s as a commit' % opts.merge_base,
53 file=sys.stderr)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000054 return 1
55
iannucci@chromium.org10fbe872014-05-16 22:31:13 +000056 manual_merge_base(cur, opts.merge_base, upstream(cur))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000057
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 Tambre80ee78e2019-05-06 22:41:05 +000062 print("Invalid merge_base %s" % opts.merge_base)
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000063
Raul Tambre80ee78e2019-05-06 22:41:05 +000064 print("merge_base(%s): %s" % (cur, actual))
iannucci@chromium.orgc050a5b2014-03-26 06:18:50 +000065 return ret
66
67
68if __name__ == '__main__':
sbc@chromium.org013731e2015-02-26 18:28:43 +000069 try:
70 sys.exit(main(sys.argv[1:]))
71 except KeyboardInterrupt:
72 sys.stderr.write('interrupted\n')
73 sys.exit(1)