forked from CyanogenMod/hudson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repopick.py
executable file
·162 lines (141 loc) · 6.31 KB
/
repopick.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python
#
# Copyright (C) 2013 The CyanogenMod Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Run repopick.py -h for a description of this utility.
#
from __future__ import print_function
import sys
import json
import os
import subprocess
import re
import argparse
import textwrap
import textwrap
try:
# For python3
import urllib.request
except ImportError:
# For python2
import imp
import urllib2
urllib = imp.new_module('urllib')
urllib.request = urllib2
# Locate the repo and git binaries
repo_bin = 'repo'
git_bin = 'git'
# Parse the command line
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=textwrap.dedent('''\
repopick.py is a utility to simplify the process of cherry picking
patches from CyanogenMod's Gerrit instance.
Given a list of change numbers, repopick will cd into the project path
and cherry pick the latest patch available.
With the --start-branch argument, the user can specify that a branch
should be created before cherry picking. This is useful for
cherry-picking many patches into a common branch which can be easily
abandoned later (good for testing other's changes.)
The --abandon-first argument, when used in conjuction with the
--start-branch option, will cause repopick to abandon the specified
branch in all repos first before performing any cherry picks.'''))
parser.add_argument('change_number', nargs='+', help='change number to cherry pick')
parser.add_argument('--ignore-missing', action='store_true', help='do not error out if a patch applies to a missing directory')
parser.add_argument('--start-branch', nargs=1, help='start the specified branch before cherry picking')
parser.add_argument('--abandon-first', action='store_true', help='before cherry picking, abandon the branch specified in --start-branch')
parser.add_argument('--quiet', action='store_true', help='print as little as possible')
parser.add_argument('--verbose', action='store_true', help='print extra information to aid in debug')
args = parser.parse_args()
if args.start_branch == None and args.abandon_first:
parser.error('if --abandon-first is set, you must also give the branch name with --start-branch')
def execute_cmd(cmd):
if args.verbose:
print('Executing: ' + cmd)
cmd = cmd.split(' ')
subprocess.call(cmd)
# If --abandon-first is given, abandon the branch before starting
if args.abandon_first:
if not args.quiet:
print('Abandoning branch: %s' % args.start_branch[0])
os.system('%s abandon %s' % (repo_bin, args.start_branch[0]))
if not args.quiet:
print('')
for change in args.change_number:
if not args.quiet:
print('Applying change number %s ...' % change)
# Fetch information about the change from Gerrit's REST API
#
# gerrit returns two lines, a magic string and then valid JSON:
# )]}'
# [ ... valid JSON ... ]
url = 'http://review.cyanogenmod.com/changes/?q=%s&o=CURRENT_REVISION&o=CURRENT_COMMIT&pp=0' % change
if args.verbose:
print('Fetching from: %s\n' % url)
f = urllib.request.urlopen(url)
d = f.read().decode()
# Parse the result
if args.verbose:
print('Result from request:\n' + d)
d = d.split('\n')[1]
d = re.sub(r'\[(.*)\]', r'\1', d)
data = json.loads(d)
# Extract information from the JSON response
project_name = data['project']
change_number = data['_number']
current_revision = data['revisions'][data['current_revision']]
patch_number = current_revision['_number']
fetch_url = current_revision['fetch']['http']['url']
fetch_ref = current_revision['fetch']['http']['ref']
author_name = current_revision['commit']['author']['name']
author_email = current_revision['commit']['author']['email']
author_date = current_revision['commit']['author']['date']
committer_name = current_revision['commit']['committer']['name']
committer_email = current_revision['commit']['committer']['email']
committer_date = current_revision['commit']['committer']['date']
subject = current_revision['commit']['subject']
# Get the list of projects that repo knows about
# - convert the project name to a project path
plist = subprocess.Popen(["repo","list"], stdout=subprocess.PIPE)
while(True):
retcode = plist.poll()
pline = plist.stdout.readline().rstrip()
ppaths = re.split('\s*:\s*', pline.decode())
if ppaths[1] == project_name:
project_path = ppaths[0]
break
if(retcode is not None):
break
# Check that the project path exists
if not os.path.isdir(project_path):
if args.ignore_missing:
sys.stderr.write('WARNING: Skipping %d since there is no project directory: %s' % (change_number, project_path))
next;
else:
sys.stderr.write('ERROR: For %d, there is no project directory: %s' % (change_number, project_path))
sys.exit(1)
# If --start-branch is given, create the branch (more than once per path is okay; repo ignores gracefully)
if args.start_branch:
os.system('%s start %s %s' % (repo_bin, args.start_branch[0], project_path))
# Print out some useful info
if not args.quiet:
print('--> Subject: "%s"' % subject)
print('--> Project path: %s' % project_path)
print('--> Change number: %d (Patch Set %d)' % (change_number, patch_number))
print('--> Author: %s <%s> %s' % (author_name, author_email, author_date))
print('--> Committer: %s <%s> %s' % (committer_name, committer_email, committer_date))
# Perform the cherry-pick
os.system('cd %s && git fetch %s %s && git cherry-pick FETCH_HEAD' % (project_path, fetch_url, fetch_ref))
if not args.quiet:
print('')