codereview.py 107 KB

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