/lib/codereview/codereview.py

https://code.google.com/p/go-uuid/ · Python · 3291 lines · 2577 code · 259 blank · 455 comment · 599 complexity · cb8a7871ced764bda93ec1e72f7c9f48 MD5 · raw file

Large files are truncated click here to view the full file

  1. # coding=utf-8
  2. # (The line above is necessary so that I can use 世界 in the
  3. # *comment* below without Python getting all bent out of shape.)
  4. # Copyright 2007-2009 Google Inc.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. '''Mercurial interface to codereview.appspot.com.
  18. To configure, set the following options in
  19. your repository's .hg/hgrc file.
  20. [extensions]
  21. codereview = path/to/codereview.py
  22. [codereview]
  23. server = codereview.appspot.com
  24. The server should be running Rietveld; see http://code.google.com/p/rietveld/.
  25. In addition to the new commands, this extension introduces
  26. the file pattern syntax @nnnnnn, where nnnnnn is a change list
  27. number, to mean the files included in that change list, which
  28. must be associated with the current client.
  29. For example, if change 123456 contains the files x.go and y.go,
  30. "hg diff @123456" is equivalent to"hg diff x.go y.go".
  31. '''
  32. from mercurial import cmdutil, commands, hg, util, error, match
  33. from mercurial.node import nullrev, hex, nullid, short
  34. import os, re, time
  35. import stat
  36. import subprocess
  37. import threading
  38. from HTMLParser import HTMLParser
  39. # The standard 'json' package is new in Python 2.6.
  40. # Before that it was an external package named simplejson.
  41. try:
  42. # Standard location in 2.6 and beyond.
  43. import json
  44. except Exception, e:
  45. try:
  46. # Conventional name for earlier package.
  47. import simplejson as json
  48. except:
  49. try:
  50. # Was also bundled with django, which is commonly installed.
  51. from django.utils import simplejson as json
  52. except:
  53. # We give up.
  54. raise e
  55. try:
  56. hgversion = util.version()
  57. except:
  58. from mercurial.version import version as v
  59. hgversion = v.get_version()
  60. try:
  61. from mercurial.discovery import findcommonincoming
  62. except:
  63. def findcommonincoming(repo, remote):
  64. return repo.findcommonincoming(remote)
  65. # in Mercurial 1.9 the cmdutil.match and cmdutil.revpair moved to scmutil
  66. if hgversion >= '1.9':
  67. from mercurial import scmutil
  68. else:
  69. scmutil = cmdutil
  70. oldMessage = """
  71. The code review extension requires Mercurial 1.3 or newer.
  72. To install a new Mercurial,
  73. sudo easy_install mercurial
  74. works on most systems.
  75. """
  76. linuxMessage = """
  77. You may need to clear your current Mercurial installation by running:
  78. sudo apt-get remove mercurial mercurial-common
  79. sudo rm -rf /etc/mercurial
  80. """
  81. if hgversion < '1.3':
  82. msg = oldMessage
  83. if os.access("/etc/mercurial", 0):
  84. msg += linuxMessage
  85. raise util.Abort(msg)
  86. def promptyesno(ui, msg):
  87. # Arguments to ui.prompt changed between 1.3 and 1.3.1.
  88. # Even so, some 1.3.1 distributions seem to have the old prompt!?!?
  89. # What a terrible way to maintain software.
  90. try:
  91. return ui.promptchoice(msg, ["&yes", "&no"], 0) == 0
  92. except AttributeError:
  93. return ui.prompt(msg, ["&yes", "&no"], "y") != "n"
  94. # To experiment with Mercurial in the python interpreter:
  95. # >>> repo = hg.repository(ui.ui(), path = ".")
  96. #######################################################################
  97. # Normally I would split this into multiple files, but it simplifies
  98. # import path headaches to keep it all in one file. Sorry.
  99. import sys
  100. if __name__ == "__main__":
  101. print >>sys.stderr, "This is a Mercurial extension and should not be invoked directly."
  102. sys.exit(2)
  103. server = "codereview.appspot.com"
  104. server_url_base = None
  105. defaultcc = None
  106. contributors = {}
  107. missing_codereview = None
  108. real_rollback = None
  109. releaseBranch = None
  110. #######################################################################
  111. # RE: UNICODE STRING HANDLING
  112. #
  113. # Python distinguishes between the str (string of bytes)
  114. # and unicode (string of code points) types. Most operations
  115. # work on either one just fine, but some (like regexp matching)
  116. # require unicode, and others (like write) require str.
  117. #
  118. # As befits the language, Python hides the distinction between
  119. # unicode and str by converting between them silently, but
  120. # *only* if all the bytes/code points involved are 7-bit ASCII.
  121. # This means that if you're not careful, your program works
  122. # fine on "hello, world" and fails on "hello, 世界". And of course,
  123. # the obvious way to be careful - use static types - is unavailable.
  124. # So the only way is trial and error to find where to put explicit
  125. # conversions.
  126. #
  127. # Because more functions do implicit conversion to str (string of bytes)
  128. # than do implicit conversion to unicode (string of code points),
  129. # the convention in this module is to represent all text as str,
  130. # converting to unicode only when calling a unicode-only function
  131. # and then converting back to str as soon as possible.
  132. def typecheck(s, t):
  133. if type(s) != t:
  134. raise util.Abort("type check failed: %s has type %s != %s" % (repr(s), type(s), t))
  135. # If we have to pass unicode instead of str, ustr does that conversion clearly.
  136. def ustr(s):
  137. typecheck(s, str)
  138. return s.decode("utf-8")
  139. # Even with those, Mercurial still sometimes turns unicode into str
  140. # and then tries to use it as ascii. Change Mercurial's default.
  141. def set_mercurial_encoding_to_utf8():
  142. from mercurial import encoding
  143. encoding.encoding = 'utf-8'
  144. set_mercurial_encoding_to_utf8()
  145. # Even with those we still run into problems.
  146. # I tried to do things by the book but could not convince
  147. # Mercurial to let me check in a change with UTF-8 in the
  148. # CL description or author field, no matter how many conversions
  149. # between str and unicode I inserted and despite changing the
  150. # default encoding. I'm tired of this game, so set the default
  151. # encoding for all of Python to 'utf-8', not 'ascii'.
  152. def default_to_utf8():
  153. import sys
  154. reload(sys) # site.py deleted setdefaultencoding; get it back
  155. sys.setdefaultencoding('utf-8')
  156. default_to_utf8()
  157. #######################################################################
  158. # Change list parsing.
  159. #
  160. # Change lists are stored in .hg/codereview/cl.nnnnnn
  161. # where nnnnnn is the number assigned by the code review server.
  162. # Most data about a change list is stored on the code review server
  163. # too: the description, reviewer, and cc list are all stored there.
  164. # The only thing in the cl.nnnnnn file is the list of relevant files.
  165. # Also, the existence of the cl.nnnnnn file marks this repository
  166. # as the one where the change list lives.
  167. emptydiff = """Index: ~rietveld~placeholder~
  168. ===================================================================
  169. diff --git a/~rietveld~placeholder~ b/~rietveld~placeholder~
  170. new file mode 100644
  171. """
  172. class CL(object):
  173. def __init__(self, name):
  174. typecheck(name, str)
  175. self.name = name
  176. self.desc = ''
  177. self.files = []
  178. self.reviewer = []
  179. self.cc = []
  180. self.url = ''
  181. self.local = False
  182. self.web = False
  183. self.copied_from = None # None means current user
  184. self.mailed = False
  185. self.private = False
  186. def DiskText(self):
  187. cl = self
  188. s = ""
  189. if cl.copied_from:
  190. s += "Author: " + cl.copied_from + "\n\n"
  191. if cl.private:
  192. s += "Private: " + str(self.private) + "\n"
  193. s += "Mailed: " + str(self.mailed) + "\n"
  194. s += "Description:\n"
  195. s += Indent(cl.desc, "\t")
  196. s += "Files:\n"
  197. for f in cl.files:
  198. s += "\t" + f + "\n"
  199. typecheck(s, str)
  200. return s
  201. def EditorText(self):
  202. cl = self
  203. s = _change_prolog
  204. s += "\n"
  205. if cl.copied_from:
  206. s += "Author: " + cl.copied_from + "\n"
  207. if cl.url != '':
  208. s += 'URL: ' + cl.url + ' # cannot edit\n\n'
  209. if cl.private:
  210. s += "Private: True\n"
  211. s += "Reviewer: " + JoinComma(cl.reviewer) + "\n"
  212. s += "CC: " + JoinComma(cl.cc) + "\n"
  213. s += "\n"
  214. s += "Description:\n"
  215. if cl.desc == '':
  216. s += "\t<enter description here>\n"
  217. else:
  218. s += Indent(cl.desc, "\t")
  219. s += "\n"
  220. if cl.local or cl.name == "new":
  221. s += "Files:\n"
  222. for f in cl.files:
  223. s += "\t" + f + "\n"
  224. s += "\n"
  225. typecheck(s, str)
  226. return s
  227. def PendingText(self):
  228. cl = self
  229. s = cl.name + ":" + "\n"
  230. s += Indent(cl.desc, "\t")
  231. s += "\n"
  232. if cl.copied_from:
  233. s += "\tAuthor: " + cl.copied_from + "\n"
  234. s += "\tReviewer: " + JoinComma(cl.reviewer) + "\n"
  235. s += "\tCC: " + JoinComma(cl.cc) + "\n"
  236. s += "\tFiles:\n"
  237. for f in cl.files:
  238. s += "\t\t" + f + "\n"
  239. typecheck(s, str)
  240. return s
  241. def Flush(self, ui, repo):
  242. if self.name == "new":
  243. self.Upload(ui, repo, gofmt_just_warn=True, creating=True)
  244. dir = CodeReviewDir(ui, repo)
  245. path = dir + '/cl.' + self.name
  246. f = open(path+'!', "w")
  247. f.write(self.DiskText())
  248. f.close()
  249. if sys.platform == "win32" and os.path.isfile(path):
  250. os.remove(path)
  251. os.rename(path+'!', path)
  252. if self.web and not self.copied_from:
  253. EditDesc(self.name, desc=self.desc,
  254. reviewers=JoinComma(self.reviewer), cc=JoinComma(self.cc),
  255. private=self.private)
  256. def Delete(self, ui, repo):
  257. dir = CodeReviewDir(ui, repo)
  258. os.unlink(dir + "/cl." + self.name)
  259. def Subject(self):
  260. s = line1(self.desc)
  261. if len(s) > 60:
  262. s = s[0:55] + "..."
  263. if self.name != "new":
  264. s = "code review %s: %s" % (self.name, s)
  265. typecheck(s, str)
  266. return s
  267. def Upload(self, ui, repo, send_mail=False, gofmt=True, gofmt_just_warn=False, creating=False, quiet=False):
  268. if not self.files and not creating:
  269. ui.warn("no files in change list\n")
  270. if ui.configbool("codereview", "force_gofmt", True) and gofmt:
  271. CheckFormat(ui, repo, self.files, just_warn=gofmt_just_warn)
  272. set_status("uploading CL metadata + diffs")
  273. os.chdir(repo.root)
  274. form_fields = [
  275. ("content_upload", "1"),
  276. ("reviewers", JoinComma(self.reviewer)),
  277. ("cc", JoinComma(self.cc)),
  278. ("description", self.desc),
  279. ("base_hashes", ""),
  280. ]
  281. if self.name != "new":
  282. form_fields.append(("issue", self.name))
  283. vcs = None
  284. # We do not include files when creating the issue,
  285. # because we want the patch sets to record the repository
  286. # and base revision they are diffs against. We use the patch
  287. # set message for that purpose, but there is no message with
  288. # the first patch set. Instead the message gets used as the
  289. # new CL's overall subject. So omit the diffs when creating
  290. # and then we'll run an immediate upload.
  291. # This has the effect that every CL begins with an empty "Patch set 1".
  292. if self.files and not creating:
  293. vcs = MercurialVCS(upload_options, ui, repo)
  294. data = vcs.GenerateDiff(self.files)
  295. files = vcs.GetBaseFiles(data)
  296. if len(data) > MAX_UPLOAD_SIZE:
  297. uploaded_diff_file = []
  298. form_fields.append(("separate_patches", "1"))
  299. else:
  300. uploaded_diff_file = [("data", "data.diff", data)]
  301. else:
  302. uploaded_diff_file = [("data", "data.diff", emptydiff)]
  303. if vcs and self.name != "new":
  304. form_fields.append(("subject", "diff -r " + vcs.base_rev + " " + getremote(ui, repo, {}).path))
  305. else:
  306. # First upload sets the subject for the CL itself.
  307. form_fields.append(("subject", self.Subject()))
  308. ctype, body = EncodeMultipartFormData(form_fields, uploaded_diff_file)
  309. response_body = MySend("/upload", body, content_type=ctype)
  310. patchset = None
  311. msg = response_body
  312. lines = msg.splitlines()
  313. if len(lines) >= 2:
  314. msg = lines[0]
  315. patchset = lines[1].strip()
  316. patches = [x.split(" ", 1) for x in lines[2:]]
  317. if response_body.startswith("Issue updated.") and quiet:
  318. pass
  319. else:
  320. ui.status(msg + "\n")
  321. set_status("uploaded CL metadata + diffs")
  322. if not response_body.startswith("Issue created.") and not response_body.startswith("Issue updated."):
  323. raise util.Abort("failed to update issue: " + response_body)
  324. issue = msg[msg.rfind("/")+1:]
  325. self.name = issue
  326. if not self.url:
  327. self.url = server_url_base + self.name
  328. if not uploaded_diff_file:
  329. set_status("uploading patches")
  330. patches = UploadSeparatePatches(issue, rpc, patchset, data, upload_options)
  331. if vcs:
  332. set_status("uploading base files")
  333. vcs.UploadBaseFiles(issue, rpc, patches, patchset, upload_options, files)
  334. if send_mail:
  335. set_status("sending mail")
  336. MySend("/" + issue + "/mail", payload="")
  337. self.web = True
  338. set_status("flushing changes to disk")
  339. self.Flush(ui, repo)
  340. return
  341. def Mail(self, ui, repo):
  342. pmsg = "Hello " + JoinComma(self.reviewer)
  343. if self.cc:
  344. pmsg += " (cc: %s)" % (', '.join(self.cc),)
  345. pmsg += ",\n"
  346. pmsg += "\n"
  347. repourl = getremote(ui, repo, {}).path
  348. if not self.mailed:
  349. pmsg += "I'd like you to review this change to\n" + repourl + "\n"
  350. else:
  351. pmsg += "Please take another look.\n"
  352. typecheck(pmsg, str)
  353. PostMessage(ui, self.name, pmsg, subject=self.Subject())
  354. self.mailed = True
  355. self.Flush(ui, repo)
  356. def GoodCLName(name):
  357. typecheck(name, str)
  358. return re.match("^[0-9]+$", name)
  359. def ParseCL(text, name):
  360. typecheck(text, str)
  361. typecheck(name, str)
  362. sname = None
  363. lineno = 0
  364. sections = {
  365. 'Author': '',
  366. 'Description': '',
  367. 'Files': '',
  368. 'URL': '',
  369. 'Reviewer': '',
  370. 'CC': '',
  371. 'Mailed': '',
  372. 'Private': '',
  373. }
  374. for line in text.split('\n'):
  375. lineno += 1
  376. line = line.rstrip()
  377. if line != '' and line[0] == '#':
  378. continue
  379. if line == '' or line[0] == ' ' or line[0] == '\t':
  380. if sname == None and line != '':
  381. return None, lineno, 'text outside section'
  382. if sname != None:
  383. sections[sname] += line + '\n'
  384. continue
  385. p = line.find(':')
  386. if p >= 0:
  387. s, val = line[:p].strip(), line[p+1:].strip()
  388. if s in sections:
  389. sname = s
  390. if val != '':
  391. sections[sname] += val + '\n'
  392. continue
  393. return None, lineno, 'malformed section header'
  394. for k in sections:
  395. sections[k] = StripCommon(sections[k]).rstrip()
  396. cl = CL(name)
  397. if sections['Author']:
  398. cl.copied_from = sections['Author']
  399. cl.desc = sections['Description']
  400. for line in sections['Files'].split('\n'):
  401. i = line.find('#')
  402. if i >= 0:
  403. line = line[0:i].rstrip()
  404. line = line.strip()
  405. if line == '':
  406. continue
  407. cl.files.append(line)
  408. cl.reviewer = SplitCommaSpace(sections['Reviewer'])
  409. cl.cc = SplitCommaSpace(sections['CC'])
  410. cl.url = sections['URL']
  411. if sections['Mailed'] != 'False':
  412. # Odd default, but avoids spurious mailings when
  413. # reading old CLs that do not have a Mailed: line.
  414. # CLs created with this update will always have
  415. # Mailed: False on disk.
  416. cl.mailed = True
  417. if sections['Private'] in ('True', 'true', 'Yes', 'yes'):
  418. cl.private = True
  419. if cl.desc == '<enter description here>':
  420. cl.desc = ''
  421. return cl, 0, ''
  422. def SplitCommaSpace(s):
  423. typecheck(s, str)
  424. s = s.strip()
  425. if s == "":
  426. return []
  427. return re.split(", *", s)
  428. def CutDomain(s):
  429. typecheck(s, str)
  430. i = s.find('@')
  431. if i >= 0:
  432. s = s[0:i]
  433. return s
  434. def JoinComma(l):
  435. for s in l:
  436. typecheck(s, str)
  437. return ", ".join(l)
  438. def ExceptionDetail():
  439. s = str(sys.exc_info()[0])
  440. if s.startswith("<type '") and s.endswith("'>"):
  441. s = s[7:-2]
  442. elif s.startswith("<class '") and s.endswith("'>"):
  443. s = s[8:-2]
  444. arg = str(sys.exc_info()[1])
  445. if len(arg) > 0:
  446. s += ": " + arg
  447. return s
  448. def IsLocalCL(ui, repo, name):
  449. return GoodCLName(name) and os.access(CodeReviewDir(ui, repo) + "/cl." + name, 0)
  450. # Load CL from disk and/or the web.
  451. def LoadCL(ui, repo, name, web=True):
  452. typecheck(name, str)
  453. set_status("loading CL " + name)
  454. if not GoodCLName(name):
  455. return None, "invalid CL name"
  456. dir = CodeReviewDir(ui, repo)
  457. path = dir + "cl." + name
  458. if os.access(path, 0):
  459. ff = open(path)
  460. text = ff.read()
  461. ff.close()
  462. cl, lineno, err = ParseCL(text, name)
  463. if err != "":
  464. return None, "malformed CL data: "+err
  465. cl.local = True
  466. else:
  467. cl = CL(name)
  468. if web:
  469. set_status("getting issue metadata from web")
  470. d = JSONGet(ui, "/api/" + name + "?messages=true")
  471. set_status(None)
  472. if d is None:
  473. return None, "cannot load CL %s from server" % (name,)
  474. if 'owner_email' not in d or 'issue' not in d or str(d['issue']) != name:
  475. return None, "malformed response loading CL data from code review server"
  476. cl.dict = d
  477. cl.reviewer = d.get('reviewers', [])
  478. cl.cc = d.get('cc', [])
  479. if cl.local and cl.copied_from and cl.desc:
  480. # local copy of CL written by someone else
  481. # and we saved a description. use that one,
  482. # so that committers can edit the description
  483. # before doing hg submit.
  484. pass
  485. else:
  486. cl.desc = d.get('description', "")
  487. cl.url = server_url_base + name
  488. cl.web = True
  489. cl.private = d.get('private', False) != False
  490. set_status("loaded CL " + name)
  491. return cl, ''
  492. global_status = None
  493. def set_status(s):
  494. # print >>sys.stderr, "\t", time.asctime(), s
  495. global global_status
  496. global_status = s
  497. class StatusThread(threading.Thread):
  498. def __init__(self):
  499. threading.Thread.__init__(self)
  500. def run(self):
  501. # pause a reasonable amount of time before
  502. # starting to display status messages, so that
  503. # most hg commands won't ever see them.
  504. time.sleep(30)
  505. # now show status every 15 seconds
  506. while True:
  507. time.sleep(15 - time.time() % 15)
  508. s = global_status
  509. if s is None:
  510. continue
  511. if s == "":
  512. s = "(unknown status)"
  513. print >>sys.stderr, time.asctime(), s
  514. def start_status_thread():
  515. t = StatusThread()
  516. t.setDaemon(True) # allowed to exit if t is still running
  517. t.start()
  518. class LoadCLThread(threading.Thread):
  519. def __init__(self, ui, repo, dir, f, web):
  520. threading.Thread.__init__(self)
  521. self.ui = ui
  522. self.repo = repo
  523. self.dir = dir
  524. self.f = f
  525. self.web = web
  526. self.cl = None
  527. def run(self):
  528. cl, err = LoadCL(self.ui, self.repo, self.f[3:], web=self.web)
  529. if err != '':
  530. self.ui.warn("loading "+self.dir+self.f+": " + err + "\n")
  531. return
  532. self.cl = cl
  533. # Load all the CLs from this repository.
  534. def LoadAllCL(ui, repo, web=True):
  535. dir = CodeReviewDir(ui, repo)
  536. m = {}
  537. files = [f for f in os.listdir(dir) if f.startswith('cl.')]
  538. if not files:
  539. return m
  540. active = []
  541. first = True
  542. for f in files:
  543. t = LoadCLThread(ui, repo, dir, f, web)
  544. t.start()
  545. if web and first:
  546. # first request: wait in case it needs to authenticate
  547. # otherwise we get lots of user/password prompts
  548. # running in parallel.
  549. t.join()
  550. if t.cl:
  551. m[t.cl.name] = t.cl
  552. first = False
  553. else:
  554. active.append(t)
  555. for t in active:
  556. t.join()
  557. if t.cl:
  558. m[t.cl.name] = t.cl
  559. return m
  560. # Find repository root. On error, ui.warn and return None
  561. def RepoDir(ui, repo):
  562. url = repo.url();
  563. if not url.startswith('file:'):
  564. ui.warn("repository %s is not in local file system\n" % (url,))
  565. return None
  566. url = url[5:]
  567. if url.endswith('/'):
  568. url = url[:-1]
  569. typecheck(url, str)
  570. return url
  571. # Find (or make) code review directory. On error, ui.warn and return None
  572. def CodeReviewDir(ui, repo):
  573. dir = RepoDir(ui, repo)
  574. if dir == None:
  575. return None
  576. dir += '/.hg/codereview/'
  577. if not os.path.isdir(dir):
  578. try:
  579. os.mkdir(dir, 0700)
  580. except:
  581. ui.warn('cannot mkdir %s: %s\n' % (dir, ExceptionDetail()))
  582. return None
  583. typecheck(dir, str)
  584. return dir
  585. # Turn leading tabs into spaces, so that the common white space
  586. # prefix doesn't get confused when people's editors write out
  587. # some lines with spaces, some with tabs. Only a heuristic
  588. # (some editors don't use 8 spaces either) but a useful one.
  589. def TabsToSpaces(line):
  590. i = 0
  591. while i < len(line) and line[i] == '\t':
  592. i += 1
  593. return ' '*(8*i) + line[i:]
  594. # Strip maximal common leading white space prefix from text
  595. def StripCommon(text):
  596. typecheck(text, str)
  597. ws = None
  598. for line in text.split('\n'):
  599. line = line.rstrip()
  600. if line == '':
  601. continue
  602. line = TabsToSpaces(line)
  603. white = line[:len(line)-len(line.lstrip())]
  604. if ws == None:
  605. ws = white
  606. else:
  607. common = ''
  608. for i in range(min(len(white), len(ws))+1):
  609. if white[0:i] == ws[0:i]:
  610. common = white[0:i]
  611. ws = common
  612. if ws == '':
  613. break
  614. if ws == None:
  615. return text
  616. t = ''
  617. for line in text.split('\n'):
  618. line = line.rstrip()
  619. line = TabsToSpaces(line)
  620. if line.startswith(ws):
  621. line = line[len(ws):]
  622. if line == '' and t == '':
  623. continue
  624. t += line + '\n'
  625. while len(t) >= 2 and t[-2:] == '\n\n':
  626. t = t[:-1]
  627. typecheck(t, str)
  628. return t
  629. # Indent text with indent.
  630. def Indent(text, indent):
  631. typecheck(text, str)
  632. typecheck(indent, str)
  633. t = ''
  634. for line in text.split('\n'):
  635. t += indent + line + '\n'
  636. typecheck(t, str)
  637. return t
  638. # Return the first line of l
  639. def line1(text):
  640. typecheck(text, str)
  641. return text.split('\n')[0]
  642. _change_prolog = """# Change list.
  643. # Lines beginning with # are ignored.
  644. # Multi-line values should be indented.
  645. """
  646. #######################################################################
  647. # Mercurial helper functions
  648. # Get effective change nodes taking into account applied MQ patches
  649. def effective_revpair(repo):
  650. try:
  651. return scmutil.revpair(repo, ['qparent'])
  652. except:
  653. return scmutil.revpair(repo, None)
  654. # Return list of changed files in repository that match pats.
  655. # Warn about patterns that did not match.
  656. def matchpats(ui, repo, pats, opts):
  657. matcher = scmutil.match(repo, pats, opts)
  658. node1, node2 = effective_revpair(repo)
  659. modified, added, removed, deleted, unknown, ignored, clean = repo.status(node1, node2, matcher, ignored=True, clean=True, unknown=True)
  660. return (modified, added, removed, deleted, unknown, ignored, clean)
  661. # Return list of changed files in repository that match pats.
  662. # The patterns came from the command line, so we warn
  663. # if they have no effect or cannot be understood.
  664. def ChangedFiles(ui, repo, pats, opts, taken=None):
  665. taken = taken or {}
  666. # Run each pattern separately so that we can warn about
  667. # patterns that didn't do anything useful.
  668. for p in pats:
  669. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, [p], opts)
  670. redo = False
  671. for f in unknown:
  672. promptadd(ui, repo, f)
  673. redo = True
  674. for f in deleted:
  675. promptremove(ui, repo, f)
  676. redo = True
  677. if redo:
  678. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, [p], opts)
  679. for f in modified + added + removed:
  680. if f in taken:
  681. ui.warn("warning: %s already in CL %s\n" % (f, taken[f].name))
  682. if not modified and not added and not removed:
  683. ui.warn("warning: %s did not match any modified files\n" % (p,))
  684. # Again, all at once (eliminates duplicates)
  685. modified, added, removed = matchpats(ui, repo, pats, opts)[:3]
  686. l = modified + added + removed
  687. l.sort()
  688. if taken:
  689. l = Sub(l, taken.keys())
  690. return l
  691. # Return list of changed files in repository that match pats and still exist.
  692. def ChangedExistingFiles(ui, repo, pats, opts):
  693. modified, added = matchpats(ui, repo, pats, opts)[:2]
  694. l = modified + added
  695. l.sort()
  696. return l
  697. # Return list of files claimed by existing CLs
  698. def Taken(ui, repo):
  699. all = LoadAllCL(ui, repo, web=False)
  700. taken = {}
  701. for _, cl in all.items():
  702. for f in cl.files:
  703. taken[f] = cl
  704. return taken
  705. # Return list of changed files that are not claimed by other CLs
  706. def DefaultFiles(ui, repo, pats, opts):
  707. return ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  708. def Sub(l1, l2):
  709. return [l for l in l1 if l not in l2]
  710. def Add(l1, l2):
  711. l = l1 + Sub(l2, l1)
  712. l.sort()
  713. return l
  714. def Intersect(l1, l2):
  715. return [l for l in l1 if l in l2]
  716. def getremote(ui, repo, opts):
  717. # save $http_proxy; creating the HTTP repo object will
  718. # delete it in an attempt to "help"
  719. proxy = os.environ.get('http_proxy')
  720. source = hg.parseurl(ui.expandpath("default"), None)[0]
  721. try:
  722. remoteui = hg.remoteui # hg 1.6
  723. except:
  724. remoteui = cmdutil.remoteui
  725. other = hg.repository(remoteui(repo, opts), source)
  726. if proxy is not None:
  727. os.environ['http_proxy'] = proxy
  728. return other
  729. def Incoming(ui, repo, opts):
  730. _, incoming, _ = findcommonincoming(repo, getremote(ui, repo, opts))
  731. return incoming
  732. desc_re = '^(.+: |(tag )?(release|weekly)\.|fix build|undo CL)'
  733. desc_msg = '''Your CL description appears not to use the standard form.
  734. The first line of your change description is conventionally a
  735. one-line summary of the change, prefixed by the primary affected package,
  736. and is used as the subject for code review mail; the rest of the description
  737. elaborates.
  738. Examples:
  739. encoding/rot13: new package
  740. math: add IsInf, IsNaN
  741. net: fix cname in LookupHost
  742. unicode: update to Unicode 5.0.2
  743. '''
  744. def promptremove(ui, repo, f):
  745. if promptyesno(ui, "hg remove %s (y/n)?" % (f,)):
  746. if commands.remove(ui, repo, 'path:'+f) != 0:
  747. ui.warn("error removing %s" % (f,))
  748. def promptadd(ui, repo, f):
  749. if promptyesno(ui, "hg add %s (y/n)?" % (f,)):
  750. if commands.add(ui, repo, 'path:'+f) != 0:
  751. ui.warn("error adding %s" % (f,))
  752. def EditCL(ui, repo, cl):
  753. set_status(None) # do not show status
  754. s = cl.EditorText()
  755. while True:
  756. s = ui.edit(s, ui.username())
  757. clx, line, err = ParseCL(s, cl.name)
  758. if err != '':
  759. if not promptyesno(ui, "error parsing change list: line %d: %s\nre-edit (y/n)?" % (line, err)):
  760. return "change list not modified"
  761. continue
  762. # Check description.
  763. if clx.desc == '':
  764. if promptyesno(ui, "change list should have a description\nre-edit (y/n)?"):
  765. continue
  766. elif re.search('<enter reason for undo>', clx.desc):
  767. if promptyesno(ui, "change list description omits reason for undo\nre-edit (y/n)?"):
  768. continue
  769. elif not re.match(desc_re, clx.desc.split('\n')[0]):
  770. if promptyesno(ui, desc_msg + "re-edit (y/n)?"):
  771. continue
  772. # Check file list for files that need to be hg added or hg removed
  773. # or simply aren't understood.
  774. pats = ['path:'+f for f in clx.files]
  775. modified, added, removed, deleted, unknown, ignored, clean = matchpats(ui, repo, pats, {})
  776. files = []
  777. for f in clx.files:
  778. if f in modified or f in added or f in removed:
  779. files.append(f)
  780. continue
  781. if f in deleted:
  782. promptremove(ui, repo, f)
  783. files.append(f)
  784. continue
  785. if f in unknown:
  786. promptadd(ui, repo, f)
  787. files.append(f)
  788. continue
  789. if f in ignored:
  790. ui.warn("error: %s is excluded by .hgignore; omitting\n" % (f,))
  791. continue
  792. if f in clean:
  793. ui.warn("warning: %s is listed in the CL but unchanged\n" % (f,))
  794. files.append(f)
  795. continue
  796. p = repo.root + '/' + f
  797. if os.path.isfile(p):
  798. ui.warn("warning: %s is a file but not known to hg\n" % (f,))
  799. files.append(f)
  800. continue
  801. if os.path.isdir(p):
  802. ui.warn("error: %s is a directory, not a file; omitting\n" % (f,))
  803. continue
  804. ui.warn("error: %s does not exist; omitting\n" % (f,))
  805. clx.files = files
  806. cl.desc = clx.desc
  807. cl.reviewer = clx.reviewer
  808. cl.cc = clx.cc
  809. cl.files = clx.files
  810. cl.private = clx.private
  811. break
  812. return ""
  813. # For use by submit, etc. (NOT by change)
  814. # Get change list number or list of files from command line.
  815. # If files are given, make a new change list.
  816. def CommandLineCL(ui, repo, pats, opts, defaultcc=None):
  817. if len(pats) > 0 and GoodCLName(pats[0]):
  818. if len(pats) != 1:
  819. return None, "cannot specify change number and file names"
  820. if opts.get('message'):
  821. return None, "cannot use -m with existing CL"
  822. cl, err = LoadCL(ui, repo, pats[0], web=True)
  823. if err != "":
  824. return None, err
  825. else:
  826. cl = CL("new")
  827. cl.local = True
  828. cl.files = ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  829. if not cl.files:
  830. return None, "no files changed"
  831. if opts.get('reviewer'):
  832. cl.reviewer = Add(cl.reviewer, SplitCommaSpace(opts.get('reviewer')))
  833. if opts.get('cc'):
  834. cl.cc = Add(cl.cc, SplitCommaSpace(opts.get('cc')))
  835. if defaultcc:
  836. cl.cc = Add(cl.cc, defaultcc)
  837. if cl.name == "new":
  838. if opts.get('message'):
  839. cl.desc = opts.get('message')
  840. else:
  841. err = EditCL(ui, repo, cl)
  842. if err != '':
  843. return None, err
  844. return cl, ""
  845. # reposetup replaces cmdutil.match with this wrapper,
  846. # which expands the syntax @clnumber to mean the files
  847. # in that CL.
  848. original_match = None
  849. def ReplacementForCmdutilMatch(repo, pats=None, opts=None, globbed=False, default='relpath'):
  850. taken = []
  851. files = []
  852. pats = pats or []
  853. opts = opts or {}
  854. for p in pats:
  855. if p.startswith('@'):
  856. taken.append(p)
  857. clname = p[1:]
  858. if not GoodCLName(clname):
  859. raise util.Abort("invalid CL name " + clname)
  860. cl, err = LoadCL(repo.ui, repo, clname, web=False)
  861. if err != '':
  862. raise util.Abort("loading CL " + clname + ": " + err)
  863. if not cl.files:
  864. raise util.Abort("no files in CL " + clname)
  865. files = Add(files, cl.files)
  866. pats = Sub(pats, taken) + ['path:'+f for f in files]
  867. # work-around for http://selenic.com/hg/rev/785bbc8634f8
  868. if hgversion >= '1.9' and not hasattr(repo, 'match'):
  869. repo = repo[None]
  870. return original_match(repo, pats=pats, opts=opts, globbed=globbed, default=default)
  871. def RelativePath(path, cwd):
  872. n = len(cwd)
  873. if path.startswith(cwd) and path[n] == '/':
  874. return path[n+1:]
  875. return path
  876. def CheckFormat(ui, repo, files, just_warn=False):
  877. set_status("running gofmt")
  878. CheckGofmt(ui, repo, files, just_warn)
  879. CheckTabfmt(ui, repo, files, just_warn)
  880. # Check that gofmt run on the list of files does not change them
  881. def CheckGofmt(ui, repo, files, just_warn):
  882. files = [f for f in files if (f.startswith('src/') or f.startswith('test/bench/')) and f.endswith('.go')]
  883. if not files:
  884. return
  885. cwd = os.getcwd()
  886. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  887. files = [f for f in files if os.access(f, 0)]
  888. if not files:
  889. return
  890. try:
  891. cmd = subprocess.Popen(["gofmt", "-l"] + files, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=sys.platform != "win32")
  892. cmd.stdin.close()
  893. except:
  894. raise util.Abort("gofmt: " + ExceptionDetail())
  895. data = cmd.stdout.read()
  896. errors = cmd.stderr.read()
  897. cmd.wait()
  898. set_status("done with gofmt")
  899. if len(errors) > 0:
  900. ui.warn("gofmt errors:\n" + errors.rstrip() + "\n")
  901. return
  902. if len(data) > 0:
  903. msg = "gofmt needs to format these files (run hg gofmt):\n" + Indent(data, "\t").rstrip()
  904. if just_warn:
  905. ui.warn("warning: " + msg + "\n")
  906. else:
  907. raise util.Abort(msg)
  908. return
  909. # Check that *.[chys] files indent using tabs.
  910. def CheckTabfmt(ui, repo, files, just_warn):
  911. files = [f for f in files if f.startswith('src/') and re.search(r"\.[chys]$", f)]
  912. if not files:
  913. return
  914. cwd = os.getcwd()
  915. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  916. files = [f for f in files if os.access(f, 0)]
  917. badfiles = []
  918. for f in files:
  919. try:
  920. for line in open(f, 'r'):
  921. # Four leading spaces is enough to complain about,
  922. # except that some Plan 9 code uses four spaces as the label indent,
  923. # so allow that.
  924. if line.startswith(' ') and not re.match(' [A-Za-z0-9_]+:', line):
  925. badfiles.append(f)
  926. break
  927. except:
  928. # ignore cannot open file, etc.
  929. pass
  930. if len(badfiles) > 0:
  931. msg = "these files use spaces for indentation (use tabs instead):\n\t" + "\n\t".join(badfiles)
  932. if just_warn:
  933. ui.warn("warning: " + msg + "\n")
  934. else:
  935. raise util.Abort(msg)
  936. return
  937. #######################################################################
  938. # Mercurial commands
  939. # every command must take a ui and and repo as arguments.
  940. # opts is a dict where you can find other command line flags
  941. #
  942. # Other parameters are taken in order from items on the command line that
  943. # don't start with a dash. If no default value is given in the parameter list,
  944. # they are required.
  945. #
  946. def change(ui, repo, *pats, **opts):
  947. """create, edit or delete a change list
  948. Create, edit or delete a change list.
  949. A change list is a group of files to be reviewed and submitted together,
  950. plus a textual description of the change.
  951. Change lists are referred to by simple alphanumeric names.
  952. Changes must be reviewed before they can be submitted.
  953. In the absence of options, the change command opens the
  954. change list for editing in the default editor.
  955. Deleting a change with the -d or -D flag does not affect
  956. the contents of the files listed in that change. To revert
  957. the files listed in a change, use
  958. hg revert @123456
  959. before running hg change -d 123456.
  960. """
  961. if missing_codereview:
  962. return missing_codereview
  963. dirty = {}
  964. if len(pats) > 0 and GoodCLName(pats[0]):
  965. name = pats[0]
  966. if len(pats) != 1:
  967. return "cannot specify CL name and file patterns"
  968. pats = pats[1:]
  969. cl, err = LoadCL(ui, repo, name, web=True)
  970. if err != '':
  971. return err
  972. if not cl.local and (opts["stdin"] or not opts["stdout"]):
  973. return "cannot change non-local CL " + name
  974. else:
  975. if repo[None].branch() != "default":
  976. return "cannot run hg change outside default branch"
  977. name = "new"
  978. cl = CL("new")
  979. dirty[cl] = True
  980. files = ChangedFiles(ui, repo, pats, opts, taken=Taken(ui, repo))
  981. if opts["delete"] or opts["deletelocal"]:
  982. if opts["delete"] and opts["deletelocal"]:
  983. return "cannot use -d and -D together"
  984. flag = "-d"
  985. if opts["deletelocal"]:
  986. flag = "-D"
  987. if name == "new":
  988. return "cannot use "+flag+" with file patterns"
  989. if opts["stdin"] or opts["stdout"]:
  990. return "cannot use "+flag+" with -i or -o"
  991. if not cl.local:
  992. return "cannot change non-local CL " + name
  993. if opts["delete"]:
  994. if cl.copied_from:
  995. return "original author must delete CL; hg change -D will remove locally"
  996. PostMessage(ui, cl.name, "*** Abandoned ***", send_mail=cl.mailed)
  997. EditDesc(cl.name, closed=True, private=cl.private)
  998. cl.Delete(ui, repo)
  999. return
  1000. if opts["stdin"]:
  1001. s = sys.stdin.read()
  1002. clx, line, err = ParseCL(s, name)
  1003. if err != '':
  1004. return "error parsing change list: line %d: %s" % (line, err)
  1005. if clx.desc is not None:
  1006. cl.desc = clx.desc;
  1007. dirty[cl] = True
  1008. if clx.reviewer is not None:
  1009. cl.reviewer = clx.reviewer
  1010. dirty[cl] = True
  1011. if clx.cc is not None:
  1012. cl.cc = clx.cc
  1013. dirty[cl] = True
  1014. if clx.files is not None:
  1015. cl.files = clx.files
  1016. dirty[cl] = True
  1017. if clx.private != cl.private:
  1018. cl.private = clx.private
  1019. dirty[cl] = True
  1020. if not opts["stdin"] and not opts["stdout"]:
  1021. if name == "new":
  1022. cl.files = files
  1023. err = EditCL(ui, repo, cl)
  1024. if err != "":
  1025. return err
  1026. dirty[cl] = True
  1027. for d, _ in dirty.items():
  1028. name = d.name
  1029. d.Flush(ui, repo)
  1030. if name == "new":
  1031. d.Upload(ui, repo, quiet=True)
  1032. if opts["stdout"]:
  1033. ui.write(cl.EditorText())
  1034. elif opts["pending"]:
  1035. ui.write(cl.PendingText())
  1036. elif name == "new":
  1037. if ui.quiet:
  1038. ui.write(cl.name)
  1039. else:
  1040. ui.write("CL created: " + cl.url + "\n")
  1041. return
  1042. def code_login(ui, repo, **opts):
  1043. """log in to code review server
  1044. Logs in to the code review server, saving a cookie in
  1045. a file in your home directory.
  1046. """
  1047. if missing_codereview:
  1048. return missing_codereview
  1049. MySend(None)
  1050. def clpatch(ui, repo, clname, **opts):
  1051. """import a patch from the code review server
  1052. Imports a patch from the code review server into the local client.
  1053. If the local client has already modified any of the files that the
  1054. patch modifies, this command will refuse to apply the patch.
  1055. Submitting an imported patch will keep the original author's
  1056. name as the Author: line but add your own name to a Committer: line.
  1057. """
  1058. if repo[None].branch() != "default":
  1059. return "cannot run hg clpatch outside default branch"
  1060. return clpatch_or_undo(ui, repo, clname, opts, mode="clpatch")
  1061. def undo(ui, repo, clname, **opts):
  1062. """undo the effect of a CL
  1063. Creates a new CL that undoes an earlier CL.
  1064. After creating the CL, opens the CL text for editing so that
  1065. you can add the reason for the undo to the description.
  1066. """
  1067. if repo[None].branch() != "default":
  1068. return "cannot run hg undo outside default branch"
  1069. return clpatch_or_undo(ui, repo, clname, opts, mode="undo")
  1070. def release_apply(ui, repo, clname, **opts):
  1071. """apply a CL to the release branch
  1072. Creates a new CL copying a previously committed change
  1073. from the main branch to the release branch.
  1074. The current client must either be clean or already be in
  1075. the release branch.
  1076. The release branch must be created by starting with a
  1077. clean client, disabling the code review plugin, and running:
  1078. hg update weekly.YYYY-MM-DD
  1079. hg branch release-branch.rNN
  1080. hg commit -m 'create release-branch.rNN'
  1081. hg push --new-branch
  1082. Then re-enable the code review plugin.
  1083. People can test the release branch by running
  1084. hg update release-branch.rNN
  1085. in a clean client. To return to the normal tree,
  1086. hg update default
  1087. Move changes since the weekly into the release branch
  1088. using hg release-apply followed by the usual code review
  1089. process and hg submit.
  1090. When it comes time to tag the release, record the
  1091. final long-form tag of the release-branch.rNN
  1092. in the *default* branch's .hgtags file. That is, run
  1093. hg update default
  1094. and then edit .hgtags as you would for a weekly.
  1095. """
  1096. c = repo[None]
  1097. if not releaseBranch:
  1098. return "no active release branches"
  1099. if c.branch() != releaseBranch:
  1100. if c.modified() or c.added() or c.removed():
  1101. raise util.Abort("uncommitted local changes - cannot switch branches")
  1102. err = hg.clean(repo, releaseBranch)
  1103. if err:
  1104. return err
  1105. try:
  1106. err = clpatch_or_undo(ui, repo, clname, opts, mode="backport")
  1107. if err:
  1108. raise util.Abort(err)
  1109. except Exception, e:
  1110. hg.clean(repo, "default")
  1111. raise e
  1112. return None
  1113. def rev2clname(rev):
  1114. # Extract CL name from revision description.
  1115. # The last line in the description that is a codereview URL is the real one.
  1116. # Earlier lines might be part of the user-written description.
  1117. all = re.findall('(?m)^http://codereview.appspot.com/([0-9]+)$', rev.description())
  1118. if len(all) > 0:
  1119. return all[-1]
  1120. return ""
  1121. undoHeader = """undo CL %s / %s
  1122. <enter reason for undo>
  1123. ««« original CL description
  1124. """
  1125. undoFooter = """
  1126. »»»
  1127. """
  1128. backportHeader = """[%s] %s
  1129. ««« CL %s / %s
  1130. """
  1131. backportFooter = """
  1132. »»»
  1133. """
  1134. # Implementation of clpatch/undo.
  1135. def clpatch_or_undo(ui, repo, clname, opts, mode):
  1136. if missing_codereview:
  1137. return missing_codereview
  1138. if mode == "undo" or mode == "backport":
  1139. if hgversion < '1.4':
  1140. # Don't have cmdutil.match (see implementation of sync command).
  1141. return "hg is too old to run hg %s - update to 1.4 or newer" % mode
  1142. # Find revision in Mercurial repository.
  1143. # Assume CL number is 7+ decimal digits.
  1144. # Otherwise is either change log sequence number (fewer decimal digits),
  1145. # hexadecimal hash, or tag name.
  1146. # Mercurial will fall over long before the change log
  1147. # sequence numbers get to be 7 digits long.
  1148. if re.match('^[0-9]{7,}$', clname):
  1149. found = False
  1150. matchfn = scmutil.match(repo, [], {'rev': None})
  1151. def prep(ctx, fns):
  1152. pass
  1153. for ctx in cmdutil.walkchangerevs(repo, matchfn, {'rev': None}, prep):
  1154. rev = repo[ctx.rev()]
  1155. # Last line with a code review URL is the actual review URL.
  1156. # Earlier ones might be part of the CL description.
  1157. n = rev2clname(rev)
  1158. if n == clname:
  1159. found = True
  1160. break
  1161. if not found:
  1162. return "cannot find CL %s in local repository" % clname
  1163. else:
  1164. rev = repo[clname]
  1165. if not rev:
  1166. return "unknown revision %s" % clname
  1167. clname = rev2clname(rev)
  1168. if clname == "":
  1169. return "cannot find CL name in revision description"
  1170. # Create fresh CL and start with patch that would reverse the change.
  1171. vers = short(rev.node())
  1172. cl = CL("new")
  1173. desc = str(rev.description())
  1174. if mode == "undo":
  1175. cl.desc = (undoHeader % (clname, vers)) + desc + undoFooter
  1176. else:
  1177. cl.desc = (backportHeader % (releaseBranch, line1(desc), clname, vers)) + desc + undoFooter
  1178. v1 = vers
  1179. v0 = short(rev.parents()[0].node())
  1180. if mode == "undo":
  1181. arg = v1 + ":" + v0
  1182. else:
  1183. vers = v0
  1184. arg = v0 + ":" + v1
  1185. patch = RunShell(["hg", "diff", "--git", "-r", arg])
  1186. else: # clpatch
  1187. cl, vers, patch, err = DownloadCL(ui, repo, clname)
  1188. if err != "":
  1189. return err
  1190. if patch == emptydiff:
  1191. return "codereview issue %s has no diff" % clname
  1192. # find current hg version (hg identify)
  1193. ctx = repo[None]
  1194. parents = ctx.parents()
  1195. id = '+'.join([short(p.node()) for p in parents])
  1196. # if version does not match the patch version,
  1197. # try to update the patch line numbers.
  1198. if vers != "" and id != vers:
  1199. # "vers in repo" gives the wrong answer
  1200. # on some versions of Mercurial. Instead, do the actual
  1201. # lookup and catch the exception.
  1202. try:
  1203. repo[vers].description()
  1204. except:
  1205. return "local repository is out of date; sync to get %s" % (vers)
  1206. patch1, err = portPatch(repo, patch, vers, id)
  1207. if err != "":
  1208. if not opts["ignore_hgpatch_failure"]:
  1209. return "codereview issue %s is out of date: %s (%s->%s)" % (clname, err, vers, id)
  1210. else:
  1211. patch = patch1
  1212. argv = ["hgpatch"]
  1213. if opts["no_incoming"] or mode == "backport":
  1214. argv += ["--checksync=false"]
  1215. try:
  1216. cmd = subprocess.Popen(argv, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, close_fds=sys.platform != "win32")
  1217. except:
  1218. return "hgpatch: " + ExceptionDetail()
  1219. out, err = cmd.communicate(patch)
  1220. if cmd.returncode != 0 and not opts["ignore_hgpatch_failure"]:
  1221. return "hgpatch failed"
  1222. cl.local = True
  1223. cl.files = out.strip().split()
  1224. if not cl.files and not opts["ignore_hgpatch_failure"]:
  1225. return "codereview issue %s has no changed files" % clname
  1226. files = ChangedFiles(ui, repo, [], opts)
  1227. extra = Sub(cl.files, files)
  1228. if extra:
  1229. ui.warn("warning: these files were listed in the patch but not changed:\n\t" + "\n\t".join(extra) + "\n")
  1230. cl.Flush(ui, repo)
  1231. if mode == "undo":
  1232. err = EditCL(ui, repo, cl)
  1233. if err != "":
  1234. return "CL created, but error editing: " + err
  1235. cl.Flush(ui, repo)
  1236. else:
  1237. ui.write(cl.PendingText() + "\n")
  1238. # portPatch rewrites patch from being a patch against
  1239. # oldver to being a patch against newver.
  1240. def portPatch(repo, patch, oldver, newver):
  1241. lines = patch.splitlines(True) # True = keep \n
  1242. delta = None
  1243. for i in range(len(lines)):
  1244. line = lines[i]
  1245. if line.startswith('--- a/'):
  1246. file = line[6:-1]
  1247. delta = fileDeltas(repo, file, oldver, newver)
  1248. if not delta or not line.startswith('@@ '):
  1249. continue
  1250. # @@ -x,y +z,w @@ means the patch chunk replaces
  1251. # the original file's line numbers x up to x+y with the
  1252. # line numbers z up to z+w in the new file.
  1253. # Find the delta from x in the original to the same
  1254. # line in the current version and add that delta to both
  1255. # x and z.
  1256. m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
  1257. if not m:
  1258. return None, "error parsing patch line numbers"
  1259. n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
  1260. d, err = lineDelta(delta, n1, len1)
  1261. if err != "":
  1262. return "", err
  1263. n1 += d
  1264. n2 += d
  1265. lines[i] = "@@ -%d,%d +%d,%d @@\n" % (n1, len1, n2, len2)
  1266. newpatch = ''.join(lines)
  1267. return newpatch, ""
  1268. # fileDelta returns the line number deltas for the given file's
  1269. # changes from oldver to newver.
  1270. # The deltas are a list of (n, len, newdelta) triples that say
  1271. # lines [n, n+len) were modified, and after that range the
  1272. # line numbers are +newdelta from what they were before.
  1273. def fileDeltas(repo, file, oldver, newver):
  1274. cmd = ["hg", "diff", "--git", "-r", oldver + ":" + newver, "path:" + file]
  1275. data = RunShell(cmd, silent_ok=True)
  1276. deltas = []
  1277. for line in data.splitlines():
  1278. m = re.match('@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @@', line)
  1279. if not m:
  1280. continue
  1281. n1, len1, n2, len2 = int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
  1282. deltas.append((n1, len1, n2+len2-(n1+len1)))
  1283. return deltas
  1284. # lineDelta finds the appropriate line number delta to apply to the lines [n, n+len).
  1285. # It returns an error if those lines were rewritten by the patch.
  1286. def lineDelta(deltas, n, len):
  1287. d = 0
  1288. for (old, oldlen, newdelta) in deltas:
  1289. if old >= n+len:
  1290. break
  1291. if old+len > n:
  1292. return 0, "patch and recent changes conflict"
  1293. d = newdelta
  1294. return d, ""
  1295. def download(ui, repo, clname, **opts):
  1296. """download a change from the code review server
  1297. Download prints a description of the given change list
  1298. followed by its diff, downloaded from the code review server.
  1299. """
  1300. if missing_codereview:
  1301. return missing_codereview
  1302. cl, vers, patch, err = DownloadCL(ui, repo, clname)
  1303. if err != "":
  1304. return err
  1305. ui.write(cl.EditorText() + "\n")
  1306. ui.write(patch + "\n")
  1307. return
  1308. def file(ui, repo, clname, pat, *pats, **opts):
  1309. """assign files to or remove files from a change list
  1310. Assign files to or (with -d) remove files from a change list.
  1311. The -d option only removes files from the change list.
  1312. It does not edit them or remove them from the repository.
  1313. """
  1314. if missing_codereview:
  1315. return missing_codereview
  1316. pats = tuple([pat] + list(pats))
  1317. if not GoodCLName(clname):
  1318. return "invalid CL name " + clname
  1319. dirty = {}
  1320. cl, err = LoadCL(ui, repo, clname, web=False)
  1321. if err != '':
  1322. return err
  1323. if not cl.local:
  1324. return "cannot change non-local CL " + clname
  1325. files = ChangedFiles(ui, repo, pats, opts)
  1326. if opts["delete"]:
  1327. oldfiles = Intersect(files, cl.files)
  1328. if oldfiles:
  1329. if not ui.quiet:
  1330. ui.status("# Removing files from CL. To undo:\n")
  1331. ui.status("# cd %s\n" % (repo.root))
  1332. for f in oldfiles:
  1333. ui.status("# hg file %s %s\n" % (cl.name, f))
  1334. cl.files = Sub(cl.files, oldfiles)
  1335. cl.Flush(ui, repo)
  1336. else:
  1337. ui.status("no such files in CL")
  1338. return
  1339. if not files:
  1340. return "no such modified files"
  1341. files = Sub(files, cl.files)
  1342. taken = Taken(ui, repo)
  1343. warned = False
  1344. for f in files:
  1345. if f in taken:
  1346. if not warned and not ui.quiet:
  1347. ui.status("# Taking files from other CLs. To undo:\n")
  1348. ui.status("# cd %s\n" % (repo.root))
  1349. warned = True
  1350. ocl = taken[f]
  1351. if not ui.quiet:
  1352. ui.status("# hg file %s %s\n" % (ocl.name, f))
  1353. if ocl not in dirty:
  1354. ocl.files = Sub(ocl.files, files)
  1355. dirty[ocl] = True
  1356. cl.files = Add(cl.files, files)
  1357. dirty[cl] = True
  1358. for d, _ in dirty.items():
  1359. d.Flush(ui, repo)
  1360. return
  1361. def gofmt(ui, repo, *pats, **opts):
  1362. """apply gofmt to modified files
  1363. Applies gofmt to the modified files in the repository that match
  1364. the given patterns.
  1365. """
  1366. if missing_codereview:
  1367. return missing_codereview
  1368. files = ChangedExistingFiles(ui, repo, pats, opts)
  1369. files = [f for f in files if f.endswith(".go")]
  1370. if not files:
  1371. return "no modified go files"
  1372. cwd = os.getcwd()
  1373. files = [RelativePath(repo.root + '/' + f, cwd) for f in files]
  1374. try:
  1375. cmd = ["gofmt", "-l"]
  1376. if not opts["list"]:
  1377. cmd += ["-w"]
  1378. if os.spawnvp(os.P_WAIT, "gofmt", cmd + files) != 0:
  1379. raise util.Abort("gofmt did not exit cleanly")
  1380. except error.Abort, e:
  1381. raise
  1382. except:
  1383. raise util.Abort("gofmt: " + ExceptionDetail())
  1384. return
  1385. def mail(ui, repo, *pats, **opts):
  1386. """mail a change for review
  1387. Uploads a patch to the code review server and then sends mail
  1388. to the reviewer and CC list asking for a review.
  1389. """
  1390. if missing_codereview:
  1391. return missing_codereview
  1392. cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
  1393. if err != "":
  1394. return err
  1395. cl.Upload(ui, repo, gofmt_just_warn=True)
  1396. if not cl.reviewer:
  1397. # If no reviewer is listed, assign the review to defaultcc.
  1398. # This makes sure that it appears in the
  1399. # codereview.appspot.com/user/defaultcc
  1400. # page, so that it doesn't get dropped on the floor.
  1401. if not defaultcc:
  1402. return "no reviewers listed in CL"
  1403. cl.cc = Sub(cl.cc, defaultcc)
  1404. cl.reviewer = defaultcc
  1405. cl.Flush(ui, repo)
  1406. if cl.files == []:
  1407. return "no changed files, not sending mail"
  1408. cl.Mail(ui, repo)
  1409. def pending(ui, repo, *pats, **opts):
  1410. """show pending changes
  1411. Lists pending changes followed by a list of unassigned but modified files.
  1412. """
  1413. if missing_codereview:
  1414. return missing_codereview
  1415. m = LoadAllCL(ui, repo, web=True)
  1416. names = m.keys()
  1417. names.sort()
  1418. for name in names:
  1419. cl = m[name]
  1420. ui.write(cl.PendingText() + "\n")
  1421. files = DefaultFiles(ui, repo, [], opts)
  1422. if len(files) > 0:
  1423. s = "Changed files not in any CL:\n"
  1424. for f in files:
  1425. s += "\t" + f + "\n"
  1426. ui.write(s)
  1427. def reposetup(ui, repo):
  1428. global original_match
  1429. if original_match is None:
  1430. start_status_thread()
  1431. original_match = scmutil.match
  1432. scmutil.match = ReplacementForCmdutilMatch
  1433. RietveldSetup(ui, repo)
  1434. def CheckContributor(ui, repo, user=None):
  1435. set_status("checking CONTRIBUTORS file")
  1436. user, userline = FindContributor(ui, repo, user, warn=False)
  1437. if not userline:
  1438. raise util.Abort("cannot find %s in CONTRIBUTORS" % (user,))
  1439. return userline
  1440. def FindContributor(ui, repo, user=None, warn=True):
  1441. if not user:
  1442. user = ui.config("ui", "username")
  1443. if not user:
  1444. raise util.Abort("[ui] username is not configured in .hgrc")
  1445. user = user.lower()
  1446. m = re.match(r".*<(.*)>", user)
  1447. if m:
  1448. user = m.group(1)
  1449. if user not in contributors:
  1450. if warn:
  1451. ui.warn("warning: cannot find %s in CONTRIBUTORS\n" % (user,))
  1452. return user, None
  1453. user, email = contributors[user]
  1454. return email, "%s <%s>" % (user, email)
  1455. def submit(ui, repo, *pats, **opts):
  1456. """submit change to remote repository
  1457. Submits change to remote repository.
  1458. Bails out if the local repository is not in sync with the remote one.
  1459. """
  1460. if missing_codereview:
  1461. return missing_codereview
  1462. # We already called this on startup but sometimes Mercurial forgets.
  1463. set_mercurial_encoding_to_utf8()
  1464. repo.ui.quiet = True
  1465. if not opts["no_incoming"] and Incoming(ui, repo, opts):
  1466. return "local repository out of date; must sync before submit"
  1467. cl, err = CommandLineCL(ui, repo, pats, opts, defaultcc=defaultcc)
  1468. if err != "":
  1469. return err
  1470. user = None
  1471. if cl.copied_from:
  1472. user = cl.copied_from
  1473. userline = CheckContributor(ui, repo, user)
  1474. typecheck(userline, str)
  1475. about = ""
  1476. if cl.reviewer:
  1477. about += "R=" + JoinComma([CutDomain(s) for s in cl.reviewer]) + "\n"
  1478. if opts.get('tbr'):
  1479. tbr = SplitCommaSpace(opts.get('tbr'))
  1480. cl.reviewer = Add(cl.reviewer, tbr)
  1481. about += "TBR=" + JoinComma([CutDomain(s) for s in tbr]) + "\n"
  1482. if cl.cc:
  1483. about += "CC=" + JoinComma([CutDomain(s) for s in cl.cc]) + "\n"
  1484. if not cl.reviewer:
  1485. return "no reviewers listed in CL"
  1486. if not cl.local:
  1487. return "cannot submit non-local CL"
  1488. # upload, to sync current patch and also get change number if CL is new.
  1489. if not cl.copied_from:
  1490. cl.Upload(ui, repo, gofmt_just_warn=True)
  1491. # check gofmt for real; allowed upload to warn in order to save CL.
  1492. cl.Flush(ui, repo)
  1493. CheckFormat(ui, repo, cl.files)
  1494. about += "%s%s\n" % (server_url_base, cl.name)
  1495. if cl.copied_from:
  1496. about += "\nCommitter: " + CheckContributor(ui, repo, None) + "\n"
  1497. typecheck(about, str)
  1498. if not cl.mailed and not cl.copied_from: