Blame


1 26b4bea8 2011-08-02 rsc # coding=utf-8
2 26b4bea8 2011-08-02 rsc # (The line above is necessary so that I can use 世界 in the
3 26b4bea8 2011-08-02 rsc # *comment* below without Python getting all bent out of shape.)
4 26b4bea8 2011-08-02 rsc
5 ef27bfa4 2010-01-12 rsc # Copyright 2007-2009 Google Inc.
6 ef27bfa4 2010-01-12 rsc #
7 ef27bfa4 2010-01-12 rsc # Licensed under the Apache License, Version 2.0 (the "License");
8 ef27bfa4 2010-01-12 rsc # you may not use this file except in compliance with the License.
9 ef27bfa4 2010-01-12 rsc # You may obtain a copy of the License at
10 ef27bfa4 2010-01-12 rsc #
11 26b4bea8 2011-08-02 rsc # http://www.apache.org/licenses/LICENSE-2.0
12 ef27bfa4 2010-01-12 rsc #
13 ef27bfa4 2010-01-12 rsc # Unless required by applicable law or agreed to in writing, software
14 ef27bfa4 2010-01-12 rsc # distributed under the License is distributed on an "AS IS" BASIS,
15 ef27bfa4 2010-01-12 rsc # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ef27bfa4 2010-01-12 rsc # See the License for the specific language governing permissions and
17 ef27bfa4 2010-01-12 rsc # limitations under the License.
18 ef27bfa4 2010-01-12 rsc
19 ef27bfa4 2010-01-12 rsc '''Mercurial interface to codereview.appspot.com.
20 ef27bfa4 2010-01-12 rsc
21 ef27bfa4 2010-01-12 rsc To configure, set the following options in
22 ef27bfa4 2010-01-12 rsc your repository's .hg/hgrc file.
23 ef27bfa4 2010-01-12 rsc
24 ef27bfa4 2010-01-12 rsc [extensions]
25 5c934375 2012-01-20 rsc codereview = /path/to/codereview.py
26 ef27bfa4 2010-01-12 rsc
27 ef27bfa4 2010-01-12 rsc [codereview]
28 ef27bfa4 2010-01-12 rsc server = codereview.appspot.com
29 ef27bfa4 2010-01-12 rsc
30 ef27bfa4 2010-01-12 rsc The server should be running Rietveld; see http://code.google.com/p/rietveld/.
31 ef27bfa4 2010-01-12 rsc
32 ef27bfa4 2010-01-12 rsc In addition to the new commands, this extension introduces
33 ef27bfa4 2010-01-12 rsc the file pattern syntax @nnnnnn, where nnnnnn is a change list
34 ef27bfa4 2010-01-12 rsc number, to mean the files included in that change list, which
35 ef27bfa4 2010-01-12 rsc must be associated with the current client.
36 ef27bfa4 2010-01-12 rsc
37 ef27bfa4 2010-01-12 rsc For example, if change 123456 contains the files x.go and y.go,
38 ef27bfa4 2010-01-12 rsc "hg diff @123456" is equivalent to"hg diff x.go y.go".
39 ef27bfa4 2010-01-12 rsc '''
40 ef27bfa4 2010-01-12 rsc
41 5c934375 2012-01-20 rsc import sys
42 5c934375 2012-01-20 rsc
43 5c934375 2012-01-20 rsc if __name__ == "__main__":
44 5c934375 2012-01-20 rsc print >>sys.stderr, "This is a Mercurial extension and should not be invoked directly."
45 5c934375 2012-01-20 rsc sys.exit(2)
46 5c934375 2012-01-20 rsc
47 5c934375 2012-01-20 rsc # We require Python 2.6 for the json package.
48 5c934375 2012-01-20 rsc if sys.version < '2.6':
49 5c934375 2012-01-20 rsc print >>sys.stderr, "The codereview extension requires Python 2.6 or newer."
50 5c934375 2012-01-20 rsc print >>sys.stderr, "You are running Python " + sys.version
51 5c934375 2012-01-20 rsc sys.exit(2)
52 5c934375 2012-01-20 rsc
53 5c934375 2012-01-20 rsc import json
54 5c934375 2012-01-20 rsc import os
55 5c934375 2012-01-20 rsc import re
56 ef27bfa4 2010-01-12 rsc import stat
57 ef27bfa4 2010-01-12 rsc import subprocess
58 ef27bfa4 2010-01-12 rsc import threading
59 5c934375 2012-01-20 rsc import time
60 26b4bea8 2011-08-02 rsc
61 5c934375 2012-01-20 rsc from mercurial import commands as hg_commands
62 5c934375 2012-01-20 rsc from mercurial import util as hg_util
63 ef27bfa4 2010-01-12 rsc
64 5c934375 2012-01-20 rsc defaultcc = None
65 5c934375 2012-01-20 rsc codereview_disabled = None
66 5c934375 2012-01-20 rsc real_rollback = None
67 5c934375 2012-01-20 rsc releaseBranch = None
68 5c934375 2012-01-20 rsc server = "codereview.appspot.com"
69 5c934375 2012-01-20 rsc server_url_base = None
70 ef27bfa4 2010-01-12 rsc
71 5c934375 2012-01-20 rsc #######################################################################
72 5c934375 2012-01-20 rsc # Normally I would split this into multiple files, but it simplifies
73 5c934375 2012-01-20 rsc # import path headaches to keep it all in one file. Sorry.
74 5c934375 2012-01-20 rsc # The different parts of the file are separated by banners like this one.
75 a06877af 2010-08-05 rsc
76 5c934375 2012-01-20 rsc #######################################################################
77 5c934375 2012-01-20 rsc # Helpers
78 ef27bfa4 2010-01-12 rsc
79 5c934375 2012-01-20 rsc def RelativePath(path, cwd):
80 5c934375 2012-01-20 rsc n = len(cwd)
81 5c934375 2012-01-20 rsc if path.startswith(cwd) and path[n] == '/':
82 5c934375 2012-01-20 rsc return path[n+1:]
83 5c934375 2012-01-20 rsc return path
84 ef27bfa4 2010-01-12 rsc
85 5c934375 2012-01-20 rsc def Sub(l1, l2):
86 5c934375 2012-01-20 rsc return [l for l in l1 if l not in l2]
87 ef27bfa4 2010-01-12 rsc
88 5c934375 2012-01-20 rsc def Add(l1, l2):
89 5c934375 2012-01-20 rsc l = l1 + Sub(l2, l1)
90 5c934375 2012-01-20 rsc l.sort()
91 5c934375 2012-01-20 rsc return l
92 ef27bfa4 2010-01-12 rsc
93 5c934375 2012-01-20 rsc def Intersect(l1, l2):
94 5c934375 2012-01-20 rsc return [l for l in l1 if l in l2]
95 ef27bfa4 2010-01-12 rsc
96 ef27bfa4 2010-01-12 rsc #######################################################################
97 26b4bea8 2011-08-02 rsc # RE: UNICODE STRING HANDLING
98 26b4bea8 2011-08-02 rsc #
99 26b4bea8 2011-08-02 rsc # Python distinguishes between the str (string of bytes)
100 26b4bea8 2011-08-02 rsc # and unicode (string of code points) types. Most operations
101 26b4bea8 2011-08-02 rsc # work on either one just fine, but some (like regexp matching)
102 26b4bea8 2011-08-02 rsc # require unicode, and others (like write) require str.
103 26b4bea8 2011-08-02 rsc #
104 26b4bea8 2011-08-02 rsc # As befits the language, Python hides the distinction between
105 26b4bea8 2011-08-02 rsc # unicode and str by converting between them silently, but
106 26b4bea8 2011-08-02 rsc # *only* if all the bytes/code points involved are 7-bit ASCII.
107 26b4bea8 2011-08-02 rsc # This means that if you're not careful, your program works
108 26b4bea8 2011-08-02 rsc # fine on "hello, world" and fails on "hello, 世界". And of course,
109 26b4bea8 2011-08-02 rsc # the obvious way to be careful - use static types - is unavailable.
110 26b4bea8 2011-08-02 rsc # So the only way is trial and error to find where to put explicit
111 26b4bea8 2011-08-02 rsc # conversions.
112 26b4bea8 2011-08-02 rsc #
113 26b4bea8 2011-08-02 rsc # Because more functions do implicit conversion to str (string of bytes)
114 26b4bea8 2011-08-02 rsc # than do implicit conversion to unicode (string of code points),
115 26b4bea8 2011-08-02 rsc # the convention in this module is to represent all text as str,
116 26b4bea8 2011-08-02 rsc # converting to unicode only when calling a unicode-only function
117 26b4bea8 2011-08-02 rsc # and then converting back to str as soon as possible.
118 26b4bea8 2011-08-02 rsc
119 26b4bea8 2011-08-02 rsc def typecheck(s, t):
120 26b4bea8 2011-08-02 rsc if type(s) != t:
121 5c934375 2012-01-20 rsc raise hg_util.Abort("type check failed: %s has type %s != %s" % (repr(s), type(s), t))
122 26b4bea8 2011-08-02 rsc
123 26b4bea8 2011-08-02 rsc # If we have to pass unicode instead of str, ustr does that conversion clearly.
124 26b4bea8 2011-08-02 rsc def ustr(s):
125 26b4bea8 2011-08-02 rsc typecheck(s, str)
126 26b4bea8 2011-08-02 rsc return s.decode("utf-8")
127 26b4bea8 2011-08-02 rsc
128 26b4bea8 2011-08-02 rsc # Even with those, Mercurial still sometimes turns unicode into str
129 26b4bea8 2011-08-02 rsc # and then tries to use it as ascii. Change Mercurial's default.
130 26b4bea8 2011-08-02 rsc def set_mercurial_encoding_to_utf8():
131 26b4bea8 2011-08-02 rsc from mercurial import encoding
132 26b4bea8 2011-08-02 rsc encoding.encoding = 'utf-8'
133 26b4bea8 2011-08-02 rsc
134 26b4bea8 2011-08-02 rsc set_mercurial_encoding_to_utf8()
135 26b4bea8 2011-08-02 rsc
136 26b4bea8 2011-08-02 rsc # Even with those we still run into problems.
137 26b4bea8 2011-08-02 rsc # I tried to do things by the book but could not convince
138 26b4bea8 2011-08-02 rsc # Mercurial to let me check in a change with UTF-8 in the
139 26b4bea8 2011-08-02 rsc # CL description or author field, no matter how many conversions
140 26b4bea8 2011-08-02 rsc # between str and unicode I inserted and despite changing the
141 26b4bea8 2011-08-02 rsc # default encoding. I'm tired of this game, so set the default
142 26b4bea8 2011-08-02 rsc # encoding for all of Python to 'utf-8', not 'ascii'.
143 26b4bea8 2011-08-02 rsc def default_to_utf8():
144 26b4bea8 2011-08-02 rsc import sys
145 338bd4bb 2011-11-08 rsc stdout, __stdout__ = sys.stdout, sys.__stdout__
146 26b4bea8 2011-08-02 rsc reload(sys) # site.py deleted setdefaultencoding; get it back
147 338bd4bb 2011-11-08 rsc sys.stdout, sys.__stdout__ = stdout, __stdout__
148 26b4bea8 2011-08-02 rsc sys.setdefaultencoding('utf-8')
149 26b4bea8 2011-08-02 rsc
150 26b4bea8 2011-08-02 rsc default_to_utf8()
151 26b4bea8 2011-08-02 rsc
152 26b4bea8 2011-08-02 rsc #######################################################################
153 5c934375 2012-01-20 rsc # Status printer for long-running commands
154 5c934375 2012-01-20 rsc
155 5c934375 2012-01-20 rsc global_status = None
156 5c934375 2012-01-20 rsc
157 5c934375 2012-01-20 rsc def set_status(s):
158 5c934375 2012-01-20 rsc # print >>sys.stderr, "\t", time.asctime(), s
159 5c934375 2012-01-20 rsc global global_status
160 5c934375 2012-01-20 rsc global_status = s
161 5c934375 2012-01-20 rsc
162 5c934375 2012-01-20 rsc class StatusThread(threading.Thread):
163 5c934375 2012-01-20 rsc def __init__(self):
164 5c934375 2012-01-20 rsc threading.Thread.__init__(self)
165 5c934375 2012-01-20 rsc def run(self):
166 5c934375 2012-01-20 rsc # pause a reasonable amount of time before
167 5c934375 2012-01-20 rsc # starting to display status messages, so that
168 5c934375 2012-01-20 rsc # most hg commands won't ever see them.
169 5c934375 2012-01-20 rsc time.sleep(30)
170 5c934375 2012-01-20 rsc
171 5c934375 2012-01-20 rsc # now show status every 15 seconds
172 5c934375 2012-01-20 rsc while True:
173 5c934375 2012-01-20 rsc time.sleep(15 - time.time() % 15)
174 5c934375 2012-01-20 rsc s = global_status
175 5c934375 2012-01-20 rsc if s is None:
176 5c934375 2012-01-20 rsc continue
177 5c934375 2012-01-20 rsc if s == "":
178 5c934375 2012-01-20 rsc s = "(unknown status)"
179 5c934375 2012-01-20 rsc print >>sys.stderr, time.asctime(), s
180 5c934375 2012-01-20 rsc
181 5c934375 2012-01-20 rsc def start_status_thread():
182 5c934375 2012-01-20 rsc t = StatusThread()
183 5c934375 2012-01-20 rsc t.setDaemon(True) # allowed to exit if t is still running
184 5c934375 2012-01-20 rsc t.start()
185 5c934375 2012-01-20 rsc
186 5c934375 2012-01-20 rsc #######################################################################
187 ef27bfa4 2010-01-12 rsc # Change list parsing.
188 ef27bfa4 2010-01-12 rsc #
189 ef27bfa4 2010-01-12 rsc # Change lists are stored in .hg/codereview/cl.nnnnnn
190 ef27bfa4 2010-01-12 rsc # where nnnnnn is the number assigned by the code review server.
191 ef27bfa4 2010-01-12 rsc # Most data about a change list is stored on the code review server
192 ef27bfa4 2010-01-12 rsc # too: the description, reviewer, and cc list are all stored there.
193 ef27bfa4 2010-01-12 rsc # The only thing in the cl.nnnnnn file is the list of relevant files.
194 ef27bfa4 2010-01-12 rsc # Also, the existence of the cl.nnnnnn file marks this repository
195 ef27bfa4 2010-01-12 rsc # as the one where the change list lives.
196 ef27bfa4 2010-01-12 rsc
197 ef27bfa4 2010-01-12 rsc emptydiff = """Index: ~rietveld~placeholder~
198 ef27bfa4 2010-01-12 rsc ===================================================================
199 ef27bfa4 2010-01-12 rsc diff --git a/~rietveld~placeholder~ b/~rietveld~placeholder~
200 ef27bfa4 2010-01-12 rsc new file mode 100644
201 ef27bfa4 2010-01-12 rsc """
202 ef27bfa4 2010-01-12 rsc
203 ef27bfa4 2010-01-12 rsc class CL(object):
204 ef27bfa4 2010-01-12 rsc def __init__(self, name):
205 26b4bea8 2011-08-02 rsc typecheck(name, str)
206 ef27bfa4 2010-01-12 rsc self.name = name
207 ef27bfa4 2010-01-12 rsc self.desc = ''
208 ef27bfa4 2010-01-12 rsc self.files = []
209 ef27bfa4 2010-01-12 rsc self.reviewer = []
210 ef27bfa4 2010-01-12 rsc self.cc = []
211 ef27bfa4 2010-01-12 rsc self.url = ''
212 ef27bfa4 2010-01-12 rsc self.local = False
213 ef27bfa4 2010-01-12 rsc self.web = False
214 ef27bfa4 2010-01-12 rsc self.copied_from = None # None means current user
215 ef27bfa4 2010-01-12 rsc self.mailed = False
216 26b4bea8 2011-08-02 rsc self.private = False
217 338bd4bb 2011-11-08 rsc self.lgtm = []
218 ef27bfa4 2010-01-12 rsc
219 ef27bfa4 2010-01-12 rsc def DiskText(self):
220 ef27bfa4 2010-01-12 rsc cl = self
221 ef27bfa4 2010-01-12 rsc s = ""
222 ef27bfa4 2010-01-12 rsc if cl.copied_from:
223 ef27bfa4 2010-01-12 rsc s += "Author: " + cl.copied_from + "\n\n"
224 26b4bea8 2011-08-02 rsc if cl.private:
225 26b4bea8 2011-08-02 rsc s += "Private: " + str(self.private) + "\n"
226 ef27bfa4 2010-01-12 rsc s += "Mailed: " + str(self.mailed) + "\n"
227 ef27bfa4 2010-01-12 rsc s += "Description:\n"
228 ef27bfa4 2010-01-12 rsc s += Indent(cl.desc, "\t")
229 ef27bfa4 2010-01-12 rsc s += "Files:\n"
230 ef27bfa4 2010-01-12 rsc for f in cl.files:
231 ef27bfa4 2010-01-12 rsc s += "\t" + f + "\n"
232 26b4bea8 2011-08-02 rsc typecheck(s, str)
233 ef27bfa4 2010-01-12 rsc return s
234 ef27bfa4 2010-01-12 rsc
235 ef27bfa4 2010-01-12 rsc def EditorText(self):
236 ef27bfa4 2010-01-12 rsc cl = self
237 ef27bfa4 2010-01-12 rsc s = _change_prolog
238 ef27bfa4 2010-01-12 rsc s += "\n"
239 ef27bfa4 2010-01-12 rsc if cl.copied_from:
240 ef27bfa4 2010-01-12 rsc s += "Author: " + cl.copied_from + "\n"
241 ef27bfa4 2010-01-12 rsc if cl.url != '':
242 ef27bfa4 2010-01-12 rsc s += 'URL: ' + cl.url + ' # cannot edit\n\n'
243 26b4bea8 2011-08-02 rsc if cl.private:
244 26b4bea8 2011-08-02 rsc s += "Private: True\n"
245 ef27bfa4 2010-01-12 rsc s += "Reviewer: " + JoinComma(cl.reviewer) + "\n"
246 ef27bfa4 2010-01-12 rsc s += "CC: " + JoinComma(cl.cc) + "\n"
247 ef27bfa4 2010-01-12 rsc s += "\n"
248 ef27bfa4 2010-01-12 rsc s += "Description:\n"
249 ef27bfa4 2010-01-12 rsc if cl.desc == '':
250 ef27bfa4 2010-01-12 rsc s += "\t<enter description here>\n"
251 ef27bfa4 2010-01-12 rsc else:
252 ef27bfa4 2010-01-12 rsc s += Indent(cl.desc, "\t")
253 ef27bfa4 2010-01-12 rsc s += "\n"
254 ef27bfa4 2010-01-12 rsc if cl.local or cl.name == "new":
255 ef27bfa4 2010-01-12 rsc s += "Files:\n"
256 ef27bfa4 2010-01-12 rsc for f in cl.files:
257 ef27bfa4 2010-01-12 rsc s += "\t" + f + "\n"
258 ef27bfa4 2010-01-12 rsc s += "\n"
259 26b4bea8 2011-08-02 rsc typecheck(s, str)
260 ef27bfa4 2010-01-12 rsc return s
261 ef27bfa4 2010-01-12 rsc
262 5c934375 2012-01-20 rsc def PendingText(self, quick=False):
263 ef27bfa4 2010-01-12 rsc cl = self
264 ef27bfa4 2010-01-12 rsc s = cl.name + ":" + "\n"
265 ef27bfa4 2010-01-12 rsc s += Indent(cl.desc, "\t")
266 ef27bfa4 2010-01-12 rsc s += "\n"
267 ef27bfa4 2010-01-12 rsc if cl.copied_from:
268 ef27bfa4 2010-01-12 rsc s += "\tAuthor: " + cl.copied_from + "\n"
269 5c934375 2012-01-20 rsc if not quick:
270 5c934375 2012-01-20 rsc s += "\tReviewer: " + JoinComma(cl.reviewer) + "\n"
271 5c934375 2012-01-20 rsc for (who, line) in cl.lgtm:
272 5c934375 2012-01-20 rsc s += "\t\t" + who + ": " + line + "\n"
273 5c934375 2012-01-20 rsc s += "\tCC: " + JoinComma(cl.cc) + "\n"
274 ef27bfa4 2010-01-12 rsc s += "\tFiles:\n"
275 ef27bfa4 2010-01-12 rsc for f in cl.files:
276 ef27bfa4 2010-01-12 rsc s += "\t\t" + f + "\n"
277 26b4bea8 2011-08-02 rsc typecheck(s, str)
278 ef27bfa4 2010-01-12 rsc return s
279 ef27bfa4 2010-01-12 rsc
280 ef27bfa4 2010-01-12 rsc def Flush(self, ui, repo):
281 ef27bfa4 2010-01-12 rsc if self.name == "new":
282 26b4bea8 2011-08-02 rsc self.Upload(ui, repo, gofmt_just_warn=True, creating=True)
283 ef27bfa4 2010-01-12 rsc dir = CodeReviewDir(ui, repo)
284 ef27bfa4 2010-01-12 rsc path = dir + '/cl.' + self.name
285 ef27bfa4 2010-01-12 rsc f = open(path+'!', "w")
286 ef27bfa4 2010-01-12 rsc f.write(self.DiskText())
287 ef27bfa4 2010-01-12 rsc f.close()
288 ef27bfa4 2010-01-12 rsc if sys.platform == "win32" and os.path.isfile(path):
289 ef27bfa4 2010-01-12 rsc os.remove(path)
290 ef27bfa4 2010-01-12 rsc os.rename(path+'!', path)
291 ef27bfa4 2010-01-12 rsc if self.web and not self.copied_from:
292 ef27bfa4 2010-01-12 rsc EditDesc(self.name, desc=self.desc,
293 26b4bea8 2011-08-02 rsc reviewers=JoinComma(self.reviewer), cc=JoinComma(self.cc),
294 26b4bea8 2011-08-02 rsc private=self.private)
295 ef27bfa4 2010-01-12 rsc
296 ef27bfa4 2010-01-12 rsc def Delete(self, ui, repo):
297 ef27bfa4 2010-01-12 rsc dir = CodeReviewDir(ui, repo)
298 ef27bfa4 2010-01-12 rsc os.unlink(dir + "/cl." + self.name)
299 ef27bfa4 2010-01-12 rsc
300 ef27bfa4 2010-01-12 rsc def Subject(self):
301 ef27bfa4 2010-01-12 rsc s = line1(self.desc)
302 ef27bfa4 2010-01-12 rsc if len(s) > 60:
303 ef27bfa4 2010-01-12 rsc s = s[0:55] + "..."
304 ef27bfa4 2010-01-12 rsc if self.name != "new":
305 ef27bfa4 2010-01-12 rsc s = "code review %s: %s" % (self.name, s)
306 26b4bea8 2011-08-02 rsc typecheck(s, str)
307 ef27bfa4 2010-01-12 rsc return s
308 ef27bfa4 2010-01-12 rsc
309 26b4bea8 2011-08-02 rsc def Upload(self, ui, repo, send_mail=False, gofmt=True, gofmt_just_warn=False, creating=False, quiet=False):
310 26b4bea8 2011-08-02 rsc if not self.files and not creating:
311 ef27bfa4 2010-01-12 rsc ui.warn("no files in change list\n")
312 ef27bfa4 2010-01-12 rsc if ui.configbool("codereview", "force_gofmt", True) and gofmt:
313 26b4bea8 2011-08-02 rsc CheckFormat(ui, repo, self.files, just_warn=gofmt_just_warn)
314 26b4bea8 2011-08-02 rsc set_status("uploading CL metadata + diffs")
315 ef27bfa4 2010-01-12 rsc os.chdir(repo.root)
316 ef27bfa4 2010-01-12 rsc form_fields = [
317 ef27bfa4 2010-01-12 rsc ("content_upload", "1"),
318 ef27bfa4 2010-01-12 rsc ("reviewers", JoinComma(self.reviewer)),
319 ef27bfa4 2010-01-12 rsc ("cc", JoinComma(self.cc)),
320 ef27bfa4 2010-01-12 rsc ("description", self.desc),
321 ef27bfa4 2010-01-12 rsc ("base_hashes", ""),
322 ef27bfa4 2010-01-12 rsc ]
323 ef27bfa4 2010-01-12 rsc
324 ef27bfa4 2010-01-12 rsc if self.name != "new":
325 ef27bfa4 2010-01-12 rsc form_fields.append(("issue", self.name))
326 ef27bfa4 2010-01-12 rsc vcs = None
327 26b4bea8 2011-08-02 rsc # We do not include files when creating the issue,
328 26b4bea8 2011-08-02 rsc # because we want the patch sets to record the repository
329 26b4bea8 2011-08-02 rsc # and base revision they are diffs against. We use the patch
330 26b4bea8 2011-08-02 rsc # set message for that purpose, but there is no message with
331 26b4bea8 2011-08-02 rsc # the first patch set. Instead the message gets used as the
332 26b4bea8 2011-08-02 rsc # new CL's overall subject. So omit the diffs when creating
333 26b4bea8 2011-08-02 rsc # and then we'll run an immediate upload.
334 26b4bea8 2011-08-02 rsc # This has the effect that every CL begins with an empty "Patch set 1".
335 26b4bea8 2011-08-02 rsc if self.files and not creating:
336 26b4bea8 2011-08-02 rsc vcs = MercurialVCS(upload_options, ui, repo)
337 ef27bfa4 2010-01-12 rsc data = vcs.GenerateDiff(self.files)
338 ef27bfa4 2010-01-12 rsc files = vcs.GetBaseFiles(data)
339 ef27bfa4 2010-01-12 rsc if len(data) > MAX_UPLOAD_SIZE:
340 ef27bfa4 2010-01-12 rsc uploaded_diff_file = []
341 ef27bfa4 2010-01-12 rsc form_fields.append(("separate_patches", "1"))
342 ef27bfa4 2010-01-12 rsc else:
343 ef27bfa4 2010-01-12 rsc uploaded_diff_file = [("data", "data.diff", data)]
344 ef27bfa4 2010-01-12 rsc else:
345 ef27bfa4 2010-01-12 rsc uploaded_diff_file = [("data", "data.diff", emptydiff)]
346 26b4bea8 2011-08-02 rsc
347 26b4bea8 2011-08-02 rsc if vcs and self.name != "new":
348 5c934375 2012-01-20 rsc form_fields.append(("subject", "diff -r " + vcs.base_rev + " " + ui.expandpath("default")))
349 26b4bea8 2011-08-02 rsc else:
350 26b4bea8 2011-08-02 rsc # First upload sets the subject for the CL itself.
351 26b4bea8 2011-08-02 rsc form_fields.append(("subject", self.Subject()))
352 ef27bfa4 2010-01-12 rsc ctype, body = EncodeMultipartFormData(form_fields, uploaded_diff_file)
353 ef27bfa4 2010-01-12 rsc response_body = MySend("/upload", body, content_type=ctype)
354 ef27bfa4 2010-01-12 rsc patchset = None
355 ef27bfa4 2010-01-12 rsc msg = response_body
356 ef27bfa4 2010-01-12 rsc lines = msg.splitlines()
357 ef27bfa4 2010-01-12 rsc if len(lines) >= 2:
358 ef27bfa4 2010-01-12 rsc msg = lines[0]
359 ef27bfa4 2010-01-12 rsc patchset = lines[1].strip()
360 ef27bfa4 2010-01-12 rsc patches = [x.split(" ", 1) for x in lines[2:]]
361 26b4bea8 2011-08-02 rsc if response_body.startswith("Issue updated.") and quiet:
362 26b4bea8 2011-08-02 rsc pass
363 26b4bea8 2011-08-02 rsc else:
364 26b4bea8 2011-08-02 rsc ui.status(msg + "\n")
365 26b4bea8 2011-08-02 rsc set_status("uploaded CL metadata + diffs")
366 ef27bfa4 2010-01-12 rsc if not response_body.startswith("Issue created.") and not response_body.startswith("Issue updated."):
367 5c934375 2012-01-20 rsc raise hg_util.Abort("failed to update issue: " + response_body)
368 ef27bfa4 2010-01-12 rsc issue = msg[msg.rfind("/")+1:]
369 ef27bfa4 2010-01-12 rsc self.name = issue
370 ef27bfa4 2010-01-12 rsc if not self.url:
371 ef27bfa4 2010-01-12 rsc self.url = server_url_base + self.name
372 ef27bfa4 2010-01-12 rsc if not uploaded_diff_file:
373 26b4bea8 2011-08-02 rsc set_status("uploading patches")
374 ef27bfa4 2010-01-12 rsc patches = UploadSeparatePatches(issue, rpc, patchset, data, upload_options)
375 ef27bfa4 2010-01-12 rsc if vcs:
376 26b4bea8 2011-08-02 rsc set_status("uploading base files")
377 ef27bfa4 2010-01-12 rsc vcs.UploadBaseFiles(issue, rpc, patches, patchset, upload_options, files)
378 ef27bfa4 2010-01-12 rsc if send_mail:
379 26b4bea8 2011-08-02 rsc set_status("sending mail")
380 ef27bfa4 2010-01-12 rsc MySend("/" + issue + "/mail", payload="")
381 ef27bfa4 2010-01-12 rsc self.web = True
382 26b4bea8 2011-08-02 rsc set_status("flushing changes to disk")
383 ef27bfa4 2010-01-12 rsc self.Flush(ui, repo)
384 ef27bfa4 2010-01-12 rsc return
385 ef27bfa4 2010-01-12 rsc
386 26b4bea8 2011-08-02 rsc def Mail(self, ui, repo):
387 ef27bfa4 2010-01-12 rsc pmsg = "Hello " + JoinComma(self.reviewer)
388 ef27bfa4 2010-01-12 rsc if self.cc:
389 ef27bfa4 2010-01-12 rsc pmsg += " (cc: %s)" % (', '.join(self.cc),)
390 ef27bfa4 2010-01-12 rsc pmsg += ",\n"
391 ef27bfa4 2010-01-12 rsc pmsg += "\n"
392 5c934375 2012-01-20 rsc repourl = ui.expandpath("default")
393 ef27bfa4 2010-01-12 rsc if not self.mailed:
394 26b4bea8 2011-08-02 rsc pmsg += "I'd like you to review this change to\n" + repourl + "\n"
395 ef27bfa4 2010-01-12 rsc else:
396 ef27bfa4 2010-01-12 rsc pmsg += "Please take another look.\n"
397 26b4bea8 2011-08-02 rsc typecheck(pmsg, str)
398 ef27bfa4 2010-01-12 rsc PostMessage(ui, self.name, pmsg, subject=self.Subject())
399 ef27bfa4 2010-01-12 rsc self.mailed = True
400 ef27bfa4 2010-01-12 rsc self.Flush(ui, repo)
401 ef27bfa4 2010-01-12 rsc
402 ef27bfa4 2010-01-12 rsc def GoodCLName(name):
403 26b4bea8 2011-08-02 rsc typecheck(name, str)
404 ef27bfa4 2010-01-12 rsc return re.match("^[0-9]+$", name)
405 ef27bfa4 2010-01-12 rsc
406 ef27bfa4 2010-01-12 rsc def ParseCL(text, name):
407 26b4bea8 2011-08-02 rsc typecheck(text, str)
408 26b4bea8 2011-08-02 rsc typecheck(name, str)
409 ef27bfa4 2010-01-12 rsc sname = None
410 ef27bfa4 2010-01-12 rsc lineno = 0
411 ef27bfa4 2010-01-12 rsc sections = {
412 ef27bfa4 2010-01-12 rsc 'Author': '',
413 ef27bfa4 2010-01-12 rsc 'Description': '',
414 ef27bfa4 2010-01-12 rsc 'Files': '',
415 ef27bfa4 2010-01-12 rsc 'URL': '',
416 ef27bfa4 2010-01-12 rsc 'Reviewer': '',
417 ef27bfa4 2010-01-12 rsc 'CC': '',
418 ef27bfa4 2010-01-12 rsc 'Mailed': '',
419 26b4bea8 2011-08-02 rsc 'Private': '',
420 ef27bfa4 2010-01-12 rsc }
421 ef27bfa4 2010-01-12 rsc for line in text.split('\n'):
422 ef27bfa4 2010-01-12 rsc lineno += 1
423 ef27bfa4 2010-01-12 rsc line = line.rstrip()
424 ef27bfa4 2010-01-12 rsc if line != '' and line[0] == '#':
425 ef27bfa4 2010-01-12 rsc continue
426 ef27bfa4 2010-01-12 rsc if line == '' or line[0] == ' ' or line[0] == '\t':
427 ef27bfa4 2010-01-12 rsc if sname == None and line != '':
428 ef27bfa4 2010-01-12 rsc return None, lineno, 'text outside section'
429 ef27bfa4 2010-01-12 rsc if sname != None:
430 ef27bfa4 2010-01-12 rsc sections[sname] += line + '\n'
431 ef27bfa4 2010-01-12 rsc continue
432 ef27bfa4 2010-01-12 rsc p = line.find(':')
433 ef27bfa4 2010-01-12 rsc if p >= 0:
434 ef27bfa4 2010-01-12 rsc s, val = line[:p].strip(), line[p+1:].strip()
435 ef27bfa4 2010-01-12 rsc if s in sections:
436 ef27bfa4 2010-01-12 rsc sname = s
437 ef27bfa4 2010-01-12 rsc if val != '':
438 ef27bfa4 2010-01-12 rsc sections[sname] += val + '\n'
439 ef27bfa4 2010-01-12 rsc continue
440 ef27bfa4 2010-01-12 rsc return None, lineno, 'malformed section header'
441 ef27bfa4 2010-01-12 rsc
442 ef27bfa4 2010-01-12 rsc for k in sections:
443 ef27bfa4 2010-01-12 rsc sections[k] = StripCommon(sections[k]).rstrip()
444 ef27bfa4 2010-01-12 rsc
445 ef27bfa4 2010-01-12 rsc cl = CL(name)
446 ef27bfa4 2010-01-12 rsc if sections['Author']:
447 ef27bfa4 2010-01-12 rsc cl.copied_from = sections['Author']
448 ef27bfa4 2010-01-12 rsc cl.desc = sections['Description']
449 ef27bfa4 2010-01-12 rsc for line in sections['Files'].split('\n'):
450 ef27bfa4 2010-01-12 rsc i = line.find('#')
451 ef27bfa4 2010-01-12 rsc if i >= 0:
452 ef27bfa4 2010-01-12 rsc line = line[0:i].rstrip()
453 26b4bea8 2011-08-02 rsc line = line.strip()
454 ef27bfa4 2010-01-12 rsc if line == '':
455 ef27bfa4 2010-01-12 rsc continue
456 ef27bfa4 2010-01-12 rsc cl.files.append(line)
457 ef27bfa4 2010-01-12 rsc cl.reviewer = SplitCommaSpace(sections['Reviewer'])
458 ef27bfa4 2010-01-12 rsc cl.cc = SplitCommaSpace(sections['CC'])
459 ef27bfa4 2010-01-12 rsc cl.url = sections['URL']
460 ef27bfa4 2010-01-12 rsc if sections['Mailed'] != 'False':
461 ef27bfa4 2010-01-12 rsc # Odd default, but avoids spurious mailings when
462 ef27bfa4 2010-01-12 rsc # reading old CLs that do not have a Mailed: line.
463 26b4bea8 2011-08-02 rsc # CLs created with this update will always have
464 ef27bfa4 2010-01-12 rsc # Mailed: False on disk.
465 ef27bfa4 2010-01-12 rsc cl.mailed = True
466 26b4bea8 2011-08-02 rsc if sections['Private'] in ('True', 'true', 'Yes', 'yes'):
467 26b4bea8 2011-08-02 rsc cl.private = True
468 ef27bfa4 2010-01-12 rsc if cl.desc == '<enter description here>':
469 ef27bfa4 2010-01-12 rsc cl.desc = ''
470 ef27bfa4 2010-01-12 rsc return cl, 0, ''
471 ef27bfa4 2010-01-12 rsc
472 ef27bfa4 2010-01-12 rsc def SplitCommaSpace(s):
473 26b4bea8 2011-08-02 rsc typecheck(s, str)
474 a06877af 2010-08-05 rsc s = s.strip()
475 a06877af 2010-08-05 rsc if s == "":
476 a06877af 2010-08-05 rsc return []
477 a06877af 2010-08-05 rsc return re.split(", *", s)
478 ef27bfa4 2010-01-12 rsc
479 ef27bfa4 2010-01-12 rsc def CutDomain(s):
480 26b4bea8 2011-08-02 rsc typecheck(s, str)
481 ef27bfa4 2010-01-12 rsc i = s.find('@')
482 ef27bfa4 2010-01-12 rsc if i >= 0:
483 ef27bfa4 2010-01-12 rsc s = s[0:i]
484 ef27bfa4 2010-01-12 rsc return s
485 ef27bfa4 2010-01-12 rsc
486 ef27bfa4 2010-01-12 rsc def JoinComma(l):
487 26b4bea8 2011-08-02 rsc for s in l:
488 26b4bea8 2011-08-02 rsc typecheck(s, str)
489 ef27bfa4 2010-01-12 rsc return ", ".join(l)
490 ef27bfa4 2010-01-12 rsc
491 ef27bfa4 2010-01-12 rsc def ExceptionDetail():
492 ef27bfa4 2010-01-12 rsc s = str(sys.exc_info()[0])
493 ef27bfa4 2010-01-12 rsc if s.startswith("<type '") and s.endswith("'>"):
494 ef27bfa4 2010-01-12 rsc s = s[7:-2]
495 ef27bfa4 2010-01-12 rsc elif s.startswith("<class '") and s.endswith("'>"):
496 ef27bfa4 2010-01-12 rsc s = s[8:-2]
497 ef27bfa4 2010-01-12 rsc arg = str(sys.exc_info()[1])
498 ef27bfa4 2010-01-12 rsc if len(arg) > 0:
499 ef27bfa4 2010-01-12 rsc s += ": " + arg
500 ef27bfa4 2010-01-12 rsc return s
501 ef27bfa4 2010-01-12 rsc
502 ef27bfa4 2010-01-12 rsc def IsLocalCL(ui, repo, name):
503 ef27bfa4 2010-01-12 rsc return GoodCLName(name) and os.access(CodeReviewDir(ui, repo) + "/cl." + name, 0)
504 ef27bfa4 2010-01-12 rsc
505 ef27bfa4 2010-01-12 rsc # Load CL from disk and/or the web.
506 ef27bfa4 2010-01-12 rsc def LoadCL(ui, repo, name, web=True):
507 26b4bea8 2011-08-02 rsc typecheck(name, str)
508 26b4bea8 2011-08-02 rsc set_status("loading CL " + name)
509 ef27bfa4 2010-01-12 rsc if not GoodCLName(name):
510 ef27bfa4 2010-01-12 rsc return None, "invalid CL name"
511 ef27bfa4 2010-01-12 rsc dir = CodeReviewDir(ui, repo)
512 ef27bfa4 2010-01-12 rsc path = dir + "cl." + name
513 ef27bfa4 2010-01-12 rsc if os.access(path, 0):
514 ef27bfa4 2010-01-12 rsc ff = open(path)
515 ef27bfa4 2010-01-12 rsc text = ff.read()
516 ef27bfa4 2010-01-12 rsc ff.close()
517 ef27bfa4 2010-01-12 rsc cl, lineno, err = ParseCL(text, name)
518 ef27bfa4 2010-01-12 rsc if err != "":
519 ef27bfa4 2010-01-12 rsc return None, "malformed CL data: "+err
520 ef27bfa4 2010-01-12 rsc cl.local = True
521 ef27bfa4 2010-01-12 rsc else:
522 ef27bfa4 2010-01-12 rsc cl = CL(name)
523 ef27bfa4 2010-01-12 rsc if web:
524 26b4bea8 2011-08-02 rsc set_status("getting issue metadata from web")
525 26b4bea8 2011-08-02 rsc d = JSONGet(ui, "/api/" + name + "?messages=true")
526 26b4bea8 2011-08-02 rsc set_status(None)
527 26b4bea8 2011-08-02 rsc if d is None:
528 26b4bea8 2011-08-02 rsc return None, "cannot load CL %s from server" % (name,)
529 26b4bea8 2011-08-02 rsc if 'owner_email' not in d or 'issue' not in d or str(d['issue']) != name:
530 ef27bfa4 2010-01-12 rsc return None, "malformed response loading CL data from code review server"
531 26b4bea8 2011-08-02 rsc cl.dict = d
532 26b4bea8 2011-08-02 rsc cl.reviewer = d.get('reviewers', [])
533 26b4bea8 2011-08-02 rsc cl.cc = d.get('cc', [])
534 ef27bfa4 2010-01-12 rsc if cl.local and cl.copied_from and cl.desc:
535 ef27bfa4 2010-01-12 rsc # local copy of CL written by someone else
536 ef27bfa4 2010-01-12 rsc # and we saved a description. use that one,
537 ef27bfa4 2010-01-12 rsc # so that committers can edit the description
538 ef27bfa4 2010-01-12 rsc # before doing hg submit.
539 ef27bfa4 2010-01-12 rsc pass
540 ef27bfa4 2010-01-12 rsc else:
541 26b4bea8 2011-08-02 rsc cl.desc = d.get('description', "")
542 ef27bfa4 2010-01-12 rsc cl.url = server_url_base + name
543 ef27bfa4 2010-01-12 rsc cl.web = True
544 26b4bea8 2011-08-02 rsc cl.private = d.get('private', False) != False
545 338bd4bb 2011-11-08 rsc cl.lgtm = []
546 338bd4bb 2011-11-08 rsc for m in d.get('messages', []):
547 338bd4bb 2011-11-08 rsc if m.get('approval', False) == True:
548 338bd4bb 2011-11-08 rsc who = re.sub('@.*', '', m.get('sender', ''))
549 338bd4bb 2011-11-08 rsc text = re.sub("\n(.|\n)*", '', m.get('text', ''))
550 338bd4bb 2011-11-08 rsc cl.lgtm.append((who, text))
551 338bd4bb 2011-11-08 rsc
552 26b4bea8 2011-08-02 rsc set_status("loaded CL " + name)
553 ef27bfa4 2010-01-12 rsc return cl, ''
554 ef27bfa4 2010-01-12 rsc
555 ef27bfa4 2010-01-12 rsc class LoadCLThread(threading.Thread):
556 ef27bfa4 2010-01-12 rsc def __init__(self, ui, repo, dir, f, web):
557 ef27bfa4 2010-01-12 rsc threading.Thread.__init__(self)
558 ef27bfa4 2010-01-12 rsc self.ui = ui
559 ef27bfa4 2010-01-12 rsc self.repo = repo
560 ef27bfa4 2010-01-12 rsc self.dir = dir
561 ef27bfa4 2010-01-12 rsc self.f = f
562 ef27bfa4 2010-01-12 rsc self.web = web
563 ef27bfa4 2010-01-12 rsc self.cl = None
564 ef27bfa4 2010-01-12 rsc def run(self):
565 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(self.ui, self.repo, self.f[3:], web=self.web)
566 ef27bfa4 2010-01-12 rsc if err != '':
567 ef27bfa4 2010-01-12 rsc self.ui.warn("loading "+self.dir+self.f+": " + err + "\n")
568 ef27bfa4 2010-01-12 rsc return
569 ef27bfa4 2010-01-12 rsc self.cl = cl
570 ef27bfa4 2010-01-12 rsc
571 ef27bfa4 2010-01-12 rsc # Load all the CLs from this repository.
572 ef27bfa4 2010-01-12 rsc def LoadAllCL(ui, repo, web=True):
573 ef27bfa4 2010-01-12 rsc dir = CodeReviewDir(ui, repo)
574 ef27bfa4 2010-01-12 rsc m = {}
575 ef27bfa4 2010-01-12 rsc files = [f for f in os.listdir(dir) if f.startswith('cl.')]
576 ef27bfa4 2010-01-12 rsc if not files:
577 ef27bfa4 2010-01-12 rsc return m
578 ef27bfa4 2010-01-12 rsc active = []
579 ef27bfa4 2010-01-12 rsc first = True
580 ef27bfa4 2010-01-12 rsc for f in files:
581 ef27bfa4 2010-01-12 rsc t = LoadCLThread(ui, repo, dir, f, web)
582 ef27bfa4 2010-01-12 rsc t.start()
583 ef27bfa4 2010-01-12 rsc if web and first:
584 ef27bfa4 2010-01-12 rsc # first request: wait in case it needs to authenticate
585 ef27bfa4 2010-01-12 rsc # otherwise we get lots of user/password prompts
586 ef27bfa4 2010-01-12 rsc # running in parallel.
587 ef27bfa4 2010-01-12 rsc t.join()
588 ef27bfa4 2010-01-12 rsc if t.cl:
589 ef27bfa4 2010-01-12 rsc m[t.cl.name] = t.cl
590 ef27bfa4 2010-01-12 rsc first = False
591 ef27bfa4 2010-01-12 rsc else:
592 ef27bfa4 2010-01-12 rsc active.append(t)
593 ef27bfa4 2010-01-12 rsc for t in active:
594 ef27bfa4 2010-01-12 rsc t.join()
595 ef27bfa4 2010-01-12 rsc if t.cl:
596 ef27bfa4 2010-01-12 rsc m[t.cl.name] = t.cl
597 ef27bfa4 2010-01-12 rsc return m
598 ef27bfa4 2010-01-12 rsc
599 ef27bfa4 2010-01-12 rsc # Find repository root. On error, ui.warn and return None
600 ef27bfa4 2010-01-12 rsc def RepoDir(ui, repo):
601 ef27bfa4 2010-01-12 rsc url = repo.url();
602 ef27bfa4 2010-01-12 rsc if not url.startswith('file:'):
603 ef27bfa4 2010-01-12 rsc ui.warn("repository %s is not in local file system\n" % (url,))
604 ef27bfa4 2010-01-12 rsc return None
605 ef27bfa4 2010-01-12 rsc url = url[5:]
606 ef27bfa4 2010-01-12 rsc if url.endswith('/'):
607 ef27bfa4 2010-01-12 rsc url = url[:-1]
608 26b4bea8 2011-08-02 rsc typecheck(url, str)
609 ef27bfa4 2010-01-12 rsc return url
610 ef27bfa4 2010-01-12 rsc
611 ef27bfa4 2010-01-12 rsc # Find (or make) code review directory. On error, ui.warn and return None
612 ef27bfa4 2010-01-12 rsc def CodeReviewDir(ui, repo):
613 ef27bfa4 2010-01-12 rsc dir = RepoDir(ui, repo)
614 ef27bfa4 2010-01-12 rsc if dir == None:
615 ef27bfa4 2010-01-12 rsc return None
616 ef27bfa4 2010-01-12 rsc dir += '/.hg/codereview/'
617 ef27bfa4 2010-01-12 rsc if not os.path.isdir(dir):
618 ef27bfa4 2010-01-12 rsc try:
619 ef27bfa4 2010-01-12 rsc os.mkdir(dir, 0700)
620 ef27bfa4 2010-01-12 rsc except:
621 ef27bfa4 2010-01-12 rsc ui.warn('cannot mkdir %s: %s\n' % (dir, ExceptionDetail()))
622 ef27bfa4 2010-01-12 rsc return None
623 26b4bea8 2011-08-02 rsc typecheck(dir, str)
624 ef27bfa4 2010-01-12 rsc return dir
625 ef27bfa4 2010-01-12 rsc
626 26b4bea8 2011-08-02 rsc # Turn leading tabs into spaces, so that the common white space
627 26b4bea8 2011-08-02 rsc # prefix doesn't get confused when people's editors write out
628 26b4bea8 2011-08-02 rsc # some lines with spaces, some with tabs. Only a heuristic
629 26b4bea8 2011-08-02 rsc # (some editors don't use 8 spaces either) but a useful one.
630 26b4bea8 2011-08-02 rsc def TabsToSpaces(line):
631 26b4bea8 2011-08-02 rsc i = 0
632 26b4bea8 2011-08-02 rsc while i < len(line) and line[i] == '\t':
633 26b4bea8 2011-08-02 rsc i += 1
634 26b4bea8 2011-08-02 rsc return ' '*(8*i) + line[i:]
635 26b4bea8 2011-08-02 rsc
636 ef27bfa4 2010-01-12 rsc # Strip maximal common leading white space prefix from text
637 ef27bfa4 2010-01-12 rsc def StripCommon(text):
638 26b4bea8 2011-08-02 rsc typecheck(text, str)
639 ef27bfa4 2010-01-12 rsc ws = None
640 ef27bfa4 2010-01-12 rsc for line in text.split('\n'):
641 ef27bfa4 2010-01-12 rsc line = line.rstrip()
642 ef27bfa4 2010-01-12 rsc if line == '':
643 ef27bfa4 2010-01-12 rsc continue
644 26b4bea8 2011-08-02 rsc line = TabsToSpaces(line)
645 ef27bfa4 2010-01-12 rsc white = line[:len(line)-len(line.lstrip())]
646 ef27bfa4 2010-01-12 rsc if ws == None:
647 ef27bfa4 2010-01-12 rsc ws = white
648 ef27bfa4 2010-01-12 rsc else:
649 ef27bfa4 2010-01-12 rsc common = ''
650 ef27bfa4 2010-01-12 rsc for i in range(min(len(white), len(ws))+1):
651 ef27bfa4 2010-01-12 rsc if white[0:i] == ws[0:i]:
652 ef27bfa4 2010-01-12 rsc common = white[0:i]
653 ef27bfa4 2010-01-12 rsc ws = common
654 ef27bfa4 2010-01-12 rsc if ws == '':
655 ef27bfa4 2010-01-12 rsc break
656 ef27bfa4 2010-01-12 rsc if ws == None:
657 ef27bfa4 2010-01-12 rsc return text
658 ef27bfa4 2010-01-12 rsc t = ''
659 ef27bfa4 2010-01-12 rsc for line in text.split('\n'):
660 ef27bfa4 2010-01-12 rsc line = line.rstrip()
661 26b4bea8 2011-08-02 rsc line = TabsToSpaces(line)
662 ef27bfa4 2010-01-12 rsc if line.startswith(ws):
663 ef27bfa4 2010-01-12 rsc line = line[len(ws):]
664 ef27bfa4 2010-01-12 rsc if line == '' and t == '':
665 ef27bfa4 2010-01-12 rsc continue
666 ef27bfa4 2010-01-12 rsc t += line + '\n'
667 ef27bfa4 2010-01-12 rsc while len(t) >= 2 and t[-2:] == '\n\n':
668 ef27bfa4 2010-01-12 rsc t = t[:-1]
669 26b4bea8 2011-08-02 rsc typecheck(t, str)
670 ef27bfa4 2010-01-12 rsc return t
671 ef27bfa4 2010-01-12 rsc
672 ef27bfa4 2010-01-12 rsc # Indent text with indent.
673 ef27bfa4 2010-01-12 rsc def Indent(text, indent):
674 26b4bea8 2011-08-02 rsc typecheck(text, str)
675 26b4bea8 2011-08-02 rsc typecheck(indent, str)
676 ef27bfa4 2010-01-12 rsc t = ''
677 ef27bfa4 2010-01-12 rsc for line in text.split('\n'):
678 ef27bfa4 2010-01-12 rsc t += indent + line + '\n'
679 26b4bea8 2011-08-02 rsc typecheck(t, str)
680 ef27bfa4 2010-01-12 rsc return t
681 ef27bfa4 2010-01-12 rsc
682 ef27bfa4 2010-01-12 rsc # Return the first line of l
683 ef27bfa4 2010-01-12 rsc def line1(text):
684 26b4bea8 2011-08-02 rsc typecheck(text, str)
685 ef27bfa4 2010-01-12 rsc return text.split('\n')[0]
686 ef27bfa4 2010-01-12 rsc
687 ef27bfa4 2010-01-12 rsc _change_prolog = """# Change list.
688 ef27bfa4 2010-01-12 rsc # Lines beginning with # are ignored.
689 ef27bfa4 2010-01-12 rsc # Multi-line values should be indented.
690 ef27bfa4 2010-01-12 rsc """
691 ef27bfa4 2010-01-12 rsc
692 26b4bea8 2011-08-02 rsc desc_re = '^(.+: |(tag )?(release|weekly)\.|fix build|undo CL)'
693 26b4bea8 2011-08-02 rsc
694 26b4bea8 2011-08-02 rsc desc_msg = '''Your CL description appears not to use the standard form.
695 26b4bea8 2011-08-02 rsc
696 26b4bea8 2011-08-02 rsc The first line of your change description is conventionally a
697 26b4bea8 2011-08-02 rsc one-line summary of the change, prefixed by the primary affected package,
698 26b4bea8 2011-08-02 rsc and is used as the subject for code review mail; the rest of the description
699 26b4bea8 2011-08-02 rsc elaborates.
700 26b4bea8 2011-08-02 rsc
701 26b4bea8 2011-08-02 rsc Examples:
702 26b4bea8 2011-08-02 rsc
703 26b4bea8 2011-08-02 rsc encoding/rot13: new package
704 26b4bea8 2011-08-02 rsc
705 26b4bea8 2011-08-02 rsc math: add IsInf, IsNaN
706 26b4bea8 2011-08-02 rsc
707 26b4bea8 2011-08-02 rsc net: fix cname in LookupHost
708 26b4bea8 2011-08-02 rsc
709 26b4bea8 2011-08-02 rsc unicode: update to Unicode 5.0.2
710 26b4bea8 2011-08-02 rsc
711 26b4bea8 2011-08-02 rsc '''
712 26b4bea8 2011-08-02 rsc
713 5c934375 2012-01-20 rsc def promptyesno(ui, msg):
714 5c934375 2012-01-20 rsc return ui.promptchoice(msg, ["&yes", "&no"], 0) == 0
715 26b4bea8 2011-08-02 rsc
716 26b4bea8 2011-08-02 rsc def promptremove(ui, repo, f):
717 26b4bea8 2011-08-02 rsc if promptyesno(ui, "hg remove %s (y/n)?" % (f,)):
718 5c934375 2012-01-20 rsc if hg_commands.remove(ui, repo, 'path:'+f) != 0:
719 26b4bea8 2011-08-02 rsc ui.warn("error removing %s" % (f,))
720 26b4bea8 2011-08-02 rsc
721 26b4bea8 2011-08-02 rsc def promptadd(ui, repo, f):
722 26b4bea8 2011-08-02 rsc if promptyesno(ui, "hg add %s (y/n)?" % (f,)):
723 5c934375 2012-01-20 rsc if hg_commands.add(ui, repo, 'path:'+f) != 0:
724 26b4bea8 2011-08-02 rsc ui.warn("error adding %s" % (f,))
725 26b4bea8 2011-08-02 rsc
726 ef27bfa4 2010-01-12 rsc def EditCL(ui, repo, cl):
727 26b4bea8 2011-08-02 rsc set_status(None) # do not show status
728 ef27bfa4 2010-01-12 rsc s = cl.EditorText()
729 ef27bfa4 2010-01-12 rsc while True:
730 ef27bfa4 2010-01-12 rsc s = ui.edit(s, ui.username())
731 338bd4bb 2011-11-08 rsc
732 338bd4bb 2011-11-08 rsc # We can't trust Mercurial + Python not to die before making the change,
733 338bd4bb 2011-11-08 rsc # so, by popular demand, just scribble the most recent CL edit into
734 338bd4bb 2011-11-08 rsc # $(hg root)/last-change so that if Mercurial does die, people
735 338bd4bb 2011-11-08 rsc # can look there for their work.
736 338bd4bb 2011-11-08 rsc try:
737 338bd4bb 2011-11-08 rsc f = open(repo.root+"/last-change", "w")
738 338bd4bb 2011-11-08 rsc f.write(s)
739 338bd4bb 2011-11-08 rsc f.close()
740 338bd4bb 2011-11-08 rsc except:
741 338bd4bb 2011-11-08 rsc pass
742 338bd4bb 2011-11-08 rsc
743 ef27bfa4 2010-01-12 rsc clx, line, err = ParseCL(s, cl.name)
744 ef27bfa4 2010-01-12 rsc if err != '':
745 a06877af 2010-08-05 rsc if not promptyesno(ui, "error parsing change list: line %d: %s\nre-edit (y/n)?" % (line, err)):
746 ef27bfa4 2010-01-12 rsc return "change list not modified"
747 ef27bfa4 2010-01-12 rsc continue
748 26b4bea8 2011-08-02 rsc
749 26b4bea8 2011-08-02 rsc # Check description.
750 26b4bea8 2011-08-02 rsc if clx.desc == '':
751 26b4bea8 2011-08-02 rsc if promptyesno(ui, "change list should have a description\nre-edit (y/n)?"):
752 26b4bea8 2011-08-02 rsc continue
753 26b4bea8 2011-08-02 rsc elif re.search('<enter reason for undo>', clx.desc):
754 26b4bea8 2011-08-02 rsc if promptyesno(ui, "change list description omits reason for undo\nre-edit (y/n)?"):
755 26b4bea8 2011-08-02 rsc continue
756 26b4bea8 2011-08-02 rsc elif not re.match(desc_re, clx.desc.split('\n')[0]):
757 26b4bea8 2011-08-02 rsc if promptyesno(ui, desc_msg + "re-edit (y/n)?"):
758 26b4bea8 2011-08-02 rsc continue
759 26b4bea8 2011-08-02 rsc
760 26b4bea8 2011-08-02 rsc # Check file list for files that need to be hg added or hg removed
761 26b4bea8 2011-08-02 rsc # or simply aren't understood.
762 26b4bea8 2011-08-02 rsc pats = ['path:'+f for f in clx.files]
763 5c934375 2012-01-20 rsc changed = hg_matchPattern(ui, repo, *pats, modified=True, added=True, removed=True)
764 5c934375 2012-01-20 rsc deleted = hg_matchPattern(ui, repo, *pats, deleted=True)
765 5c934375 2012-01-20 rsc unknown = hg_matchPattern(ui, repo, *pats, unknown=True)
766 5c934375 2012-01-20 rsc ignored = hg_matchPattern(ui, repo, *pats, ignored=True)
767 5c934375 2012-01-20 rsc clean = hg_matchPattern(ui, repo, *pats, clean=True)
768 26b4bea8 2011-08-02 rsc files = []
769 26b4bea8 2011-08-02 rsc for f in clx.files:
770 5c934375 2012-01-20 rsc if f in changed:
771 26b4bea8 2011-08-02 rsc files.append(f)
772 26b4bea8 2011-08-02 rsc continue
773 26b4bea8 2011-08-02 rsc if f in deleted:
774 26b4bea8 2011-08-02 rsc promptremove(ui, repo, f)
775 26b4bea8 2011-08-02 rsc files.append(f)
776 26b4bea8 2011-08-02 rsc continue
777 26b4bea8 2011-08-02 rsc if f in unknown:
778 26b4bea8 2011-08-02 rsc promptadd(ui, repo, f)
779 26b4bea8 2011-08-02 rsc files.append(f)
780 26b4bea8 2011-08-02 rsc continue
781 26b4bea8 2011-08-02 rsc if f in ignored:
782 26b4bea8 2011-08-02 rsc ui.warn("error: %s is excluded by .hgignore; omitting\n" % (f,))
783 26b4bea8 2011-08-02 rsc continue
784 26b4bea8 2011-08-02 rsc if f in clean:
785 26b4bea8 2011-08-02 rsc ui.warn("warning: %s is listed in the CL but unchanged\n" % (f,))
786 26b4bea8 2011-08-02 rsc files.append(f)
787 26b4bea8 2011-08-02 rsc continue
788 26b4bea8 2011-08-02 rsc p = repo.root + '/' + f
789 26b4bea8 2011-08-02 rsc if os.path.isfile(p):
790 26b4bea8 2011-08-02 rsc ui.warn("warning: %s is a file but not known to hg\n" % (f,))
791 26b4bea8 2011-08-02 rsc files.append(f)
792 26b4bea8 2011-08-02 rsc continue
793 26b4bea8 2011-08-02 rsc if os.path.isdir(p):
794 26b4bea8 2011-08-02 rsc ui.warn("error: %s is a directory, not a file; omitting\n" % (f,))
795 26b4bea8 2011-08-02 rsc continue
796 26b4bea8 2011-08-02 rsc ui.warn("error: %s does not exist; omitting\n" % (f,))
797 26b4bea8 2011-08-02 rsc clx.files = files
798 26b4bea8 2011-08-02 rsc
799 26b4bea8 2011-08-02 rsc cl.desc = clx.desc
800 ef27bfa4 2010-01-12 rsc cl.reviewer = clx.reviewer
801 ef27bfa4 2010-01-12 rsc cl.cc = clx.cc
802 ef27bfa4 2010-01-12 rsc cl.files = clx.files
803 26b4bea8 2011-08-02 rsc cl.private = clx.private
804 ef27bfa4 2010-01-12 rsc break
805 ef27bfa4 2010-01-12 rsc return ""
806 ef27bfa4 2010-01-12 rsc
807 ef27bfa4 2010-01-12 rsc # For use by submit, etc. (NOT by change)
808 ef27bfa4 2010-01-12 rsc # Get change list number or list of files from command line.
809 ef27bfa4 2010-01-12 rsc # If files are given, make a new change list.
810 ef27bfa4 2010-01-12 rsc def CommandLineCL(ui, repo, pats, opts, defaultcc=None):
811 ef27bfa4 2010-01-12 rsc if len(pats) > 0 and GoodCLName(pats[0]):
812 ef27bfa4 2010-01-12 rsc if len(pats) != 1:
813 ef27bfa4 2010-01-12 rsc return None, "cannot specify change number and file names"
814 ef27bfa4 2010-01-12 rsc if opts.get('message'):
815 ef27bfa4 2010-01-12 rsc return None, "cannot use -m with existing CL"
816 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(ui, repo, pats[0], web=True)
817 ef27bfa4 2010-01-12 rsc if err != "":
818 ef27bfa4 2010-01-12 rsc return None, err
819 ef27bfa4 2010-01-12 rsc else:
820 ef27bfa4 2010-01-12 rsc cl = CL("new")
821 ef27bfa4 2010-01-12 rsc cl.local = True
822 5c934375 2012-01-20 rsc cl.files = ChangedFiles(ui, repo, pats, taken=Taken(ui, repo))
823 ef27bfa4 2010-01-12 rsc if not cl.files:
824 ef27bfa4 2010-01-12 rsc return None, "no files changed"
825 ef27bfa4 2010-01-12 rsc if opts.get('reviewer'):
826 ef27bfa4 2010-01-12 rsc cl.reviewer = Add(cl.reviewer, SplitCommaSpace(opts.get('reviewer')))
827 ef27bfa4 2010-01-12 rsc if opts.get('cc'):
828 ef27bfa4 2010-01-12 rsc cl.cc = Add(cl.cc, SplitCommaSpace(opts.get('cc')))
829 ef27bfa4 2010-01-12 rsc if defaultcc:
830 ef27bfa4 2010-01-12 rsc cl.cc = Add(cl.cc, defaultcc)
831 ef27bfa4 2010-01-12 rsc if cl.name == "new":
832 ef27bfa4 2010-01-12 rsc if opts.get('message'):
833 ef27bfa4 2010-01-12 rsc cl.desc = opts.get('message')
834 ef27bfa4 2010-01-12 rsc else:
835 ef27bfa4 2010-01-12 rsc err = EditCL(ui, repo, cl)
836 ef27bfa4 2010-01-12 rsc if err != '':
837 ef27bfa4 2010-01-12 rsc return None, err
838 ef27bfa4 2010-01-12 rsc return cl, ""
839 ef27bfa4 2010-01-12 rsc
840 5c934375 2012-01-20 rsc #######################################################################
841 5c934375 2012-01-20 rsc # Change list file management
842 338bd4bb 2011-11-08 rsc
843 5c934375 2012-01-20 rsc # Return list of changed files in repository that match pats.
844 5c934375 2012-01-20 rsc # The patterns came from the command line, so we warn
845 5c934375 2012-01-20 rsc # if they have no effect or cannot be understood.
846 5c934375 2012-01-20 rsc def ChangedFiles(ui, repo, pats, taken=None):
847 5c934375 2012-01-20 rsc taken = taken or {}
848 5c934375 2012-01-20 rsc # Run each pattern separately so that we can warn about
849 5c934375 2012-01-20 rsc # patterns that didn't do anything useful.
850 5c934375 2012-01-20 rsc for p in pats:
851 5c934375 2012-01-20 rsc for f in hg_matchPattern(ui, repo, p, unknown=True):
852 5c934375 2012-01-20 rsc promptadd(ui, repo, f)
853 5c934375 2012-01-20 rsc for f in hg_matchPattern(ui, repo, p, removed=True):
854 5c934375 2012-01-20 rsc promptremove(ui, repo, f)
855 5c934375 2012-01-20 rsc files = hg_matchPattern(ui, repo, p, modified=True, added=True, removed=True)
856 5c934375 2012-01-20 rsc for f in files:
857 5c934375 2012-01-20 rsc if f in taken:
858 5c934375 2012-01-20 rsc ui.warn("warning: %s already in CL %s\n" % (f, taken[f].name))
859 5c934375 2012-01-20 rsc if not files:
860 5c934375 2012-01-20 rsc ui.warn("warning: %s did not match any modified files\n" % (p,))
861 ef27bfa4 2010-01-12 rsc
862 5c934375 2012-01-20 rsc # Again, all at once (eliminates duplicates)
863 5c934375 2012-01-20 rsc l = hg_matchPattern(ui, repo, *pats, modified=True, added=True, removed=True)
864 5c934375 2012-01-20 rsc l.sort()
865 5c934375 2012-01-20 rsc if taken:
866 5c934375 2012-01-20 rsc l = Sub(l, taken.keys())
867 5c934375 2012-01-20 rsc return l
868 ef27bfa4 2010-01-12 rsc
869 5c934375 2012-01-20 rsc # Return list of changed files in repository that match pats and still exist.
870 5c934375 2012-01-20 rsc def ChangedExistingFiles(ui, repo, pats, opts):
871 5c934375 2012-01-20 rsc l = hg_matchPattern(ui, repo, *pats, modified=True, added=True)
872 5c934375 2012-01-20 rsc l.sort()
873 5c934375 2012-01-20 rsc return l
874 5c934375 2012-01-20 rsc
875 5c934375 2012-01-20 rsc # Return list of files claimed by existing CLs
876 5c934375 2012-01-20 rsc def Taken(ui, repo):
877 5c934375 2012-01-20 rsc all = LoadAllCL(ui, repo, web=False)
878 5c934375 2012-01-20 rsc taken = {}
879 5c934375 2012-01-20 rsc for _, cl in all.items():
880 5c934375 2012-01-20 rsc for f in cl.files:
881 5c934375 2012-01-20 rsc taken[f] = cl
882 5c934375 2012-01-20 rsc return taken
883 5c934375 2012-01-20 rsc
884 5c934375 2012-01-20 rsc # Return list of changed files that are not claimed by other CLs
885 5c934375 2012-01-20 rsc def DefaultFiles(ui, repo, pats):
886 5c934375 2012-01-20 rsc return ChangedFiles(ui, repo, pats, taken=Taken(ui, repo))
887 5c934375 2012-01-20 rsc
888 5c934375 2012-01-20 rsc #######################################################################
889 5c934375 2012-01-20 rsc # File format checking.
890 5c934375 2012-01-20 rsc
891 26b4bea8 2011-08-02 rsc def CheckFormat(ui, repo, files, just_warn=False):
892 26b4bea8 2011-08-02 rsc set_status("running gofmt")
893 26b4bea8 2011-08-02 rsc CheckGofmt(ui, repo, files, just_warn)
894 26b4bea8 2011-08-02 rsc CheckTabfmt(ui, repo, files, just_warn)
895 26b4bea8 2011-08-02 rsc
896 ef27bfa4 2010-01-12 rsc # Check that gofmt run on the list of files does not change them
897 26b4bea8 2011-08-02 rsc def CheckGofmt(ui, repo, files, just_warn):
898 63550fce 2012-07-14 rsc files = gofmt_required(files)
899 ef27bfa4 2010-01-12 rsc if not files:
900 ef27bfa4 2010-01-12 rsc return
901 ef27bfa4 2010-01-12 rsc cwd = os.getcwd()
902 ef27bfa4 2010-01-12 rsc files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
903 ef27bfa4 2010-01-12 rsc files = [f for f in files if os.access(f, 0)]
904 a06877af 2010-08-05 rsc if not files:
905 a06877af 2010-08-05 rsc return
906 ef27bfa4 2010-01-12 rsc try:
907 26b4bea8 2011-08-02 rsc cmd = subprocess.Popen(["gofmt", "-l"] + files, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=sys.platform != "win32")
908 ef27bfa4 2010-01-12 rsc cmd.stdin.close()
909 ef27bfa4 2010-01-12 rsc except:
910 5c934375 2012-01-20 rsc raise hg_util.Abort("gofmt: " + ExceptionDetail())
911 ef27bfa4 2010-01-12 rsc data = cmd.stdout.read()
912 ef27bfa4 2010-01-12 rsc errors = cmd.stderr.read()
913 ef27bfa4 2010-01-12 rsc cmd.wait()
914 26b4bea8 2011-08-02 rsc set_status("done with gofmt")
915 ef27bfa4 2010-01-12 rsc if len(errors) > 0:
916 ef27bfa4 2010-01-12 rsc ui.warn("gofmt errors:\n" + errors.rstrip() + "\n")
917 ef27bfa4 2010-01-12 rsc return
918 ef27bfa4 2010-01-12 rsc if len(data) > 0:
919 ef27bfa4 2010-01-12 rsc msg = "gofmt needs to format these files (run hg gofmt):\n" + Indent(data, "\t").rstrip()
920 26b4bea8 2011-08-02 rsc if just_warn:
921 26b4bea8 2011-08-02 rsc ui.warn("warning: " + msg + "\n")
922 26b4bea8 2011-08-02 rsc else:
923 5c934375 2012-01-20 rsc raise hg_util.Abort(msg)
924 26b4bea8 2011-08-02 rsc return
925 26b4bea8 2011-08-02 rsc
926 26b4bea8 2011-08-02 rsc # Check that *.[chys] files indent using tabs.
927 26b4bea8 2011-08-02 rsc def CheckTabfmt(ui, repo, files, just_warn):
928 63550fce 2012-07-14 rsc files = [f for f in files if f.startswith('src/') and re.search(r"\.[chys]$", f) and not re.search(r"\.tab\.[ch]$", f)]
929 26b4bea8 2011-08-02 rsc if not files:
930 26b4bea8 2011-08-02 rsc return
931 26b4bea8 2011-08-02 rsc cwd = os.getcwd()
932 26b4bea8 2011-08-02 rsc files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
933 26b4bea8 2011-08-02 rsc files = [f for f in files if os.access(f, 0)]
934 26b4bea8 2011-08-02 rsc badfiles = []
935 26b4bea8 2011-08-02 rsc for f in files:
936 26b4bea8 2011-08-02 rsc try:
937 26b4bea8 2011-08-02 rsc for line in open(f, 'r'):
938 26b4bea8 2011-08-02 rsc # Four leading spaces is enough to complain about,
939 26b4bea8 2011-08-02 rsc # except that some Plan 9 code uses four spaces as the label indent,
940 26b4bea8 2011-08-02 rsc # so allow that.
941 26b4bea8 2011-08-02 rsc if line.startswith(' ') and not re.match(' [A-Za-z0-9_]+:', line):
942 26b4bea8 2011-08-02 rsc badfiles.append(f)
943 26b4bea8 2011-08-02 rsc break
944 26b4bea8 2011-08-02 rsc except:
945 26b4bea8 2011-08-02 rsc # ignore cannot open file, etc.
946 26b4bea8 2011-08-02 rsc pass
947 26b4bea8 2011-08-02 rsc if len(badfiles) > 0:
948 26b4bea8 2011-08-02 rsc msg = "these files use spaces for indentation (use tabs instead):\n\t" + "\n\t".join(badfiles)
949 ef27bfa4 2010-01-12 rsc if just_warn:
950 ef27bfa4 2010-01-12 rsc ui.warn("warning: " + msg + "\n")
951 ef27bfa4 2010-01-12 rsc else:
952 5c934375 2012-01-20 rsc raise hg_util.Abort(msg)
953 ef27bfa4 2010-01-12 rsc return
954 ef27bfa4 2010-01-12 rsc
955 ef27bfa4 2010-01-12 rsc #######################################################################
956 5c934375 2012-01-20 rsc # CONTRIBUTORS file parsing
957 ef27bfa4 2010-01-12 rsc
958 63550fce 2012-07-14 rsc contributorsCache = None
959 63550fce 2012-07-14 rsc contributorsURL = None
960 5c934375 2012-01-20 rsc
961 5c934375 2012-01-20 rsc def ReadContributors(ui, repo):
962 63550fce 2012-07-14 rsc global contributorsCache
963 63550fce 2012-07-14 rsc if contributorsCache is not None:
964 63550fce 2012-07-14 rsc return contributorsCache
965 63550fce 2012-07-14 rsc
966 63550fce 2012-07-14 rsc try:
967 63550fce 2012-07-14 rsc if contributorsURL is not None:
968 63550fce 2012-07-14 rsc opening = contributorsURL
969 63550fce 2012-07-14 rsc f = urllib2.urlopen(contributorsURL)
970 63550fce 2012-07-14 rsc else:
971 63550fce 2012-07-14 rsc opening = repo.root + '/CONTRIBUTORS'
972 63550fce 2012-07-14 rsc f = open(repo.root + '/CONTRIBUTORS', 'r')
973 5c934375 2012-01-20 rsc except:
974 63550fce 2012-07-14 rsc ui.write("warning: cannot open %s: %s\n" % (opening, ExceptionDetail()))
975 5c934375 2012-01-20 rsc return
976 5c934375 2012-01-20 rsc
977 63550fce 2012-07-14 rsc contributors = {}
978 5c934375 2012-01-20 rsc for line in f:
979 5c934375 2012-01-20 rsc # CONTRIBUTORS is a list of lines like:
980 5c934375 2012-01-20 rsc # Person <email>
981 5c934375 2012-01-20 rsc # Person <email> <alt-email>
982 5c934375 2012-01-20 rsc # The first email address is the one used in commit logs.
983 5c934375 2012-01-20 rsc if line.startswith('#'):
984 5c934375 2012-01-20 rsc continue
985 5c934375 2012-01-20 rsc m = re.match(r"([^<>]+\S)\s+(<[^<>\s]+>)((\s+<[^<>\s]+>)*)\s*$", line)
986 5c934375 2012-01-20 rsc if m:
987 5c934375 2012-01-20 rsc name = m.group(1)
988 5c934375 2012-01-20 rsc email = m.group(2)[1:-1]
989 5c934375 2012-01-20 rsc contributors[email.lower()] = (name, email)
990 5c934375 2012-01-20 rsc for extra in m.group(3).split():
991 5c934375 2012-01-20 rsc contributors[extra[1:-1].lower()] = (name, email)
992 5c934375 2012-01-20 rsc
993 63550fce 2012-07-14 rsc contributorsCache = contributors
994 63550fce 2012-07-14 rsc return contributors
995 63550fce 2012-07-14 rsc
996 5c934375 2012-01-20 rsc def CheckContributor(ui, repo, user=None):
997 5c934375 2012-01-20 rsc set_status("checking CONTRIBUTORS file")
998 5c934375 2012-01-20 rsc user, userline = FindContributor(ui, repo, user, warn=False)
999 5c934375 2012-01-20 rsc if not userline:
1000 5c934375 2012-01-20 rsc raise hg_util.Abort("cannot find %s in CONTRIBUTORS" % (user,))
1001 5c934375 2012-01-20 rsc return userline
1002 5c934375 2012-01-20 rsc
1003 5c934375 2012-01-20 rsc def FindContributor(ui, repo, user=None, warn=True):
1004 5c934375 2012-01-20 rsc if not user:
1005 5c934375 2012-01-20 rsc user = ui.config("ui", "username")
1006 5c934375 2012-01-20 rsc if not user:
1007 5c934375 2012-01-20 rsc raise hg_util.Abort("[ui] username is not configured in .hgrc")
1008 5c934375 2012-01-20 rsc user = user.lower()
1009 5c934375 2012-01-20 rsc m = re.match(r".*<(.*)>", user)
1010 5c934375 2012-01-20 rsc if m:
1011 5c934375 2012-01-20 rsc user = m.group(1)
1012 5c934375 2012-01-20 rsc
1013 63550fce 2012-07-14 rsc contributors = ReadContributors(ui, repo)
1014 5c934375 2012-01-20 rsc if user not in contributors:
1015 5c934375 2012-01-20 rsc if warn:
1016 5c934375 2012-01-20 rsc ui.warn("warning: cannot find %s in CONTRIBUTORS\n" % (user,))
1017 5c934375 2012-01-20 rsc return user, None
1018 5c934375 2012-01-20 rsc
1019 5c934375 2012-01-20 rsc user, email = contributors[user]
1020 5c934375 2012-01-20 rsc return email, "%s <%s>" % (user, email)
1021 5c934375 2012-01-20 rsc
1022 5c934375 2012-01-20 rsc #######################################################################
1023 5c934375 2012-01-20 rsc # Mercurial helper functions.
1024 5c934375 2012-01-20 rsc # Read http://mercurial.selenic.com/wiki/MercurialApi before writing any of these.
1025 5c934375 2012-01-20 rsc # We use the ui.pushbuffer/ui.popbuffer + hg_commands.xxx tricks for all interaction
1026 5c934375 2012-01-20 rsc # with Mercurial. It has proved the most stable as they make changes.
1027 5c934375 2012-01-20 rsc
1028 5c934375 2012-01-20 rsc hgversion = hg_util.version()
1029 5c934375 2012-01-20 rsc
1030 5c934375 2012-01-20 rsc # We require Mercurial 1.9 and suggest Mercurial 2.0.
1031 5c934375 2012-01-20 rsc # The details of the scmutil package changed then,
1032 5c934375 2012-01-20 rsc # so allowing earlier versions would require extra band-aids below.
1033 5c934375 2012-01-20 rsc # Ubuntu 11.10 ships with Mercurial 1.9.1 as the default version.
1034 5c934375 2012-01-20 rsc hg_required = "1.9"
1035 5c934375 2012-01-20 rsc hg_suggested = "2.0"
1036 5c934375 2012-01-20 rsc
1037 5c934375 2012-01-20 rsc old_message = """
1038 5c934375 2012-01-20 rsc
1039 5c934375 2012-01-20 rsc The code review extension requires Mercurial """+hg_required+""" or newer.
1040 5c934375 2012-01-20 rsc You are using Mercurial """+hgversion+""".
1041 5c934375 2012-01-20 rsc
1042 5c934375 2012-01-20 rsc To install a new Mercurial, use
1043 5c934375 2012-01-20 rsc
1044 5c934375 2012-01-20 rsc sudo easy_install mercurial=="""+hg_suggested+"""
1045 5c934375 2012-01-20 rsc
1046 5c934375 2012-01-20 rsc or visit http://mercurial.selenic.com/downloads/.
1047 5c934375 2012-01-20 rsc """
1048 5c934375 2012-01-20 rsc
1049 5c934375 2012-01-20 rsc linux_message = """
1050 5c934375 2012-01-20 rsc You may need to clear your current Mercurial installation by running:
1051 5c934375 2012-01-20 rsc
1052 5c934375 2012-01-20 rsc sudo apt-get remove mercurial mercurial-common
1053 5c934375 2012-01-20 rsc sudo rm -rf /etc/mercurial
1054 5c934375 2012-01-20 rsc """
1055 5c934375 2012-01-20 rsc
1056 5c934375 2012-01-20 rsc if hgversion < hg_required:
1057 5c934375 2012-01-20 rsc msg = old_message
1058 5c934375 2012-01-20 rsc if os.access("/etc/mercurial", 0):
1059 5c934375 2012-01-20 rsc msg += linux_message
1060 5c934375 2012-01-20 rsc raise hg_util.Abort(msg)
1061 5c934375 2012-01-20 rsc
1062 5c934375 2012-01-20 rsc from mercurial.hg import clean as hg_clean
1063 5c934375 2012-01-20 rsc from mercurial import cmdutil as hg_cmdutil
1064 5c934375 2012-01-20 rsc from mercurial import error as hg_error
1065 5c934375 2012-01-20 rsc from mercurial import match as hg_match
1066 5c934375 2012-01-20 rsc from mercurial import node as hg_node
1067 5c934375 2012-01-20 rsc
1068 5c934375 2012-01-20 rsc class uiwrap(object):
1069 5c934375 2012-01-20 rsc def __init__(self, ui):
1070 5c934375 2012-01-20 rsc self.ui = ui
1071 5c934375 2012-01-20 rsc ui.pushbuffer()
1072 5c934375 2012-01-20 rsc self.oldQuiet = ui.quiet
1073 5c934375 2012-01-20 rsc ui.quiet = True
1074 5c934375 2012-01-20 rsc self.oldVerbose = ui.verbose
1075 5c934375 2012-01-20 rsc ui.verbose = False
1076 5c934375 2012-01-20 rsc def output(self):
1077 5c934375 2012-01-20 rsc ui = self.ui
1078 5c934375 2012-01-20 rsc ui.quiet = self.oldQuiet
1079 5c934375 2012-01-20 rsc ui.verbose = self.oldVerbose
1080 5c934375 2012-01-20 rsc return ui.popbuffer()
1081 5c934375 2012-01-20 rsc
1082 5c934375 2012-01-20 rsc def to_slash(path):
1083 5c934375 2012-01-20 rsc if sys.platform == "win32":
1084 5c934375 2012-01-20 rsc return path.replace('\\', '/')
1085 5c934375 2012-01-20 rsc return path
1086 5c934375 2012-01-20 rsc
1087 5c934375 2012-01-20 rsc def hg_matchPattern(ui, repo, *pats, **opts):
1088 5c934375 2012-01-20 rsc w = uiwrap(ui)
1089 5c934375 2012-01-20 rsc hg_commands.status(ui, repo, *pats, **opts)
1090 5c934375 2012-01-20 rsc text = w.output()
1091 5c934375 2012-01-20 rsc ret = []
1092 5c934375 2012-01-20 rsc prefix = to_slash(os.path.realpath(repo.root))+'/'
1093 5c934375 2012-01-20 rsc for line in text.split('\n'):
1094 5c934375 2012-01-20 rsc f = line.split()
1095 5c934375 2012-01-20 rsc if len(f) > 1:
1096 5c934375 2012-01-20 rsc if len(pats) > 0:
1097 5c934375 2012-01-20 rsc # Given patterns, Mercurial shows relative to cwd
1098 5c934375 2012-01-20 rsc p = to_slash(os.path.realpath(f[1]))
1099 5c934375 2012-01-20 rsc if not p.startswith(prefix):
1100 5c934375 2012-01-20 rsc print >>sys.stderr, "File %s not in repo root %s.\n" % (p, prefix)
1101 5c934375 2012-01-20 rsc else:
1102 5c934375 2012-01-20 rsc ret.append(p[len(prefix):])
1103 5c934375 2012-01-20 rsc else:
1104 5c934375 2012-01-20 rsc # Without patterns, Mercurial shows relative to root (what we want)
1105 5c934375 2012-01-20 rsc ret.append(to_slash(f[1]))
1106 5c934375 2012-01-20 rsc return ret
1107 5c934375 2012-01-20 rsc
1108 5c934375 2012-01-20 rsc def hg_heads(ui, repo):
1109 5c934375 2012-01-20 rsc w = uiwrap(ui)
1110 63550fce 2012-07-14 rsc hg_commands.heads(ui, repo)
1111 5c934375 2012-01-20 rsc return w.output()
1112 5c934375 2012-01-20 rsc
1113 5c934375 2012-01-20 rsc noise = [
1114 5c934375 2012-01-20 rsc "",
1115 5c934375 2012-01-20 rsc "resolving manifests",
1116 5c934375 2012-01-20 rsc "searching for changes",
1117 5c934375 2012-01-20 rsc "couldn't find merge tool hgmerge",
1118 5c934375 2012-01-20 rsc "adding changesets",
1119 5c934375 2012-01-20 rsc "adding manifests",
1120 5c934375 2012-01-20 rsc "adding file changes",
1121 5c934375 2012-01-20 rsc "all local heads known remotely",
1122 5c934375 2012-01-20 rsc ]
1123 5c934375 2012-01-20 rsc
1124 5c934375 2012-01-20 rsc def isNoise(line):
1125 5c934375 2012-01-20 rsc line = str(line)
1126 5c934375 2012-01-20 rsc for x in noise:
1127 5c934375 2012-01-20 rsc if line == x:
1128 5c934375 2012-01-20 rsc return True
1129 5c934375 2012-01-20 rsc return False
1130 ef27bfa4 2010-01-12 rsc
1131 5c934375 2012-01-20 rsc def hg_incoming(ui, repo):
1132 5c934375 2012-01-20 rsc w = uiwrap(ui)
1133 5c934375 2012-01-20 rsc ret = hg_commands.incoming(ui, repo, force=False, bundle="")
1134 5c934375 2012-01-20 rsc if ret and ret != 1:
1135 5c934375 2012-01-20 rsc raise hg_util.Abort(ret)
1136 5c934375 2012-01-20 rsc return w.output()
1137 5c934375 2012-01-20 rsc
1138 5c934375 2012-01-20 rsc def hg_log(ui, repo, **opts):
1139 5c934375 2012-01-20 rsc for k in ['date', 'keyword', 'rev', 'user']:
1140 5c934375 2012-01-20 rsc if not opts.has_key(k):
1141 5c934375 2012-01-20 rsc opts[k] = ""
1142 5c934375 2012-01-20 rsc w = uiwrap(ui)
1143 5c934375 2012-01-20 rsc ret = hg_commands.log(ui, repo, **opts)
1144 5c934375 2012-01-20 rsc if ret:
1145 5c934375 2012-01-20 rsc raise hg_util.Abort(ret)
1146 5c934375 2012-01-20 rsc return w.output()
1147 5c934375 2012-01-20 rsc
1148 5c934375 2012-01-20 rsc def hg_outgoing(ui, repo, **opts):
1149 5c934375 2012-01-20 rsc w = uiwrap(ui)
1150 5c934375 2012-01-20 rsc ret = hg_commands.outgoing(ui, repo, **opts)
1151 5c934375 2012-01-20 rsc if ret and ret != 1:
1152 5c934375 2012-01-20 rsc raise hg_util.Abort(ret)
1153 5c934375 2012-01-20 rsc return w.output()
1154 5c934375 2012-01-20 rsc
1155 5c934375 2012-01-20 rsc def hg_pull(ui, repo, **opts):
1156 5c934375 2012-01-20 rsc w = uiwrap(ui)
1157 5c934375 2012-01-20 rsc ui.quiet = False
1158 5c934375 2012-01-20 rsc ui.verbose = True # for file list
1159 5c934375 2012-01-20 rsc err = hg_commands.pull(ui, repo, **opts)
1160 5c934375 2012-01-20 rsc for line in w.output().split('\n'):
1161 5c934375 2012-01-20 rsc if isNoise(line):
1162 5c934375 2012-01-20 rsc continue
1163 5c934375 2012-01-20 rsc if line.startswith('moving '):
1164 5c934375 2012-01-20 rsc line = 'mv ' + line[len('moving '):]
1165 5c934375 2012-01-20 rsc if line.startswith('getting ') and line.find(' to ') >= 0:
1166 5c934375 2012-01-20 rsc line = 'mv ' + line[len('getting '):]
1167 5c934375 2012-01-20 rsc if line.startswith('getting '):
1168 5c934375 2012-01-20 rsc line = '+ ' + line[len('getting '):]
1169 5c934375 2012-01-20 rsc if line.startswith('removing '):
1170 5c934375 2012-01-20 rsc line = '- ' + line[len('removing '):]
1171 5c934375 2012-01-20 rsc ui.write(line + '\n')
1172 5c934375 2012-01-20 rsc return err
1173 5c934375 2012-01-20 rsc
1174 5c934375 2012-01-20 rsc def hg_push(ui, repo, **opts):
1175 5c934375 2012-01-20 rsc w = uiwrap(ui)
1176 5c934375 2012-01-20 rsc ui.quiet = False
1177 5c934375 2012-01-20 rsc ui.verbose = True
1178 5c934375 2012-01-20 rsc err = hg_commands.push(ui, repo, **opts)
1179 5c934375 2012-01-20 rsc for line in w.output().split('\n'):
1180 5c934375 2012-01-20 rsc if not isNoise(line):
1181 5c934375 2012-01-20 rsc ui.write(line + '\n')
1182 5c934375 2012-01-20 rsc return err
1183 5c934375 2012-01-20 rsc
1184 5c934375 2012-01-20 rsc def hg_commit(ui, repo, *pats, **opts):
1185 5c934375 2012-01-20 rsc return hg_commands.commit(ui, repo, *pats, **opts)
1186 5c934375 2012-01-20 rsc
1187 5c934375 2012-01-20 rsc #######################################################################
1188 5c934375 2012-01-20 rsc # Mercurial precommit hook to disable commit except through this interface.
1189 5c934375 2012-01-20 rsc
1190 5c934375 2012-01-20 rsc commit_okay = False
1191 5c934375 2012-01-20 rsc
1192 5c934375 2012-01-20 rsc def precommithook(ui, repo, **opts):
1193 5c934375 2012-01-20 rsc if commit_okay:
1194 5c934375 2012-01-20 rsc return False # False means okay.
1195 5c934375 2012-01-20 rsc ui.write("\ncodereview extension enabled; use mail, upload, or submit instead of commit\n\n")
1196 5c934375 2012-01-20 rsc return True
1197 5c934375 2012-01-20 rsc
1198 5c934375 2012-01-20 rsc #######################################################################
1199 5c934375 2012-01-20 rsc # @clnumber file pattern support
1200 5c934375 2012-01-20 rsc
1201 5c934375 2012-01-20 rsc # We replace scmutil.match with the MatchAt wrapper to add the @clnumber pattern.
1202 5c934375 2012-01-20 rsc
1203 5c934375 2012-01-20 rsc match_repo = None
1204 5c934375 2012-01-20 rsc match_ui = None
1205 5c934375 2012-01-20 rsc match_orig = None
1206 5c934375 2012-01-20 rsc
1207 5c934375 2012-01-20 rsc def InstallMatch(ui, repo):
1208 5c934375 2012-01-20 rsc global match_repo
1209 5c934375 2012-01-20 rsc global match_ui
1210 5c934375 2012-01-20 rsc global match_orig
1211 5c934375 2012-01-20 rsc
1212 5c934375 2012-01-20 rsc match_ui = ui
1213 5c934375 2012-01-20 rsc match_repo = repo
1214 5c934375 2012-01-20 rsc
1215 5c934375 2012-01-20 rsc from mercurial import scmutil
1216 5c934375 2012-01-20 rsc match_orig = scmutil.match
1217 5c934375 2012-01-20 rsc scmutil.match = MatchAt
1218 5c934375 2012-01-20 rsc
1219 5c934375 2012-01-20 rsc def MatchAt(ctx, pats=None, opts=None, globbed=False, default='relpath'):
1220 5c934375 2012-01-20 rsc taken = []
1221 5c934375 2012-01-20 rsc files = []
1222 5c934375 2012-01-20 rsc pats = pats or []
1223 5c934375 2012-01-20 rsc opts = opts or {}
1224 5c934375 2012-01-20 rsc
1225 5c934375 2012-01-20 rsc for p in pats:
1226 5c934375 2012-01-20 rsc if p.startswith('@'):
1227 5c934375 2012-01-20 rsc taken.append(p)
1228 5c934375 2012-01-20 rsc clname = p[1:]
1229 5c934375 2012-01-20 rsc if clname == "default":
1230 5c934375 2012-01-20 rsc files = DefaultFiles(match_ui, match_repo, [])
1231 5c934375 2012-01-20 rsc else:
1232 5c934375 2012-01-20 rsc if not GoodCLName(clname):
1233 5c934375 2012-01-20 rsc raise hg_util.Abort("invalid CL name " + clname)
1234 5c934375 2012-01-20 rsc cl, err = LoadCL(match_repo.ui, match_repo, clname, web=False)
1235 5c934375 2012-01-20 rsc if err != '':
1236 5c934375 2012-01-20 rsc raise hg_util.Abort("loading CL " + clname + ": " + err)
1237 5c934375 2012-01-20 rsc if not cl.files:
1238 5c934375 2012-01-20 rsc raise hg_util.Abort("no files in CL " + clname)
1239 5c934375 2012-01-20 rsc files = Add(files, cl.files)
1240 5c934375 2012-01-20 rsc pats = Sub(pats, taken) + ['path:'+f for f in files]
1241 5c934375 2012-01-20 rsc
1242 5c934375 2012-01-20 rsc # work-around for http://selenic.com/hg/rev/785bbc8634f8
1243 5c934375 2012-01-20 rsc if not hasattr(ctx, 'match'):
1244 5c934375 2012-01-20 rsc ctx = ctx[None]
1245 5c934375 2012-01-20 rsc return match_orig(ctx, pats=pats, opts=opts, globbed=globbed, default=default)
1246 5c934375 2012-01-20 rsc
1247 5c934375 2012-01-20 rsc #######################################################################
1248 5c934375 2012-01-20 rsc # Commands added by code review extension.
1249 63550fce 2012-07-14 rsc
1250 63550fce 2012-07-14 rsc # As of Mercurial 2.1 the commands are all required to return integer
1251 63550fce 2012-07-14 rsc # exit codes, whereas earlier versions allowed returning arbitrary strings
1252 63550fce 2012-07-14 rsc # to be printed as errors. We wrap the old functions to make sure we
1253 63550fce 2012-07-14 rsc # always return integer exit codes now. Otherwise Mercurial dies
1254 63550fce 2012-07-14 rsc # with a TypeError traceback (unsupported operand type(s) for &: 'str' and 'int').
1255 63550fce 2012-07-14 rsc # Introduce a Python decorator to convert old functions to the new
1256 63550fce 2012-07-14 rsc # stricter convention.
1257 5c934375 2012-01-20 rsc
1258 63550fce 2012-07-14 rsc def hgcommand(f):
1259 63550fce 2012-07-14 rsc def wrapped(ui, repo, *pats, **opts):
1260 63550fce 2012-07-14 rsc err = f(ui, repo, *pats, **opts)
1261 63550fce 2012-07-14 rsc if type(err) is int:
1262 63550fce 2012-07-14 rsc return err
1263 63550fce 2012-07-14 rsc if not err:
1264 63550fce 2012-07-14 rsc return 0
1265 63550fce 2012-07-14 rsc raise hg_util.Abort(err)
1266 63550fce 2012-07-14 rsc wrapped.__doc__ = f.__doc__
1267 63550fce 2012-07-14 rsc return wrapped
1268 63550fce 2012-07-14 rsc
1269 5c934375 2012-01-20 rsc #######################################################################
1270 5c934375 2012-01-20 rsc # hg change
1271 5c934375 2012-01-20 rsc
1272 63550fce 2012-07-14 rsc @hgcommand
1273 ef27bfa4 2010-01-12 rsc def change(ui, repo, *pats, **opts):
1274 a06877af 2010-08-05 rsc """create, edit or delete a change list
1275 ef27bfa4 2010-01-12 rsc
1276 a06877af 2010-08-05 rsc Create, edit or delete a change list.
1277 ef27bfa4 2010-01-12 rsc A change list is a group of files to be reviewed and submitted together,
1278 ef27bfa4 2010-01-12 rsc plus a textual description of the change.
1279 ef27bfa4 2010-01-12 rsc Change lists are referred to by simple alphanumeric names.
1280 ef27bfa4 2010-01-12 rsc
1281 ef27bfa4 2010-01-12 rsc Changes must be reviewed before they can be submitted.
1282 ef27bfa4 2010-01-12 rsc
1283 ef27bfa4 2010-01-12 rsc In the absence of options, the change command opens the
1284 ef27bfa4 2010-01-12 rsc change list for editing in the default editor.
1285 ef27bfa4 2010-01-12 rsc
1286 ef27bfa4 2010-01-12 rsc Deleting a change with the -d or -D flag does not affect
1287 ef27bfa4 2010-01-12 rsc the contents of the files listed in that change. To revert
1288 ef27bfa4 2010-01-12 rsc the files listed in a change, use
1289 ef27bfa4 2010-01-12 rsc
1290 ef27bfa4 2010-01-12 rsc hg revert @123456
1291 ef27bfa4 2010-01-12 rsc
1292 ef27bfa4 2010-01-12 rsc before running hg change -d 123456.
1293 ef27bfa4 2010-01-12 rsc """
1294 ef27bfa4 2010-01-12 rsc
1295 5c934375 2012-01-20 rsc if codereview_disabled:
1296 5c934375 2012-01-20 rsc return codereview_disabled
1297 26b4bea8 2011-08-02 rsc
1298 ef27bfa4 2010-01-12 rsc dirty = {}
1299 ef27bfa4 2010-01-12 rsc if len(pats) > 0 and GoodCLName(pats[0]):
1300 ef27bfa4 2010-01-12 rsc name = pats[0]
1301 ef27bfa4 2010-01-12 rsc if len(pats) != 1:
1302 ef27bfa4 2010-01-12 rsc return "cannot specify CL name and file patterns"
1303 ef27bfa4 2010-01-12 rsc pats = pats[1:]
1304 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(ui, repo, name, web=True)
1305 ef27bfa4 2010-01-12 rsc if err != '':
1306 ef27bfa4 2010-01-12 rsc return err
1307 ef27bfa4 2010-01-12 rsc if not cl.local and (opts["stdin"] or not opts["stdout"]):
1308 ef27bfa4 2010-01-12 rsc return "cannot change non-local CL " + name
1309 ef27bfa4 2010-01-12 rsc else:
1310 ef27bfa4 2010-01-12 rsc name = "new"
1311 ef27bfa4 2010-01-12 rsc cl = CL("new")
1312 5c934375 2012-01-20 rsc if repo[None].branch() != "default":
1313 63550fce 2012-07-14 rsc return "cannot create CL outside default branch; switch with 'hg update default'"
1314 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1315 5c934375 2012-01-20 rsc files = ChangedFiles(ui, repo, pats, taken=Taken(ui, repo))
1316 ef27bfa4 2010-01-12 rsc
1317 ef27bfa4 2010-01-12 rsc if opts["delete"] or opts["deletelocal"]:
1318 ef27bfa4 2010-01-12 rsc if opts["delete"] and opts["deletelocal"]:
1319 ef27bfa4 2010-01-12 rsc return "cannot use -d and -D together"
1320 ef27bfa4 2010-01-12 rsc flag = "-d"
1321 ef27bfa4 2010-01-12 rsc if opts["deletelocal"]:
1322 ef27bfa4 2010-01-12 rsc flag = "-D"
1323 ef27bfa4 2010-01-12 rsc if name == "new":
1324 ef27bfa4 2010-01-12 rsc return "cannot use "+flag+" with file patterns"
1325 ef27bfa4 2010-01-12 rsc if opts["stdin"] or opts["stdout"]:
1326 ef27bfa4 2010-01-12 rsc return "cannot use "+flag+" with -i or -o"
1327 ef27bfa4 2010-01-12 rsc if not cl.local:
1328 ef27bfa4 2010-01-12 rsc return "cannot change non-local CL " + name
1329 ef27bfa4 2010-01-12 rsc if opts["delete"]:
1330 ef27bfa4 2010-01-12 rsc if cl.copied_from:
1331 ef27bfa4 2010-01-12 rsc return "original author must delete CL; hg change -D will remove locally"
1332 26b4bea8 2011-08-02 rsc PostMessage(ui, cl.name, "*** Abandoned ***", send_mail=cl.mailed)
1333 26b4bea8 2011-08-02 rsc EditDesc(cl.name, closed=True, private=cl.private)
1334 ef27bfa4 2010-01-12 rsc cl.Delete(ui, repo)
1335 ef27bfa4 2010-01-12 rsc return
1336 ef27bfa4 2010-01-12 rsc
1337 ef27bfa4 2010-01-12 rsc if opts["stdin"]:
1338 ef27bfa4 2010-01-12 rsc s = sys.stdin.read()
1339 ef27bfa4 2010-01-12 rsc clx, line, err = ParseCL(s, name)
1340 ef27bfa4 2010-01-12 rsc if err != '':
1341 ef27bfa4 2010-01-12 rsc return "error parsing change list: line %d: %s" % (line, err)
1342 ef27bfa4 2010-01-12 rsc if clx.desc is not None:
1343 ef27bfa4 2010-01-12 rsc cl.desc = clx.desc;
1344 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1345 ef27bfa4 2010-01-12 rsc if clx.reviewer is not None:
1346 ef27bfa4 2010-01-12 rsc cl.reviewer = clx.reviewer
1347 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1348 ef27bfa4 2010-01-12 rsc if clx.cc is not None:
1349 ef27bfa4 2010-01-12 rsc cl.cc = clx.cc
1350 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1351 ef27bfa4 2010-01-12 rsc if clx.files is not None:
1352 ef27bfa4 2010-01-12 rsc cl.files = clx.files
1353 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1354 26b4bea8 2011-08-02 rsc if clx.private != cl.private:
1355 26b4bea8 2011-08-02 rsc cl.private = clx.private
1356 26b4bea8 2011-08-02 rsc dirty[cl] = True
1357 ef27bfa4 2010-01-12 rsc
1358 ef27bfa4 2010-01-12 rsc if not opts["stdin"] and not opts["stdout"]:
1359 ef27bfa4 2010-01-12 rsc if name == "new":
1360 ef27bfa4 2010-01-12 rsc cl.files = files
1361 ef27bfa4 2010-01-12 rsc err = EditCL(ui, repo, cl)
1362 ef27bfa4 2010-01-12 rsc if err != "":
1363 ef27bfa4 2010-01-12 rsc return err
1364 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1365 ef27bfa4 2010-01-12 rsc
1366 ef27bfa4 2010-01-12 rsc for d, _ in dirty.items():
1367 26b4bea8 2011-08-02 rsc name = d.name
1368 ef27bfa4 2010-01-12 rsc d.Flush(ui, repo)
1369 26b4bea8 2011-08-02 rsc if name == "new":
1370 26b4bea8 2011-08-02 rsc d.Upload(ui, repo, quiet=True)
1371 ef27bfa4 2010-01-12 rsc
1372 ef27bfa4 2010-01-12 rsc if opts["stdout"]:
1373 ef27bfa4 2010-01-12 rsc ui.write(cl.EditorText())
1374 26b4bea8 2011-08-02 rsc elif opts["pending"]:
1375 26b4bea8 2011-08-02 rsc ui.write(cl.PendingText())
1376 ef27bfa4 2010-01-12 rsc elif name == "new":
1377 ef27bfa4 2010-01-12 rsc if ui.quiet:
1378 ef27bfa4 2010-01-12 rsc ui.write(cl.name)
1379 ef27bfa4 2010-01-12 rsc else:
1380 ef27bfa4 2010-01-12 rsc ui.write("CL created: " + cl.url + "\n")
1381 ef27bfa4 2010-01-12 rsc return
1382 ef27bfa4 2010-01-12 rsc
1383 5c934375 2012-01-20 rsc #######################################################################
1384 5c934375 2012-01-20 rsc # hg code-login (broken?)
1385 5c934375 2012-01-20 rsc
1386 63550fce 2012-07-14 rsc @hgcommand
1387 ef27bfa4 2010-01-12 rsc def code_login(ui, repo, **opts):
1388 ef27bfa4 2010-01-12 rsc """log in to code review server
1389 ef27bfa4 2010-01-12 rsc
1390 ef27bfa4 2010-01-12 rsc Logs in to the code review server, saving a cookie in
1391 ef27bfa4 2010-01-12 rsc a file in your home directory.
1392 ef27bfa4 2010-01-12 rsc """
1393 5c934375 2012-01-20 rsc if codereview_disabled:
1394 5c934375 2012-01-20 rsc return codereview_disabled
1395 26b4bea8 2011-08-02 rsc
1396 ef27bfa4 2010-01-12 rsc MySend(None)
1397 ef27bfa4 2010-01-12 rsc
1398 5c934375 2012-01-20 rsc #######################################################################
1399 5c934375 2012-01-20 rsc # hg clpatch / undo / release-apply / download
1400 5c934375 2012-01-20 rsc # All concerned with applying or unapplying patches to the repository.
1401 5c934375 2012-01-20 rsc
1402 63550fce 2012-07-14 rsc @hgcommand
1403 ef27bfa4 2010-01-12 rsc def clpatch(ui, repo, clname, **opts):
1404 ef27bfa4 2010-01-12 rsc """import a patch from the code review server
1405 ef27bfa4 2010-01-12 rsc
1406 ef27bfa4 2010-01-12 rsc Imports a patch from the code review server into the local client.
1407 ef27bfa4 2010-01-12 rsc If the local client has already modified any of the files that the
1408 ef27bfa4 2010-01-12 rsc patch modifies, this command will refuse to apply the patch.
1409 ef27bfa4 2010-01-12 rsc
1410 ef27bfa4 2010-01-12 rsc Submitting an imported patch will keep the original author's
1411 ef27bfa4 2010-01-12 rsc name as the Author: line but add your own name to a Committer: line.
1412 ef27bfa4 2010-01-12 rsc """
1413 26b4bea8 2011-08-02 rsc if repo[None].branch() != "default":
1414 26b4bea8 2011-08-02 rsc return "cannot run hg clpatch outside default branch"
1415 26b4bea8 2011-08-02 rsc return clpatch_or_undo(ui, repo, clname, opts, mode="clpatch")
1416 26b4bea8 2011-08-02 rsc
1417 63550fce 2012-07-14 rsc @hgcommand
1418 26b4bea8 2011-08-02 rsc def undo(ui, repo, clname, **opts):
1419 26b4bea8 2011-08-02 rsc """undo the effect of a CL
1420 26b4bea8 2011-08-02 rsc
1421 26b4bea8 2011-08-02 rsc Creates a new CL that undoes an earlier CL.
1422 26b4bea8 2011-08-02 rsc After creating the CL, opens the CL text for editing so that
1423 26b4bea8 2011-08-02 rsc you can add the reason for the undo to the description.
1424 26b4bea8 2011-08-02 rsc """
1425 26b4bea8 2011-08-02 rsc if repo[None].branch() != "default":
1426 26b4bea8 2011-08-02 rsc return "cannot run hg undo outside default branch"
1427 26b4bea8 2011-08-02 rsc return clpatch_or_undo(ui, repo, clname, opts, mode="undo")
1428 26b4bea8 2011-08-02 rsc
1429 63550fce 2012-07-14 rsc @hgcommand
1430 26b4bea8 2011-08-02 rsc def release_apply(ui, repo, clname, **opts):
1431 26b4bea8 2011-08-02 rsc """apply a CL to the release branch
1432 26b4bea8 2011-08-02 rsc
1433 26b4bea8 2011-08-02 rsc Creates a new CL copying a previously committed change
1434 26b4bea8 2011-08-02 rsc from the main branch to the release branch.
1435 26b4bea8 2011-08-02 rsc The current client must either be clean or already be in
1436 26b4bea8 2011-08-02 rsc the release branch.
1437 26b4bea8 2011-08-02 rsc
1438 26b4bea8 2011-08-02 rsc The release branch must be created by starting with a
1439 26b4bea8 2011-08-02 rsc clean client, disabling the code review plugin, and running:
1440 26b4bea8 2011-08-02 rsc
1441 26b4bea8 2011-08-02 rsc hg update weekly.YYYY-MM-DD
1442 26b4bea8 2011-08-02 rsc hg branch release-branch.rNN
1443 26b4bea8 2011-08-02 rsc hg commit -m 'create release-branch.rNN'
1444 26b4bea8 2011-08-02 rsc hg push --new-branch
1445 26b4bea8 2011-08-02 rsc
1446 26b4bea8 2011-08-02 rsc Then re-enable the code review plugin.
1447 26b4bea8 2011-08-02 rsc
1448 26b4bea8 2011-08-02 rsc People can test the release branch by running
1449 26b4bea8 2011-08-02 rsc
1450 26b4bea8 2011-08-02 rsc hg update release-branch.rNN
1451 26b4bea8 2011-08-02 rsc
1452 26b4bea8 2011-08-02 rsc in a clean client. To return to the normal tree,
1453 26b4bea8 2011-08-02 rsc
1454 26b4bea8 2011-08-02 rsc hg update default
1455 26b4bea8 2011-08-02 rsc
1456 26b4bea8 2011-08-02 rsc Move changes since the weekly into the release branch
1457 26b4bea8 2011-08-02 rsc using hg release-apply followed by the usual code review
1458 26b4bea8 2011-08-02 rsc process and hg submit.
1459 26b4bea8 2011-08-02 rsc
1460 26b4bea8 2011-08-02 rsc When it comes time to tag the release, record the
1461 26b4bea8 2011-08-02 rsc final long-form tag of the release-branch.rNN
1462 26b4bea8 2011-08-02 rsc in the *default* branch's .hgtags file. That is, run
1463 26b4bea8 2011-08-02 rsc
1464 26b4bea8 2011-08-02 rsc hg update default
1465 26b4bea8 2011-08-02 rsc
1466 26b4bea8 2011-08-02 rsc and then edit .hgtags as you would for a weekly.
1467 26b4bea8 2011-08-02 rsc
1468 26b4bea8 2011-08-02 rsc """
1469 26b4bea8 2011-08-02 rsc c = repo[None]
1470 26b4bea8 2011-08-02 rsc if not releaseBranch:
1471 26b4bea8 2011-08-02 rsc return "no active release branches"
1472 26b4bea8 2011-08-02 rsc if c.branch() != releaseBranch:
1473 26b4bea8 2011-08-02 rsc if c.modified() or c.added() or c.removed():
1474 5c934375 2012-01-20 rsc raise hg_util.Abort("uncommitted local changes - cannot switch branches")
1475 5c934375 2012-01-20 rsc err = hg_clean(repo, releaseBranch)
1476 26b4bea8 2011-08-02 rsc if err:
1477 26b4bea8 2011-08-02 rsc return err
1478 ef27bfa4 2010-01-12 rsc try:
1479 26b4bea8 2011-08-02 rsc err = clpatch_or_undo(ui, repo, clname, opts, mode="backport")
1480 26b4bea8 2011-08-02 rsc if err:
1481 5c934375 2012-01-20 rsc raise hg_util.Abort(err)
1482 26b4bea8 2011-08-02 rsc except Exception, e:
1483 5c934375 2012-01-20 rsc hg_clean(repo, "default")
1484 26b4bea8 2011-08-02 rsc raise e
1485 26b4bea8 2011-08-02 rsc return None
1486 26b4bea8 2011-08-02 rsc
1487 26b4bea8 2011-08-02 rsc def rev2clname(rev):
1488 26b4bea8 2011-08-02 rsc # Extract CL name from revision description.
1489 26b4bea8 2011-08-02 rsc # The last line in the description that is a codereview URL is the real one.
1490 26b4bea8 2011-08-02 rsc # Earlier lines might be part of the user-written description.
1491 26b4bea8 2011-08-02 rsc all = re.findall('(?m)^http://codereview.appspot.com/([0-9]+)$', rev.description())
1492 26b4bea8 2011-08-02 rsc if len(all) > 0:
1493 26b4bea8 2011-08-02 rsc return all[-1]
1494 26b4bea8 2011-08-02 rsc return ""
1495 26b4bea8 2011-08-02 rsc
1496 26b4bea8 2011-08-02 rsc undoHeader = """undo CL %s / %s
1497 26b4bea8 2011-08-02 rsc
1498 26b4bea8 2011-08-02 rsc <enter reason for undo>
1499 26b4bea8 2011-08-02 rsc
1500 26b4bea8 2011-08-02 rsc ««« original CL description
1501 26b4bea8 2011-08-02 rsc """
1502 26b4bea8 2011-08-02 rsc
1503 26b4bea8 2011-08-02 rsc undoFooter = """
1504 26b4bea8 2011-08-02 rsc »»»
1505 26b4bea8 2011-08-02 rsc """
1506 26b4bea8 2011-08-02 rsc
1507 26b4bea8 2011-08-02 rsc backportHeader = """[%s] %s
1508 26b4bea8 2011-08-02 rsc
1509 26b4bea8 2011-08-02 rsc ««« CL %s / %s
1510 26b4bea8 2011-08-02 rsc """
1511 26b4bea8 2011-08-02 rsc
1512 26b4bea8 2011-08-02 rsc backportFooter = """
1513 26b4bea8 2011-08-02 rsc »»»
1514 26b4bea8 2011-08-02 rsc """
1515 26b4bea8 2011-08-02 rsc
1516 26b4bea8 2011-08-02 rsc # Implementation of clpatch/undo.
1517 26b4bea8 2011-08-02 rsc def clpatch_or_undo(ui, repo, clname, opts, mode):
1518 5c934375 2012-01-20 rsc if codereview_disabled:
1519 5c934375 2012-01-20 rsc return codereview_disabled
1520 26b4bea8 2011-08-02 rsc
1521 26b4bea8 2011-08-02 rsc if mode == "undo" or mode == "backport":
1522 26b4bea8 2011-08-02 rsc # Find revision in Mercurial repository.
1523 26b4bea8 2011-08-02 rsc # Assume CL number is 7+ decimal digits.
1524 26b4bea8 2011-08-02 rsc # Otherwise is either change log sequence number (fewer decimal digits),
1525 26b4bea8 2011-08-02 rsc # hexadecimal hash, or tag name.
1526 26b4bea8 2011-08-02 rsc # Mercurial will fall over long before the change log
1527 26b4bea8 2011-08-02 rsc # sequence numbers get to be 7 digits long.
1528 26b4bea8 2011-08-02 rsc if re.match('^[0-9]{7,}$', clname):
1529 26b4bea8 2011-08-02 rsc found = False
1530 5c934375 2012-01-20 rsc for r in hg_log(ui, repo, keyword="codereview.appspot.com/"+clname, limit=100, template="{node}\n").split():
1531 5c934375 2012-01-20 rsc rev = repo[r]
1532 26b4bea8 2011-08-02 rsc # Last line with a code review URL is the actual review URL.
1533 26b4bea8 2011-08-02 rsc # Earlier ones might be part of the CL description.
1534 26b4bea8 2011-08-02 rsc n = rev2clname(rev)
1535 26b4bea8 2011-08-02 rsc if n == clname:
1536 26b4bea8 2011-08-02 rsc found = True
1537 26b4bea8 2011-08-02 rsc break
1538 26b4bea8 2011-08-02 rsc if not found:
1539 26b4bea8 2011-08-02 rsc return "cannot find CL %s in local repository" % clname
1540 26b4bea8 2011-08-02 rsc else:
1541 26b4bea8 2011-08-02 rsc rev = repo[clname]
1542 26b4bea8 2011-08-02 rsc if not rev:
1543 26b4bea8 2011-08-02 rsc return "unknown revision %s" % clname
1544 26b4bea8 2011-08-02 rsc clname = rev2clname(rev)
1545 26b4bea8 2011-08-02 rsc if clname == "":
1546 26b4bea8 2011-08-02 rsc return "cannot find CL name in revision description"
1547 26b4bea8 2011-08-02 rsc
1548 26b4bea8 2011-08-02 rsc # Create fresh CL and start with patch that would reverse the change.
1549 5c934375 2012-01-20 rsc vers = hg_node.short(rev.node())
1550 26b4bea8 2011-08-02 rsc cl = CL("new")
1551 26b4bea8 2011-08-02 rsc desc = str(rev.description())
1552 26b4bea8 2011-08-02 rsc if mode == "undo":
1553 26b4bea8 2011-08-02 rsc cl.desc = (undoHeader % (clname, vers)) + desc + undoFooter
1554 26b4bea8 2011-08-02 rsc else:
1555 26b4bea8 2011-08-02 rsc cl.desc = (backportHeader % (releaseBranch, line1(desc), clname, vers)) + desc + undoFooter
1556 26b4bea8 2011-08-02 rsc v1 = vers
1557 5c934375 2012-01-20 rsc v0 = hg_node.short(rev.parents()[0].node())
1558 26b4bea8 2011-08-02 rsc if mode == "undo":
1559 26b4bea8 2011-08-02 rsc arg = v1 + ":" + v0
1560 26b4bea8 2011-08-02 rsc else:
1561 26b4bea8 2011-08-02 rsc vers = v0
1562 26b4bea8 2011-08-02 rsc arg = v0 + ":" + v1
1563 26b4bea8 2011-08-02 rsc patch = RunShell(["hg", "diff", "--git", "-r", arg])
1564 26b4bea8 2011-08-02 rsc
1565 26b4bea8 2011-08-02 rsc else: # clpatch
1566 26b4bea8 2011-08-02 rsc cl, vers, patch, err = DownloadCL(ui, repo, clname)
1567 26b4bea8 2011-08-02 rsc if err != "":
1568 26b4bea8 2011-08-02 rsc return err
1569 26b4bea8 2011-08-02 rsc if patch == emptydiff:
1570 26b4bea8 2011-08-02 rsc return "codereview issue %s has no diff" % clname
1571 26b4bea8 2011-08-02 rsc
1572 26b4bea8 2011-08-02 rsc # find current hg version (hg identify)
1573 26b4bea8 2011-08-02 rsc ctx = repo[None]
1574 26b4bea8 2011-08-02 rsc parents = ctx.parents()
1575 5c934375 2012-01-20 rsc id = '+'.join([hg_node.short(p.node()) for p in parents])
1576 26b4bea8 2011-08-02 rsc
1577 26b4bea8 2011-08-02 rsc # if version does not match the patch version,
1578 26b4bea8 2011-08-02 rsc # try to update the patch line numbers.
1579 26b4bea8 2011-08-02 rsc if vers != "" and id != vers:
1580 26b4bea8 2011-08-02 rsc # "vers in repo" gives the wrong answer
1581 26b4bea8 2011-08-02 rsc # on some versions of Mercurial. Instead, do the actual
1582 26b4bea8 2011-08-02 rsc # lookup and catch the exception.
1583 26b4bea8 2011-08-02 rsc try:
1584 26b4bea8 2011-08-02 rsc repo[vers].description()
1585 26b4bea8 2011-08-02 rsc except:
1586 26b4bea8 2011-08-02 rsc return "local repository is out of date; sync to get %s" % (vers)
1587 26b4bea8 2011-08-02 rsc patch1, err = portPatch(repo, patch, vers, id)
1588 26b4bea8 2011-08-02 rsc if err != "":
1589 26b4bea8 2011-08-02 rsc if not opts["ignore_hgpatch_failure"]:
1590 26b4bea8 2011-08-02 rsc return "codereview issue %s is out of date: %s (%s->%s)" % (clname, err, vers, id)
1591 26b4bea8 2011-08-02 rsc else:
1592 26b4bea8 2011-08-02 rsc patch = patch1
1593 26b4bea8 2011-08-02 rsc argv = ["hgpatch"]
1594 26b4bea8 2011-08-02 rsc if opts["no_incoming"] or mode == "backport":
1595 26b4bea8 2011-08-02 rsc argv += ["--checksync=false"]
1596 26b4bea8 2011-08-02 rsc try:
1597 26b4bea8 2011-08-02 rsc cmd = subprocess.Popen(argv, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, close_fds=sys.platform != "win32")
1598 ef27bfa4 2010-01-12 rsc except:
1599 63550fce 2012-07-14 rsc return "hgpatch: " + ExceptionDetail() + "\nInstall hgpatch with:\n$ go get code.google.com/p/go.codereview/cmd/hgpatch\n"
1600 26b4bea8 2011-08-02 rsc
1601 26b4bea8 2011-08-02 rsc out, err = cmd.communicate(patch)
1602 26b4bea8 2011-08-02 rsc if cmd.returncode != 0 and not opts["ignore_hgpatch_failure"]:
1603 ef27bfa4 2010-01-12 rsc return "hgpatch failed"
1604 ef27bfa4 2010-01-12 rsc cl.local = True
1605 ef27bfa4 2010-01-12 rsc cl.files = out.strip().split()
1606 26b4bea8 2011-08-02 rsc if not cl.files and not opts["ignore_hgpatch_failure"]:
1607 26b4bea8 2011-08-02 rsc return "codereview issue %s has no changed files" % clname
1608 5c934375 2012-01-20 rsc files = ChangedFiles(ui, repo, [])
1609 ef27bfa4 2010-01-12 rsc extra = Sub(cl.files, files)
1610 ef27bfa4 2010-01-12 rsc if extra:
1611 ef27bfa4 2010-01-12 rsc ui.warn("warning: these files were listed in the patch but not changed:\n\t" + "\n\t".join(extra) + "\n")
1612 ef27bfa4 2010-01-12 rsc cl.Flush(ui, repo)
1613 26b4bea8 2011-08-02 rsc if mode == "undo":
1614 26b4bea8 2011-08-02 rsc err = EditCL(ui, repo, cl)
1615 26b4bea8 2011-08-02 rsc if err != "":
1616 26b4bea8 2011-08-02 rsc return "CL created, but error editing: " + err
1617 26b4bea8 2011-08-02 rsc cl.Flush(ui, repo)
1618 26b4bea8 2011-08-02 rsc else:
1619 26b4bea8 2011-08-02 rsc ui.write(cl.PendingText() + "\n")
1620 26b4bea8 2011-08-02 rsc
1621 26b4bea8 2011-08-02 rsc # portPatch rewrites patch from being a patch against
1622 26b4bea8 2011-08-02 rsc # oldver to being a patch against newver.
1623 26b4bea8 2011-08-02 rsc def portPatch(repo, patch, oldver, newver):
1624 26b4bea8 2011-08-02 rsc lines = patch.splitlines(True) # True = keep \n
1625 26b4bea8 2011-08-02 rsc delta = None
1626 26b4bea8 2011-08-02 rsc for i in range(len(lines)):
1627 26b4bea8 2011-08-02 rsc line = lines[i]
1628 26b4bea8 2011-08-02 rsc if line.startswith('--- a/'):
1629 26b4bea8 2011-08-02 rsc file = line[6:-1]
1630 26b4bea8 2011-08-02 rsc delta = fileDeltas(repo, file, oldver, newver)
1631 26b4bea8 2011-08-02 rsc if not delta or not line.startswith('@@ '):
1632 26b4bea8 2011-08-02 rsc continue
1633 26b4bea8 2011-08-02 rsc # @@ -x,y +z,w @@ means the patch chunk replaces
1634 26b4bea8 2011-08-02 rsc # the original file's line numbers x up to x+y with the
1635 26b4bea8 2011-08-02 rsc # line numbers z up to z+w in the new file.
1636 26b4bea8 2011-08-02 rsc # Find the delta from x in the original to the same
1637 26b4bea8 2011-08-02 rsc # line in the current version and add that delta to both
1638 26b4bea8 2011-08-02 rsc # x and z.
1639 26b4bea8 2011-08-02 rsc m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
1640 26b4bea8 2011-08-02 rsc if not m:
1641 26b4bea8 2011-08-02 rsc return None, "error parsing patch line numbers"
1642 26b4bea8 2011-08-02 rsc n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
1643 26b4bea8 2011-08-02 rsc d, err = lineDelta(delta, n1, len1)
1644 26b4bea8 2011-08-02 rsc if err != "":
1645 26b4bea8 2011-08-02 rsc return "", err
1646 26b4bea8 2011-08-02 rsc n1 += d
1647 26b4bea8 2011-08-02 rsc n2 += d
1648 26b4bea8 2011-08-02 rsc lines[i] = "@@ -%d,%d +%d,%d @@\n" % (n1, len1, n2, len2)
1649 26b4bea8 2011-08-02 rsc
1650 26b4bea8 2011-08-02 rsc newpatch = ''.join(lines)
1651 26b4bea8 2011-08-02 rsc return newpatch, ""
1652 ef27bfa4 2010-01-12 rsc
1653 26b4bea8 2011-08-02 rsc # fileDelta returns the line number deltas for the given file's
1654 26b4bea8 2011-08-02 rsc # changes from oldver to newver.
1655 26b4bea8 2011-08-02 rsc # The deltas are a list of (n, len, newdelta) triples that say
1656 26b4bea8 2011-08-02 rsc # lines [n, n+len) were modified, and after that range the
1657 26b4bea8 2011-08-02 rsc # line numbers are +newdelta from what they were before.
1658 26b4bea8 2011-08-02 rsc def fileDeltas(repo, file, oldver, newver):
1659 26b4bea8 2011-08-02 rsc cmd = ["hg", "diff", "--git", "-r", oldver + ":" + newver, "path:" + file]
1660 26b4bea8 2011-08-02 rsc data = RunShell(cmd, silent_ok=True)
1661 26b4bea8 2011-08-02 rsc deltas = []
1662 26b4bea8 2011-08-02 rsc for line in data.splitlines():
1663 26b4bea8 2011-08-02 rsc m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
1664 26b4bea8 2011-08-02 rsc if not m:
1665 26b4bea8 2011-08-02 rsc continue
1666 26b4bea8 2011-08-02 rsc n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
1667 26b4bea8 2011-08-02 rsc deltas.append((n1, len1, n2+len2-(n1+len1)))
1668 26b4bea8 2011-08-02 rsc return deltas
1669 26b4bea8 2011-08-02 rsc
1670 26b4bea8 2011-08-02 rsc # lineDelta finds the appropriate line number delta to apply to the lines [n, n+len).
1671 26b4bea8 2011-08-02 rsc # It returns an error if those lines were rewritten by the patch.
1672 26b4bea8 2011-08-02 rsc def lineDelta(deltas, n, len):
1673 26b4bea8 2011-08-02 rsc d = 0
1674 26b4bea8 2011-08-02 rsc for (old, oldlen, newdelta) in deltas:
1675 26b4bea8 2011-08-02 rsc if old >= n+len:
1676 26b4bea8 2011-08-02 rsc break
1677 26b4bea8 2011-08-02 rsc if old+len > n:
1678 26b4bea8 2011-08-02 rsc return 0, "patch and recent changes conflict"
1679 26b4bea8 2011-08-02 rsc d = newdelta
1680 26b4bea8 2011-08-02 rsc return d, ""
1681 26b4bea8 2011-08-02 rsc
1682 63550fce 2012-07-14 rsc @hgcommand
1683 ef27bfa4 2010-01-12 rsc def download(ui, repo, clname, **opts):
1684 ef27bfa4 2010-01-12 rsc """download a change from the code review server
1685 ef27bfa4 2010-01-12 rsc
1686 ef27bfa4 2010-01-12 rsc Download prints a description of the given change list
1687 ef27bfa4 2010-01-12 rsc followed by its diff, downloaded from the code review server.
1688 ef27bfa4 2010-01-12 rsc """
1689 5c934375 2012-01-20 rsc if codereview_disabled:
1690 5c934375 2012-01-20 rsc return codereview_disabled
1691 26b4bea8 2011-08-02 rsc
1692 26b4bea8 2011-08-02 rsc cl, vers, patch, err = DownloadCL(ui, repo, clname)
1693 ef27bfa4 2010-01-12 rsc if err != "":
1694 ef27bfa4 2010-01-12 rsc return err
1695 ef27bfa4 2010-01-12 rsc ui.write(cl.EditorText() + "\n")
1696 ef27bfa4 2010-01-12 rsc ui.write(patch + "\n")
1697 ef27bfa4 2010-01-12 rsc return
1698 ef27bfa4 2010-01-12 rsc
1699 5c934375 2012-01-20 rsc #######################################################################
1700 5c934375 2012-01-20 rsc # hg file
1701 5c934375 2012-01-20 rsc
1702 63550fce 2012-07-14 rsc @hgcommand
1703 ef27bfa4 2010-01-12 rsc def file(ui, repo, clname, pat, *pats, **opts):
1704 ef27bfa4 2010-01-12 rsc """assign files to or remove files from a change list
1705 ef27bfa4 2010-01-12 rsc
1706 ef27bfa4 2010-01-12 rsc Assign files to or (with -d) remove files from a change list.
1707 ef27bfa4 2010-01-12 rsc
1708 ef27bfa4 2010-01-12 rsc The -d option only removes files from the change list.
1709 ef27bfa4 2010-01-12 rsc It does not edit them or remove them from the repository.
1710 ef27bfa4 2010-01-12 rsc """
1711 5c934375 2012-01-20 rsc if codereview_disabled:
1712 5c934375 2012-01-20 rsc return codereview_disabled
1713 26b4bea8 2011-08-02 rsc
1714 ef27bfa4 2010-01-12 rsc pats = tuple([pat] + list(pats))
1715 ef27bfa4 2010-01-12 rsc if not GoodCLName(clname):
1716 ef27bfa4 2010-01-12 rsc return "invalid CL name " + clname
1717 ef27bfa4 2010-01-12 rsc
1718 ef27bfa4 2010-01-12 rsc dirty = {}
1719 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(ui, repo, clname, web=False)
1720 ef27bfa4 2010-01-12 rsc if err != '':
1721 ef27bfa4 2010-01-12 rsc return err
1722 ef27bfa4 2010-01-12 rsc if not cl.local:
1723 ef27bfa4 2010-01-12 rsc return "cannot change non-local CL " + clname
1724 ef27bfa4 2010-01-12 rsc
1725 5c934375 2012-01-20 rsc files = ChangedFiles(ui, repo, pats)
1726 ef27bfa4 2010-01-12 rsc
1727 ef27bfa4 2010-01-12 rsc if opts["delete"]:
1728 ef27bfa4 2010-01-12 rsc oldfiles = Intersect(files, cl.files)
1729 ef27bfa4 2010-01-12 rsc if oldfiles:
1730 ef27bfa4 2010-01-12 rsc if not ui.quiet:
1731 ef27bfa4 2010-01-12 rsc ui.status("# Removing files from CL. To undo:\n")
1732 ef27bfa4 2010-01-12 rsc ui.status("# cd %s\n" % (repo.root))
1733 ef27bfa4 2010-01-12 rsc for f in oldfiles:
1734 ef27bfa4 2010-01-12 rsc ui.status("# hg file %s %s\n" % (cl.name, f))
1735 ef27bfa4 2010-01-12 rsc cl.files = Sub(cl.files, oldfiles)
1736 ef27bfa4 2010-01-12 rsc cl.Flush(ui, repo)
1737 ef27bfa4 2010-01-12 rsc else:
1738 ef27bfa4 2010-01-12 rsc ui.status("no such files in CL")
1739 ef27bfa4 2010-01-12 rsc return
1740 ef27bfa4 2010-01-12 rsc
1741 ef27bfa4 2010-01-12 rsc if not files:
1742 ef27bfa4 2010-01-12 rsc return "no such modified files"
1743 ef27bfa4 2010-01-12 rsc
1744 ef27bfa4 2010-01-12 rsc files = Sub(files, cl.files)
1745 ef27bfa4 2010-01-12 rsc taken = Taken(ui, repo)
1746 ef27bfa4 2010-01-12 rsc warned = False
1747 ef27bfa4 2010-01-12 rsc for f in files:
1748 ef27bfa4 2010-01-12 rsc if f in taken:
1749 ef27bfa4 2010-01-12 rsc if not warned and not ui.quiet:
1750 ef27bfa4 2010-01-12 rsc ui.status("# Taking files from other CLs. To undo:\n")
1751 ef27bfa4 2010-01-12 rsc ui.status("# cd %s\n" % (repo.root))
1752 ef27bfa4 2010-01-12 rsc warned = True
1753 ef27bfa4 2010-01-12 rsc ocl = taken[f]
1754 ef27bfa4 2010-01-12 rsc if not ui.quiet:
1755 ef27bfa4 2010-01-12 rsc ui.status("# hg file %s %s\n" % (ocl.name, f))
1756 ef27bfa4 2010-01-12 rsc if ocl not in dirty:
1757 ef27bfa4 2010-01-12 rsc ocl.files = Sub(ocl.files, files)
1758 ef27bfa4 2010-01-12 rsc dirty[ocl] = True
1759 ef27bfa4 2010-01-12 rsc cl.files = Add(cl.files, files)
1760 ef27bfa4 2010-01-12 rsc dirty[cl] = True
1761 ef27bfa4 2010-01-12 rsc for d, _ in dirty.items():
1762 ef27bfa4 2010-01-12 rsc d.Flush(ui, repo)
1763 ef27bfa4 2010-01-12 rsc return
1764 ef27bfa4 2010-01-12 rsc
1765 5c934375 2012-01-20 rsc #######################################################################
1766 5c934375 2012-01-20 rsc # hg gofmt
1767 5c934375 2012-01-20 rsc
1768 63550fce 2012-07-14 rsc @hgcommand
1769 ef27bfa4 2010-01-12 rsc def gofmt(ui, repo, *pats, **opts):
1770 ef27bfa4 2010-01-12 rsc """apply gofmt to modified files
1771 ef27bfa4 2010-01-12 rsc
1772 ef27bfa4 2010-01-12 rsc Applies gofmt to the modified files in the repository that match
1773 ef27bfa4 2010-01-12 rsc the given patterns.
1774 ef27bfa4 2010-01-12 rsc """
1775 5c934375 2012-01-20 rsc if codereview_disabled:
1776 5c934375 2012-01-20 rsc return codereview_disabled
1777 26b4bea8 2011-08-02 rsc
1778 ef27bfa4 2010-01-12 rsc files = ChangedExistingFiles(ui, repo, pats, opts)
1779 63550fce 2012-07-14 rsc files = gofmt_required(files)
1780 ef27bfa4 2010-01-12 rsc if not files:
1781 ef27bfa4 2010-01-12 rsc return "no modified go files"
1782 ef27bfa4 2010-01-12 rsc cwd = os.getcwd()
1783 ef27bfa4 2010-01-12 rsc files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
1784 ef27bfa4 2010-01-12 rsc try:
1785 ef27bfa4 2010-01-12 rsc cmd = ["gofmt", "-l"]
1786 ef27bfa4 2010-01-12 rsc if not opts["list"]:
1787 ef27bfa4 2010-01-12 rsc cmd += ["-w"]
1788 ef27bfa4 2010-01-12 rsc if os.spawnvp(os.P_WAIT, "gofmt", cmd + files) != 0:
1789 5c934375 2012-01-20 rsc raise hg_util.Abort("gofmt did not exit cleanly")
1790 5c934375 2012-01-20 rsc except hg_error.Abort, e:
1791 ef27bfa4 2010-01-12 rsc raise
1792 ef27bfa4 2010-01-12 rsc except:
1793 5c934375 2012-01-20 rsc raise hg_util.Abort("gofmt: " + ExceptionDetail())
1794 ef27bfa4 2010-01-12 rsc return
1795 ef27bfa4 2010-01-12 rsc
1796 63550fce 2012-07-14 rsc def gofmt_required(files):
1797 63550fce 2012-07-14 rsc return [f for f in files if (not f.startswith('test/') or f.startswith('test/bench/')) and f.endswith('.go')]
1798 63550fce 2012-07-14 rsc
1799 5c934375 2012-01-20 rsc #######################################################################
1800 5c934375 2012-01-20 rsc # hg mail
1801 5c934375 2012-01-20 rsc
1802 63550fce 2012-07-14 rsc @hgcommand
1803 ef27bfa4 2010-01-12 rsc def mail(ui, repo, *pats, **opts):
1804 ef27bfa4 2010-01-12 rsc """mail a change for review
1805 ef27bfa4 2010-01-12 rsc
1806 ef27bfa4 2010-01-12 rsc Uploads a patch to the code review server and then sends mail
1807 ef27bfa4 2010-01-12 rsc to the reviewer and CC list asking for a review.
1808 ef27bfa4 2010-01-12 rsc """
1809 5c934375 2012-01-20 rsc if codereview_disabled:
1810 5c934375 2012-01-20 rsc return codereview_disabled
1811 26b4bea8 2011-08-02 rsc
1812 ef27bfa4 2010-01-12 rsc cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
1813 ef27bfa4 2010-01-12 rsc if err != "":
1814 ef27bfa4 2010-01-12 rsc return err
1815 ef27bfa4 2010-01-12 rsc cl.Upload(ui, repo, gofmt_just_warn=True)
1816 a06877af 2010-08-05 rsc if not cl.reviewer:
1817 a06877af 2010-08-05 rsc # If no reviewer is listed, assign the review to defaultcc.
1818 26b4bea8 2011-08-02 rsc # This makes sure that it appears in the
1819 a06877af 2010-08-05 rsc # codereview.appspot.com/user/defaultcc
1820 a06877af 2010-08-05 rsc # page, so that it doesn't get dropped on the floor.
1821 a06877af 2010-08-05 rsc if not defaultcc:
1822 a06877af 2010-08-05 rsc return "no reviewers listed in CL"
1823 a06877af 2010-08-05 rsc cl.cc = Sub(cl.cc, defaultcc)
1824 a06877af 2010-08-05 rsc cl.reviewer = defaultcc
1825 a06877af 2010-08-05 rsc cl.Flush(ui, repo)
1826 ef27bfa4 2010-01-12 rsc
1827 26b4bea8 2011-08-02 rsc if cl.files == []:
1828 26b4bea8 2011-08-02 rsc return "no changed files, not sending mail"
1829 ef27bfa4 2010-01-12 rsc
1830 26b4bea8 2011-08-02 rsc cl.Mail(ui, repo)
1831 5c934375 2012-01-20 rsc
1832 5c934375 2012-01-20 rsc #######################################################################
1833 5c934375 2012-01-20 rsc # hg p / hg pq / hg ps / hg pending
1834 5c934375 2012-01-20 rsc
1835 63550fce 2012-07-14 rsc @hgcommand
1836 5c934375 2012-01-20 rsc def ps(ui, repo, *pats, **opts):
1837 5c934375 2012-01-20 rsc """alias for hg p --short
1838 5c934375 2012-01-20 rsc """
1839 5c934375 2012-01-20 rsc opts['short'] = True
1840 5c934375 2012-01-20 rsc return pending(ui, repo, *pats, **opts)
1841 5c934375 2012-01-20 rsc
1842 63550fce 2012-07-14 rsc @hgcommand
1843 5c934375 2012-01-20 rsc def pq(ui, repo, *pats, **opts):
1844 5c934375 2012-01-20 rsc """alias for hg p --quick
1845 5c934375 2012-01-20 rsc """
1846 5c934375 2012-01-20 rsc opts['quick'] = True
1847 5c934375 2012-01-20 rsc return pending(ui, repo, *pats, **opts)
1848 26b4bea8 2011-08-02 rsc
1849 63550fce 2012-07-14 rsc @hgcommand
1850 ef27bfa4 2010-01-12 rsc def pending(ui, repo, *pats, **opts):
1851 ef27bfa4 2010-01-12 rsc """show pending changes
1852 ef27bfa4 2010-01-12 rsc
1853 ef27bfa4 2010-01-12 rsc Lists pending changes followed by a list of unassigned but modified files.
1854 ef27bfa4 2010-01-12 rsc """
1855 5c934375 2012-01-20 rsc if codereview_disabled:
1856 5c934375 2012-01-20 rsc return codereview_disabled
1857 26b4bea8 2011-08-02 rsc
1858 5c934375 2012-01-20 rsc quick = opts.get('quick', False)
1859 5c934375 2012-01-20 rsc short = opts.get('short', False)
1860 5c934375 2012-01-20 rsc m = LoadAllCL(ui, repo, web=not quick and not short)
1861 ef27bfa4 2010-01-12 rsc names = m.keys()
1862 ef27bfa4 2010-01-12 rsc names.sort()
1863 ef27bfa4 2010-01-12 rsc for name in names:
1864 ef27bfa4 2010-01-12 rsc cl = m[name]
1865 5c934375 2012-01-20 rsc if short:
1866 5c934375 2012-01-20 rsc ui.write(name + "\t" + line1(cl.desc) + "\n")
1867 5c934375 2012-01-20 rsc else:
1868 5c934375 2012-01-20 rsc ui.write(cl.PendingText(quick=quick) + "\n")
1869 ef27bfa4 2010-01-12 rsc
1870 5c934375 2012-01-20 rsc if short:
1871 5c934375 2012-01-20 rsc return
1872 5c934375 2012-01-20 rsc files = DefaultFiles(ui, repo, [])
1873 ef27bfa4 2010-01-12 rsc if len(files) > 0:
1874 ef27bfa4 2010-01-12 rsc s = "Changed files not in any CL:\n"
1875 ef27bfa4 2010-01-12 rsc for f in files:
1876 ef27bfa4 2010-01-12 rsc s += "\t" + f + "\n"
1877 ef27bfa4 2010-01-12 rsc ui.write(s)
1878 ef27bfa4 2010-01-12 rsc
1879 5c934375 2012-01-20 rsc #######################################################################
1880 5c934375 2012-01-20 rsc # hg submit
1881 ef27bfa4 2010-01-12 rsc
1882 5c934375 2012-01-20 rsc def need_sync():
1883 5c934375 2012-01-20 rsc raise hg_util.Abort("local repository out of date; must sync before submit")
1884 ef27bfa4 2010-01-12 rsc
1885 63550fce 2012-07-14 rsc @hgcommand
1886 ef27bfa4 2010-01-12 rsc def submit(ui, repo, *pats, **opts):
1887 ef27bfa4 2010-01-12 rsc """submit change to remote repository
1888 ef27bfa4 2010-01-12 rsc
1889 ef27bfa4 2010-01-12 rsc Submits change to remote repository.
1890 ef27bfa4 2010-01-12 rsc Bails out if the local repository is not in sync with the remote one.
1891 ef27bfa4 2010-01-12 rsc """
1892 5c934375 2012-01-20 rsc if codereview_disabled:
1893 5c934375 2012-01-20 rsc return codereview_disabled
1894 26b4bea8 2011-08-02 rsc
1895 26b4bea8 2011-08-02 rsc # We already called this on startup but sometimes Mercurial forgets.
1896 26b4bea8 2011-08-02 rsc set_mercurial_encoding_to_utf8()
1897 26b4bea8 2011-08-02 rsc
1898 5c934375 2012-01-20 rsc if not opts["no_incoming"] and hg_incoming(ui, repo):
1899 5c934375 2012-01-20 rsc need_sync()
1900 ef27bfa4 2010-01-12 rsc
1901 ef27bfa4 2010-01-12 rsc cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
1902 ef27bfa4 2010-01-12 rsc if err != "":
1903 ef27bfa4 2010-01-12 rsc return err
1904 ef27bfa4 2010-01-12 rsc
1905 ef27bfa4 2010-01-12 rsc user = None
1906 ef27bfa4 2010-01-12 rsc if cl.copied_from:
1907 ef27bfa4 2010-01-12 rsc user = cl.copied_from
1908 ef27bfa4 2010-01-12 rsc userline = CheckContributor(ui, repo, user)
1909 26b4bea8 2011-08-02 rsc typecheck(userline, str)
1910 ef27bfa4 2010-01-12 rsc
1911 ef27bfa4 2010-01-12 rsc about = ""
1912 ef27bfa4 2010-01-12 rsc if cl.reviewer:
1913 ef27bfa4 2010-01-12 rsc about += "R=" + JoinComma([CutDomain(s) for s in cl.reviewer]) + "\n"
1914 ef27bfa4 2010-01-12 rsc if opts.get('tbr'):
1915 ef27bfa4 2010-01-12 rsc tbr = SplitCommaSpace(opts.get('tbr'))
1916 ef27bfa4 2010-01-12 rsc cl.reviewer = Add(cl.reviewer, tbr)
1917 ef27bfa4 2010-01-12 rsc about += "TBR=" + JoinComma([CutDomain(s) for s in tbr]) + "\n"
1918 ef27bfa4 2010-01-12 rsc if cl.cc:
1919 ef27bfa4 2010-01-12 rsc about += "CC=" + JoinComma([CutDomain(s) for s in cl.cc]) + "\n"
1920 ef27bfa4 2010-01-12 rsc
1921 ef27bfa4 2010-01-12 rsc if not cl.reviewer:
1922 ef27bfa4 2010-01-12 rsc return "no reviewers listed in CL"
1923 ef27bfa4 2010-01-12 rsc
1924 ef27bfa4 2010-01-12 rsc if not cl.local:
1925 ef27bfa4 2010-01-12 rsc return "cannot submit non-local CL"
1926 ef27bfa4 2010-01-12 rsc
1927 ef27bfa4 2010-01-12 rsc # upload, to sync current patch and also get change number if CL is new.
1928 ef27bfa4 2010-01-12 rsc if not cl.copied_from:
1929 ef27bfa4 2010-01-12 rsc cl.Upload(ui, repo, gofmt_just_warn=True)
1930 ef27bfa4 2010-01-12 rsc
1931 ef27bfa4 2010-01-12 rsc # check gofmt for real; allowed upload to warn in order to save CL.
1932 ef27bfa4 2010-01-12 rsc cl.Flush(ui, repo)
1933 26b4bea8 2011-08-02 rsc CheckFormat(ui, repo, cl.files)
1934 ef27bfa4 2010-01-12 rsc
1935 ef27bfa4 2010-01-12 rsc about += "%s%s\n" % (server_url_base, cl.name)
1936 ef27bfa4 2010-01-12 rsc
1937 ef27bfa4 2010-01-12 rsc if cl.copied_from:
1938 ef27bfa4 2010-01-12 rsc about += "\nCommitter: " + CheckContributor(ui, repo, None) + "\n"
1939 26b4bea8 2011-08-02 rsc typecheck(about, str)
1940 ef27bfa4 2010-01-12 rsc
1941 ef27bfa4 2010-01-12 rsc if not cl.mailed and not cl.copied_from: # in case this is TBR
1942 ef27bfa4 2010-01-12 rsc cl.Mail(ui, repo)
1943 ef27bfa4 2010-01-12 rsc
1944 ef27bfa4 2010-01-12 rsc # submit changes locally
1945 5c934375 2012-01-20 rsc message = cl.desc.rstrip() + "\n\n" + about
1946 5c934375 2012-01-20 rsc typecheck(message, str)
1947 ef27bfa4 2010-01-12 rsc
1948 338bd4bb 2011-11-08 rsc set_status("pushing " + cl.name + " to remote server")
1949 338bd4bb 2011-11-08 rsc
1950 5c934375 2012-01-20 rsc if hg_outgoing(ui, repo):
1951 5c934375 2012-01-20 rsc raise hg_util.Abort("local repository corrupt or out-of-phase with remote: found outgoing changes")
1952 5c934375 2012-01-20 rsc
1953 5c934375 2012-01-20 rsc old_heads = len(hg_heads(ui, repo).split())
1954 338bd4bb 2011-11-08 rsc
1955 5c934375 2012-01-20 rsc global commit_okay
1956 5c934375 2012-01-20 rsc commit_okay = True
1957 5c934375 2012-01-20 rsc ret = hg_commit(ui, repo, *['path:'+f for f in cl.files], message=message, user=userline)
1958 5c934375 2012-01-20 rsc commit_okay = False
1959 5c934375 2012-01-20 rsc if ret:
1960 ef27bfa4 2010-01-12 rsc return "nothing changed"
1961 5c934375 2012-01-20 rsc node = repo["-1"].node()
1962 ef27bfa4 2010-01-12 rsc # push to remote; if it fails for any reason, roll back
1963 ef27bfa4 2010-01-12 rsc try:
1964 5c934375 2012-01-20 rsc new_heads = len(hg_heads(ui, repo).split())
1965 63550fce 2012-07-14 rsc if old_heads != new_heads and not (old_heads == 0 and new_heads == 1):
1966 5c934375 2012-01-20 rsc # Created new head, so we weren't up to date.
1967 5c934375 2012-01-20 rsc need_sync()
1968 ef27bfa4 2010-01-12 rsc
1969 5c934375 2012-01-20 rsc # Push changes to remote. If it works, we're committed. If not, roll back.
1970 5c934375 2012-01-20 rsc try:
1971 5c934375 2012-01-20 rsc hg_push(ui, repo)
1972 5c934375 2012-01-20 rsc except hg_error.Abort, e:
1973 5c934375 2012-01-20 rsc if e.message.find("push creates new heads") >= 0:
1974 5c934375 2012-01-20 rsc # Remote repository had changes we missed.
1975 5c934375 2012-01-20 rsc need_sync()
1976 5c934375 2012-01-20 rsc raise
1977 ef27bfa4 2010-01-12 rsc except:
1978 26b4bea8 2011-08-02 rsc real_rollback()
1979 ef27bfa4 2010-01-12 rsc raise
1980 ef27bfa4 2010-01-12 rsc
1981 5c934375 2012-01-20 rsc # We're committed. Upload final patch, close review, add commit message.
1982 5c934375 2012-01-20 rsc changeURL = hg_node.short(node)
1983 5c934375 2012-01-20 rsc url = ui.expandpath("default")
1984 63550fce 2012-07-14 rsc m = re.match("(^https?://([^@/]+@)?([^.]+)\.googlecode\.com/hg/?)" + "|" +
1985 63550fce 2012-07-14 rsc "(^https?://([^@/]+@)?code\.google\.com/p/([^/.]+)(\.[^./]+)?/?)", url)
1986 ef27bfa4 2010-01-12 rsc if m:
1987 63550fce 2012-07-14 rsc if m.group(1): # prj.googlecode.com/hg/ case
1988 63550fce 2012-07-14 rsc changeURL = "http://code.google.com/p/%s/source/detail?r=%s" % (m.group(3), changeURL)
1989 63550fce 2012-07-14 rsc elif m.group(4) and m.group(7): # code.google.com/p/prj.subrepo/ case
1990 63550fce 2012-07-14 rsc changeURL = "http://code.google.com/p/%s/source/detail?r=%s&repo=%s" % (m.group(6), changeURL, m.group(7)[1:])
1991 63550fce 2012-07-14 rsc elif m.group(4): # code.google.com/p/prj/ case
1992 63550fce 2012-07-14 rsc changeURL = "http://code.google.com/p/%s/source/detail?r=%s" % (m.group(6), changeURL)
1993 63550fce 2012-07-14 rsc else:
1994 63550fce 2012-07-14 rsc print >>sys.stderr, "URL: ", url
1995 ef27bfa4 2010-01-12 rsc else:
1996 ef27bfa4 2010-01-12 rsc print >>sys.stderr, "URL: ", url
1997 5c934375 2012-01-20 rsc pmsg = "*** Submitted as " + changeURL + " ***\n\n" + message
1998 ef27bfa4 2010-01-12 rsc
1999 ef27bfa4 2010-01-12 rsc # When posting, move reviewers to CC line,
2000 ef27bfa4 2010-01-12 rsc # so that the issue stops showing up in their "My Issues" page.
2001 ef27bfa4 2010-01-12 rsc PostMessage(ui, cl.name, pmsg, reviewers="", cc=JoinComma(cl.reviewer+cl.cc))
2002 ef27bfa4 2010-01-12 rsc
2003 ef27bfa4 2010-01-12 rsc if not cl.copied_from:
2004 26b4bea8 2011-08-02 rsc EditDesc(cl.name, closed=True, private=cl.private)
2005 ef27bfa4 2010-01-12 rsc cl.Delete(ui, repo)
2006 5c934375 2012-01-20 rsc
2007 26b4bea8 2011-08-02 rsc c = repo[None]
2008 26b4bea8 2011-08-02 rsc if c.branch() == releaseBranch and not c.modified() and not c.added() and not c.removed():
2009 26b4bea8 2011-08-02 rsc ui.write("switching from %s to default branch.\n" % releaseBranch)
2010 5c934375 2012-01-20 rsc err = hg_clean(repo, "default")
2011 26b4bea8 2011-08-02 rsc if err:
2012 26b4bea8 2011-08-02 rsc return err
2013 26b4bea8 2011-08-02 rsc return None
2014 ef27bfa4 2010-01-12 rsc
2015 5c934375 2012-01-20 rsc #######################################################################
2016 5c934375 2012-01-20 rsc # hg sync
2017 5c934375 2012-01-20 rsc
2018 63550fce 2012-07-14 rsc @hgcommand
2019 ef27bfa4 2010-01-12 rsc def sync(ui, repo, **opts):
2020 ef27bfa4 2010-01-12 rsc """synchronize with remote repository
2021 ef27bfa4 2010-01-12 rsc
2022 ef27bfa4 2010-01-12 rsc Incorporates recent changes from the remote repository
2023 ef27bfa4 2010-01-12 rsc into the local repository.
2024 ef27bfa4 2010-01-12 rsc """
2025 5c934375 2012-01-20 rsc if codereview_disabled:
2026 5c934375 2012-01-20 rsc return codereview_disabled
2027 26b4bea8 2011-08-02 rsc
2028 ef27bfa4 2010-01-12 rsc if not opts["local"]:
2029 5c934375 2012-01-20 rsc err = hg_pull(ui, repo, update=True)
2030 ef27bfa4 2010-01-12 rsc if err:
2031 ef27bfa4 2010-01-12 rsc return err
2032 ef27bfa4 2010-01-12 rsc sync_changes(ui, repo)
2033 ef27bfa4 2010-01-12 rsc
2034 ef27bfa4 2010-01-12 rsc def sync_changes(ui, repo):
2035 ef27bfa4 2010-01-12 rsc # Look through recent change log descriptions to find
2036 ef27bfa4 2010-01-12 rsc # potential references to http://.*/our-CL-number.
2037 ef27bfa4 2010-01-12 rsc # Double-check them by looking at the Rietveld log.
2038 5c934375 2012-01-20 rsc for rev in hg_log(ui, repo, limit=100, template="{node}\n").split():
2039 ef27bfa4 2010-01-12 rsc desc = repo[rev].description().strip()
2040 ef27bfa4 2010-01-12 rsc for clname in re.findall('(?m)^http://(?:[^\n]+)/([0-9]+)$', desc):
2041 ef27bfa4 2010-01-12 rsc if IsLocalCL(ui, repo, clname) and IsRietveldSubmitted(ui, clname, repo[rev].hex()):
2042 ef27bfa4 2010-01-12 rsc ui.warn("CL %s submitted as %s; closing\n" % (clname, repo[rev]))
2043 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(ui, repo, clname, web=False)
2044 ef27bfa4 2010-01-12 rsc if err != "":
2045 ef27bfa4 2010-01-12 rsc ui.warn("loading CL %s: %s\n" % (clname, err))
2046 ef27bfa4 2010-01-12 rsc continue
2047 ef27bfa4 2010-01-12 rsc if not cl.copied_from:
2048 26b4bea8 2011-08-02 rsc EditDesc(cl.name, closed=True, private=cl.private)
2049 ef27bfa4 2010-01-12 rsc cl.Delete(ui, repo)
2050 ef27bfa4 2010-01-12 rsc
2051 ef27bfa4 2010-01-12 rsc # Remove files that are not modified from the CLs in which they appear.
2052 ef27bfa4 2010-01-12 rsc all = LoadAllCL(ui, repo, web=False)
2053 5c934375 2012-01-20 rsc changed = ChangedFiles(ui, repo, [])
2054 5c934375 2012-01-20 rsc for cl in all.values():
2055 ef27bfa4 2010-01-12 rsc extra = Sub(cl.files, changed)
2056 ef27bfa4 2010-01-12 rsc if extra:
2057 ef27bfa4 2010-01-12 rsc ui.warn("Removing unmodified files from CL %s:\n" % (cl.name,))
2058 ef27bfa4 2010-01-12 rsc for f in extra:
2059 ef27bfa4 2010-01-12 rsc ui.warn("\t%s\n" % (f,))
2060 ef27bfa4 2010-01-12 rsc cl.files = Sub(cl.files, extra)
2061 ef27bfa4 2010-01-12 rsc cl.Flush(ui, repo)
2062 ef27bfa4 2010-01-12 rsc if not cl.files:
2063 26b4bea8 2011-08-02 rsc if not cl.copied_from:
2064 26b4bea8 2011-08-02 rsc ui.warn("CL %s has no files; delete (abandon) with hg change -d %s\n" % (cl.name, cl.name))
2065 26b4bea8 2011-08-02 rsc else:
2066 26b4bea8 2011-08-02 rsc ui.warn("CL %s has no files; delete locally with hg change -D %s\n" % (cl.name, cl.name))
2067 ef27bfa4 2010-01-12 rsc return
2068 ef27bfa4 2010-01-12 rsc
2069 5c934375 2012-01-20 rsc #######################################################################
2070 5c934375 2012-01-20 rsc # hg upload
2071 5c934375 2012-01-20 rsc
2072 63550fce 2012-07-14 rsc @hgcommand
2073 ef27bfa4 2010-01-12 rsc def upload(ui, repo, name, **opts):
2074 ef27bfa4 2010-01-12 rsc """upload diffs to the code review server
2075 ef27bfa4 2010-01-12 rsc
2076 ef27bfa4 2010-01-12 rsc Uploads the current modifications for a given change to the server.
2077 ef27bfa4 2010-01-12 rsc """
2078 5c934375 2012-01-20 rsc if codereview_disabled:
2079 5c934375 2012-01-20 rsc return codereview_disabled
2080 26b4bea8 2011-08-02 rsc
2081 ef27bfa4 2010-01-12 rsc repo.ui.quiet = True
2082 ef27bfa4 2010-01-12 rsc cl, err = LoadCL(ui, repo, name, web=True)
2083 ef27bfa4 2010-01-12 rsc if err != "":
2084 ef27bfa4 2010-01-12 rsc return err
2085 ef27bfa4 2010-01-12 rsc if not cl.local:
2086 ef27bfa4 2010-01-12 rsc return "cannot upload non-local change"
2087 ef27bfa4 2010-01-12 rsc cl.Upload(ui, repo)
2088 ef27bfa4 2010-01-12 rsc print "%s%s\n" % (server_url_base, cl.name)
2089 ef27bfa4 2010-01-12 rsc return
2090 ef27bfa4 2010-01-12 rsc
2091 5c934375 2012-01-20 rsc #######################################################################
2092 5c934375 2012-01-20 rsc # Table of commands, supplied to Mercurial for installation.
2093 5c934375 2012-01-20 rsc
2094 ef27bfa4 2010-01-12 rsc review_opts = [
2095 ef27bfa4 2010-01-12 rsc ('r', 'reviewer', '', 'add reviewer'),
2096 ef27bfa4 2010-01-12 rsc ('', 'cc', '', 'add cc'),
2097 ef27bfa4 2010-01-12 rsc ('', 'tbr', '', 'add future reviewer'),
2098 ef27bfa4 2010-01-12 rsc ('m', 'message', '', 'change description (for new change)'),
2099 ef27bfa4 2010-01-12 rsc ]
2100 ef27bfa4 2010-01-12 rsc
2101 ef27bfa4 2010-01-12 rsc cmdtable = {
2102 ef27bfa4 2010-01-12 rsc # The ^ means to show this command in the help text that
2103 ef27bfa4 2010-01-12 rsc # is printed when running hg with no arguments.
2104 ef27bfa4 2010-01-12 rsc "^change": (
2105 ef27bfa4 2010-01-12 rsc change,
2106 ef27bfa4 2010-01-12 rsc [
2107 ef27bfa4 2010-01-12 rsc ('d', 'delete', None, 'delete existing change list'),
2108 ef27bfa4 2010-01-12 rsc ('D', 'deletelocal', None, 'delete locally, but do not change CL on server'),
2109 ef27bfa4 2010-01-12 rsc ('i', 'stdin', None, 'read change list from standard input'),
2110 ef27bfa4 2010-01-12 rsc ('o', 'stdout', None, 'print change list to standard output'),
2111 26b4bea8 2011-08-02 rsc ('p', 'pending', None, 'print pending summary to standard output'),
2112 ef27bfa4 2010-01-12 rsc ],
2113 ef27bfa4 2010-01-12 rsc "[-d | -D] [-i] [-o] change# or FILE ..."
2114 ef27bfa4 2010-01-12 rsc ),
2115 ef27bfa4 2010-01-12 rsc "^clpatch": (
2116 ef27bfa4 2010-01-12 rsc clpatch,
2117 ef27bfa4 2010-01-12 rsc [
2118 ef27bfa4 2010-01-12 rsc ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
2119 ef27bfa4 2010-01-12 rsc ('', 'no_incoming', None, 'disable check for incoming changes'),
2120 ef27bfa4 2010-01-12 rsc ],
2121 ef27bfa4 2010-01-12 rsc "change#"
2122 ef27bfa4 2010-01-12 rsc ),
2123 ef27bfa4 2010-01-12 rsc # Would prefer to call this codereview-login, but then
2124 ef27bfa4 2010-01-12 rsc # hg help codereview prints the help for this command
2125 ef27bfa4 2010-01-12 rsc # instead of the help for the extension.
2126 ef27bfa4 2010-01-12 rsc "code-login": (
2127 ef27bfa4 2010-01-12 rsc code_login,
2128 ef27bfa4 2010-01-12 rsc [],
2129 ef27bfa4 2010-01-12 rsc "",
2130 ef27bfa4 2010-01-12 rsc ),
2131 ef27bfa4 2010-01-12 rsc "^download": (
2132 ef27bfa4 2010-01-12 rsc download,
2133 ef27bfa4 2010-01-12 rsc [],
2134 ef27bfa4 2010-01-12 rsc "change#"
2135 ef27bfa4 2010-01-12 rsc ),
2136 ef27bfa4 2010-01-12 rsc "^file": (
2137 ef27bfa4 2010-01-12 rsc file,
2138 ef27bfa4 2010-01-12 rsc [
2139 ef27bfa4 2010-01-12 rsc ('d', 'delete', None, 'delete files from change list (but not repository)'),
2140 ef27bfa4 2010-01-12 rsc ],
2141 ef27bfa4 2010-01-12 rsc "[-d] change# FILE ..."
2142 ef27bfa4 2010-01-12 rsc ),
2143 ef27bfa4 2010-01-12 rsc "^gofmt": (
2144 ef27bfa4 2010-01-12 rsc gofmt,
2145 ef27bfa4 2010-01-12 rsc [
2146 ef27bfa4 2010-01-12 rsc ('l', 'list', None, 'list files that would change, but do not edit them'),
2147 ef27bfa4 2010-01-12 rsc ],
2148 ef27bfa4 2010-01-12 rsc "FILE ..."
2149 ef27bfa4 2010-01-12 rsc ),
2150 ef27bfa4 2010-01-12 rsc "^pending|p": (
2151 ef27bfa4 2010-01-12 rsc pending,
2152 5c934375 2012-01-20 rsc [
2153 5c934375 2012-01-20 rsc ('s', 'short', False, 'show short result form'),
2154 5c934375 2012-01-20 rsc ('', 'quick', False, 'do not consult codereview server'),
2155 5c934375 2012-01-20 rsc ],
2156 5c934375 2012-01-20 rsc "[FILE ...]"
2157 5c934375 2012-01-20 rsc ),
2158 5c934375 2012-01-20 rsc "^ps": (
2159 5c934375 2012-01-20 rsc ps,
2160 ef27bfa4 2010-01-12 rsc [],
2161 ef27bfa4 2010-01-12 rsc "[FILE ...]"
2162 ef27bfa4 2010-01-12 rsc ),
2163 5c934375 2012-01-20 rsc "^pq": (
2164 5c934375 2012-01-20 rsc pq,
2165 5c934375 2012-01-20 rsc [],
2166 5c934375 2012-01-20 rsc "[FILE ...]"
2167 5c934375 2012-01-20 rsc ),
2168 ef27bfa4 2010-01-12 rsc "^mail": (
2169 ef27bfa4 2010-01-12 rsc mail,
2170 ef27bfa4 2010-01-12 rsc review_opts + [
2171 5c934375 2012-01-20 rsc ] + hg_commands.walkopts,
2172 ef27bfa4 2010-01-12 rsc "[-r reviewer] [--cc cc] [change# | file ...]"
2173 ef27bfa4 2010-01-12 rsc ),
2174 26b4bea8 2011-08-02 rsc "^release-apply": (
2175 26b4bea8 2011-08-02 rsc release_apply,
2176 26b4bea8 2011-08-02 rsc [
2177 26b4bea8 2011-08-02 rsc ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
2178 26b4bea8 2011-08-02 rsc ('', 'no_incoming', None, 'disable check for incoming changes'),
2179 26b4bea8 2011-08-02 rsc ],
2180 26b4bea8 2011-08-02 rsc "change#"
2181 26b4bea8 2011-08-02 rsc ),
2182 26b4bea8 2011-08-02 rsc # TODO: release-start, release-tag, weekly-tag
2183 ef27bfa4 2010-01-12 rsc "^submit": (
2184 ef27bfa4 2010-01-12 rsc submit,
2185 ef27bfa4 2010-01-12 rsc review_opts + [
2186 ef27bfa4 2010-01-12 rsc ('', 'no_incoming', None, 'disable initial incoming check (for testing)'),
2187 5c934375 2012-01-20 rsc ] + hg_commands.walkopts + hg_commands.commitopts + hg_commands.commitopts2,
2188 ef27bfa4 2010-01-12 rsc "[-r reviewer] [--cc cc] [change# | file ...]"
2189 ef27bfa4 2010-01-12 rsc ),
2190 ef27bfa4 2010-01-12 rsc "^sync": (
2191 ef27bfa4 2010-01-12 rsc sync,
2192 ef27bfa4 2010-01-12 rsc [
2193 ef27bfa4 2010-01-12 rsc ('', 'local', None, 'do not pull changes from remote repository')
2194 ef27bfa4 2010-01-12 rsc ],
2195 ef27bfa4 2010-01-12 rsc "[--local]",
2196 ef27bfa4 2010-01-12 rsc ),
2197 26b4bea8 2011-08-02 rsc "^undo": (
2198 26b4bea8 2011-08-02 rsc undo,
2199 26b4bea8 2011-08-02 rsc [
2200 26b4bea8 2011-08-02 rsc ('', 'ignore_hgpatch_failure', None, 'create CL metadata even if hgpatch fails'),
2201 26b4bea8 2011-08-02 rsc ('', 'no_incoming', None, 'disable check for incoming changes'),
2202 26b4bea8 2011-08-02 rsc ],
2203 26b4bea8 2011-08-02 rsc "change#"
2204 26b4bea8 2011-08-02 rsc ),
2205 ef27bfa4 2010-01-12 rsc "^upload": (
2206 ef27bfa4 2010-01-12 rsc upload,
2207 ef27bfa4 2010-01-12 rsc [],
2208 ef27bfa4 2010-01-12 rsc "change#"
2209 ef27bfa4 2010-01-12 rsc ),
2210 ef27bfa4 2010-01-12 rsc }
2211 5c934375 2012-01-20 rsc
2212 5c934375 2012-01-20 rsc #######################################################################
2213 5c934375 2012-01-20 rsc # Mercurial extension initialization
2214 5c934375 2012-01-20 rsc
2215 5c934375 2012-01-20 rsc def norollback(*pats, **opts):
2216 5c934375 2012-01-20 rsc """(disabled when using this extension)"""
2217 5c934375 2012-01-20 rsc raise hg_util.Abort("codereview extension enabled; use undo instead of rollback")
2218 5c934375 2012-01-20 rsc
2219 63550fce 2012-07-14 rsc codereview_init = False
2220 63550fce 2012-07-14 rsc
2221 5c934375 2012-01-20 rsc def reposetup(ui, repo):
2222 5c934375 2012-01-20 rsc global codereview_disabled
2223 5c934375 2012-01-20 rsc global defaultcc
2224 5c934375 2012-01-20 rsc
2225 63550fce 2012-07-14 rsc # reposetup gets called both for the local repository
2226 63550fce 2012-07-14 rsc # and also for any repository we are pulling or pushing to.
2227 63550fce 2012-07-14 rsc # Only initialize the first time.
2228 63550fce 2012-07-14 rsc global codereview_init
2229 63550fce 2012-07-14 rsc if codereview_init:
2230 63550fce 2012-07-14 rsc return
2231 63550fce 2012-07-14 rsc codereview_init = True
2232 63550fce 2012-07-14 rsc
2233 63550fce 2012-07-14 rsc # Read repository-specific options from lib/codereview/codereview.cfg or codereview.cfg.
2234 63550fce 2012-07-14 rsc root = ''
2235 63550fce 2012-07-14 rsc try:
2236 63550fce 2012-07-14 rsc root = repo.root
2237 63550fce 2012-07-14 rsc except:
2238 63550fce 2012-07-14 rsc # Yes, repo might not have root; see issue 959.
2239 63550fce 2012-07-14 rsc codereview_disabled = 'codereview disabled: repository has no root'
2240 63550fce 2012-07-14 rsc return
2241 63550fce 2012-07-14 rsc
2242 5c934375 2012-01-20 rsc repo_config_path = ''
2243 63550fce 2012-07-14 rsc p1 = root + '/lib/codereview/codereview.cfg'
2244 63550fce 2012-07-14 rsc p2 = root + '/codereview.cfg'
2245 63550fce 2012-07-14 rsc if os.access(p1, os.F_OK):
2246 63550fce 2012-07-14 rsc repo_config_path = p1
2247 63550fce 2012-07-14 rsc else:
2248 63550fce 2012-07-14 rsc repo_config_path = p2
2249 5c934375 2012-01-20 rsc try:
2250 5c934375 2012-01-20 rsc f = open(repo_config_path)
2251 5c934375 2012-01-20 rsc for line in f:
2252 63550fce 2012-07-14 rsc if line.startswith('defaultcc:'):
2253 63550fce 2012-07-14 rsc defaultcc = SplitCommaSpace(line[len('defaultcc:'):])
2254 63550fce 2012-07-14 rsc if line.startswith('contributors:'):
2255 63550fce 2012-07-14 rsc global contributorsURL
2256 63550fce 2012-07-14 rsc contributorsURL = line[len('contributors:'):].strip()
2257 5c934375 2012-01-20 rsc except:
2258 63550fce 2012-07-14 rsc codereview_disabled = 'codereview disabled: cannot open ' + repo_config_path
2259 5c934375 2012-01-20 rsc return
2260 ef27bfa4 2010-01-12 rsc
2261 63550fce 2012-07-14 rsc remote = ui.config("paths", "default", "")
2262 63550fce 2012-07-14 rsc if remote.find("://") < 0:
2263 63550fce 2012-07-14 rsc raise hg_util.Abort("codereview: default path '%s' is not a URL" % (remote,))
2264 63550fce 2012-07-14 rsc
2265 5c934375 2012-01-20 rsc InstallMatch(ui, repo)
2266 5c934375 2012-01-20 rsc RietveldSetup(ui, repo)
2267 5c934375 2012-01-20 rsc
2268 5c934375 2012-01-20 rsc # Disable the Mercurial commands that might change the repository.
2269 5c934375 2012-01-20 rsc # Only commands in this extension are supposed to do that.
2270 5c934375 2012-01-20 rsc ui.setconfig("hooks", "precommit.codereview", precommithook)
2271 5c934375 2012-01-20 rsc
2272 5c934375 2012-01-20 rsc # Rollback removes an existing commit. Don't do that either.
2273 5c934375 2012-01-20 rsc global real_rollback
2274 5c934375 2012-01-20 rsc real_rollback = repo.rollback
2275 5c934375 2012-01-20 rsc repo.rollback = norollback
2276 5c934375 2012-01-20 rsc
2277 ef27bfa4 2010-01-12 rsc
2278 ef27bfa4 2010-01-12 rsc #######################################################################
2279 ef27bfa4 2010-01-12 rsc # Wrappers around upload.py for interacting with Rietveld
2280 ef27bfa4 2010-01-12 rsc
2281 5c934375 2012-01-20 rsc from HTMLParser import HTMLParser
2282 5c934375 2012-01-20 rsc
2283 ef27bfa4 2010-01-12 rsc # HTML form parser
2284 ef27bfa4 2010-01-12 rsc class FormParser(HTMLParser):
2285 ef27bfa4 2010-01-12 rsc def __init__(self):
2286 ef27bfa4 2010-01-12 rsc self.map = {}
2287 ef27bfa4 2010-01-12 rsc self.curtag = None
2288 ef27bfa4 2010-01-12 rsc self.curdata = None
2289 ef27bfa4 2010-01-12 rsc HTMLParser.__init__(self)
2290 ef27bfa4 2010-01-12 rsc def handle_starttag(self, tag, attrs):
2291 ef27bfa4 2010-01-12 rsc if tag == "input":
2292 ef27bfa4 2010-01-12 rsc key = None
2293 ef27bfa4 2010-01-12 rsc value = ''
2294 ef27bfa4 2010-01-12 rsc for a in attrs:
2295 ef27bfa4 2010-01-12 rsc if a[0] == 'name':
2296 ef27bfa4 2010-01-12 rsc key = a[1]
2297 ef27bfa4 2010-01-12 rsc if a[0] == 'value':
2298 ef27bfa4 2010-01-12 rsc value = a[1]
2299 ef27bfa4 2010-01-12 rsc if key is not None:
2300 ef27bfa4 2010-01-12 rsc self.map[key] = value
2301 ef27bfa4 2010-01-12 rsc if tag == "textarea":
2302 ef27bfa4 2010-01-12 rsc key = None
2303 ef27bfa4 2010-01-12 rsc for a in attrs:
2304 ef27bfa4 2010-01-12 rsc if a[0] == 'name':
2305 ef27bfa4 2010-01-12 rsc key = a[1]
2306 ef27bfa4 2010-01-12 rsc if key is not None:
2307 ef27bfa4 2010-01-12 rsc self.curtag = key
2308 ef27bfa4 2010-01-12 rsc self.curdata = ''
2309 ef27bfa4 2010-01-12 rsc def handle_endtag(self, tag):
2310 ef27bfa4 2010-01-12 rsc if tag == "textarea" and self.curtag is not None:
2311 ef27bfa4 2010-01-12 rsc self.map[self.curtag] = self.curdata
2312 ef27bfa4 2010-01-12 rsc self.curtag = None
2313 ef27bfa4 2010-01-12 rsc self.curdata = None
2314 ef27bfa4 2010-01-12 rsc def handle_charref(self, name):
2315 ef27bfa4 2010-01-12 rsc self.handle_data(unichr(int(name)))
2316 ef27bfa4 2010-01-12 rsc def handle_entityref(self, name):
2317 ef27bfa4 2010-01-12 rsc import htmlentitydefs
2318 ef27bfa4 2010-01-12 rsc if name in htmlentitydefs.entitydefs:
2319 ef27bfa4 2010-01-12 rsc self.handle_data(htmlentitydefs.entitydefs[name])
2320 ef27bfa4 2010-01-12 rsc else:
2321 ef27bfa4 2010-01-12 rsc self.handle_data("&" + name + ";")
2322 ef27bfa4 2010-01-12 rsc def handle_data(self, data):
2323 ef27bfa4 2010-01-12 rsc if self.curdata is not None:
2324 26b4bea8 2011-08-02 rsc self.curdata += data
2325 ef27bfa4 2010-01-12 rsc
2326 26b4bea8 2011-08-02 rsc def JSONGet(ui, path):
2327 ef27bfa4 2010-01-12 rsc try:
2328 26b4bea8 2011-08-02 rsc data = MySend(path, force_auth=False)
2329 26b4bea8 2011-08-02 rsc typecheck(data, str)
2330 26b4bea8 2011-08-02 rsc d = fix_json(json.loads(data))
2331 ef27bfa4 2010-01-12 rsc except:
2332 26b4bea8 2011-08-02 rsc ui.warn("JSONGet %s: %s\n" % (path, ExceptionDetail()))
2333 ef27bfa4 2010-01-12 rsc return None
2334 26b4bea8 2011-08-02 rsc return d
2335 ef27bfa4 2010-01-12 rsc
2336 26b4bea8 2011-08-02 rsc # Clean up json parser output to match our expectations:
2337 26b4bea8 2011-08-02 rsc # * all strings are UTF-8-encoded str, not unicode.
2338 26b4bea8 2011-08-02 rsc # * missing fields are missing, not None,
2339 26b4bea8 2011-08-02 rsc # so that d.get("foo", defaultvalue) works.
2340 26b4bea8 2011-08-02 rsc def fix_json(x):
2341 26b4bea8 2011-08-02 rsc if type(x) in [str, int, float, bool, type(None)]:
2342 26b4bea8 2011-08-02 rsc pass
2343 26b4bea8 2011-08-02 rsc elif type(x) is unicode:
2344 26b4bea8 2011-08-02 rsc x = x.encode("utf-8")
2345 26b4bea8 2011-08-02 rsc elif type(x) is list:
2346 26b4bea8 2011-08-02 rsc for i in range(len(x)):
2347 26b4bea8 2011-08-02 rsc x[i] = fix_json(x[i])
2348 26b4bea8 2011-08-02 rsc elif type(x) is dict:
2349 26b4bea8 2011-08-02 rsc todel = []
2350 26b4bea8 2011-08-02 rsc for k in x:
2351 26b4bea8 2011-08-02 rsc if x[k] is None:
2352 26b4bea8 2011-08-02 rsc todel.append(k)
2353 26b4bea8 2011-08-02 rsc else:
2354 26b4bea8 2011-08-02 rsc x[k] = fix_json(x[k])
2355 26b4bea8 2011-08-02 rsc for k in todel:
2356 26b4bea8 2011-08-02 rsc del x[k]
2357 26b4bea8 2011-08-02 rsc else:
2358 5c934375 2012-01-20 rsc raise hg_util.Abort("unknown type " + str(type(x)) + " in fix_json")
2359 26b4bea8 2011-08-02 rsc if type(x) is str:
2360 26b4bea8 2011-08-02 rsc x = x.replace('\r\n', '\n')
2361 26b4bea8 2011-08-02 rsc return x
2362 26b4bea8 2011-08-02 rsc
2363 26b4bea8 2011-08-02 rsc def IsRietveldSubmitted(ui, clname, hex):
2364 26b4bea8 2011-08-02 rsc dict = JSONGet(ui, "/api/" + clname + "?messages=true")
2365 26b4bea8 2011-08-02 rsc if dict is None:
2366 ef27bfa4 2010-01-12 rsc return False
2367 26b4bea8 2011-08-02 rsc for msg in dict.get("messages", []):
2368 26b4bea8 2011-08-02 rsc text = msg.get("text", "")
2369 ef27bfa4 2010-01-12 rsc m = re.match('\*\*\* Submitted as [^*]*?([0-9a-f]+) \*\*\*', text)
2370 ef27bfa4 2010-01-12 rsc if m is not None and len(m.group(1)) >= 8 and hex.startswith(m.group(1)):
2371 ef27bfa4 2010-01-12 rsc return True
2372 ef27bfa4 2010-01-12 rsc return False
2373 ef27bfa4 2010-01-12 rsc
2374 26b4bea8 2011-08-02 rsc def IsRietveldMailed(cl):
2375 26b4bea8 2011-08-02 rsc for msg in cl.dict.get("messages", []):
2376 26b4bea8 2011-08-02 rsc if msg.get("text", "").find("I'd like you to review this change") >= 0:
2377 26b4bea8 2011-08-02 rsc return True
2378 26b4bea8 2011-08-02 rsc return False
2379 26b4bea8 2011-08-02 rsc
2380 ef27bfa4 2010-01-12 rsc def DownloadCL(ui, repo, clname):
2381 26b4bea8 2011-08-02 rsc set_status("downloading CL " + clname)
2382 26b4bea8 2011-08-02 rsc cl, err = LoadCL(ui, repo, clname, web=True)
2383 ef27bfa4 2010-01-12 rsc if err != "":
2384 26b4bea8 2011-08-02 rsc return None, None, None, "error loading CL %s: %s" % (clname, err)
2385 ef27bfa4 2010-01-12 rsc
2386 ef27bfa4 2010-01-12 rsc # Find most recent diff
2387 26b4bea8 2011-08-02 rsc diffs = cl.dict.get("patchsets", [])
2388 26b4bea8 2011-08-02 rsc if not diffs:
2389 26b4bea8 2011-08-02 rsc return None, None, None, "CL has no patch sets"
2390 26b4bea8 2011-08-02 rsc patchid = diffs[-1]
2391 26b4bea8 2011-08-02 rsc
2392 26b4bea8 2011-08-02 rsc patchset = JSONGet(ui, "/api/" + clname + "/" + str(patchid))
2393 26b4bea8 2011-08-02 rsc if patchset is None:
2394 26b4bea8 2011-08-02 rsc return None, None, None, "error loading CL patchset %s/%d" % (clname, patchid)
2395 26b4bea8 2011-08-02 rsc if patchset.get("patchset", 0) != patchid:
2396 26b4bea8 2011-08-02 rsc return None, None, None, "malformed patchset information"
2397 26b4bea8 2011-08-02 rsc
2398 26b4bea8 2011-08-02 rsc vers = ""
2399 26b4bea8 2011-08-02 rsc msg = patchset.get("message", "").split()
2400 26b4bea8 2011-08-02 rsc if len(msg) >= 3 and msg[0] == "diff" and msg[1] == "-r":
2401 26b4bea8 2011-08-02 rsc vers = msg[2]
2402 26b4bea8 2011-08-02 rsc diff = "/download/issue" + clname + "_" + str(patchid) + ".diff"
2403 ef27bfa4 2010-01-12 rsc
2404 26b4bea8 2011-08-02 rsc diffdata = MySend(diff, force_auth=False)
2405 26b4bea8 2011-08-02 rsc
2406 ef27bfa4 2010-01-12 rsc # Print warning if email is not in CONTRIBUTORS file.
2407 26b4bea8 2011-08-02 rsc email = cl.dict.get("owner_email", "")
2408 26b4bea8 2011-08-02 rsc if not email:
2409 26b4bea8 2011-08-02 rsc return None, None, None, "cannot find owner for %s" % (clname)
2410 26b4bea8 2011-08-02 rsc him = FindContributor(ui, repo, email)
2411 26b4bea8 2011-08-02 rsc me = FindContributor(ui, repo, None)
2412 26b4bea8 2011-08-02 rsc if him == me:
2413 26b4bea8 2011-08-02 rsc cl.mailed = IsRietveldMailed(cl)
2414 26b4bea8 2011-08-02 rsc else:
2415 26b4bea8 2011-08-02 rsc cl.copied_from = email
2416 ef27bfa4 2010-01-12 rsc
2417 26b4bea8 2011-08-02 rsc return cl, vers, diffdata, ""
2418 ef27bfa4 2010-01-12 rsc
2419 ef27bfa4 2010-01-12 rsc def MySend(request_path, payload=None,
2420 26b4bea8 2011-08-02 rsc content_type="application/octet-stream",
2421 26b4bea8 2011-08-02 rsc timeout=None, force_auth=True,
2422 26b4bea8 2011-08-02 rsc **kwargs):
2423 26b4bea8 2011-08-02 rsc """Run MySend1 maybe twice, because Rietveld is unreliable."""
2424 26b4bea8 2011-08-02 rsc try:
2425 26b4bea8 2011-08-02 rsc return MySend1(request_path, payload, content_type, timeout, force_auth, **kwargs)
2426 26b4bea8 2011-08-02 rsc except Exception, e:
2427 26b4bea8 2011-08-02 rsc if type(e) != urllib2.HTTPError or e.code != 500: # only retry on HTTP 500 error
2428 26b4bea8 2011-08-02 rsc raise
2429 26b4bea8 2011-08-02 rsc print >>sys.stderr, "Loading "+request_path+": "+ExceptionDetail()+"; trying again in 2 seconds."
2430 26b4bea8 2011-08-02 rsc time.sleep(2)
2431 26b4bea8 2011-08-02 rsc return MySend1(request_path, payload, content_type, timeout, force_auth, **kwargs)
2432 ef27bfa4 2010-01-12 rsc
2433 ef27bfa4 2010-01-12 rsc # Like upload.py Send but only authenticates when the
2434 ef27bfa4 2010-01-12 rsc # redirect is to www.google.com/accounts. This keeps
2435 ef27bfa4 2010-01-12 rsc # unnecessary redirects from happening during testing.
2436 ef27bfa4 2010-01-12 rsc def MySend1(request_path, payload=None,
2437 26b4bea8 2011-08-02 rsc content_type="application/octet-stream",
2438 26b4bea8 2011-08-02 rsc timeout=None, force_auth=True,
2439 26b4bea8 2011-08-02 rsc **kwargs):
2440 26b4bea8 2011-08-02 rsc """Sends an RPC and returns the response.
2441 ef27bfa4 2010-01-12 rsc
2442 26b4bea8 2011-08-02 rsc Args:
2443 26b4bea8 2011-08-02 rsc request_path: The path to send the request to, eg /api/appversion/create.
2444 26b4bea8 2011-08-02 rsc payload: The body of the request, or None to send an empty request.
2445 26b4bea8 2011-08-02 rsc content_type: The Content-Type header to use.
2446 26b4bea8 2011-08-02 rsc timeout: timeout in seconds; default None i.e. no timeout.
2447 26b4bea8 2011-08-02 rsc (Note: for large requests on OS X, the timeout doesn't work right.)
2448 26b4bea8 2011-08-02 rsc kwargs: Any keyword arguments are converted into query string parameters.
2449 ef27bfa4 2010-01-12 rsc
2450 26b4bea8 2011-08-02 rsc Returns:
2451 26b4bea8 2011-08-02 rsc The response body, as a string.
2452 26b4bea8 2011-08-02 rsc """
2453 26b4bea8 2011-08-02 rsc # TODO: Don't require authentication. Let the server say
2454 26b4bea8 2011-08-02 rsc # whether it is necessary.
2455 26b4bea8 2011-08-02 rsc global rpc
2456 26b4bea8 2011-08-02 rsc if rpc == None:
2457 26b4bea8 2011-08-02 rsc rpc = GetRpcServer(upload_options)
2458 26b4bea8 2011-08-02 rsc self = rpc
2459 26b4bea8 2011-08-02 rsc if not self.authenticated and force_auth:
2460 26b4bea8 2011-08-02 rsc self._Authenticate()
2461 26b4bea8 2011-08-02 rsc if request_path is None:
2462 26b4bea8 2011-08-02 rsc return
2463 ef27bfa4 2010-01-12 rsc
2464 26b4bea8 2011-08-02 rsc old_timeout = socket.getdefaulttimeout()
2465 26b4bea8 2011-08-02 rsc socket.setdefaulttimeout(timeout)
2466 26b4bea8 2011-08-02 rsc try:
2467 26b4bea8 2011-08-02 rsc tries = 0
2468 26b4bea8 2011-08-02 rsc while True:
2469 26b4bea8 2011-08-02 rsc tries += 1
2470 26b4bea8 2011-08-02 rsc args = dict(kwargs)
2471 26b4bea8 2011-08-02 rsc url = "http://%s%s" % (self.host, request_path)
2472 26b4bea8 2011-08-02 rsc if args:
2473 26b4bea8 2011-08-02 rsc url += "?" + urllib.urlencode(args)
2474 26b4bea8 2011-08-02 rsc req = self._CreateRequest(url=url, data=payload)
2475 26b4bea8 2011-08-02 rsc req.add_header("Content-Type", content_type)
2476 26b4bea8 2011-08-02 rsc try:
2477 26b4bea8 2011-08-02 rsc f = self.opener.open(req)
2478 26b4bea8 2011-08-02 rsc response = f.read()
2479 26b4bea8 2011-08-02 rsc f.close()
2480 26b4bea8 2011-08-02 rsc # Translate \r\n into \n, because Rietveld doesn't.
2481 26b4bea8 2011-08-02 rsc response = response.replace('\r\n', '\n')
2482 26b4bea8 2011-08-02 rsc # who knows what urllib will give us
2483 26b4bea8 2011-08-02 rsc if type(response) == unicode:
2484 26b4bea8 2011-08-02 rsc response = response.encode("utf-8")
2485 26b4bea8 2011-08-02 rsc typecheck(response, str)
2486 26b4bea8 2011-08-02 rsc return response
2487 26b4bea8 2011-08-02 rsc except urllib2.HTTPError, e:
2488 26b4bea8 2011-08-02 rsc if tries > 3:
2489 26b4bea8 2011-08-02 rsc raise
2490 26b4bea8 2011-08-02 rsc elif e.code == 401:
2491 26b4bea8 2011-08-02 rsc self._Authenticate()
2492 26b4bea8 2011-08-02 rsc elif e.code == 302:
2493 26b4bea8 2011-08-02 rsc loc = e.info()["location"]
2494 26b4bea8 2011-08-02 rsc if not loc.startswith('https://www.google.com/a') or loc.find('/ServiceLogin') < 0:
2495 26b4bea8 2011-08-02 rsc return ''
2496 26b4bea8 2011-08-02 rsc self._Authenticate()
2497 26b4bea8 2011-08-02 rsc else:
2498 26b4bea8 2011-08-02 rsc raise
2499 26b4bea8 2011-08-02 rsc finally:
2500 26b4bea8 2011-08-02 rsc socket.setdefaulttimeout(old_timeout)
2501 26b4bea8 2011-08-02 rsc
2502 ef27bfa4 2010-01-12 rsc def GetForm(url):
2503 ef27bfa4 2010-01-12 rsc f = FormParser()
2504 26b4bea8 2011-08-02 rsc f.feed(ustr(MySend(url))) # f.feed wants unicode
2505 ef27bfa4 2010-01-12 rsc f.close()
2506 26b4bea8 2011-08-02 rsc # convert back to utf-8 to restore sanity
2507 26b4bea8 2011-08-02 rsc m = {}
2508 ef27bfa4 2010-01-12 rsc for k,v in f.map.items():
2509 26b4bea8 2011-08-02 rsc m[k.encode("utf-8")] = v.replace("\r\n", "\n").encode("utf-8")
2510 26b4bea8 2011-08-02 rsc return m
2511 ef27bfa4 2010-01-12 rsc
2512 26b4bea8 2011-08-02 rsc def EditDesc(issue, subject=None, desc=None, reviewers=None, cc=None, closed=False, private=False):
2513 26b4bea8 2011-08-02 rsc set_status("uploading change to description")
2514 ef27bfa4 2010-01-12 rsc form_fields = GetForm("/" + issue + "/edit")
2515 ef27bfa4 2010-01-12 rsc if subject is not None:
2516 ef27bfa4 2010-01-12 rsc form_fields['subject'] = subject
2517 ef27bfa4 2010-01-12 rsc if desc is not None:
2518 ef27bfa4 2010-01-12 rsc form_fields['description'] = desc
2519 ef27bfa4 2010-01-12 rsc if reviewers is not None:
2520 ef27bfa4 2010-01-12 rsc form_fields['reviewers'] = reviewers
2521 ef27bfa4 2010-01-12 rsc if cc is not None:
2522 ef27bfa4 2010-01-12 rsc form_fields['cc'] = cc
2523 26b4bea8 2011-08-02 rsc if closed:
2524 26b4bea8 2011-08-02 rsc form_fields['closed'] = "checked"
2525 26b4bea8 2011-08-02 rsc if private:
2526 26b4bea8 2011-08-02 rsc form_fields['private'] = "checked"
2527 ef27bfa4 2010-01-12 rsc ctype, body = EncodeMultipartFormData(form_fields.items(), [])
2528 ef27bfa4 2010-01-12 rsc response = MySend("/" + issue + "/edit", body, content_type=ctype)
2529 ef27bfa4 2010-01-12 rsc if response != "":
2530 ef27bfa4 2010-01-12 rsc print >>sys.stderr, "Error editing description:\n" + "Sent form: \n", form_fields, "\n", response
2531 ef27bfa4 2010-01-12 rsc sys.exit(2)
2532 ef27bfa4 2010-01-12 rsc
2533 ef27bfa4 2010-01-12 rsc def PostMessage(ui, issue, message, reviewers=None, cc=None, send_mail=True, subject=None):
2534 26b4bea8 2011-08-02 rsc set_status("uploading message")
2535 ef27bfa4 2010-01-12 rsc form_fields = GetForm("/" + issue + "/publish")
2536 ef27bfa4 2010-01-12 rsc if reviewers is not None:
2537 ef27bfa4 2010-01-12 rsc form_fields['reviewers'] = reviewers
2538 ef27bfa4 2010-01-12 rsc if cc is not None:
2539 ef27bfa4 2010-01-12 rsc form_fields['cc'] = cc
2540 ef27bfa4 2010-01-12 rsc if send_mail:
2541 ef27bfa4 2010-01-12 rsc form_fields['send_mail'] = "checked"
2542 ef27bfa4 2010-01-12 rsc else:
2543 ef27bfa4 2010-01-12 rsc del form_fields['send_mail']
2544 ef27bfa4 2010-01-12 rsc if subject is not None:
2545 ef27bfa4 2010-01-12 rsc form_fields['subject'] = subject
2546 ef27bfa4 2010-01-12 rsc form_fields['message'] = message
2547 26b4bea8 2011-08-02 rsc
2548 ef27bfa4 2010-01-12 rsc form_fields['message_only'] = '1' # Don't include draft comments
2549 ef27bfa4 2010-01-12 rsc if reviewers is not None or cc is not None:
2550 ef27bfa4 2010-01-12 rsc form_fields['message_only'] = '' # Must set '' in order to override cc/reviewer
2551 ef27bfa4 2010-01-12 rsc ctype = "applications/x-www-form-urlencoded"
2552 ef27bfa4 2010-01-12 rsc body = urllib.urlencode(form_fields)
2553 ef27bfa4 2010-01-12 rsc response = MySend("/" + issue + "/publish", body, content_type=ctype)
2554 ef27bfa4 2010-01-12 rsc if response != "":
2555 ef27bfa4 2010-01-12 rsc print response
2556 ef27bfa4 2010-01-12 rsc sys.exit(2)
2557 ef27bfa4 2010-01-12 rsc
2558 ef27bfa4 2010-01-12 rsc class opt(object):
2559 ef27bfa4 2010-01-12 rsc pass
2560 26b4bea8 2011-08-02 rsc
2561 ef27bfa4 2010-01-12 rsc def RietveldSetup(ui, repo):
2562 5c934375 2012-01-20 rsc global force_google_account
2563 5c934375 2012-01-20 rsc global rpc
2564 5c934375 2012-01-20 rsc global server
2565 5c934375 2012-01-20 rsc global server_url_base
2566 5c934375 2012-01-20 rsc global upload_options
2567 5c934375 2012-01-20 rsc global verbosity
2568 a06877af 2010-08-05 rsc
2569 ef27bfa4 2010-01-12 rsc if not ui.verbose:
2570 ef27bfa4 2010-01-12 rsc verbosity = 0
2571 ef27bfa4 2010-01-12 rsc
2572 ef27bfa4 2010-01-12 rsc # Config options.
2573 ef27bfa4 2010-01-12 rsc x = ui.config("codereview", "server")
2574 ef27bfa4 2010-01-12 rsc if x is not None:
2575 ef27bfa4 2010-01-12 rsc server = x
2576 ef27bfa4 2010-01-12 rsc
2577 ef27bfa4 2010-01-12 rsc # TODO(rsc): Take from ui.username?
2578 ef27bfa4 2010-01-12 rsc email = None
2579 ef27bfa4 2010-01-12 rsc x = ui.config("codereview", "email")
2580 ef27bfa4 2010-01-12 rsc if x is not None:
2581 ef27bfa4 2010-01-12 rsc email = x
2582 ef27bfa4 2010-01-12 rsc
2583 ef27bfa4 2010-01-12 rsc server_url_base = "http://" + server + "/"
2584 ef27bfa4 2010-01-12 rsc
2585 ef27bfa4 2010-01-12 rsc testing = ui.config("codereview", "testing")
2586 ef27bfa4 2010-01-12 rsc force_google_account = ui.configbool("codereview", "force_google_account", False)
2587 ef27bfa4 2010-01-12 rsc
2588 ef27bfa4 2010-01-12 rsc upload_options = opt()
2589 ef27bfa4 2010-01-12 rsc upload_options.email = email
2590 ef27bfa4 2010-01-12 rsc upload_options.host = None
2591 ef27bfa4 2010-01-12 rsc upload_options.verbose = 0
2592 ef27bfa4 2010-01-12 rsc upload_options.description = None
2593 ef27bfa4 2010-01-12 rsc upload_options.description_file = None
2594 ef27bfa4 2010-01-12 rsc upload_options.reviewers = None
2595 ef27bfa4 2010-01-12 rsc upload_options.cc = None
2596 ef27bfa4 2010-01-12 rsc upload_options.message = None
2597 ef27bfa4 2010-01-12 rsc upload_options.issue = None
2598 ef27bfa4 2010-01-12 rsc upload_options.download_base = False
2599 ef27bfa4 2010-01-12 rsc upload_options.revision = None
2600 ef27bfa4 2010-01-12 rsc upload_options.send_mail = False
2601 ef27bfa4 2010-01-12 rsc upload_options.vcs = None
2602 ef27bfa4 2010-01-12 rsc upload_options.server = server
2603 ef27bfa4 2010-01-12 rsc upload_options.save_cookies = True
2604 ef27bfa4 2010-01-12 rsc
2605 ef27bfa4 2010-01-12 rsc if testing:
2606 ef27bfa4 2010-01-12 rsc upload_options.save_cookies = False
2607 ef27bfa4 2010-01-12 rsc upload_options.email = "test@example.com"
2608 ef27bfa4 2010-01-12 rsc
2609 ef27bfa4 2010-01-12 rsc rpc = None
2610 26b4bea8 2011-08-02 rsc
2611 26b4bea8 2011-08-02 rsc global releaseBranch
2612 26b4bea8 2011-08-02 rsc tags = repo.branchtags().keys()
2613 63550fce 2012-07-14 rsc if 'release-branch.go10' in tags:
2614 26b4bea8 2011-08-02 rsc # NOTE(rsc): This tags.sort is going to get the wrong
2615 63550fce 2012-07-14 rsc # answer when comparing release-branch.go9 with
2616 63550fce 2012-07-14 rsc # release-branch.go10. It will be a while before we care.
2617 63550fce 2012-07-14 rsc raise hg_util.Abort('tags.sort needs to be fixed for release-branch.go10')
2618 26b4bea8 2011-08-02 rsc tags.sort()
2619 26b4bea8 2011-08-02 rsc for t in tags:
2620 63550fce 2012-07-14 rsc if t.startswith('release-branch.go'):
2621 26b4bea8 2011-08-02 rsc releaseBranch = t
2622 ef27bfa4 2010-01-12 rsc
2623 ef27bfa4 2010-01-12 rsc #######################################################################
2624 26b4bea8 2011-08-02 rsc # http://codereview.appspot.com/static/upload.py, heavily edited.
2625 ef27bfa4 2010-01-12 rsc
2626 ef27bfa4 2010-01-12 rsc #!/usr/bin/env python
2627 ef27bfa4 2010-01-12 rsc #
2628 ef27bfa4 2010-01-12 rsc # Copyright 2007 Google Inc.
2629 ef27bfa4 2010-01-12 rsc #
2630 ef27bfa4 2010-01-12 rsc # Licensed under the Apache License, Version 2.0 (the "License");
2631 ef27bfa4 2010-01-12 rsc # you may not use this file except in compliance with the License.
2632 ef27bfa4 2010-01-12 rsc # You may obtain a copy of the License at
2633 ef27bfa4 2010-01-12 rsc #
2634 26b4bea8 2011-08-02 rsc # http://www.apache.org/licenses/LICENSE-2.0
2635 ef27bfa4 2010-01-12 rsc #
2636 ef27bfa4 2010-01-12 rsc # Unless required by applicable law or agreed to in writing, software
2637 ef27bfa4 2010-01-12 rsc # distributed under the License is distributed on an "AS IS" BASIS,
2638 ef27bfa4 2010-01-12 rsc # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2639 ef27bfa4 2010-01-12 rsc # See the License for the specific language governing permissions and
2640 ef27bfa4 2010-01-12 rsc # limitations under the License.
2641 ef27bfa4 2010-01-12 rsc
2642 ef27bfa4 2010-01-12 rsc """Tool for uploading diffs from a version control system to the codereview app.
2643 ef27bfa4 2010-01-12 rsc
2644 ef27bfa4 2010-01-12 rsc Usage summary: upload.py [options] [-- diff_options]
2645 ef27bfa4 2010-01-12 rsc
2646 ef27bfa4 2010-01-12 rsc Diff options are passed to the diff command of the underlying system.
2647 ef27bfa4 2010-01-12 rsc
2648 ef27bfa4 2010-01-12 rsc Supported version control systems:
2649 26b4bea8 2011-08-02 rsc Git
2650 26b4bea8 2011-08-02 rsc Mercurial
2651 26b4bea8 2011-08-02 rsc Subversion
2652 ef27bfa4 2010-01-12 rsc
2653 ef27bfa4 2010-01-12 rsc It is important for Git/Mercurial users to specify a tree/node/branch to diff
2654 ef27bfa4 2010-01-12 rsc against by using the '--rev' option.
2655 ef27bfa4 2010-01-12 rsc """
2656 ef27bfa4 2010-01-12 rsc # This code is derived from appcfg.py in the App Engine SDK (open source),
2657 ef27bfa4 2010-01-12 rsc # and from ASPN recipe #146306.
2658 ef27bfa4 2010-01-12 rsc
2659 ef27bfa4 2010-01-12 rsc import cookielib
2660 ef27bfa4 2010-01-12 rsc import getpass
2661 ef27bfa4 2010-01-12 rsc import logging
2662 ef27bfa4 2010-01-12 rsc import mimetypes
2663 ef27bfa4 2010-01-12 rsc import optparse
2664 ef27bfa4 2010-01-12 rsc import os
2665 ef27bfa4 2010-01-12 rsc import re
2666 ef27bfa4 2010-01-12 rsc import socket
2667 ef27bfa4 2010-01-12 rsc import subprocess
2668 ef27bfa4 2010-01-12 rsc import sys
2669 ef27bfa4 2010-01-12 rsc import urllib
2670 ef27bfa4 2010-01-12 rsc import urllib2
2671 ef27bfa4 2010-01-12 rsc import urlparse
2672 ef27bfa4 2010-01-12 rsc
2673 ef27bfa4 2010-01-12 rsc # The md5 module was deprecated in Python 2.5.
2674 ef27bfa4 2010-01-12 rsc try:
2675 26b4bea8 2011-08-02 rsc from hashlib import md5
2676 ef27bfa4 2010-01-12 rsc except ImportError:
2677 26b4bea8 2011-08-02 rsc from md5 import md5
2678 ef27bfa4 2010-01-12 rsc
2679 ef27bfa4 2010-01-12 rsc try:
2680 26b4bea8 2011-08-02 rsc import readline
2681 ef27bfa4 2010-01-12 rsc except ImportError:
2682 26b4bea8 2011-08-02 rsc pass
2683 ef27bfa4 2010-01-12 rsc
2684 ef27bfa4 2010-01-12 rsc # The logging verbosity:
2685 ef27bfa4 2010-01-12 rsc # 0: Errors only.
2686 ef27bfa4 2010-01-12 rsc # 1: Status messages.
2687 ef27bfa4 2010-01-12 rsc # 2: Info logs.
2688 ef27bfa4 2010-01-12 rsc # 3: Debug logs.
2689 ef27bfa4 2010-01-12 rsc verbosity = 1
2690 ef27bfa4 2010-01-12 rsc
2691 ef27bfa4 2010-01-12 rsc # Max size of patch or base file.
2692 ef27bfa4 2010-01-12 rsc MAX_UPLOAD_SIZE = 900 * 1024
2693 ef27bfa4 2010-01-12 rsc
2694 ef27bfa4 2010-01-12 rsc # whitelist for non-binary filetypes which do not start with "text/"
2695 ef27bfa4 2010-01-12 rsc # .mm (Objective-C) shows up as application/x-freemind on my Linux box.
2696 26b4bea8 2011-08-02 rsc TEXT_MIMETYPES = [
2697 26b4bea8 2011-08-02 rsc 'application/javascript',
2698 26b4bea8 2011-08-02 rsc 'application/x-javascript',
2699 26b4bea8 2011-08-02 rsc 'application/x-freemind'
2700 26b4bea8 2011-08-02 rsc ]
2701 ef27bfa4 2010-01-12 rsc
2702 ef27bfa4 2010-01-12 rsc def GetEmail(prompt):
2703 26b4bea8 2011-08-02 rsc """Prompts the user for their email address and returns it.
2704 ef27bfa4 2010-01-12 rsc
2705 26b4bea8 2011-08-02 rsc The last used email address is saved to a file and offered up as a suggestion
2706 26b4bea8 2011-08-02 rsc to the user. If the user presses enter without typing in anything the last
2707 26b4bea8 2011-08-02 rsc used email address is used. If the user enters a new address, it is saved
2708 26b4bea8 2011-08-02 rsc for next time we prompt.
2709 ef27bfa4 2010-01-12 rsc
2710 26b4bea8 2011-08-02 rsc """
2711 26b4bea8 2011-08-02 rsc last_email_file_name = os.path.expanduser("~/.last_codereview_email_address")
2712 26b4bea8 2011-08-02 rsc last_email = ""
2713 26b4bea8 2011-08-02 rsc if os.path.exists(last_email_file_name):
2714 26b4bea8 2011-08-02 rsc try:
2715 26b4bea8 2011-08-02 rsc last_email_file = open(last_email_file_name, "r")
2716 26b4bea8 2011-08-02 rsc last_email = last_email_file.readline().strip("\n")
2717 26b4bea8 2011-08-02 rsc last_email_file.close()
2718 26b4bea8 2011-08-02 rsc prompt += " [%s]" % last_email
2719 26b4bea8 2011-08-02 rsc except IOError, e:
2720 26b4bea8 2011-08-02 rsc pass
2721 26b4bea8 2011-08-02 rsc email = raw_input(prompt + ": ").strip()
2722 26b4bea8 2011-08-02 rsc if email:
2723 26b4bea8 2011-08-02 rsc try:
2724 26b4bea8 2011-08-02 rsc last_email_file = open(last_email_file_name, "w")
2725 26b4bea8 2011-08-02 rsc last_email_file.write(email)
2726 26b4bea8 2011-08-02 rsc last_email_file.close()
2727 26b4bea8 2011-08-02 rsc except IOError, e:
2728 26b4bea8 2011-08-02 rsc pass
2729 26b4bea8 2011-08-02 rsc else:
2730 26b4bea8 2011-08-02 rsc email = last_email
2731 26b4bea8 2011-08-02 rsc return email
2732 ef27bfa4 2010-01-12 rsc
2733 ef27bfa4 2010-01-12 rsc
2734 ef27bfa4 2010-01-12 rsc def StatusUpdate(msg):
2735 26b4bea8 2011-08-02 rsc """Print a status message to stdout.
2736 ef27bfa4 2010-01-12 rsc
2737 26b4bea8 2011-08-02 rsc If 'verbosity' is greater than 0, print the message.
2738 ef27bfa4 2010-01-12 rsc
2739 26b4bea8 2011-08-02 rsc Args:
2740 26b4bea8 2011-08-02 rsc msg: The string to print.
2741 26b4bea8 2011-08-02 rsc """
2742 26b4bea8 2011-08-02 rsc if verbosity > 0:
2743 26b4bea8 2011-08-02 rsc print msg
2744 ef27bfa4 2010-01-12 rsc
2745 ef27bfa4 2010-01-12 rsc
2746 ef27bfa4 2010-01-12 rsc def ErrorExit(msg):
2747 26b4bea8 2011-08-02 rsc """Print an error message to stderr and exit."""
2748 26b4bea8 2011-08-02 rsc print >>sys.stderr, msg
2749 26b4bea8 2011-08-02 rsc sys.exit(1)
2750 ef27bfa4 2010-01-12 rsc
2751 ef27bfa4 2010-01-12 rsc
2752 ef27bfa4 2010-01-12 rsc class ClientLoginError(urllib2.HTTPError):
2753 26b4bea8 2011-08-02 rsc """Raised to indicate there was an error authenticating with ClientLogin."""
2754 ef27bfa4 2010-01-12 rsc
2755 26b4bea8 2011-08-02 rsc def __init__(self, url, code, msg, headers, args):
2756 26b4bea8 2011-08-02 rsc urllib2.HTTPError.__init__(self, url, code, msg, headers, None)
2757 26b4bea8 2011-08-02 rsc self.args = args
2758 26b4bea8 2011-08-02 rsc self.reason = args["Error"]
2759 ef27bfa4 2010-01-12 rsc
2760 ef27bfa4 2010-01-12 rsc
2761 ef27bfa4 2010-01-12 rsc class AbstractRpcServer(object):
2762 26b4bea8 2011-08-02 rsc """Provides a common interface for a simple RPC server."""
2763 ef27bfa4 2010-01-12 rsc
2764 26b4bea8 2011-08-02 rsc def __init__(self, host, auth_function, host_override=None, extra_headers={}, save_cookies=False):
2765 26b4bea8 2011-08-02 rsc """Creates a new HttpRpcServer.
2766 ef27bfa4 2010-01-12 rsc
2767 26b4bea8 2011-08-02 rsc Args:
2768 26b4bea8 2011-08-02 rsc host: The host to send requests to.
2769 26b4bea8 2011-08-02 rsc auth_function: A function that takes no arguments and returns an
2770 26b4bea8 2011-08-02 rsc (email, password) tuple when called. Will be called if authentication
2771 26b4bea8 2011-08-02 rsc is required.
2772 26b4bea8 2011-08-02 rsc host_override: The host header to send to the server (defaults to host).
2773 26b4bea8 2011-08-02 rsc extra_headers: A dict of extra headers to append to every request.
2774 26b4bea8 2011-08-02 rsc save_cookies: If True, save the authentication cookies to local disk.
2775 26b4bea8 2011-08-02 rsc If False, use an in-memory cookiejar instead. Subclasses must
2776 26b4bea8 2011-08-02 rsc implement this functionality. Defaults to False.
2777 26b4bea8 2011-08-02 rsc """
2778 26b4bea8 2011-08-02 rsc self.host = host
2779 26b4bea8 2011-08-02 rsc self.host_override = host_override
2780 26b4bea8 2011-08-02 rsc self.auth_function = auth_function
2781 26b4bea8 2011-08-02 rsc self.authenticated = False
2782 26b4bea8 2011-08-02 rsc self.extra_headers = extra_headers
2783 26b4bea8 2011-08-02 rsc self.save_cookies = save_cookies
2784 26b4bea8 2011-08-02 rsc self.opener = self._GetOpener()
2785 26b4bea8 2011-08-02 rsc if self.host_override:
2786 26b4bea8 2011-08-02 rsc logging.info("Server: %s; Host: %s", self.host, self.host_override)
2787 26b4bea8 2011-08-02 rsc else:
2788 26b4bea8 2011-08-02 rsc logging.info("Server: %s", self.host)
2789 ef27bfa4 2010-01-12 rsc
2790 26b4bea8 2011-08-02 rsc def _GetOpener(self):
2791 26b4bea8 2011-08-02 rsc """Returns an OpenerDirector for making HTTP requests.
2792 ef27bfa4 2010-01-12 rsc
2793 26b4bea8 2011-08-02 rsc Returns:
2794 26b4bea8 2011-08-02 rsc A urllib2.OpenerDirector object.
2795 26b4bea8 2011-08-02 rsc """
2796 26b4bea8 2011-08-02 rsc raise NotImplementedError()
2797 ef27bfa4 2010-01-12 rsc
2798 26b4bea8 2011-08-02 rsc def _CreateRequest(self, url, data=None):
2799 26b4bea8 2011-08-02 rsc """Creates a new urllib request."""
2800 26b4bea8 2011-08-02 rsc logging.debug("Creating request for: '%s' with payload:\n%s", url, data)
2801 26b4bea8 2011-08-02 rsc req = urllib2.Request(url, data=data)
2802 26b4bea8 2011-08-02 rsc if self.host_override:
2803 26b4bea8 2011-08-02 rsc req.add_header("Host", self.host_override)
2804 26b4bea8 2011-08-02 rsc for key, value in self.extra_headers.iteritems():
2805 26b4bea8 2011-08-02 rsc req.add_header(key, value)
2806 26b4bea8 2011-08-02 rsc return req
2807 ef27bfa4 2010-01-12 rsc
2808 26b4bea8 2011-08-02 rsc def _GetAuthToken(self, email, password):
2809 26b4bea8 2011-08-02 rsc """Uses ClientLogin to authenticate the user, returning an auth token.
2810 ef27bfa4 2010-01-12 rsc
2811 26b4bea8 2011-08-02 rsc Args:
2812 26b4bea8 2011-08-02 rsc email: The user's email address
2813 26b4bea8 2011-08-02 rsc password: The user's password
2814 ef27bfa4 2010-01-12 rsc
2815 26b4bea8 2011-08-02 rsc Raises:
2816 26b4bea8 2011-08-02 rsc ClientLoginError: If there was an error authenticating with ClientLogin.
2817 26b4bea8 2011-08-02 rsc HTTPError: If there was some other form of HTTP error.
2818 ef27bfa4 2010-01-12 rsc
2819 26b4bea8 2011-08-02 rsc Returns:
2820 26b4bea8 2011-08-02 rsc The authentication token returned by ClientLogin.
2821 26b4bea8 2011-08-02 rsc """
2822 26b4bea8 2011-08-02 rsc account_type = "GOOGLE"
2823 26b4bea8 2011-08-02 rsc if self.host.endswith(".google.com") and not force_google_account:
2824 26b4bea8 2011-08-02 rsc # Needed for use inside Google.
2825 26b4bea8 2011-08-02 rsc account_type = "HOSTED"
2826 26b4bea8 2011-08-02 rsc req = self._CreateRequest(
2827 26b4bea8 2011-08-02 rsc url="https://www.google.com/accounts/ClientLogin",
2828 26b4bea8 2011-08-02 rsc data=urllib.urlencode({
2829 26b4bea8 2011-08-02 rsc "Email": email,
2830 26b4bea8 2011-08-02 rsc "Passwd": password,
2831 26b4bea8 2011-08-02 rsc "service": "ah",
2832 26b4bea8 2011-08-02 rsc "source": "rietveld-codereview-upload",
2833 26b4bea8 2011-08-02 rsc "accountType": account_type,
2834 26b4bea8 2011-08-02 rsc }),
2835 26b4bea8 2011-08-02 rsc )
2836 26b4bea8 2011-08-02 rsc try:
2837 26b4bea8 2011-08-02 rsc response = self.opener.open(req)
2838 26b4bea8 2011-08-02 rsc response_body = response.read()
2839 26b4bea8 2011-08-02 rsc response_dict = dict(x.split("=") for x in response_body.split("\n") if x)
2840 26b4bea8 2011-08-02 rsc return response_dict["Auth"]
2841 26b4bea8 2011-08-02 rsc except urllib2.HTTPError, e:
2842 26b4bea8 2011-08-02 rsc if e.code == 403:
2843 26b4bea8 2011-08-02 rsc body = e.read()
2844 26b4bea8 2011-08-02 rsc response_dict = dict(x.split("=", 1) for x in body.split("\n") if x)
2845 26b4bea8 2011-08-02 rsc raise ClientLoginError(req.get_full_url(), e.code, e.msg, e.headers, response_dict)
2846 26b4bea8 2011-08-02 rsc else:
2847 26b4bea8 2011-08-02 rsc raise
2848 ef27bfa4 2010-01-12 rsc
2849 26b4bea8 2011-08-02 rsc def _GetAuthCookie(self, auth_token):
2850 26b4bea8 2011-08-02 rsc """Fetches authentication cookies for an authentication token.
2851 ef27bfa4 2010-01-12 rsc
2852 26b4bea8 2011-08-02 rsc Args:
2853 26b4bea8 2011-08-02 rsc auth_token: The authentication token returned by ClientLogin.
2854 ef27bfa4 2010-01-12 rsc
2855 26b4bea8 2011-08-02 rsc Raises:
2856 26b4bea8 2011-08-02 rsc HTTPError: If there was an error fetching the authentication cookies.
2857 26b4bea8 2011-08-02 rsc """
2858 26b4bea8 2011-08-02 rsc # This is a dummy value to allow us to identify when we're successful.
2859 26b4bea8 2011-08-02 rsc continue_location = "http://localhost/"
2860 26b4bea8 2011-08-02 rsc args = {"continue": continue_location, "auth": auth_token}
2861 26b4bea8 2011-08-02 rsc req = self._CreateRequest("http://%s/_ah/login?%s" % (self.host, urllib.urlencode(args)))
2862 26b4bea8 2011-08-02 rsc try:
2863 26b4bea8 2011-08-02 rsc response = self.opener.open(req)
2864 26b4bea8 2011-08-02 rsc except urllib2.HTTPError, e:
2865 26b4bea8 2011-08-02 rsc response = e
2866 26b4bea8 2011-08-02 rsc if (response.code != 302 or
2867 26b4bea8 2011-08-02 rsc response.info()["location"] != continue_location):
2868 26b4bea8 2011-08-02 rsc raise urllib2.HTTPError(req.get_full_url(), response.code, response.msg, response.headers, response.fp)
2869 26b4bea8 2011-08-02 rsc self.authenticated = True
2870 ef27bfa4 2010-01-12 rsc
2871 26b4bea8 2011-08-02 rsc def _Authenticate(self):
2872 26b4bea8 2011-08-02 rsc """Authenticates the user.
2873 ef27bfa4 2010-01-12 rsc
2874 26b4bea8 2011-08-02 rsc The authentication process works as follows:
2875 26b4bea8 2011-08-02 rsc 1) We get a username and password from the user
2876 26b4bea8 2011-08-02 rsc 2) We use ClientLogin to obtain an AUTH token for the user
2877 26b4bea8 2011-08-02 rsc (see http://code.google.com/apis/accounts/AuthForInstalledApps.html).
2878 26b4bea8 2011-08-02 rsc 3) We pass the auth token to /_ah/login on the server to obtain an
2879 26b4bea8 2011-08-02 rsc authentication cookie. If login was successful, it tries to redirect
2880 26b4bea8 2011-08-02 rsc us to the URL we provided.
2881 ef27bfa4 2010-01-12 rsc
2882 26b4bea8 2011-08-02 rsc If we attempt to access the upload API without first obtaining an
2883 26b4bea8 2011-08-02 rsc authentication cookie, it returns a 401 response (or a 302) and
2884 26b4bea8 2011-08-02 rsc directs us to authenticate ourselves with ClientLogin.
2885 26b4bea8 2011-08-02 rsc """
2886 26b4bea8 2011-08-02 rsc for i in range(3):
2887 26b4bea8 2011-08-02 rsc credentials = self.auth_function()
2888 26b4bea8 2011-08-02 rsc try:
2889 26b4bea8 2011-08-02 rsc auth_token = self._GetAuthToken(credentials[0], credentials[1])
2890 26b4bea8 2011-08-02 rsc except ClientLoginError, e:
2891 26b4bea8 2011-08-02 rsc if e.reason == "BadAuthentication":
2892 26b4bea8 2011-08-02 rsc print >>sys.stderr, "Invalid username or password."
2893 26b4bea8 2011-08-02 rsc continue
2894 26b4bea8 2011-08-02 rsc if e.reason == "CaptchaRequired":
2895 26b4bea8 2011-08-02 rsc print >>sys.stderr, (
2896 26b4bea8 2011-08-02 rsc "Please go to\n"
2897 26b4bea8 2011-08-02 rsc "https://www.google.com/accounts/DisplayUnlockCaptcha\n"
2898 26b4bea8 2011-08-02 rsc "and verify you are a human. Then try again.")
2899 26b4bea8 2011-08-02 rsc break
2900 26b4bea8 2011-08-02 rsc if e.reason == "NotVerified":
2901 26b4bea8 2011-08-02 rsc print >>sys.stderr, "Account not verified."
2902 26b4bea8 2011-08-02 rsc break
2903 26b4bea8 2011-08-02 rsc if e.reason == "TermsNotAgreed":
2904 26b4bea8 2011-08-02 rsc print >>sys.stderr, "User has not agreed to TOS."
2905 26b4bea8 2011-08-02 rsc break
2906 26b4bea8 2011-08-02 rsc if e.reason == "AccountDeleted":
2907 26b4bea8 2011-08-02 rsc print >>sys.stderr, "The user account has been deleted."
2908 26b4bea8 2011-08-02 rsc break
2909 26b4bea8 2011-08-02 rsc if e.reason == "AccountDisabled":
2910 26b4bea8 2011-08-02 rsc print >>sys.stderr, "The user account has been disabled."
2911 26b4bea8 2011-08-02 rsc break
2912 26b4bea8 2011-08-02 rsc if e.reason == "ServiceDisabled":
2913 26b4bea8 2011-08-02 rsc print >>sys.stderr, "The user's access to the service has been disabled."
2914 26b4bea8 2011-08-02 rsc break
2915 26b4bea8 2011-08-02 rsc if e.reason == "ServiceUnavailable":
2916 26b4bea8 2011-08-02 rsc print >>sys.stderr, "The service is not available; try again later."
2917 26b4bea8 2011-08-02 rsc break
2918 26b4bea8 2011-08-02 rsc raise
2919 26b4bea8 2011-08-02 rsc self._GetAuthCookie(auth_token)
2920 26b4bea8 2011-08-02 rsc return
2921 ef27bfa4 2010-01-12 rsc
2922 26b4bea8 2011-08-02 rsc def Send(self, request_path, payload=None,
2923 26b4bea8 2011-08-02 rsc content_type="application/octet-stream",
2924 26b4bea8 2011-08-02 rsc timeout=None,
2925 26b4bea8 2011-08-02 rsc **kwargs):
2926 26b4bea8 2011-08-02 rsc """Sends an RPC and returns the response.
2927 ef27bfa4 2010-01-12 rsc
2928 26b4bea8 2011-08-02 rsc Args:
2929 26b4bea8 2011-08-02 rsc request_path: The path to send the request to, eg /api/appversion/create.
2930 26b4bea8 2011-08-02 rsc payload: The body of the request, or None to send an empty request.
2931 26b4bea8 2011-08-02 rsc content_type: The Content-Type header to use.
2932 26b4bea8 2011-08-02 rsc timeout: timeout in seconds; default None i.e. no timeout.
2933 26b4bea8 2011-08-02 rsc (Note: for large requests on OS X, the timeout doesn't work right.)
2934 26b4bea8 2011-08-02 rsc kwargs: Any keyword arguments are converted into query string parameters.
2935 ef27bfa4 2010-01-12 rsc
2936 26b4bea8 2011-08-02 rsc Returns:
2937 26b4bea8 2011-08-02 rsc The response body, as a string.
2938 26b4bea8 2011-08-02 rsc """
2939 26b4bea8 2011-08-02 rsc # TODO: Don't require authentication. Let the server say
2940 26b4bea8 2011-08-02 rsc # whether it is necessary.
2941 26b4bea8 2011-08-02 rsc if not self.authenticated:
2942 26b4bea8 2011-08-02 rsc self._Authenticate()
2943 ef27bfa4 2010-01-12 rsc
2944 26b4bea8 2011-08-02 rsc old_timeout = socket.getdefaulttimeout()
2945 26b4bea8 2011-08-02 rsc socket.setdefaulttimeout(timeout)
2946 26b4bea8 2011-08-02 rsc try:
2947 26b4bea8 2011-08-02 rsc tries = 0
2948 26b4bea8 2011-08-02 rsc while True:
2949 26b4bea8 2011-08-02 rsc tries += 1
2950 26b4bea8 2011-08-02 rsc args = dict(kwargs)
2951 26b4bea8 2011-08-02 rsc url = "http://%s%s" % (self.host, request_path)
2952 26b4bea8 2011-08-02 rsc if args:
2953 26b4bea8 2011-08-02 rsc url += "?" + urllib.urlencode(args)
2954 26b4bea8 2011-08-02 rsc req = self._CreateRequest(url=url, data=payload)
2955 26b4bea8 2011-08-02 rsc req.add_header("Content-Type", content_type)
2956 26b4bea8 2011-08-02 rsc try:
2957 26b4bea8 2011-08-02 rsc f = self.opener.open(req)
2958 26b4bea8 2011-08-02 rsc response = f.read()
2959 26b4bea8 2011-08-02 rsc f.close()
2960 26b4bea8 2011-08-02 rsc return response
2961 26b4bea8 2011-08-02 rsc except urllib2.HTTPError, e:
2962 26b4bea8 2011-08-02 rsc if tries > 3:
2963 26b4bea8 2011-08-02 rsc raise
2964 26b4bea8 2011-08-02 rsc elif e.code == 401 or e.code == 302:
2965 26b4bea8 2011-08-02 rsc self._Authenticate()
2966 26b4bea8 2011-08-02 rsc else:
2967 26b4bea8 2011-08-02 rsc raise
2968 26b4bea8 2011-08-02 rsc finally:
2969 26b4bea8 2011-08-02 rsc socket.setdefaulttimeout(old_timeout)
2970 ef27bfa4 2010-01-12 rsc
2971 ef27bfa4 2010-01-12 rsc
2972 ef27bfa4 2010-01-12 rsc class HttpRpcServer(AbstractRpcServer):
2973 26b4bea8 2011-08-02 rsc """Provides a simplified RPC-style interface for HTTP requests."""
2974 ef27bfa4 2010-01-12 rsc
2975 26b4bea8 2011-08-02 rsc def _Authenticate(self):
2976 26b4bea8 2011-08-02 rsc """Save the cookie jar after authentication."""
2977 26b4bea8 2011-08-02 rsc super(HttpRpcServer, self)._Authenticate()
2978 26b4bea8 2011-08-02 rsc if self.save_cookies:
2979 26b4bea8 2011-08-02 rsc StatusUpdate("Saving authentication cookies to %s" % self.cookie_file)
2980 26b4bea8 2011-08-02 rsc self.cookie_jar.save()
2981 ef27bfa4 2010-01-12 rsc
2982 26b4bea8 2011-08-02 rsc def _GetOpener(self):
2983 26b4bea8 2011-08-02 rsc """Returns an OpenerDirector that supports cookies and ignores redirects.
2984 ef27bfa4 2010-01-12 rsc
2985 26b4bea8 2011-08-02 rsc Returns:
2986 26b4bea8 2011-08-02 rsc A urllib2.OpenerDirector object.
2987 26b4bea8 2011-08-02 rsc """
2988 26b4bea8 2011-08-02 rsc opener = urllib2.OpenerDirector()
2989 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.ProxyHandler())
2990 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.UnknownHandler())
2991 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.HTTPHandler())
2992 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.HTTPDefaultErrorHandler())
2993 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.HTTPSHandler())
2994 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.HTTPErrorProcessor())
2995 26b4bea8 2011-08-02 rsc if self.save_cookies:
2996 26b4bea8 2011-08-02 rsc self.cookie_file = os.path.expanduser("~/.codereview_upload_cookies_" + server)
2997 26b4bea8 2011-08-02 rsc self.cookie_jar = cookielib.MozillaCookieJar(self.cookie_file)
2998 26b4bea8 2011-08-02 rsc if os.path.exists(self.cookie_file):
2999 26b4bea8 2011-08-02 rsc try:
3000 26b4bea8 2011-08-02 rsc self.cookie_jar.load()
3001 26b4bea8 2011-08-02 rsc self.authenticated = True
3002 26b4bea8 2011-08-02 rsc StatusUpdate("Loaded authentication cookies from %s" % self.cookie_file)
3003 26b4bea8 2011-08-02 rsc except (cookielib.LoadError, IOError):
3004 26b4bea8 2011-08-02 rsc # Failed to load cookies - just ignore them.
3005 26b4bea8 2011-08-02 rsc pass
3006 26b4bea8 2011-08-02 rsc else:
3007 26b4bea8 2011-08-02 rsc # Create an empty cookie file with mode 600
3008 26b4bea8 2011-08-02 rsc fd = os.open(self.cookie_file, os.O_CREAT, 0600)
3009 26b4bea8 2011-08-02 rsc os.close(fd)
3010 26b4bea8 2011-08-02 rsc # Always chmod the cookie file
3011 26b4bea8 2011-08-02 rsc os.chmod(self.cookie_file, 0600)
3012 26b4bea8 2011-08-02 rsc else:
3013 26b4bea8 2011-08-02 rsc # Don't save cookies across runs of update.py.
3014 26b4bea8 2011-08-02 rsc self.cookie_jar = cookielib.CookieJar()
3015 26b4bea8 2011-08-02 rsc opener.add_handler(urllib2.HTTPCookieProcessor(self.cookie_jar))
3016 26b4bea8 2011-08-02 rsc return opener
3017 ef27bfa4 2010-01-12 rsc
3018 ef27bfa4 2010-01-12 rsc
3019 26b4bea8 2011-08-02 rsc def GetRpcServer(options):
3020 26b4bea8 2011-08-02 rsc """Returns an instance of an AbstractRpcServer.
3021 ef27bfa4 2010-01-12 rsc
3022 26b4bea8 2011-08-02 rsc Returns:
3023 26b4bea8 2011-08-02 rsc A new AbstractRpcServer, on which RPC calls can be made.
3024 26b4bea8 2011-08-02 rsc """
3025 ef27bfa4 2010-01-12 rsc
3026 26b4bea8 2011-08-02 rsc rpc_server_class = HttpRpcServer
3027 ef27bfa4 2010-01-12 rsc
3028 26b4bea8 2011-08-02 rsc def GetUserCredentials():
3029 26b4bea8 2011-08-02 rsc """Prompts the user for a username and password."""
3030 26b4bea8 2011-08-02 rsc # Disable status prints so they don't obscure the password prompt.
3031 26b4bea8 2011-08-02 rsc global global_status
3032 26b4bea8 2011-08-02 rsc st = global_status
3033 26b4bea8 2011-08-02 rsc global_status = None
3034 ef27bfa4 2010-01-12 rsc
3035 26b4bea8 2011-08-02 rsc email = options.email
3036 26b4bea8 2011-08-02 rsc if email is None:
3037 26b4bea8 2011-08-02 rsc email = GetEmail("Email (login for uploading to %s)" % options.server)
3038 26b4bea8 2011-08-02 rsc password = getpass.getpass("Password for %s: " % email)
3039 ef27bfa4 2010-01-12 rsc
3040 26b4bea8 2011-08-02 rsc # Put status back.
3041 26b4bea8 2011-08-02 rsc global_status = st
3042 26b4bea8 2011-08-02 rsc return (email, password)
3043 ef27bfa4 2010-01-12 rsc
3044 26b4bea8 2011-08-02 rsc # If this is the dev_appserver, use fake authentication.
3045 26b4bea8 2011-08-02 rsc host = (options.host or options.server).lower()
3046 26b4bea8 2011-08-02 rsc if host == "localhost" or host.startswith("localhost:"):
3047 26b4bea8 2011-08-02 rsc email = options.email
3048 26b4bea8 2011-08-02 rsc if email is None:
3049 26b4bea8 2011-08-02 rsc email = "test@example.com"
3050 26b4bea8 2011-08-02 rsc logging.info("Using debug user %s. Override with --email" % email)
3051 26b4bea8 2011-08-02 rsc server = rpc_server_class(
3052 26b4bea8 2011-08-02 rsc options.server,
3053 26b4bea8 2011-08-02 rsc lambda: (email, "password"),
3054 26b4bea8 2011-08-02 rsc host_override=options.host,
3055 26b4bea8 2011-08-02 rsc extra_headers={"Cookie": 'dev_appserver_login="%s:False"' % email},
3056 26b4bea8 2011-08-02 rsc save_cookies=options.save_cookies)
3057 26b4bea8 2011-08-02 rsc # Don't try to talk to ClientLogin.
3058 26b4bea8 2011-08-02 rsc server.authenticated = True
3059 26b4bea8 2011-08-02 rsc return server
3060 ef27bfa4 2010-01-12 rsc
3061 26b4bea8 2011-08-02 rsc return rpc_server_class(options.server, GetUserCredentials,
3062 26b4bea8 2011-08-02 rsc host_override=options.host, save_cookies=options.save_cookies)
3063 ef27bfa4 2010-01-12 rsc
3064 ef27bfa4 2010-01-12 rsc
3065 ef27bfa4 2010-01-12 rsc def EncodeMultipartFormData(fields, files):
3066 26b4bea8 2011-08-02 rsc """Encode form fields for multipart/form-data.
3067 ef27bfa4 2010-01-12 rsc
3068 26b4bea8 2011-08-02 rsc Args:
3069 26b4bea8 2011-08-02 rsc fields: A sequence of (name, value) elements for regular form fields.
3070 26b4bea8 2011-08-02 rsc files: A sequence of (name, filename, value) elements for data to be
3071 26b4bea8 2011-08-02 rsc uploaded as files.
3072 26b4bea8 2011-08-02 rsc Returns:
3073 26b4bea8 2011-08-02 rsc (content_type, body) ready for httplib.HTTP instance.
3074 ef27bfa4 2010-01-12 rsc
3075 26b4bea8 2011-08-02 rsc Source:
3076 26b4bea8 2011-08-02 rsc http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306
3077 26b4bea8 2011-08-02 rsc """
3078 26b4bea8 2011-08-02 rsc BOUNDARY = '-M-A-G-I-C---B-O-U-N-D-A-R-Y-'
3079 26b4bea8 2011-08-02 rsc CRLF = '\r\n'
3080 26b4bea8 2011-08-02 rsc lines = []
3081 26b4bea8 2011-08-02 rsc for (key, value) in fields:
3082 26b4bea8 2011-08-02 rsc typecheck(key, str)
3083 26b4bea8 2011-08-02 rsc typecheck(value, str)
3084 26b4bea8 2011-08-02 rsc lines.append('--' + BOUNDARY)
3085 26b4bea8 2011-08-02 rsc lines.append('Content-Disposition: form-data; name="%s"' % key)
3086 26b4bea8 2011-08-02 rsc lines.append('')
3087 26b4bea8 2011-08-02 rsc lines.append(value)
3088 26b4bea8 2011-08-02 rsc for (key, filename, value) in files:
3089 26b4bea8 2011-08-02 rsc typecheck(key, str)
3090 26b4bea8 2011-08-02 rsc typecheck(filename, str)
3091 26b4bea8 2011-08-02 rsc typecheck(value, str)
3092 26b4bea8 2011-08-02 rsc lines.append('--' + BOUNDARY)
3093 26b4bea8 2011-08-02 rsc lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
3094 26b4bea8 2011-08-02 rsc lines.append('Content-Type: %s' % GetContentType(filename))
3095 26b4bea8 2011-08-02 rsc lines.append('')
3096 26b4bea8 2011-08-02 rsc lines.append(value)
3097 26b4bea8 2011-08-02 rsc lines.append('--' + BOUNDARY + '--')
3098 26b4bea8 2011-08-02 rsc lines.append('')
3099 26b4bea8 2011-08-02 rsc body = CRLF.join(lines)
3100 26b4bea8 2011-08-02 rsc content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
3101 26b4bea8 2011-08-02 rsc return content_type, body
3102 ef27bfa4 2010-01-12 rsc
3103 ef27bfa4 2010-01-12 rsc
3104 ef27bfa4 2010-01-12 rsc def GetContentType(filename):
3105 26b4bea8 2011-08-02 rsc """Helper to guess the content-type from the filename."""
3106 26b4bea8 2011-08-02 rsc return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
3107 ef27bfa4 2010-01-12 rsc
3108 ef27bfa4 2010-01-12 rsc
3109 ef27bfa4 2010-01-12 rsc # Use a shell for subcommands on Windows to get a PATH search.
3110 ef27bfa4 2010-01-12 rsc use_shell = sys.platform.startswith("win")
3111 ef27bfa4 2010-01-12 rsc
3112 ef27bfa4 2010-01-12 rsc def RunShellWithReturnCode(command, print_output=False,
3113 26b4bea8 2011-08-02 rsc universal_newlines=True, env=os.environ):
3114 26b4bea8 2011-08-02 rsc """Executes a command and returns the output from stdout and the return code.
3115 ef27bfa4 2010-01-12 rsc
3116 26b4bea8 2011-08-02 rsc Args:
3117 26b4bea8 2011-08-02 rsc command: Command to execute.
3118 26b4bea8 2011-08-02 rsc print_output: If True, the output is printed to stdout.
3119 26b4bea8 2011-08-02 rsc If False, both stdout and stderr are ignored.
3120 26b4bea8 2011-08-02 rsc universal_newlines: Use universal_newlines flag (default: True).
3121 ef27bfa4 2010-01-12 rsc
3122 26b4bea8 2011-08-02 rsc Returns:
3123 26b4bea8 2011-08-02 rsc Tuple (output, return code)
3124 26b4bea8 2011-08-02 rsc """
3125 26b4bea8 2011-08-02 rsc logging.info("Running %s", command)
3126 26b4bea8 2011-08-02 rsc p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
3127 26b4bea8 2011-08-02 rsc shell=use_shell, universal_newlines=universal_newlines, env=env)
3128 26b4bea8 2011-08-02 rsc if print_output:
3129 26b4bea8 2011-08-02 rsc output_array = []
3130 26b4bea8 2011-08-02 rsc while True:
3131 26b4bea8 2011-08-02 rsc line = p.stdout.readline()
3132 26b4bea8 2011-08-02 rsc if not line:
3133 26b4bea8 2011-08-02 rsc break
3134 26b4bea8 2011-08-02 rsc print line.strip("\n")
3135 26b4bea8 2011-08-02 rsc output_array.append(line)
3136 26b4bea8 2011-08-02 rsc output = "".join(output_array)
3137 26b4bea8 2011-08-02 rsc else:
3138 26b4bea8 2011-08-02 rsc output = p.stdout.read()
3139 26b4bea8 2011-08-02 rsc p.wait()
3140 26b4bea8 2011-08-02 rsc errout = p.stderr.read()
3141 26b4bea8 2011-08-02 rsc if print_output and errout:
3142 26b4bea8 2011-08-02 rsc print >>sys.stderr, errout
3143 26b4bea8 2011-08-02 rsc p.stdout.close()
3144 26b4bea8 2011-08-02 rsc p.stderr.close()
3145 26b4bea8 2011-08-02 rsc return output, p.returncode
3146 ef27bfa4 2010-01-12 rsc
3147 ef27bfa4 2010-01-12 rsc
3148 26b4bea8 2011-08-02 rsc def RunShell(command, silent_ok=False, universal_newlines=True,
3149 26b4bea8 2011-08-02 rsc print_output=False, env=os.environ):
3150 26b4bea8 2011-08-02 rsc data, retcode = RunShellWithReturnCode(command, print_output, universal_newlines, env)
3151 26b4bea8 2011-08-02 rsc if retcode:
3152 26b4bea8 2011-08-02 rsc ErrorExit("Got error status from %s:\n%s" % (command, data))
3153 26b4bea8 2011-08-02 rsc if not silent_ok and not data:
3154 26b4bea8 2011-08-02 rsc ErrorExit("No output from %s" % command)
3155 26b4bea8 2011-08-02 rsc return data
3156 ef27bfa4 2010-01-12 rsc
3157 ef27bfa4 2010-01-12 rsc
3158 26b4bea8 2011-08-02 rsc class VersionControlSystem(object):
3159 26b4bea8 2011-08-02 rsc """Abstract base class providing an interface to the VCS."""
3160 ef27bfa4 2010-01-12 rsc
3161 26b4bea8 2011-08-02 rsc def __init__(self, options):
3162 26b4bea8 2011-08-02 rsc """Constructor.
3163 ef27bfa4 2010-01-12 rsc
3164 26b4bea8 2011-08-02 rsc Args:
3165 26b4bea8 2011-08-02 rsc options: Command line options.
3166 26b4bea8 2011-08-02 rsc """
3167 26b4bea8 2011-08-02 rsc self.options = options
3168 ef27bfa4 2010-01-12 rsc
3169 26b4bea8 2011-08-02 rsc def GenerateDiff(self, args):
3170 26b4bea8 2011-08-02 rsc """Return the current diff as a string.
3171 ef27bfa4 2010-01-12 rsc
3172 26b4bea8 2011-08-02 rsc Args:
3173 26b4bea8 2011-08-02 rsc args: Extra arguments to pass to the diff command.
3174 26b4bea8 2011-08-02 rsc """
3175 26b4bea8 2011-08-02 rsc raise NotImplementedError(
3176 26b4bea8 2011-08-02 rsc "abstract method -- subclass %s must override" % self.__class__)
3177 ef27bfa4 2010-01-12 rsc
3178 26b4bea8 2011-08-02 rsc def GetUnknownFiles(self):
3179 26b4bea8 2011-08-02 rsc """Return a list of files unknown to the VCS."""
3180 26b4bea8 2011-08-02 rsc raise NotImplementedError(
3181 26b4bea8 2011-08-02 rsc "abstract method -- subclass %s must override" % self.__class__)
3182 ef27bfa4 2010-01-12 rsc
3183 26b4bea8 2011-08-02 rsc def CheckForUnknownFiles(self):
3184 26b4bea8 2011-08-02 rsc """Show an "are you sure?" prompt if there are unknown files."""
3185 26b4bea8 2011-08-02 rsc unknown_files = self.GetUnknownFiles()
3186 26b4bea8 2011-08-02 rsc if unknown_files:
3187 26b4bea8 2011-08-02 rsc print "The following files are not added to version control:"
3188 26b4bea8 2011-08-02 rsc for line in unknown_files:
3189 26b4bea8 2011-08-02 rsc print line
3190 26b4bea8 2011-08-02 rsc prompt = "Are you sure to continue?(y/N) "
3191 26b4bea8 2011-08-02 rsc answer = raw_input(prompt).strip()
3192 26b4bea8 2011-08-02 rsc if answer != "y":
3193 26b4bea8 2011-08-02 rsc ErrorExit("User aborted")
3194 ef27bfa4 2010-01-12 rsc
3195 26b4bea8 2011-08-02 rsc def GetBaseFile(self, filename):
3196 26b4bea8 2011-08-02 rsc """Get the content of the upstream version of a file.
3197 ef27bfa4 2010-01-12 rsc
3198 26b4bea8 2011-08-02 rsc Returns:
3199 26b4bea8 2011-08-02 rsc A tuple (base_content, new_content, is_binary, status)
3200 26b4bea8 2011-08-02 rsc base_content: The contents of the base file.
3201 26b4bea8 2011-08-02 rsc new_content: For text files, this is empty. For binary files, this is
3202 26b4bea8 2011-08-02 rsc the contents of the new file, since the diff output won't contain
3203 26b4bea8 2011-08-02 rsc information to reconstruct the current file.
3204 26b4bea8 2011-08-02 rsc is_binary: True iff the file is binary.
3205 26b4bea8 2011-08-02 rsc status: The status of the file.
3206 26b4bea8 2011-08-02 rsc """
3207 ef27bfa4 2010-01-12 rsc
3208 26b4bea8 2011-08-02 rsc raise NotImplementedError(
3209 26b4bea8 2011-08-02 rsc "abstract method -- subclass %s must override" % self.__class__)
3210 ef27bfa4 2010-01-12 rsc
3211 ef27bfa4 2010-01-12 rsc
3212 26b4bea8 2011-08-02 rsc def GetBaseFiles(self, diff):
3213 26b4bea8 2011-08-02 rsc """Helper that calls GetBase file for each file in the patch.
3214 ef27bfa4 2010-01-12 rsc
3215 26b4bea8 2011-08-02 rsc Returns:
3216 26b4bea8 2011-08-02 rsc A dictionary that maps from filename to GetBaseFile's tuple. Filenames
3217 26b4bea8 2011-08-02 rsc are retrieved based on lines that start with "Index:" or
3218 26b4bea8 2011-08-02 rsc "Property changes on:".
3219 26b4bea8 2011-08-02 rsc """
3220 26b4bea8 2011-08-02 rsc files = {}
3221 26b4bea8 2011-08-02 rsc for line in diff.splitlines(True):
3222 26b4bea8 2011-08-02 rsc if line.startswith('Index:') or line.startswith('Property changes on:'):
3223 26b4bea8 2011-08-02 rsc unused, filename = line.split(':', 1)
3224 26b4bea8 2011-08-02 rsc # On Windows if a file has property changes its filename uses '\'
3225 26b4bea8 2011-08-02 rsc # instead of '/'.
3226 5c934375 2012-01-20 rsc filename = to_slash(filename.strip())
3227 26b4bea8 2011-08-02 rsc files[filename] = self.GetBaseFile(filename)
3228 26b4bea8 2011-08-02 rsc return files
3229 ef27bfa4 2010-01-12 rsc
3230 ef27bfa4 2010-01-12 rsc
3231 26b4bea8 2011-08-02 rsc def UploadBaseFiles(self, issue, rpc_server, patch_list, patchset, options,
3232 26b4bea8 2011-08-02 rsc files):
3233 26b4bea8 2011-08-02 rsc """Uploads the base files (and if necessary, the current ones as well)."""
3234 ef27bfa4 2010-01-12 rsc
3235 26b4bea8 2011-08-02 rsc def UploadFile(filename, file_id, content, is_binary, status, is_base):
3236 26b4bea8 2011-08-02 rsc """Uploads a file to the server."""
3237 26b4bea8 2011-08-02 rsc set_status("uploading " + filename)
3238 26b4bea8 2011-08-02 rsc file_too_large = False
3239 26b4bea8 2011-08-02 rsc if is_base:
3240 26b4bea8 2011-08-02 rsc type = "base"
3241 26b4bea8 2011-08-02 rsc else:
3242 26b4bea8 2011-08-02 rsc type = "current"
3243 26b4bea8 2011-08-02 rsc if len(content) > MAX_UPLOAD_SIZE:
3244 26b4bea8 2011-08-02 rsc print ("Not uploading the %s file for %s because it's too large." %
3245 26b4bea8 2011-08-02 rsc (type, filename))
3246 26b4bea8 2011-08-02 rsc file_too_large = True
3247 26b4bea8 2011-08-02 rsc content = ""
3248 26b4bea8 2011-08-02 rsc checksum = md5(content).hexdigest()
3249 26b4bea8 2011-08-02 rsc if options.verbose > 0 and not file_too_large:
3250 26b4bea8 2011-08-02 rsc print "Uploading %s file for %s" % (type, filename)
3251 26b4bea8 2011-08-02 rsc url = "/%d/upload_content/%d/%d" % (int(issue), int(patchset), file_id)
3252 26b4bea8 2011-08-02 rsc form_fields = [
3253 26b4bea8 2011-08-02 rsc ("filename", filename),
3254 26b4bea8 2011-08-02 rsc ("status", status),
3255 26b4bea8 2011-08-02 rsc ("checksum", checksum),
3256 26b4bea8 2011-08-02 rsc ("is_binary", str(is_binary)),
3257 26b4bea8 2011-08-02 rsc ("is_current", str(not is_base)),
3258 26b4bea8 2011-08-02 rsc ]
3259 26b4bea8 2011-08-02 rsc if file_too_large:
3260 26b4bea8 2011-08-02 rsc form_fields.append(("file_too_large", "1"))
3261 26b4bea8 2011-08-02 rsc if options.email:
3262 26b4bea8 2011-08-02 rsc form_fields.append(("user", options.email))
3263 26b4bea8 2011-08-02 rsc ctype, body = EncodeMultipartFormData(form_fields, [("data", filename, content)])
3264 26b4bea8 2011-08-02 rsc response_body = rpc_server.Send(url, body, content_type=ctype)
3265 26b4bea8 2011-08-02 rsc if not response_body.startswith("OK"):
3266 26b4bea8 2011-08-02 rsc StatusUpdate(" --> %s" % response_body)
3267 26b4bea8 2011-08-02 rsc sys.exit(1)
3268 ef27bfa4 2010-01-12 rsc
3269 26b4bea8 2011-08-02 rsc # Don't want to spawn too many threads, nor do we want to
3270 26b4bea8 2011-08-02 rsc # hit Rietveld too hard, or it will start serving 500 errors.
3271 26b4bea8 2011-08-02 rsc # When 8 works, it's no better than 4, and sometimes 8 is
3272 26b4bea8 2011-08-02 rsc # too many for Rietveld to handle.
3273 26b4bea8 2011-08-02 rsc MAX_PARALLEL_UPLOADS = 4
3274 ef27bfa4 2010-01-12 rsc
3275 26b4bea8 2011-08-02 rsc sema = threading.BoundedSemaphore(MAX_PARALLEL_UPLOADS)
3276 26b4bea8 2011-08-02 rsc upload_threads = []
3277 26b4bea8 2011-08-02 rsc finished_upload_threads = []
3278 26b4bea8 2011-08-02 rsc
3279 26b4bea8 2011-08-02 rsc class UploadFileThread(threading.Thread):
3280 26b4bea8 2011-08-02 rsc def __init__(self, args):
3281 26b4bea8 2011-08-02 rsc threading.Thread.__init__(self)
3282 26b4bea8 2011-08-02 rsc self.args = args
3283 26b4bea8 2011-08-02 rsc def run(self):
3284 26b4bea8 2011-08-02 rsc UploadFile(*self.args)
3285 26b4bea8 2011-08-02 rsc finished_upload_threads.append(self)
3286 26b4bea8 2011-08-02 rsc sema.release()
3287 ef27bfa4 2010-01-12 rsc
3288 26b4bea8 2011-08-02 rsc def StartUploadFile(*args):
3289 26b4bea8 2011-08-02 rsc sema.acquire()
3290 26b4bea8 2011-08-02 rsc while len(finished_upload_threads) > 0:
3291 26b4bea8 2011-08-02 rsc t = finished_upload_threads.pop()
3292 26b4bea8 2011-08-02 rsc upload_threads.remove(t)
3293 26b4bea8 2011-08-02 rsc t.join()
3294 26b4bea8 2011-08-02 rsc t = UploadFileThread(args)
3295 26b4bea8 2011-08-02 rsc upload_threads.append(t)
3296 26b4bea8 2011-08-02 rsc t.start()
3297 ef27bfa4 2010-01-12 rsc
3298 26b4bea8 2011-08-02 rsc def WaitForUploads():
3299 26b4bea8 2011-08-02 rsc for t in upload_threads:
3300 26b4bea8 2011-08-02 rsc t.join()
3301 ef27bfa4 2010-01-12 rsc
3302 26b4bea8 2011-08-02 rsc patches = dict()
3303 26b4bea8 2011-08-02 rsc [patches.setdefault(v, k) for k, v in patch_list]
3304 26b4bea8 2011-08-02 rsc for filename in patches.keys():
3305 26b4bea8 2011-08-02 rsc base_content, new_content, is_binary, status = files[filename]
3306 26b4bea8 2011-08-02 rsc file_id_str = patches.get(filename)
3307 26b4bea8 2011-08-02 rsc if file_id_str.find("nobase") != -1:
3308 26b4bea8 2011-08-02 rsc base_content = None
3309 26b4bea8 2011-08-02 rsc file_id_str = file_id_str[file_id_str.rfind("_") + 1:]
3310 26b4bea8 2011-08-02 rsc file_id = int(file_id_str)
3311 26b4bea8 2011-08-02 rsc if base_content != None:
3312 26b4bea8 2011-08-02 rsc StartUploadFile(filename, file_id, base_content, is_binary, status, True)
3313 26b4bea8 2011-08-02 rsc if new_content != None:
3314 26b4bea8 2011-08-02 rsc StartUploadFile(filename, file_id, new_content, is_binary, status, False)
3315 26b4bea8 2011-08-02 rsc WaitForUploads()
3316 ef27bfa4 2010-01-12 rsc
3317 26b4bea8 2011-08-02 rsc def IsImage(self, filename):
3318 26b4bea8 2011-08-02 rsc """Returns true if the filename has an image extension."""
3319 26b4bea8 2011-08-02 rsc mimetype = mimetypes.guess_type(filename)[0]
3320 26b4bea8 2011-08-02 rsc if not mimetype:
3321 26b4bea8 2011-08-02 rsc return False
3322 26b4bea8 2011-08-02 rsc return mimetype.startswith("image/")
3323 ef27bfa4 2010-01-12 rsc
3324 26b4bea8 2011-08-02 rsc def IsBinary(self, filename):
3325 26b4bea8 2011-08-02 rsc """Returns true if the guessed mimetyped isnt't in text group."""
3326 26b4bea8 2011-08-02 rsc mimetype = mimetypes.guess_type(filename)[0]
3327 26b4bea8 2011-08-02 rsc if not mimetype:
3328 26b4bea8 2011-08-02 rsc return False # e.g. README, "real" binaries usually have an extension
3329 26b4bea8 2011-08-02 rsc # special case for text files which don't start with text/
3330 26b4bea8 2011-08-02 rsc if mimetype in TEXT_MIMETYPES:
3331 26b4bea8 2011-08-02 rsc return False
3332 26b4bea8 2011-08-02 rsc return not mimetype.startswith("text/")
3333 338bd4bb 2011-11-08 rsc
3334 ef27bfa4 2010-01-12 rsc
3335 26b4bea8 2011-08-02 rsc class FakeMercurialUI(object):
3336 26b4bea8 2011-08-02 rsc def __init__(self):
3337 26b4bea8 2011-08-02 rsc self.quiet = True
3338 26b4bea8 2011-08-02 rsc self.output = ''
3339 26b4bea8 2011-08-02 rsc
3340 26b4bea8 2011-08-02 rsc def write(self, *args, **opts):
3341 26b4bea8 2011-08-02 rsc self.output += ' '.join(args)
3342 338bd4bb 2011-11-08 rsc def copy(self):
3343 338bd4bb 2011-11-08 rsc return self
3344 338bd4bb 2011-11-08 rsc def status(self, *args, **opts):
3345 338bd4bb 2011-11-08 rsc pass
3346 63550fce 2012-07-14 rsc
3347 63550fce 2012-07-14 rsc def formatter(self, topic, opts):
3348 63550fce 2012-07-14 rsc from mercurial.formatter import plainformatter
3349 63550fce 2012-07-14 rsc return plainformatter(self, topic, opts)
3350 338bd4bb 2011-11-08 rsc
3351 338bd4bb 2011-11-08 rsc def readconfig(self, *args, **opts):
3352 338bd4bb 2011-11-08 rsc pass
3353 338bd4bb 2011-11-08 rsc def expandpath(self, *args, **opts):
3354 338bd4bb 2011-11-08 rsc return global_ui.expandpath(*args, **opts)
3355 338bd4bb 2011-11-08 rsc def configitems(self, *args, **opts):
3356 338bd4bb 2011-11-08 rsc return global_ui.configitems(*args, **opts)
3357 338bd4bb 2011-11-08 rsc def config(self, *args, **opts):
3358 338bd4bb 2011-11-08 rsc return global_ui.config(*args, **opts)
3359 ef27bfa4 2010-01-12 rsc
3360 26b4bea8 2011-08-02 rsc use_hg_shell = False # set to True to shell out to hg always; slower
3361 ef27bfa4 2010-01-12 rsc
3362 26b4bea8 2011-08-02 rsc class MercurialVCS(VersionControlSystem):
3363 26b4bea8 2011-08-02 rsc """Implementation of the VersionControlSystem interface for Mercurial."""
3364 ef27bfa4 2010-01-12 rsc
3365 26b4bea8 2011-08-02 rsc def __init__(self, options, ui, repo):
3366 26b4bea8 2011-08-02 rsc super(MercurialVCS, self).__init__(options)
3367 26b4bea8 2011-08-02 rsc self.ui = ui
3368 26b4bea8 2011-08-02 rsc self.repo = repo
3369 338bd4bb 2011-11-08 rsc self.status = None
3370 26b4bea8 2011-08-02 rsc # Absolute path to repository (we can be in a subdir)
3371 26b4bea8 2011-08-02 rsc self.repo_dir = os.path.normpath(repo.root)
3372 26b4bea8 2011-08-02 rsc # Compute the subdir
3373 26b4bea8 2011-08-02 rsc cwd = os.path.normpath(os.getcwd())
3374 26b4bea8 2011-08-02 rsc assert cwd.startswith(self.repo_dir)
3375 26b4bea8 2011-08-02 rsc self.subdir = cwd[len(self.repo_dir):].lstrip(r"\/")
3376 26b4bea8 2011-08-02 rsc if self.options.revision:
3377 26b4bea8 2011-08-02 rsc self.base_rev = self.options.revision
3378 26b4bea8 2011-08-02 rsc else:
3379 26b4bea8 2011-08-02 rsc mqparent, err = RunShellWithReturnCode(['hg', 'log', '--rev', 'qparent', '--template={node}'])
3380 26b4bea8 2011-08-02 rsc if not err and mqparent != "":
3381 26b4bea8 2011-08-02 rsc self.base_rev = mqparent
3382 26b4bea8 2011-08-02 rsc else:
3383 63550fce 2012-07-14 rsc out = RunShell(["hg", "parents", "-q"], silent_ok=True).strip()
3384 63550fce 2012-07-14 rsc if not out:
3385 63550fce 2012-07-14 rsc # No revisions; use 0 to mean a repository with nothing.
3386 63550fce 2012-07-14 rsc out = "0:0"
3387 63550fce 2012-07-14 rsc self.base_rev = out.split(':')[1].strip()
3388 26b4bea8 2011-08-02 rsc def _GetRelPath(self, filename):
3389 26b4bea8 2011-08-02 rsc """Get relative path of a file according to the current directory,
3390 26b4bea8 2011-08-02 rsc given its logical path in the repo."""
3391 26b4bea8 2011-08-02 rsc assert filename.startswith(self.subdir), (filename, self.subdir)
3392 26b4bea8 2011-08-02 rsc return filename[len(self.subdir):].lstrip(r"\/")
3393 ef27bfa4 2010-01-12 rsc
3394 26b4bea8 2011-08-02 rsc def GenerateDiff(self, extra_args):
3395 26b4bea8 2011-08-02 rsc # If no file specified, restrict to the current subdir
3396 26b4bea8 2011-08-02 rsc extra_args = extra_args or ["."]
3397 26b4bea8 2011-08-02 rsc cmd = ["hg", "diff", "--git", "-r", self.base_rev] + extra_args
3398 26b4bea8 2011-08-02 rsc data = RunShell(cmd, silent_ok=True)
3399 26b4bea8 2011-08-02 rsc svndiff = []
3400 26b4bea8 2011-08-02 rsc filecount = 0
3401 26b4bea8 2011-08-02 rsc for line in data.splitlines():
3402 26b4bea8 2011-08-02 rsc m = re.match("diff --git a/(\S+) b/(\S+)", line)
3403 26b4bea8 2011-08-02 rsc if m:
3404 26b4bea8 2011-08-02 rsc # Modify line to make it look like as it comes from svn diff.
3405 26b4bea8 2011-08-02 rsc # With this modification no changes on the server side are required
3406 26b4bea8 2011-08-02 rsc # to make upload.py work with Mercurial repos.
3407 26b4bea8 2011-08-02 rsc # NOTE: for proper handling of moved/copied files, we have to use
3408 26b4bea8 2011-08-02 rsc # the second filename.
3409 26b4bea8 2011-08-02 rsc filename = m.group(2)
3410 26b4bea8 2011-08-02 rsc svndiff.append("Index: %s" % filename)
3411 26b4bea8 2011-08-02 rsc svndiff.append("=" * 67)
3412 26b4bea8 2011-08-02 rsc filecount += 1
3413 26b4bea8 2011-08-02 rsc logging.info(line)
3414 26b4bea8 2011-08-02 rsc else:
3415 26b4bea8 2011-08-02 rsc svndiff.append(line)
3416 26b4bea8 2011-08-02 rsc if not filecount:
3417 26b4bea8 2011-08-02 rsc ErrorExit("No valid patches found in output from hg diff")
3418 26b4bea8 2011-08-02 rsc return "\n".join(svndiff) + "\n"
3419 ef27bfa4 2010-01-12 rsc
3420 26b4bea8 2011-08-02 rsc def GetUnknownFiles(self):
3421 26b4bea8 2011-08-02 rsc """Return a list of files unknown to the VCS."""
3422 26b4bea8 2011-08-02 rsc args = []
3423 26b4bea8 2011-08-02 rsc status = RunShell(["hg", "status", "--rev", self.base_rev, "-u", "."],
3424 26b4bea8 2011-08-02 rsc silent_ok=True)
3425 26b4bea8 2011-08-02 rsc unknown_files = []
3426 26b4bea8 2011-08-02 rsc for line in status.splitlines():
3427 26b4bea8 2011-08-02 rsc st, fn = line.split(" ", 1)
3428 26b4bea8 2011-08-02 rsc if st == "?":
3429 26b4bea8 2011-08-02 rsc unknown_files.append(fn)
3430 26b4bea8 2011-08-02 rsc return unknown_files
3431 ef27bfa4 2010-01-12 rsc
3432 338bd4bb 2011-11-08 rsc def get_hg_status(self, rev, path):
3433 338bd4bb 2011-11-08 rsc # We'd like to use 'hg status -C path', but that is buggy
3434 338bd4bb 2011-11-08 rsc # (see http://mercurial.selenic.com/bts/issue3023).
3435 338bd4bb 2011-11-08 rsc # Instead, run 'hg status -C' without a path
3436 338bd4bb 2011-11-08 rsc # and skim the output for the path we want.
3437 338bd4bb 2011-11-08 rsc if self.status is None:
3438 338bd4bb 2011-11-08 rsc if use_hg_shell:
3439 338bd4bb 2011-11-08 rsc out = RunShell(["hg", "status", "-C", "--rev", rev])
3440 338bd4bb 2011-11-08 rsc else:
3441 338bd4bb 2011-11-08 rsc fui = FakeMercurialUI()
3442 5c934375 2012-01-20 rsc ret = hg_commands.status(fui, self.repo, *[], **{'rev': [rev], 'copies': True})
3443 338bd4bb 2011-11-08 rsc if ret:
3444 5c934375 2012-01-20 rsc raise hg_util.Abort(ret)
3445 338bd4bb 2011-11-08 rsc out = fui.output
3446 338bd4bb 2011-11-08 rsc self.status = out.splitlines()
3447 338bd4bb 2011-11-08 rsc for i in range(len(self.status)):
3448 338bd4bb 2011-11-08 rsc # line is
3449 338bd4bb 2011-11-08 rsc # A path
3450 338bd4bb 2011-11-08 rsc # M path
3451 338bd4bb 2011-11-08 rsc # etc
3452 5c934375 2012-01-20 rsc line = to_slash(self.status[i])
3453 338bd4bb 2011-11-08 rsc if line[2:] == path:
3454 338bd4bb 2011-11-08 rsc if i+1 < len(self.status) and self.status[i+1][:2] == ' ':
3455 338bd4bb 2011-11-08 rsc return self.status[i:i+2]
3456 338bd4bb 2011-11-08 rsc return self.status[i:i+1]
3457 5c934375 2012-01-20 rsc raise hg_util.Abort("no status for " + path)
3458 338bd4bb 2011-11-08 rsc
3459 26b4bea8 2011-08-02 rsc def GetBaseFile(self, filename):
3460 26b4bea8 2011-08-02 rsc set_status("inspecting " + filename)
3461 26b4bea8 2011-08-02 rsc # "hg status" and "hg cat" both take a path relative to the current subdir
3462 26b4bea8 2011-08-02 rsc # rather than to the repo root, but "hg diff" has given us the full path
3463 26b4bea8 2011-08-02 rsc # to the repo root.
3464 26b4bea8 2011-08-02 rsc base_content = ""
3465 26b4bea8 2011-08-02 rsc new_content = None
3466 26b4bea8 2011-08-02 rsc is_binary = False
3467 26b4bea8 2011-08-02 rsc oldrelpath = relpath = self._GetRelPath(filename)
3468 338bd4bb 2011-11-08 rsc out = self.get_hg_status(self.base_rev, relpath)
3469 26b4bea8 2011-08-02 rsc status, what = out[0].split(' ', 1)
3470 26b4bea8 2011-08-02 rsc if len(out) > 1 and status == "A" and what == relpath:
3471 26b4bea8 2011-08-02 rsc oldrelpath = out[1].strip()
3472 26b4bea8 2011-08-02 rsc status = "M"
3473 26b4bea8 2011-08-02 rsc if ":" in self.base_rev:
3474 26b4bea8 2011-08-02 rsc base_rev = self.base_rev.split(":", 1)[0]
3475 26b4bea8 2011-08-02 rsc else:
3476 26b4bea8 2011-08-02 rsc base_rev = self.base_rev
3477 26b4bea8 2011-08-02 rsc if status != "A":
3478 26b4bea8 2011-08-02 rsc if use_hg_shell:
3479 26b4bea8 2011-08-02 rsc base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath], silent_ok=True)
3480 26b4bea8 2011-08-02 rsc else:
3481 26b4bea8 2011-08-02 rsc base_content = str(self.repo[base_rev][oldrelpath].data())
3482 26b4bea8 2011-08-02 rsc is_binary = "\0" in base_content # Mercurial's heuristic
3483 26b4bea8 2011-08-02 rsc if status != "R":
3484 26b4bea8 2011-08-02 rsc new_content = open(relpath, "rb").read()
3485 26b4bea8 2011-08-02 rsc is_binary = is_binary or "\0" in new_content
3486 26b4bea8 2011-08-02 rsc if is_binary and base_content and use_hg_shell:
3487 26b4bea8 2011-08-02 rsc # Fetch again without converting newlines
3488 26b4bea8 2011-08-02 rsc base_content = RunShell(["hg", "cat", "-r", base_rev, oldrelpath],
3489 26b4bea8 2011-08-02 rsc silent_ok=True, universal_newlines=False)
3490 26b4bea8 2011-08-02 rsc if not is_binary or not self.IsImage(relpath):
3491 26b4bea8 2011-08-02 rsc new_content = None
3492 26b4bea8 2011-08-02 rsc return base_content, new_content, is_binary, status
3493 ef27bfa4 2010-01-12 rsc
3494 ef27bfa4 2010-01-12 rsc
3495 ef27bfa4 2010-01-12 rsc # NOTE: The SplitPatch function is duplicated in engine.py, keep them in sync.
3496 ef27bfa4 2010-01-12 rsc def SplitPatch(data):
3497 26b4bea8 2011-08-02 rsc """Splits a patch into separate pieces for each file.
3498 ef27bfa4 2010-01-12 rsc
3499 26b4bea8 2011-08-02 rsc Args:
3500 26b4bea8 2011-08-02 rsc data: A string containing the output of svn diff.
3501 26b4bea8 2011-08-02 rsc
3502 26b4bea8 2011-08-02 rsc Returns:
3503 26b4bea8 2011-08-02 rsc A list of 2-tuple (filename, text) where text is the svn diff output
3504 26b4bea8 2011-08-02 rsc pertaining to filename.
3505 26b4bea8 2011-08-02 rsc """
3506 26b4bea8 2011-08-02 rsc patches = []
3507 26b4bea8 2011-08-02 rsc filename = None
3508 26b4bea8 2011-08-02 rsc diff = []
3509 26b4bea8 2011-08-02 rsc for line in data.splitlines(True):
3510 26b4bea8 2011-08-02 rsc new_filename = None
3511 26b4bea8 2011-08-02 rsc if line.startswith('Index:'):
3512 26b4bea8 2011-08-02 rsc unused, new_filename = line.split(':', 1)
3513 26b4bea8 2011-08-02 rsc new_filename = new_filename.strip()
3514 26b4bea8 2011-08-02 rsc elif line.startswith('Property changes on:'):
3515 26b4bea8 2011-08-02 rsc unused, temp_filename = line.split(':', 1)
3516 26b4bea8 2011-08-02 rsc # When a file is modified, paths use '/' between directories, however
3517 26b4bea8 2011-08-02 rsc # when a property is modified '\' is used on Windows. Make them the same
3518 26b4bea8 2011-08-02 rsc # otherwise the file shows up twice.
3519 5c934375 2012-01-20 rsc temp_filename = to_slash(temp_filename.strip())
3520 26b4bea8 2011-08-02 rsc if temp_filename != filename:
3521 26b4bea8 2011-08-02 rsc # File has property changes but no modifications, create a new diff.
3522 26b4bea8 2011-08-02 rsc new_filename = temp_filename
3523 26b4bea8 2011-08-02 rsc if new_filename:
3524 26b4bea8 2011-08-02 rsc if filename and diff:
3525 26b4bea8 2011-08-02 rsc patches.append((filename, ''.join(diff)))
3526 26b4bea8 2011-08-02 rsc filename = new_filename
3527 26b4bea8 2011-08-02 rsc diff = [line]
3528 26b4bea8 2011-08-02 rsc continue
3529 26b4bea8 2011-08-02 rsc if diff is not None:
3530 26b4bea8 2011-08-02 rsc diff.append(line)
3531 26b4bea8 2011-08-02 rsc if filename and diff:
3532 26b4bea8 2011-08-02 rsc patches.append((filename, ''.join(diff)))
3533 26b4bea8 2011-08-02 rsc return patches
3534 ef27bfa4 2010-01-12 rsc
3535 ef27bfa4 2010-01-12 rsc
3536 ef27bfa4 2010-01-12 rsc def UploadSeparatePatches(issue, rpc_server, patchset, data, options):
3537 26b4bea8 2011-08-02 rsc """Uploads a separate patch for each file in the diff output.
3538 ef27bfa4 2010-01-12 rsc
3539 26b4bea8 2011-08-02 rsc Returns a list of [patch_key, filename] for each file.
3540 26b4bea8 2011-08-02 rsc """
3541 26b4bea8 2011-08-02 rsc patches = SplitPatch(data)
3542 26b4bea8 2011-08-02 rsc rv = []
3543 26b4bea8 2011-08-02 rsc for patch in patches:
3544 26b4bea8 2011-08-02 rsc set_status("uploading patch for " + patch[0])
3545 26b4bea8 2011-08-02 rsc if len(patch[1]) > MAX_UPLOAD_SIZE:
3546 26b4bea8 2011-08-02 rsc print ("Not uploading the patch for " + patch[0] +
3547 26b4bea8 2011-08-02 rsc " because the file is too large.")
3548 26b4bea8 2011-08-02 rsc continue
3549 26b4bea8 2011-08-02 rsc form_fields = [("filename", patch[0])]
3550 26b4bea8 2011-08-02 rsc if not options.download_base:
3551 26b4bea8 2011-08-02 rsc form_fields.append(("content_upload", "1"))
3552 26b4bea8 2011-08-02 rsc files = [("data", "data.diff", patch[1])]
3553 26b4bea8 2011-08-02 rsc ctype, body = EncodeMultipartFormData(form_fields, files)
3554 26b4bea8 2011-08-02 rsc url = "/%d/upload_patch/%d" % (int(issue), int(patchset))
3555 26b4bea8 2011-08-02 rsc print "Uploading patch for " + patch[0]
3556 26b4bea8 2011-08-02 rsc response_body = rpc_server.Send(url, body, content_type=ctype)
3557 26b4bea8 2011-08-02 rsc lines = response_body.splitlines()
3558 26b4bea8 2011-08-02 rsc if not lines or lines[0] != "OK":
3559 26b4bea8 2011-08-02 rsc StatusUpdate(" --> %s" % response_body)
3560 26b4bea8 2011-08-02 rsc sys.exit(1)
3561 26b4bea8 2011-08-02 rsc rv.append([lines[1], patch[0]])
3562 26b4bea8 2011-08-02 rsc return rv