forked from HariSekhon/DevOps-Python-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_check_branches_upstream.py
executable file
·138 lines (123 loc) · 5.52 KB
/
git_check_branches_upstream.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python3
# vim:ts=4:sts=4:sw=4:et
#
# Author: Hari Sekhon
# Date: 2016-07-21 16:19:19 +0100 (Thu, 21 Jul 2016)
#
# https://github.com/HariSekhon/DevOps-Python-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn
# and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
"""
Tool to check Git branches have their upstream origin branch set consistently and auto-fix if necessary
Mainly written for my https://github.com/HariSekhon/Dockerfiles repo
which has over 100 branches which get merged, pulled and pushed around
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
#from __future__ import unicode_literals
import os
import re
import sys
import traceback
import git
srcdir = os.path.abspath(os.path.dirname(__file__))
libdir = os.path.join(srcdir, 'pylib')
sys.path.append(libdir)
try:
# pylint: disable=wrong-import-position
from harisekhon.utils import die, ERRORS, find_git_root, log, log_option, uniq_list_ordered, validate_regex
from harisekhon import CLI
except ImportError as _:
print(traceback.format_exc(), end='')
sys.exit(4)
__author__ = 'Hari Sekhon'
__version__ = '0.4'
class GitCheckBranchesUpstream(CLI):
def __init__(self):
# Python 2.x
super(GitCheckBranchesUpstream, self).__init__()
# Python 3.x
# super().__init__()
self.status = "OK"
self.origin = None
self.branch_prefix = None
self.timeout_default = 86400
self.verbose_default = 2
def add_options(self):
self.add_opt('-b', '--branch-prefix', help='Branch prefix regex to check')
self.add_opt('-o', '--origin', help='Origin repo (default: origin)', default='origin')
self.add_opt('-f', '--fix', action='store_true',
help='Set any branches without upstream to corresponding origin/<branch>')
self.add_opt('-F', '--force-fix', action='store_true',
help='Override all branches\' upstreams to track corresponding origin/<branch>')
def run(self):
if not self.args:
self.usage('no git directory args given')
self.origin = self.get_opt('origin')
args = uniq_list_ordered(self.args)
self.branch_prefix = self.get_opt('branch_prefix')
if self.branch_prefix is not None:
validate_regex(self.branch_prefix, 'branch prefix')
self.branch_prefix = re.compile(self.branch_prefix)
for arg in args:
if not os.path.exists(arg):
print("'%s' not found" % arg)
sys.exit(ERRORS['WARNING'])
if os.path.isfile(arg):
log_option('file', arg)
elif os.path.isdir(arg):
log_option('directory', arg)
else:
die("path '%s' could not be determined as either a file or directory" % arg)
for arg in args:
self.check_git_branches_upstream(arg)
if self.status == "OK":
log.info('SUCCESS - All Git branches are tracking the expected upstream origin branches')
else:
log.critical('FAILED')
sys.exit(ERRORS['CRITICAL'])
def check_git_branches_upstream(self, target):
target = os.path.abspath(target)
gitroot = find_git_root(target)
if gitroot is None:
die('Failed to find git root for target {0}'.format(target))
log.debug("finding branches for target '{0}'".format(target))
repo = git.Repo(gitroot)
branches = repo.branches
if self.branch_prefix is not None:
log.debug('restricting to branches matching branch prefix')
branches = [x for x in branches if self.branch_prefix.match(str(x))]
if not branches:
log.error("No branches matching '%s' for target '%s'", self.get_opt('branch_prefix'), target)
self.status = 'NO BRANCHES'
#if log.isEnabledFor(logging.DEBUG):
#log.debug('\n\nbranches for target %s:\n\n%s\n', target, '\n'.join(list(branches)))
for branch in branches:
expected = '{0}/{1}'.format(self.origin, branch)
# have to str() this as it returns an object that will fail equality match otherwise
tracking_branch = str(branch.tracking_branch())
if tracking_branch == expected:
log.info("OK: repo '{0}' branch '{1}' is tracking '{2}'"
.format(gitroot, branch, tracking_branch))
elif self.get_opt('fix') and tracking_branch == 'None':
log.warn("WARN: setting repo '{0}' unconfigured branch '{1}' to track '{2}'"
.format(gitroot, branch, expected))
#print(list(repo.remotes.origin.refs))
branch.set_tracking_branch(git.refs.remote.RemoteReference(repo, 'refs/remotes/' + expected))
elif self.get_opt('force_fix'):
log.warn("WARN: forcibly resetting repo '{0}' branch '{1}' to track '{2}'"
.format(gitroot, branch, expected))
branch.set_tracking_branch(git.refs.remote.RemoteReference(repo, 'refs/remotes/' + expected))
else:
self.status = "ERROR"
log.error("BAD: branch '{0}' is tracking '{1}' (expected '{2}')"
.format(branch, tracking_branch, expected))
if __name__ == '__main__':
GitCheckBranchesUpstream().main()