forked from untangle/ngfw_pkgtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-branch.py
executable file
·117 lines (96 loc) · 3.99 KB
/
create-branch.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
#!/usr/bin/python3
import argparse
import logging
import sys
# relative to cwd
from lib import gitutils, simple_version, repoinfo
# functions
# CL options
parser = argparse.ArgumentParser(description='''Create release branches''')
parser.add_argument('--log-level',
dest='logLevel',
choices=['debug', 'info', 'warning'],
default='warning',
help='level at which to log')
parser.add_argument('--simulate',
dest='simulate',
action='store_true',
default=False,
help='do not push anything (default=push)')
parser.add_argument('--new-version',
dest='new_version',
action='store',
required=False,
default=None,
metavar="NEW_VERSION",
type=simple_version,
help='the new public version for the master branch (x.y)')
parser.add_argument('--branch',
dest='branch',
action='store',
required=True,
default=None,
metavar="BRANCH",
help='the new branch name (needs to start with the product name')
parser.add_argument('--product',
dest='product',
action='store',
choices=('mfw', 'ngfw', 'waf'),
required=True,
default=None,
metavar="PRODUCT",
help='product name')
# main
if __name__ == '__main__':
args = parser.parse_args()
# logging
logging.getLogger().setLevel(getattr(logging, args.logLevel.upper()))
console = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('[%(asctime)s] changelog: %(levelname)-7s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
# go
logging.info("started with %s", " ".join(sys.argv[1:]))
product = args.product
branch = args.branch
version = args.new_version
simulate = args.simulate
if version and not branch.startswith("{}-".format(product)):
logging.error("branch name must start with product name when new version is given")
sys.exit(1)
# iterate over repositories
for repo_info in repoinfo.list_repositories(product):
logging.debug(repo_info)
if repo_info.skip_versioning_entirely:
continue
repo_name = repo_info.name
repo_url = repo_info.git_url
repo_default_branch = repo_info.default_branch
repo, origin = gitutils.get_repo(repo_name, repo_url, branch=repo_default_branch)
if not repo_info.disable_branch_creation:
# checkout new branch
logging.info('creating branch %s', branch)
new_branch = repo.create_head(branch)
new_branch.checkout()
# version resources on new branch
for vr in repo_info.versioned_resources:
if vr.change_on_release_branch:
vr.set_versioning_value(repo, locals())
# push new branch
refspecs = ['{branch}:{branch}'.format(branch=new_branch),]
gitutils.push(origin, refspecs, simulate)
# version resources on master branch
refspecs = set()
if version:
for vr in repo_info.versioned_resources:
if vr.change_on_release_branch:
continue
if repo.head.reference.name != repo_default_branch:
logging.info('on branch %s', repo.head.reference)
logging.info('checking out branch %s', repo_default_branch)
default_branch = repo.heads[repo_default_branch]
default_branch.checkout()
rs = vr.set_versioning_value(repo, locals())
refspecs.update(rs)
if refspecs: # push
gitutils.push(origin, refspecs, simulate)