-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_upstream_branch
More file actions
executable file
·118 lines (96 loc) · 3.72 KB
/
find_upstream_branch
File metadata and controls
executable file
·118 lines (96 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
############################################################################
#
# Name: find_upstream_branch
# Author: Dan Berindei (http://github.com/danberindei)
# Description: This script finds the integration branch for a task branch.
# Task branches start with "t_", integration branches exist
# in the upstream repo as well.
#
############################################################################
import os
from os import path
import sys
import subprocess
import getopt
# Configuration: The following variables need to be set.
TOPIC_BRANCH_PREFIX="t_" # All branches whose name starts with this will be rebased against their updated branch point
GIT="git" # Path to the git binary executable
# The following variables need to be set in the git config:
# * sync.upstreamRepo - The upstream repo, usually upstream (if you have your own fork) or origin.
# * sync.releaseBranches - The release branches you want to sync with, must exist in the upstream repo.
upstream_repo = None
rel_branches = None
branch = None
def run_git(opts):
#print("### git %s" % opts, file=sys.stderr);
call = [GIT]
for o in opts.split(' '):
if o != '':
call.append(o)
return subprocess.run(call, capture_output=True, text=True).stdout
def find_upstream_repo():
try:
upstream = run_git("config sync.upstreamRepo").strip()
return upstream
except Exception:
print("Can't find the upstream repo! Make sure you have configured sync.upstreamRepo in this repository.", file=sys.stderr)
exit(1)
def find_rel_branches():
try:
release_branches = run_git("config sync.releaseBranches").split()
#print "Release branches are %s" % release_branches
return release_branches
except Exception:
print("Can't find any release branches! Make sure you have configured sync.releaseBranches in this repository.", file=sys.stderr)
exit(1)
def parse_args():
global upstream_repo
global rel_branches
global branch
try:
opts, args = getopt.getopt(sys.argv[1:], "hu:b:", ["help", "upstream_repo=", "rel_branches="])
except getopt.GetoptError as err:
# print help information and exit:
print(str(err), file=sys.stderr) # will print something like "option -a not recognized"
sys.exit(2)
upstream_repo = None
rel_branches = None
for o, a in opts:
if o in ("-u", "--upstream_repo"):
upstream_repo = a
elif o in ("-b", "--rel_branches"):
rel_branches = a.split(',')
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
assert False, "unhandled option"
if upstream_repo == None: upstream_repo = find_upstream_repo()
if rel_branches == None: rel_branches = find_rel_branches()
if (len(args) > 0): branch = args[0]
def usage():
print("usage: ", sys.argv[0],"[-u <upstream repo>] [-b <comma separated branches>]", file=sys.stderr)
def find_upstream_branch():
global branch
if branch == None:
branch = run_git("symbolic-ref --short HEAD").split('\n')[0]
#print("Analysing topic branch %s" % branch, file=sys.stderr)
#base_branch = subprocess.check_output("git rev-list --topo-order %s %s | git symbolic-ref --short --stdin | egrep -m1 '%s'" %
# (branch, " ".join(rel_branches), "|".join(rel_branches)), shell=True).strip()
min_commits = 1000
base_branch = "no_such_base_branch"
for relbranch in rel_branches:
if not relbranch.startswith(TOPIC_BRANCH_PREFIX):
commits = len(run_git("--no-pager log --pretty=oneline -n %d %s..%s" % (min_commits + 1, relbranch, branch)).split("\n"))
if commits < min_commits:
min_commits = commits
base_branch = relbranch
print(base_branch)
def main():
parse_args()
find_upstream_branch()
if __name__ == "__main__":
main()