PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/codereview/codereview.py

https://bitbucket.org/rsc/plan9port
Python | 3562 lines | 3429 code | 46 blank | 87 comment | 106 complexity | bd3d4fde0e3a3e86ab6808c04f5cb371 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Unlicense, LGPL-2.1

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

Large files files are truncated, but you can click here to view the full file