git-p4.py 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439
  1. #!/usr/bin/env python
  2. #
  3. # git-p4.py -- A tool for bidirectional operation between a Perforce depot and git.
  4. #
  5. # Author: Simon Hausmann <simon@lst.de>
  6. # Copyright: 2007 Simon Hausmann <simon@lst.de>
  7. # 2007 Trolltech ASA
  8. # License: MIT <http://www.opensource.org/licenses/mit-license.php>
  9. #
  10. # pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
  11. # pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
  12. # pylint: disable=wrong-import-order,unused-import,too-few-public-methods
  13. # pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
  14. # pylint: disable=line-too-long,bad-whitespace,superfluous-parens
  15. # pylint: disable=too-many-statements,too-many-instance-attributes
  16. # pylint: disable=too-many-branches,too-many-nested-blocks
  17. #
  18. import sys
  19. if sys.version_info.major < 3 and sys.version_info.minor < 7:
  20. sys.stderr.write("git-p4: requires Python 2.7 or later.\n")
  21. sys.exit(1)
  22. import os
  23. import optparse
  24. import functools
  25. import marshal
  26. import subprocess
  27. import tempfile
  28. import time
  29. import platform
  30. import re
  31. import shutil
  32. import stat
  33. import zipfile
  34. import zlib
  35. import ctypes
  36. import errno
  37. import glob
  38. # On python2.7 where raw_input() and input() are both availble,
  39. # we want raw_input's semantics, but aliased to input for python3
  40. # compatibility
  41. # support basestring in python3
  42. try:
  43. if raw_input and input:
  44. input = raw_input
  45. except:
  46. pass
  47. verbose = False
  48. # Only labels/tags matching this will be imported/exported
  49. defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
  50. # The block size is reduced automatically if required
  51. defaultBlockSize = 1<<20
  52. p4_access_checked = False
  53. def p4_build_cmd(cmd):
  54. """Build a suitable p4 command line.
  55. This consolidates building and returning a p4 command line into one
  56. location. It means that hooking into the environment, or other configuration
  57. can be done more easily.
  58. """
  59. real_cmd = ["p4"]
  60. user = gitConfig("git-p4.user")
  61. if len(user) > 0:
  62. real_cmd += ["-u",user]
  63. password = gitConfig("git-p4.password")
  64. if len(password) > 0:
  65. real_cmd += ["-P", password]
  66. port = gitConfig("git-p4.port")
  67. if len(port) > 0:
  68. real_cmd += ["-p", port]
  69. host = gitConfig("git-p4.host")
  70. if len(host) > 0:
  71. real_cmd += ["-H", host]
  72. client = gitConfig("git-p4.client")
  73. if len(client) > 0:
  74. real_cmd += ["-c", client]
  75. retries = gitConfigInt("git-p4.retries")
  76. if retries is None:
  77. # Perform 3 retries by default
  78. retries = 3
  79. if retries > 0:
  80. # Provide a way to not pass this option by setting git-p4.retries to 0
  81. real_cmd += ["-r", str(retries)]
  82. if not isinstance(cmd, list):
  83. real_cmd = ' '.join(real_cmd) + ' ' + cmd
  84. else:
  85. real_cmd += cmd
  86. # now check that we can actually talk to the server
  87. global p4_access_checked
  88. if not p4_access_checked:
  89. p4_access_checked = True # suppress access checks in p4_check_access itself
  90. p4_check_access()
  91. return real_cmd
  92. def git_dir(path):
  93. """ Return TRUE if the given path is a git directory (/path/to/dir/.git).
  94. This won't automatically add ".git" to a directory.
  95. """
  96. d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
  97. if not d or len(d) == 0:
  98. return None
  99. else:
  100. return d
  101. def chdir(path, is_client_path=False):
  102. """Do chdir to the given path, and set the PWD environment
  103. variable for use by P4. It does not look at getcwd() output.
  104. Since we're not using the shell, it is necessary to set the
  105. PWD environment variable explicitly.
  106. Normally, expand the path to force it to be absolute. This
  107. addresses the use of relative path names inside P4 settings,
  108. e.g. P4CONFIG=.p4config. P4 does not simply open the filename
  109. as given; it looks for .p4config using PWD.
  110. If is_client_path, the path was handed to us directly by p4,
  111. and may be a symbolic link. Do not call os.getcwd() in this
  112. case, because it will cause p4 to think that PWD is not inside
  113. the client path.
  114. """
  115. os.chdir(path)
  116. if not is_client_path:
  117. path = os.getcwd()
  118. os.environ['PWD'] = path
  119. def calcDiskFree():
  120. """Return free space in bytes on the disk of the given dirname."""
  121. if platform.system() == 'Windows':
  122. free_bytes = ctypes.c_ulonglong(0)
  123. ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(os.getcwd()), None, None, ctypes.pointer(free_bytes))
  124. return free_bytes.value
  125. else:
  126. st = os.statvfs(os.getcwd())
  127. return st.f_bavail * st.f_frsize
  128. def die(msg):
  129. """ Terminate execution. Make sure that any running child processes have been wait()ed for before
  130. calling this.
  131. """
  132. if verbose:
  133. raise Exception(msg)
  134. else:
  135. sys.stderr.write(msg + "\n")
  136. sys.exit(1)
  137. def prompt(prompt_text):
  138. """ Prompt the user to choose one of the choices
  139. Choices are identified in the prompt_text by square brackets around
  140. a single letter option.
  141. """
  142. choices = set(m.group(1) for m in re.finditer(r"\[(.)\]", prompt_text))
  143. while True:
  144. sys.stderr.flush()
  145. sys.stdout.write(prompt_text)
  146. sys.stdout.flush()
  147. response=sys.stdin.readline().strip().lower()
  148. if not response:
  149. continue
  150. response = response[0]
  151. if response in choices:
  152. return response
  153. # We need different encoding/decoding strategies for text data being passed
  154. # around in pipes depending on python version
  155. if bytes is not str:
  156. # For python3, always encode and decode as appropriate
  157. def decode_text_stream(s):
  158. return s.decode() if isinstance(s, bytes) else s
  159. def encode_text_stream(s):
  160. return s.encode() if isinstance(s, str) else s
  161. else:
  162. # For python2.7, pass read strings as-is, but also allow writing unicode
  163. def decode_text_stream(s):
  164. return s
  165. def encode_text_stream(s):
  166. return s.encode('utf_8') if isinstance(s, unicode) else s
  167. def decode_path(path):
  168. """Decode a given string (bytes or otherwise) using configured path encoding options
  169. """
  170. encoding = gitConfig('git-p4.pathEncoding') or 'utf_8'
  171. if bytes is not str:
  172. return path.decode(encoding, errors='replace') if isinstance(path, bytes) else path
  173. else:
  174. try:
  175. path.decode('ascii')
  176. except:
  177. path = path.decode(encoding, errors='replace')
  178. if verbose:
  179. print('Path with non-ASCII characters detected. Used {} to decode: {}'.format(encoding, path))
  180. return path
  181. def run_git_hook(cmd, param=[]):
  182. """Execute a hook if the hook exists."""
  183. if verbose:
  184. sys.stderr.write("Looking for hook: %s\n" % cmd)
  185. sys.stderr.flush()
  186. hooks_path = gitConfig("core.hooksPath")
  187. if len(hooks_path) <= 0:
  188. hooks_path = os.path.join(os.environ["GIT_DIR"], "hooks")
  189. if not isinstance(param, list):
  190. param=[param]
  191. # resolve hook file name, OS depdenent
  192. hook_file = os.path.join(hooks_path, cmd)
  193. if platform.system() == 'Windows':
  194. if not os.path.isfile(hook_file):
  195. # look for the file with an extension
  196. files = glob.glob(hook_file + ".*")
  197. if not files:
  198. return True
  199. files.sort()
  200. hook_file = files.pop()
  201. while hook_file.upper().endswith(".SAMPLE"):
  202. # The file is a sample hook. We don't want it
  203. if len(files) > 0:
  204. hook_file = files.pop()
  205. else:
  206. return True
  207. if not os.path.isfile(hook_file) or not os.access(hook_file, os.X_OK):
  208. return True
  209. return run_hook_command(hook_file, param) == 0
  210. def run_hook_command(cmd, param):
  211. """Executes a git hook command
  212. cmd = the command line file to be executed. This can be
  213. a file that is run by OS association.
  214. param = a list of parameters to pass to the cmd command
  215. On windows, the extension is checked to see if it should
  216. be run with the Git for Windows Bash shell. If there
  217. is no file extension, the file is deemed a bash shell
  218. and will be handed off to sh.exe. Otherwise, Windows
  219. will be called with the shell to handle the file assocation.
  220. For non Windows operating systems, the file is called
  221. as an executable.
  222. """
  223. cli = [cmd] + param
  224. use_shell = False
  225. if platform.system() == 'Windows':
  226. (root,ext) = os.path.splitext(cmd)
  227. if ext == "":
  228. exe_path = os.environ.get("EXEPATH")
  229. if exe_path is None:
  230. exe_path = ""
  231. else:
  232. exe_path = os.path.join(exe_path, "bin")
  233. cli = [os.path.join(exe_path, "SH.EXE")] + cli
  234. else:
  235. use_shell = True
  236. return subprocess.call(cli, shell=use_shell)
  237. def write_pipe(c, stdin):
  238. if verbose:
  239. sys.stderr.write('Writing pipe: %s\n' % str(c))
  240. expand = not isinstance(c, list)
  241. p = subprocess.Popen(c, stdin=subprocess.PIPE, shell=expand)
  242. pipe = p.stdin
  243. val = pipe.write(stdin)
  244. pipe.close()
  245. if p.wait():
  246. die('Command failed: %s' % str(c))
  247. return val
  248. def p4_write_pipe(c, stdin):
  249. real_cmd = p4_build_cmd(c)
  250. if bytes is not str and isinstance(stdin, str):
  251. stdin = encode_text_stream(stdin)
  252. return write_pipe(real_cmd, stdin)
  253. def read_pipe_full(c):
  254. """ Read output from command. Returns a tuple
  255. of the return status, stdout text and stderr
  256. text.
  257. """
  258. if verbose:
  259. sys.stderr.write('Reading pipe: %s\n' % str(c))
  260. expand = not isinstance(c, list)
  261. p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=expand)
  262. (out, err) = p.communicate()
  263. return (p.returncode, out, decode_text_stream(err))
  264. def read_pipe(c, ignore_error=False, raw=False):
  265. """ Read output from command. Returns the output text on
  266. success. On failure, terminates execution, unless
  267. ignore_error is True, when it returns an empty string.
  268. If raw is True, do not attempt to decode output text.
  269. """
  270. (retcode, out, err) = read_pipe_full(c)
  271. if retcode != 0:
  272. if ignore_error:
  273. out = ""
  274. else:
  275. die('Command failed: %s\nError: %s' % (str(c), err))
  276. if not raw:
  277. out = decode_text_stream(out)
  278. return out
  279. def read_pipe_text(c):
  280. """ Read output from a command with trailing whitespace stripped.
  281. On error, returns None.
  282. """
  283. (retcode, out, err) = read_pipe_full(c)
  284. if retcode != 0:
  285. return None
  286. else:
  287. return decode_text_stream(out).rstrip()
  288. def p4_read_pipe(c, ignore_error=False, raw=False):
  289. real_cmd = p4_build_cmd(c)
  290. return read_pipe(real_cmd, ignore_error, raw=raw)
  291. def read_pipe_lines(c):
  292. if verbose:
  293. sys.stderr.write('Reading pipe: %s\n' % str(c))
  294. expand = not isinstance(c, list)
  295. p = subprocess.Popen(c, stdout=subprocess.PIPE, shell=expand)
  296. pipe = p.stdout
  297. val = [decode_text_stream(line) for line in pipe.readlines()]
  298. if pipe.close() or p.wait():
  299. die('Command failed: %s' % str(c))
  300. return val
  301. def p4_read_pipe_lines(c):
  302. """Specifically invoke p4 on the command supplied. """
  303. real_cmd = p4_build_cmd(c)
  304. return read_pipe_lines(real_cmd)
  305. def p4_has_command(cmd):
  306. """Ask p4 for help on this command. If it returns an error, the
  307. command does not exist in this version of p4."""
  308. real_cmd = p4_build_cmd(["help", cmd])
  309. p = subprocess.Popen(real_cmd, stdout=subprocess.PIPE,
  310. stderr=subprocess.PIPE)
  311. p.communicate()
  312. return p.returncode == 0
  313. def p4_has_move_command():
  314. """See if the move command exists, that it supports -k, and that
  315. it has not been administratively disabled. The arguments
  316. must be correct, but the filenames do not have to exist. Use
  317. ones with wildcards so even if they exist, it will fail."""
  318. if not p4_has_command("move"):
  319. return False
  320. cmd = p4_build_cmd(["move", "-k", "@from", "@to"])
  321. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  322. (out, err) = p.communicate()
  323. err = decode_text_stream(err)
  324. # return code will be 1 in either case
  325. if err.find("Invalid option") >= 0:
  326. return False
  327. if err.find("disabled") >= 0:
  328. return False
  329. # assume it failed because @... was invalid changelist
  330. return True
  331. def system(cmd, ignore_error=False):
  332. expand = not isinstance(cmd, list)
  333. if verbose:
  334. sys.stderr.write("executing %s\n" % str(cmd))
  335. retcode = subprocess.call(cmd, shell=expand)
  336. if retcode and not ignore_error:
  337. raise CalledProcessError(retcode, cmd)
  338. return retcode
  339. def p4_system(cmd):
  340. """Specifically invoke p4 as the system command. """
  341. real_cmd = p4_build_cmd(cmd)
  342. expand = not isinstance(real_cmd, list)
  343. retcode = subprocess.call(real_cmd, shell=expand)
  344. if retcode:
  345. raise CalledProcessError(retcode, real_cmd)
  346. def die_bad_access(s):
  347. die("failure accessing depot: {0}".format(s.rstrip()))
  348. def p4_check_access(min_expiration=1):
  349. """ Check if we can access Perforce - account still logged in
  350. """
  351. results = p4CmdList(["login", "-s"])
  352. if len(results) == 0:
  353. # should never get here: always get either some results, or a p4ExitCode
  354. assert("could not parse response from perforce")
  355. result = results[0]
  356. if 'p4ExitCode' in result:
  357. # p4 returned non-zero status, e.g. P4PORT invalid, or p4 not in path
  358. die_bad_access("could not run p4")
  359. code = result.get("code")
  360. if not code:
  361. # we get here if we couldn't connect and there was nothing to unmarshal
  362. die_bad_access("could not connect")
  363. elif code == "stat":
  364. expiry = result.get("TicketExpiration")
  365. if expiry:
  366. expiry = int(expiry)
  367. if expiry > min_expiration:
  368. # ok to carry on
  369. return
  370. else:
  371. die_bad_access("perforce ticket expires in {0} seconds".format(expiry))
  372. else:
  373. # account without a timeout - all ok
  374. return
  375. elif code == "error":
  376. data = result.get("data")
  377. if data:
  378. die_bad_access("p4 error: {0}".format(data))
  379. else:
  380. die_bad_access("unknown error")
  381. elif code == "info":
  382. return
  383. else:
  384. die_bad_access("unknown error code {0}".format(code))
  385. _p4_version_string = None
  386. def p4_version_string():
  387. """Read the version string, showing just the last line, which
  388. hopefully is the interesting version bit.
  389. $ p4 -V
  390. Perforce - The Fast Software Configuration Management System.
  391. Copyright 1995-2011 Perforce Software. All rights reserved.
  392. Rev. P4/NTX86/2011.1/393975 (2011/12/16).
  393. """
  394. global _p4_version_string
  395. if not _p4_version_string:
  396. a = p4_read_pipe_lines(["-V"])
  397. _p4_version_string = a[-1].rstrip()
  398. return _p4_version_string
  399. def p4_integrate(src, dest):
  400. p4_system(["integrate", "-Dt", wildcard_encode(src), wildcard_encode(dest)])
  401. def p4_sync(f, *options):
  402. p4_system(["sync"] + list(options) + [wildcard_encode(f)])
  403. def p4_add(f):
  404. # forcibly add file names with wildcards
  405. if wildcard_present(f):
  406. p4_system(["add", "-f", f])
  407. else:
  408. p4_system(["add", f])
  409. def p4_delete(f):
  410. p4_system(["delete", wildcard_encode(f)])
  411. def p4_edit(f, *options):
  412. p4_system(["edit"] + list(options) + [wildcard_encode(f)])
  413. def p4_revert(f):
  414. p4_system(["revert", wildcard_encode(f)])
  415. def p4_reopen(type, f):
  416. p4_system(["reopen", "-t", type, wildcard_encode(f)])
  417. def p4_reopen_in_change(changelist, files):
  418. cmd = ["reopen", "-c", str(changelist)] + files
  419. p4_system(cmd)
  420. def p4_move(src, dest):
  421. p4_system(["move", "-k", wildcard_encode(src), wildcard_encode(dest)])
  422. def p4_last_change():
  423. results = p4CmdList(["changes", "-m", "1"], skip_info=True)
  424. return int(results[0]['change'])
  425. def p4_describe(change, shelved=False):
  426. """Make sure it returns a valid result by checking for
  427. the presence of field "time". Return a dict of the
  428. results."""
  429. cmd = ["describe", "-s"]
  430. if shelved:
  431. cmd += ["-S"]
  432. cmd += [str(change)]
  433. ds = p4CmdList(cmd, skip_info=True)
  434. if len(ds) != 1:
  435. die("p4 describe -s %d did not return 1 result: %s" % (change, str(ds)))
  436. d = ds[0]
  437. if "p4ExitCode" in d:
  438. die("p4 describe -s %d exited with %d: %s" % (change, d["p4ExitCode"],
  439. str(d)))
  440. if "code" in d:
  441. if d["code"] == "error":
  442. die("p4 describe -s %d returned error code: %s" % (change, str(d)))
  443. if "time" not in d:
  444. die("p4 describe -s %d returned no \"time\": %s" % (change, str(d)))
  445. return d
  446. #
  447. # Canonicalize the p4 type and return a tuple of the
  448. # base type, plus any modifiers. See "p4 help filetypes"
  449. # for a list and explanation.
  450. #
  451. def split_p4_type(p4type):
  452. p4_filetypes_historical = {
  453. "ctempobj": "binary+Sw",
  454. "ctext": "text+C",
  455. "cxtext": "text+Cx",
  456. "ktext": "text+k",
  457. "kxtext": "text+kx",
  458. "ltext": "text+F",
  459. "tempobj": "binary+FSw",
  460. "ubinary": "binary+F",
  461. "uresource": "resource+F",
  462. "uxbinary": "binary+Fx",
  463. "xbinary": "binary+x",
  464. "xltext": "text+Fx",
  465. "xtempobj": "binary+Swx",
  466. "xtext": "text+x",
  467. "xunicode": "unicode+x",
  468. "xutf16": "utf16+x",
  469. }
  470. if p4type in p4_filetypes_historical:
  471. p4type = p4_filetypes_historical[p4type]
  472. mods = ""
  473. s = p4type.split("+")
  474. base = s[0]
  475. mods = ""
  476. if len(s) > 1:
  477. mods = s[1]
  478. return (base, mods)
  479. #
  480. # return the raw p4 type of a file (text, text+ko, etc)
  481. #
  482. def p4_type(f):
  483. results = p4CmdList(["fstat", "-T", "headType", wildcard_encode(f)])
  484. return results[0]['headType']
  485. #
  486. # Given a type base and modifier, return a regexp matching
  487. # the keywords that can be expanded in the file
  488. #
  489. def p4_keywords_regexp_for_type(base, type_mods):
  490. if base in ("text", "unicode", "binary"):
  491. kwords = None
  492. if "ko" in type_mods:
  493. kwords = 'Id|Header'
  494. elif "k" in type_mods:
  495. kwords = 'Id|Header|Author|Date|DateTime|Change|File|Revision'
  496. else:
  497. return None
  498. pattern = r"""
  499. \$ # Starts with a dollar, followed by...
  500. (%s) # one of the keywords, followed by...
  501. (:[^$\n]+)? # possibly an old expansion, followed by...
  502. \$ # another dollar
  503. """ % kwords
  504. return pattern
  505. else:
  506. return None
  507. #
  508. # Given a file, return a regexp matching the possible
  509. # RCS keywords that will be expanded, or None for files
  510. # with kw expansion turned off.
  511. #
  512. def p4_keywords_regexp_for_file(file):
  513. if not os.path.exists(file):
  514. return None
  515. else:
  516. (type_base, type_mods) = split_p4_type(p4_type(file))
  517. return p4_keywords_regexp_for_type(type_base, type_mods)
  518. def setP4ExecBit(file, mode):
  519. # Reopens an already open file and changes the execute bit to match
  520. # the execute bit setting in the passed in mode.
  521. p4Type = "+x"
  522. if not isModeExec(mode):
  523. p4Type = getP4OpenedType(file)
  524. p4Type = re.sub('^([cku]?)x(.*)', '\\1\\2', p4Type)
  525. p4Type = re.sub('(.*?\+.*?)x(.*?)', '\\1\\2', p4Type)
  526. if p4Type[-1] == "+":
  527. p4Type = p4Type[0:-1]
  528. p4_reopen(p4Type, file)
  529. def getP4OpenedType(file):
  530. # Returns the perforce file type for the given file.
  531. result = p4_read_pipe(["opened", wildcard_encode(file)])
  532. match = re.match(".*\((.+)\)( \*exclusive\*)?\r?$", result)
  533. if match:
  534. return match.group(1)
  535. else:
  536. die("Could not determine file type for %s (result: '%s')" % (file, result))
  537. # Return the set of all p4 labels
  538. def getP4Labels(depotPaths):
  539. labels = set()
  540. if not isinstance(depotPaths, list):
  541. depotPaths = [depotPaths]
  542. for l in p4CmdList(["labels"] + ["%s..." % p for p in depotPaths]):
  543. label = l['label']
  544. labels.add(label)
  545. return labels
  546. # Return the set of all git tags
  547. def getGitTags():
  548. gitTags = set()
  549. for line in read_pipe_lines(["git", "tag"]):
  550. tag = line.strip()
  551. gitTags.add(tag)
  552. return gitTags
  553. _diff_tree_pattern = None
  554. def parseDiffTreeEntry(entry):
  555. """Parses a single diff tree entry into its component elements.
  556. See git-diff-tree(1) manpage for details about the format of the diff
  557. output. This method returns a dictionary with the following elements:
  558. src_mode - The mode of the source file
  559. dst_mode - The mode of the destination file
  560. src_sha1 - The sha1 for the source file
  561. dst_sha1 - The sha1 fr the destination file
  562. status - The one letter status of the diff (i.e. 'A', 'M', 'D', etc)
  563. status_score - The score for the status (applicable for 'C' and 'R'
  564. statuses). This is None if there is no score.
  565. src - The path for the source file.
  566. dst - The path for the destination file. This is only present for
  567. copy or renames. If it is not present, this is None.
  568. If the pattern is not matched, None is returned."""
  569. global _diff_tree_pattern
  570. if not _diff_tree_pattern:
  571. _diff_tree_pattern = re.compile(':(\d+) (\d+) (\w+) (\w+) ([A-Z])(\d+)?\t(.*?)((\t(.*))|$)')
  572. match = _diff_tree_pattern.match(entry)
  573. if match:
  574. return {
  575. 'src_mode': match.group(1),
  576. 'dst_mode': match.group(2),
  577. 'src_sha1': match.group(3),
  578. 'dst_sha1': match.group(4),
  579. 'status': match.group(5),
  580. 'status_score': match.group(6),
  581. 'src': match.group(7),
  582. 'dst': match.group(10)
  583. }
  584. return None
  585. def isModeExec(mode):
  586. # Returns True if the given git mode represents an executable file,
  587. # otherwise False.
  588. return mode[-3:] == "755"
  589. class P4Exception(Exception):
  590. """ Base class for exceptions from the p4 client """
  591. def __init__(self, exit_code):
  592. self.p4ExitCode = exit_code
  593. class P4ServerException(P4Exception):
  594. """ Base class for exceptions where we get some kind of marshalled up result from the server """
  595. def __init__(self, exit_code, p4_result):
  596. super(P4ServerException, self).__init__(exit_code)
  597. self.p4_result = p4_result
  598. self.code = p4_result[0]['code']
  599. self.data = p4_result[0]['data']
  600. class P4RequestSizeException(P4ServerException):
  601. """ One of the maxresults or maxscanrows errors """
  602. def __init__(self, exit_code, p4_result, limit):
  603. super(P4RequestSizeException, self).__init__(exit_code, p4_result)
  604. self.limit = limit
  605. class P4CommandException(P4Exception):
  606. """ Something went wrong calling p4 which means we have to give up """
  607. def __init__(self, msg):
  608. self.msg = msg
  609. def __str__(self):
  610. return self.msg
  611. def isModeExecChanged(src_mode, dst_mode):
  612. return isModeExec(src_mode) != isModeExec(dst_mode)
  613. def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
  614. errors_as_exceptions=False):
  615. if not isinstance(cmd, list):
  616. cmd = "-G " + cmd
  617. expand = True
  618. else:
  619. cmd = ["-G"] + cmd
  620. expand = False
  621. cmd = p4_build_cmd(cmd)
  622. if verbose:
  623. sys.stderr.write("Opening pipe: %s\n" % str(cmd))
  624. # Use a temporary file to avoid deadlocks without
  625. # subprocess.communicate(), which would put another copy
  626. # of stdout into memory.
  627. stdin_file = None
  628. if stdin is not None:
  629. stdin_file = tempfile.TemporaryFile(prefix='p4-stdin', mode=stdin_mode)
  630. if not isinstance(stdin, list):
  631. stdin_file.write(stdin)
  632. else:
  633. for i in stdin:
  634. stdin_file.write(encode_text_stream(i))
  635. stdin_file.write(b'\n')
  636. stdin_file.flush()
  637. stdin_file.seek(0)
  638. p4 = subprocess.Popen(cmd,
  639. shell=expand,
  640. stdin=stdin_file,
  641. stdout=subprocess.PIPE)
  642. result = []
  643. try:
  644. while True:
  645. entry = marshal.load(p4.stdout)
  646. if bytes is not str:
  647. # Decode unmarshalled dict to use str keys and values, except for:
  648. # - `data` which may contain arbitrary binary data
  649. # - `depotFile[0-9]*`, `path`, or `clientFile` which may contain non-UTF8 encoded text
  650. decoded_entry = {}
  651. for key, value in entry.items():
  652. key = key.decode()
  653. if isinstance(value, bytes) and not (key in ('data', 'path', 'clientFile') or key.startswith('depotFile')):
  654. value = value.decode()
  655. decoded_entry[key] = value
  656. # Parse out data if it's an error response
  657. if decoded_entry.get('code') == 'error' and 'data' in decoded_entry:
  658. decoded_entry['data'] = decoded_entry['data'].decode()
  659. entry = decoded_entry
  660. if skip_info:
  661. if 'code' in entry and entry['code'] == 'info':
  662. continue
  663. if cb is not None:
  664. cb(entry)
  665. else:
  666. result.append(entry)
  667. except EOFError:
  668. pass
  669. exitCode = p4.wait()
  670. if exitCode != 0:
  671. if errors_as_exceptions:
  672. if len(result) > 0:
  673. data = result[0].get('data')
  674. if data:
  675. m = re.search('Too many rows scanned \(over (\d+)\)', data)
  676. if not m:
  677. m = re.search('Request too large \(over (\d+)\)', data)
  678. if m:
  679. limit = int(m.group(1))
  680. raise P4RequestSizeException(exitCode, result, limit)
  681. raise P4ServerException(exitCode, result)
  682. else:
  683. raise P4Exception(exitCode)
  684. else:
  685. entry = {}
  686. entry["p4ExitCode"] = exitCode
  687. result.append(entry)
  688. return result
  689. def p4Cmd(cmd):
  690. list = p4CmdList(cmd)
  691. result = {}
  692. for entry in list:
  693. result.update(entry)
  694. return result;
  695. def p4Where(depotPath):
  696. if not depotPath.endswith("/"):
  697. depotPath += "/"
  698. depotPathLong = depotPath + "..."
  699. outputList = p4CmdList(["where", depotPathLong])
  700. output = None
  701. for entry in outputList:
  702. if "depotFile" in entry:
  703. # Search for the base client side depot path, as long as it starts with the branch's P4 path.
  704. # The base path always ends with "/...".
  705. entry_path = decode_path(entry['depotFile'])
  706. if entry_path.find(depotPath) == 0 and entry_path[-4:] == "/...":
  707. output = entry
  708. break
  709. elif "data" in entry:
  710. data = entry.get("data")
  711. space = data.find(" ")
  712. if data[:space] == depotPath:
  713. output = entry
  714. break
  715. if output == None:
  716. return ""
  717. if output["code"] == "error":
  718. return ""
  719. clientPath = ""
  720. if "path" in output:
  721. clientPath = decode_path(output['path'])
  722. elif "data" in output:
  723. data = output.get("data")
  724. lastSpace = data.rfind(b" ")
  725. clientPath = decode_path(data[lastSpace + 1:])
  726. if clientPath.endswith("..."):
  727. clientPath = clientPath[:-3]
  728. return clientPath
  729. def currentGitBranch():
  730. return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
  731. def isValidGitDir(path):
  732. return git_dir(path) != None
  733. def parseRevision(ref):
  734. return read_pipe("git rev-parse %s" % ref).strip()
  735. def branchExists(ref):
  736. rev = read_pipe(["git", "rev-parse", "-q", "--verify", ref],
  737. ignore_error=True)
  738. return len(rev) > 0
  739. def extractLogMessageFromGitCommit(commit):
  740. logMessage = ""
  741. ## fixme: title is first line of commit, not 1st paragraph.
  742. foundTitle = False
  743. for log in read_pipe_lines(["git", "cat-file", "commit", commit]):
  744. if not foundTitle:
  745. if len(log) == 1:
  746. foundTitle = True
  747. continue
  748. logMessage += log
  749. return logMessage
  750. def extractSettingsGitLog(log):
  751. values = {}
  752. for line in log.split("\n"):
  753. line = line.strip()
  754. m = re.search (r"^ *\[git-p4: (.*)\]$", line)
  755. if not m:
  756. continue
  757. assignments = m.group(1).split (':')
  758. for a in assignments:
  759. vals = a.split ('=')
  760. key = vals[0].strip()
  761. val = ('='.join (vals[1:])).strip()
  762. if val.endswith ('\"') and val.startswith('"'):
  763. val = val[1:-1]
  764. values[key] = val
  765. paths = values.get("depot-paths")
  766. if not paths:
  767. paths = values.get("depot-path")
  768. if paths:
  769. values['depot-paths'] = paths.split(',')
  770. return values
  771. def gitBranchExists(branch):
  772. proc = subprocess.Popen(["git", "rev-parse", branch],
  773. stderr=subprocess.PIPE, stdout=subprocess.PIPE);
  774. return proc.wait() == 0;
  775. def gitUpdateRef(ref, newvalue):
  776. subprocess.check_call(["git", "update-ref", ref, newvalue])
  777. def gitDeleteRef(ref):
  778. subprocess.check_call(["git", "update-ref", "-d", ref])
  779. _gitConfig = {}
  780. def gitConfig(key, typeSpecifier=None):
  781. if key not in _gitConfig:
  782. cmd = [ "git", "config" ]
  783. if typeSpecifier:
  784. cmd += [ typeSpecifier ]
  785. cmd += [ key ]
  786. s = read_pipe(cmd, ignore_error=True)
  787. _gitConfig[key] = s.strip()
  788. return _gitConfig[key]
  789. def gitConfigBool(key):
  790. """Return a bool, using git config --bool. It is True only if the
  791. variable is set to true, and False if set to false or not present
  792. in the config."""
  793. if key not in _gitConfig:
  794. _gitConfig[key] = gitConfig(key, '--bool') == "true"
  795. return _gitConfig[key]
  796. def gitConfigInt(key):
  797. if key not in _gitConfig:
  798. cmd = [ "git", "config", "--int", key ]
  799. s = read_pipe(cmd, ignore_error=True)
  800. v = s.strip()
  801. try:
  802. _gitConfig[key] = int(gitConfig(key, '--int'))
  803. except ValueError:
  804. _gitConfig[key] = None
  805. return _gitConfig[key]
  806. def gitConfigList(key):
  807. if key not in _gitConfig:
  808. s = read_pipe(["git", "config", "--get-all", key], ignore_error=True)
  809. _gitConfig[key] = s.strip().splitlines()
  810. if _gitConfig[key] == ['']:
  811. _gitConfig[key] = []
  812. return _gitConfig[key]
  813. def p4BranchesInGit(branchesAreInRemotes=True):
  814. """Find all the branches whose names start with "p4/", looking
  815. in remotes or heads as specified by the argument. Return
  816. a dictionary of { branch: revision } for each one found.
  817. The branch names are the short names, without any
  818. "p4/" prefix."""
  819. branches = {}
  820. cmdline = "git rev-parse --symbolic "
  821. if branchesAreInRemotes:
  822. cmdline += "--remotes"
  823. else:
  824. cmdline += "--branches"
  825. for line in read_pipe_lines(cmdline):
  826. line = line.strip()
  827. # only import to p4/
  828. if not line.startswith('p4/'):
  829. continue
  830. # special symbolic ref to p4/master
  831. if line == "p4/HEAD":
  832. continue
  833. # strip off p4/ prefix
  834. branch = line[len("p4/"):]
  835. branches[branch] = parseRevision(line)
  836. return branches
  837. def branch_exists(branch):
  838. """Make sure that the given ref name really exists."""
  839. cmd = [ "git", "rev-parse", "--symbolic", "--verify", branch ]
  840. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  841. out, _ = p.communicate()
  842. out = decode_text_stream(out)
  843. if p.returncode:
  844. return False
  845. # expect exactly one line of output: the branch name
  846. return out.rstrip() == branch
  847. def findUpstreamBranchPoint(head = "HEAD"):
  848. branches = p4BranchesInGit()
  849. # map from depot-path to branch name
  850. branchByDepotPath = {}
  851. for branch in branches.keys():
  852. tip = branches[branch]
  853. log = extractLogMessageFromGitCommit(tip)
  854. settings = extractSettingsGitLog(log)
  855. if "depot-paths" in settings:
  856. paths = ",".join(settings["depot-paths"])
  857. branchByDepotPath[paths] = "remotes/p4/" + branch
  858. settings = None
  859. parent = 0
  860. while parent < 65535:
  861. commit = head + "~%s" % parent
  862. log = extractLogMessageFromGitCommit(commit)
  863. settings = extractSettingsGitLog(log)
  864. if "depot-paths" in settings:
  865. paths = ",".join(settings["depot-paths"])
  866. if paths in branchByDepotPath:
  867. return [branchByDepotPath[paths], settings]
  868. parent = parent + 1
  869. return ["", settings]
  870. def createOrUpdateBranchesFromOrigin(localRefPrefix = "refs/remotes/p4/", silent=True):
  871. if not silent:
  872. print("Creating/updating branch(es) in %s based on origin branch(es)"
  873. % localRefPrefix)
  874. originPrefix = "origin/p4/"
  875. for line in read_pipe_lines("git rev-parse --symbolic --remotes"):
  876. line = line.strip()
  877. if (not line.startswith(originPrefix)) or line.endswith("HEAD"):
  878. continue
  879. headName = line[len(originPrefix):]
  880. remoteHead = localRefPrefix + headName
  881. originHead = line
  882. original = extractSettingsGitLog(extractLogMessageFromGitCommit(originHead))
  883. if ('depot-paths' not in original
  884. or 'change' not in original):
  885. continue
  886. update = False
  887. if not gitBranchExists(remoteHead):
  888. if verbose:
  889. print("creating %s" % remoteHead)
  890. update = True
  891. else:
  892. settings = extractSettingsGitLog(extractLogMessageFromGitCommit(remoteHead))
  893. if 'change' in settings:
  894. if settings['depot-paths'] == original['depot-paths']:
  895. originP4Change = int(original['change'])
  896. p4Change = int(settings['change'])
  897. if originP4Change > p4Change:
  898. print("%s (%s) is newer than %s (%s). "
  899. "Updating p4 branch from origin."
  900. % (originHead, originP4Change,
  901. remoteHead, p4Change))
  902. update = True
  903. else:
  904. print("Ignoring: %s was imported from %s while "
  905. "%s was imported from %s"
  906. % (originHead, ','.join(original['depot-paths']),
  907. remoteHead, ','.join(settings['depot-paths'])))
  908. if update:
  909. system("git update-ref %s %s" % (remoteHead, originHead))
  910. def originP4BranchesExist():
  911. return gitBranchExists("origin") or gitBranchExists("origin/p4") or gitBranchExists("origin/p4/master")
  912. def p4ParseNumericChangeRange(parts):
  913. changeStart = int(parts[0][1:])
  914. if parts[1] == '#head':
  915. changeEnd = p4_last_change()
  916. else:
  917. changeEnd = int(parts[1])
  918. return (changeStart, changeEnd)
  919. def chooseBlockSize(blockSize):
  920. if blockSize:
  921. return blockSize
  922. else:
  923. return defaultBlockSize
  924. def p4ChangesForPaths(depotPaths, changeRange, requestedBlockSize):
  925. assert depotPaths
  926. # Parse the change range into start and end. Try to find integer
  927. # revision ranges as these can be broken up into blocks to avoid
  928. # hitting server-side limits (maxrows, maxscanresults). But if
  929. # that doesn't work, fall back to using the raw revision specifier
  930. # strings, without using block mode.
  931. if changeRange is None or changeRange == '':
  932. changeStart = 1
  933. changeEnd = p4_last_change()
  934. block_size = chooseBlockSize(requestedBlockSize)
  935. else:
  936. parts = changeRange.split(',')
  937. assert len(parts) == 2
  938. try:
  939. (changeStart, changeEnd) = p4ParseNumericChangeRange(parts)
  940. block_size = chooseBlockSize(requestedBlockSize)
  941. except ValueError:
  942. changeStart = parts[0][1:]
  943. changeEnd = parts[1]
  944. if requestedBlockSize:
  945. die("cannot use --changes-block-size with non-numeric revisions")
  946. block_size = None
  947. changes = set()
  948. # Retrieve changes a block at a time, to prevent running
  949. # into a MaxResults/MaxScanRows error from the server. If
  950. # we _do_ hit one of those errors, turn down the block size
  951. while True:
  952. cmd = ['changes']
  953. if block_size:
  954. end = min(changeEnd, changeStart + block_size)
  955. revisionRange = "%d,%d" % (changeStart, end)
  956. else:
  957. revisionRange = "%s,%s" % (changeStart, changeEnd)
  958. for p in depotPaths:
  959. cmd += ["%s...@%s" % (p, revisionRange)]
  960. # fetch the changes
  961. try:
  962. result = p4CmdList(cmd, errors_as_exceptions=True)
  963. except P4RequestSizeException as e:
  964. if not block_size:
  965. block_size = e.limit
  966. elif block_size > e.limit:
  967. block_size = e.limit
  968. else:
  969. block_size = max(2, block_size // 2)
  970. if verbose: print("block size error, retrying with block size {0}".format(block_size))
  971. continue
  972. except P4Exception as e:
  973. die('Error retrieving changes description ({0})'.format(e.p4ExitCode))
  974. # Insert changes in chronological order
  975. for entry in reversed(result):
  976. if 'change' not in entry:
  977. continue
  978. changes.add(int(entry['change']))
  979. if not block_size:
  980. break
  981. if end >= changeEnd:
  982. break
  983. changeStart = end + 1
  984. changes = sorted(changes)
  985. return changes
  986. def p4PathStartsWith(path, prefix):
  987. # This method tries to remedy a potential mixed-case issue:
  988. #
  989. # If UserA adds //depot/DirA/file1
  990. # and UserB adds //depot/dira/file2
  991. #
  992. # we may or may not have a problem. If you have core.ignorecase=true,
  993. # we treat DirA and dira as the same directory
  994. if gitConfigBool("core.ignorecase"):
  995. return path.lower().startswith(prefix.lower())
  996. return path.startswith(prefix)
  997. def getClientSpec():
  998. """Look at the p4 client spec, create a View() object that contains
  999. all the mappings, and return it."""
  1000. specList = p4CmdList("client -o")
  1001. if len(specList) != 1:
  1002. die('Output from "client -o" is %d lines, expecting 1' %
  1003. len(specList))
  1004. # dictionary of all client parameters
  1005. entry = specList[0]
  1006. # the //client/ name
  1007. client_name = entry["Client"]
  1008. # just the keys that start with "View"
  1009. view_keys = [ k for k in entry.keys() if k.startswith("View") ]
  1010. # hold this new View
  1011. view = View(client_name)
  1012. # append the lines, in order, to the view
  1013. for view_num in range(len(view_keys)):
  1014. k = "View%d" % view_num
  1015. if k not in view_keys:
  1016. die("Expected view key %s missing" % k)
  1017. view.append(entry[k])
  1018. return view
  1019. def getClientRoot():
  1020. """Grab the client directory."""
  1021. output = p4CmdList("client -o")
  1022. if len(output) != 1:
  1023. die('Output from "client -o" is %d lines, expecting 1' % len(output))
  1024. entry = output[0]
  1025. if "Root" not in entry:
  1026. die('Client has no "Root"')
  1027. return entry["Root"]
  1028. #
  1029. # P4 wildcards are not allowed in filenames. P4 complains
  1030. # if you simply add them, but you can force it with "-f", in
  1031. # which case it translates them into %xx encoding internally.
  1032. #
  1033. def wildcard_decode(path):
  1034. # Search for and fix just these four characters. Do % last so
  1035. # that fixing it does not inadvertently create new %-escapes.
  1036. # Cannot have * in a filename in windows; untested as to
  1037. # what p4 would do in such a case.
  1038. if not platform.system() == "Windows":
  1039. path = path.replace("%2A", "*")
  1040. path = path.replace("%23", "#") \
  1041. .replace("%40", "@") \
  1042. .replace("%25", "%")
  1043. return path
  1044. def wildcard_encode(path):
  1045. # do % first to avoid double-encoding the %s introduced here
  1046. path = path.replace("%", "%25") \
  1047. .replace("*", "%2A") \
  1048. .replace("#", "%23") \
  1049. .replace("@", "%40")
  1050. return path
  1051. def wildcard_present(path):
  1052. m = re.search("[*#@%]", path)
  1053. return m is not None
  1054. class LargeFileSystem(object):
  1055. """Base class for large file system support."""
  1056. def __init__(self, writeToGitStream):
  1057. self.largeFiles = set()
  1058. self.writeToGitStream = writeToGitStream
  1059. def generatePointer(self, cloneDestination, contentFile):
  1060. """Return the content of a pointer file that is stored in Git instead of
  1061. the actual content."""
  1062. assert False, "Method 'generatePointer' required in " + self.__class__.__name__
  1063. def pushFile(self, localLargeFile):
  1064. """Push the actual content which is not stored in the Git repository to
  1065. a server."""
  1066. assert False, "Method 'pushFile' required in " + self.__class__.__name__
  1067. def hasLargeFileExtension(self, relPath):
  1068. return functools.reduce(
  1069. lambda a, b: a or b,
  1070. [relPath.endswith('.' + e) for e in gitConfigList('git-p4.largeFileExtensions')],
  1071. False
  1072. )
  1073. def generateTempFile(self, contents):
  1074. contentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=False)
  1075. for d in contents:
  1076. contentFile.write(d)
  1077. contentFile.close()
  1078. return contentFile.name
  1079. def exceedsLargeFileThreshold(self, relPath, contents):
  1080. if gitConfigInt('git-p4.largeFileThreshold'):
  1081. contentsSize = sum(len(d) for d in contents)
  1082. if contentsSize > gitConfigInt('git-p4.largeFileThreshold'):
  1083. return True
  1084. if gitConfigInt('git-p4.largeFileCompressedThreshold'):
  1085. contentsSize = sum(len(d) for d in contents)
  1086. if contentsSize <= gitConfigInt('git-p4.largeFileCompressedThreshold'):
  1087. return False
  1088. contentTempFile = self.generateTempFile(contents)
  1089. compressedContentFile = tempfile.NamedTemporaryFile(prefix='git-p4-large-file', delete=True)
  1090. with zipfile.ZipFile(compressedContentFile, mode='w') as zf:
  1091. zf.write(contentTempFile, compress_type=zipfile.ZIP_DEFLATED)
  1092. compressedContentsSize = zf.infolist()[0].compress_size
  1093. os.remove(contentTempFile)
  1094. if compressedContentsSize > gitConfigInt('git-p4.largeFileCompressedThreshold'):
  1095. return True
  1096. return False
  1097. def addLargeFile(self, relPath):
  1098. self.largeFiles.add(relPath)
  1099. def removeLargeFile(self, relPath):
  1100. self.largeFiles.remove(relPath)
  1101. def isLargeFile(self, relPath):
  1102. return relPath in self.largeFiles
  1103. def processContent(self, git_mode, relPath, contents):
  1104. """Processes the content of git fast import. This method decides if a
  1105. file is stored in the large file system and handles all necessary
  1106. steps."""
  1107. if self.exceedsLargeFileThreshold(relPath, contents) or self.hasLargeFileExtension(relPath):
  1108. contentTempFile = self.generateTempFile(contents)
  1109. (pointer_git_mode, contents, localLargeFile) = self.generatePointer(contentTempFile)
  1110. if pointer_git_mode:
  1111. git_mode = pointer_git_mode
  1112. if localLargeFile:
  1113. # Move temp file to final location in large file system
  1114. largeFileDir = os.path.dirname(localLargeFile)
  1115. if not os.path.isdir(largeFileDir):
  1116. os.makedirs(largeFileDir)
  1117. shutil.move(contentTempFile, localLargeFile)
  1118. self.addLargeFile(relPath)
  1119. if gitConfigBool('git-p4.largeFilePush'):
  1120. self.pushFile(localLargeFile)
  1121. if verbose:
  1122. sys.stderr.write("%s moved to large file system (%s)\n" % (relPath, localLargeFile))
  1123. return (git_mode, contents)
  1124. class MockLFS(LargeFileSystem):
  1125. """Mock large file system for testing."""
  1126. def generatePointer(self, contentFile):
  1127. """The pointer content is the original content prefixed with "pointer-".
  1128. The local filename of the large file storage is derived from the file content.
  1129. """
  1130. with open(contentFile, 'r') as f:
  1131. content = next(f)
  1132. gitMode = '100644'
  1133. pointerContents = 'pointer-' + content
  1134. localLargeFile = os.path.join(os.getcwd(), '.git', 'mock-storage', 'local', content[:-1])
  1135. return (gitMode, pointerContents, localLargeFile)
  1136. def pushFile(self, localLargeFile):
  1137. """The remote filename of the large file storage is the same as the local
  1138. one but in a different directory.
  1139. """
  1140. remotePath = os.path.join(os.path.dirname(localLargeFile), '..', 'remote')
  1141. if not os.path.exists(remotePath):
  1142. os.makedirs(remotePath)
  1143. shutil.copyfile(localLargeFile, os.path.join(remotePath, os.path.basename(localLargeFile)))
  1144. class GitLFS(LargeFileSystem):
  1145. """Git LFS as backend for the git-p4 large file system.
  1146. See https://git-lfs.github.com/ for details."""
  1147. def __init__(self, *args):
  1148. LargeFileSystem.__init__(self, *args)
  1149. self.baseGitAttributes = []
  1150. def generatePointer(self, contentFile):
  1151. """Generate a Git LFS pointer for the content. Return LFS Pointer file
  1152. mode and content which is stored in the Git repository instead of
  1153. the actual content. Return also the new location of the actual
  1154. content.
  1155. """
  1156. if os.path.getsize(contentFile) == 0:
  1157. return (None, '', None)
  1158. pointerProcess = subprocess.Popen(
  1159. ['git', 'lfs', 'pointer', '--file=' + contentFile],
  1160. stdout=subprocess.PIPE
  1161. )
  1162. pointerFile = decode_text_stream(pointerProcess.stdout.read())
  1163. if pointerProcess.wait():
  1164. os.remove(contentFile)
  1165. die('git-lfs pointer command failed. Did you install the extension?')
  1166. # Git LFS removed the preamble in the output of the 'pointer' command
  1167. # starting from version 1.2.0. Check for the preamble here to support
  1168. # earlier versions.
  1169. # c.f. https://github.com/github/git-lfs/commit/da2935d9a739592bc775c98d8ef4df9c72ea3b43
  1170. if pointerFile.startswith('Git LFS pointer for'):
  1171. pointerFile = re.sub(r'Git LFS pointer for.*\n\n', '', pointerFile)
  1172. oid = re.search(r'^oid \w+:(\w+)', pointerFile, re.MULTILINE).group(1)
  1173. # if someone use external lfs.storage ( not in local repo git )
  1174. lfs_path = gitConfig('lfs.storage')
  1175. if not lfs_path:
  1176. lfs_path = 'lfs'
  1177. if not os.path.isabs(lfs_path):
  1178. lfs_path = os.path.join(os.getcwd(), '.git', lfs_path)
  1179. localLargeFile = os.path.join(
  1180. lfs_path,
  1181. 'objects', oid[:2], oid[2:4],
  1182. oid,
  1183. )
  1184. # LFS Spec states that pointer files should not have the executable bit set.
  1185. gitMode = '100644'
  1186. return (gitMode, pointerFile, localLargeFile)
  1187. def pushFile(self, localLargeFile):
  1188. uploadProcess = subprocess.Popen(
  1189. ['git', 'lfs', 'push', '--object-id', 'origin', os.path.basename(localLargeFile)]
  1190. )
  1191. if uploadProcess.wait():
  1192. die('git-lfs push command failed. Did you define a remote?')
  1193. def generateGitAttributes(self):
  1194. return (
  1195. self.baseGitAttributes +
  1196. [
  1197. '\n',
  1198. '#\n',
  1199. '# Git LFS (see https://git-lfs.github.com/)\n',
  1200. '#\n',
  1201. ] +
  1202. ['*.' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
  1203. for f in sorted(gitConfigList('git-p4.largeFileExtensions'))
  1204. ] +
  1205. ['/' + f.replace(' ', '[[:space:]]') + ' filter=lfs diff=lfs merge=lfs -text\n'
  1206. for f in sorted(self.largeFiles) if not self.hasLargeFileExtension(f)
  1207. ]
  1208. )
  1209. def addLargeFile(self, relPath):
  1210. LargeFileSystem.addLargeFile(self, relPath)
  1211. self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
  1212. def removeLargeFile(self, relPath):
  1213. LargeFileSystem.removeLargeFile(self, relPath)
  1214. self.writeToGitStream('100644', '.gitattributes', self.generateGitAttributes())
  1215. def processContent(self, git_mode, relPath, contents):
  1216. if relPath == '.gitattributes':
  1217. self.baseGitAttributes = contents
  1218. return (git_mode, self.generateGitAttributes())
  1219. else:
  1220. return LargeFileSystem.processContent(self, git_mode, relPath, contents)
  1221. class Command:
  1222. delete_actions = ( "delete", "move/delete", "purge" )
  1223. add_actions = ( "add", "branch", "move/add" )
  1224. def __init__(self):
  1225. self.usage = "usage: %prog [options]"
  1226. self.needsGit = True
  1227. self.verbose = False
  1228. # This is required for the "append" update_shelve action
  1229. def ensure_value(self, attr, value):
  1230. if not hasattr(self, attr) or getattr(self, attr) is None:
  1231. setattr(self, attr, value)
  1232. return getattr(self, attr)
  1233. class P4UserMap:
  1234. def __init__(self):
  1235. self.userMapFromPerforceServer = False
  1236. self.myP4UserId = None
  1237. def p4UserId(self):
  1238. if self.myP4UserId:
  1239. return self.myP4UserId
  1240. results = p4CmdList("user -o")
  1241. for r in results:
  1242. if 'User' in r:
  1243. self.myP4UserId = r['User']
  1244. return r['User']
  1245. die("Could not find your p4 user id")
  1246. def p4UserIsMe(self, p4User):
  1247. # return True if the given p4 user is actually me
  1248. me = self.p4UserId()
  1249. if not p4User or p4User != me:
  1250. return False
  1251. else:
  1252. return True
  1253. def getUserCacheFilename(self):
  1254. home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
  1255. return home + "/.gitp4-usercache.txt"
  1256. def getUserMapFromPerforceServer(self):
  1257. if self.userMapFromPerforceServer:
  1258. return
  1259. self.users = {}
  1260. self.emails = {}
  1261. for output in p4CmdList("users"):
  1262. if "User" not in output:
  1263. continue
  1264. self.users[output["User"]] = output["FullName"] + " <" + output["Email"] + ">"
  1265. self.emails[output["Email"]] = output["User"]
  1266. mapUserConfigRegex = re.compile(r"^\s*(\S+)\s*=\s*(.+)\s*<(\S+)>\s*$", re.VERBOSE)
  1267. for mapUserConfig in gitConfigList("git-p4.mapUser"):
  1268. mapUser = mapUserConfigRegex.findall(mapUserConfig)
  1269. if mapUser and len(mapUser[0]) == 3:
  1270. user = mapUser[0][0]
  1271. fullname = mapUser[0][1]
  1272. email = mapUser[0][2]
  1273. self.users[user] = fullname + " <" + email + ">"
  1274. self.emails[email] = user
  1275. s = ''
  1276. for (key, val) in self.users.items():
  1277. s += "%s\t%s\n" % (key.expandtabs(1), val.expandtabs(1))
  1278. open(self.getUserCacheFilename(), 'w').write(s)
  1279. self.userMapFromPerforceServer = True
  1280. def loadUserMapFromCache(self):
  1281. self.users = {}
  1282. self.userMapFromPerforceServer = False
  1283. try:
  1284. cache = open(self.getUserCacheFilename(), 'r')
  1285. lines = cache.readlines()
  1286. cache.close()
  1287. for line in lines:
  1288. entry = line.strip().split("\t")
  1289. self.users[entry[0]] = entry[1]
  1290. except IOError:
  1291. self.getUserMapFromPerforceServer()
  1292. class P4Debug(Command):
  1293. def __init__(self):
  1294. Command.__init__(self)
  1295. self.options = []
  1296. self.description = "A tool to debug the output of p4 -G."
  1297. self.needsGit = False
  1298. def run(self, args):
  1299. j = 0
  1300. for output in p4CmdList(args):
  1301. print('Element: %d' % j)
  1302. j += 1
  1303. print(output)
  1304. return True
  1305. class P4RollBack(Command):
  1306. def __init__(self):
  1307. Command.__init__(self)
  1308. self.options = [
  1309. optparse.make_option("--local", dest="rollbackLocalBranches", action="store_true")
  1310. ]
  1311. self.description = "A tool to debug the multi-branch import. Don't use :)"
  1312. self.rollbackLocalBranches = False
  1313. def run(self, args):
  1314. if len(args) != 1:
  1315. return False
  1316. maxChange = int(args[0])
  1317. if "p4ExitCode" in p4Cmd("changes -m 1"):
  1318. die("Problems executing p4");
  1319. if self.rollbackLocalBranches:
  1320. refPrefix = "refs/heads/"
  1321. lines = read_pipe_lines("git rev-parse --symbolic --branches")
  1322. else:
  1323. refPrefix = "refs/remotes/"
  1324. lines = read_pipe_lines("git rev-parse --symbolic --remotes")
  1325. for line in lines:
  1326. if self.rollbackLocalBranches or (line.startswith("p4/") and line != "p4/HEAD\n"):
  1327. line = line.strip()
  1328. ref = refPrefix + line
  1329. log = extractLogMessageFromGitCommit(ref)
  1330. settings = extractSettingsGitLog(log)
  1331. depotPaths = settings['depot-paths']
  1332. change = settings['change']
  1333. changed = False
  1334. if len(p4Cmd("changes -m 1 " + ' '.join (['%s...@%s' % (p, maxChange)
  1335. for p in depotPaths]))) == 0:
  1336. print("Branch %s did not exist at change %s, deleting." % (ref, maxChange))
  1337. system("git update-ref -d %s `git rev-parse %s`" % (ref, ref))
  1338. continue
  1339. while change and int(change) > maxChange:
  1340. changed = True
  1341. if self.verbose:
  1342. print("%s is at %s ; rewinding towards %s" % (ref, change, maxChange))
  1343. system("git update-ref %s \"%s^\"" % (ref, ref))
  1344. log = extractLogMessageFromGitCommit(ref)
  1345. settings = extractSettingsGitLog(log)
  1346. depotPaths = settings['depot-paths']
  1347. change = settings['change']
  1348. if changed:
  1349. print("%s rewound to %s" % (ref, change))
  1350. return True
  1351. class P4Submit(Command, P4UserMap):
  1352. conflict_behavior_choices = ("ask", "skip", "quit")
  1353. def __init__(self):
  1354. Command.__init__(self)
  1355. P4UserMap.__init__(self)
  1356. self.options = [
  1357. optparse.make_option("--origin", dest="origin"),
  1358. optparse.make_option("-M", dest="detectRenames", action="store_true"),
  1359. # preserve the user, requires relevant p4 permissions
  1360. optparse.make_option("--preserve-user", dest="preserveUser", action="store_true"),
  1361. optparse.make_option("--export-labels", dest="exportLabels", action="store_true"),
  1362. optparse.make_option("--dry-run", "-n", dest="dry_run", action="store_true"),
  1363. optparse.make_option("--prepare-p4-only", dest="prepare_p4_only", action="store_true"),
  1364. optparse.make_option("--conflict", dest="conflict_behavior",
  1365. choices=self.conflict_behavior_choices),
  1366. optparse.make_option("--branch", dest="branch"),
  1367. optparse.make_option("--shelve", dest="shelve", action="store_true",
  1368. help="Shelve instead of submit. Shelved files are reverted, "
  1369. "restoring the workspace to the state before the shelve"),
  1370. optparse.make_option("--update-shelve", dest="update_shelve", action="append", type="int",
  1371. metavar="CHANGELIST",
  1372. help="update an existing shelved changelist, implies --shelve, "
  1373. "repeat in-order for multiple shelved changelists"),
  1374. optparse.make_option("--commit", dest="commit", metavar="COMMIT",
  1375. help="submit only the specified commit(s), one commit or xxx..xxx"),
  1376. optparse.make_option("--disable-rebase", dest="disable_rebase", action="store_true",
  1377. help="Disable rebase after submit is completed. Can be useful if you "
  1378. "work from a local git branch that is not master"),
  1379. optparse.make_option("--disable-p4sync", dest="disable_p4sync", action="store_true",
  1380. help="Skip Perforce sync of p4/master after submit or shelve"),
  1381. optparse.make_option("--no-verify", dest="no_verify", action="store_true",
  1382. help="Bypass p4-pre-submit and p4-changelist hooks"),
  1383. ]
  1384. self.description = """Submit changes from git to the perforce depot.\n
  1385. The `p4-pre-submit` hook is executed if it exists and is executable. It
  1386. can be bypassed with the `--no-verify` command line option. The hook takes
  1387. no parameters and nothing from standard input. Exiting with a non-zero status
  1388. from this script prevents `git-p4 submit` from launching.
  1389. One usage scenario is to run unit tests in the hook.
  1390. The `p4-prepare-changelist` hook is executed right after preparing the default
  1391. changelist message and before the editor is started. It takes one parameter,
  1392. the name of the file that contains the changelist text. Exiting with a non-zero
  1393. status from the script will abort the process.
  1394. The purpose of the hook is to edit the message file in place, and it is not
  1395. supressed by the `--no-verify` option. This hook is called even if
  1396. `--prepare-p4-only` is set.
  1397. The `p4-changelist` hook is executed after the changelist message has been
  1398. edited by the user. It can be bypassed with the `--no-verify` option. It
  1399. takes a single parameter, the name of the file that holds the proposed
  1400. changelist text. Exiting with a non-zero status causes the command to abort.
  1401. The hook is allowed to edit the changelist file and can be used to normalize
  1402. the text into some project standard format. It can also be used to refuse the
  1403. Submit after inspect the message file.
  1404. The `p4-post-changelist` hook is invoked after the submit has successfully
  1405. occured in P4. It takes no parameters and is meant primarily for notification
  1406. and cannot affect the outcome of the git p4 submit action.
  1407. """
  1408. self.usage += " [name of git branch to submit into perforce depot]"
  1409. self.origin = ""
  1410. self.detectRenames = False
  1411. self.preserveUser = gitConfigBool("git-p4.preserveUser")
  1412. self.dry_run = False
  1413. self.shelve = False
  1414. self.update_shelve = list()
  1415. self.commit = ""
  1416. self.disable_rebase = gitConfigBool("git-p4.disableRebase")
  1417. self.disable_p4sync = gitConfigBool("git-p4.disableP4Sync")
  1418. self.prepare_p4_only = False
  1419. self.conflict_behavior = None
  1420. self.isWindows = (platform.system() == "Windows")
  1421. self.exportLabels = False
  1422. self.p4HasMoveCommand = p4_has_move_command()
  1423. self.branch = None
  1424. self.no_verify = False
  1425. if gitConfig('git-p4.largeFileSystem'):
  1426. die("Large file system not supported for git-p4 submit command. Please remove it from config.")
  1427. def check(self):
  1428. if len(p4CmdList("opened ...")) > 0:
  1429. die("You have files opened with perforce! Close them before starting the sync.")
  1430. def separate_jobs_from_description(self, message):
  1431. """Extract and return a possible Jobs field in the commit
  1432. message. It goes into a separate section in the p4 change
  1433. specification.
  1434. A jobs line starts with "Jobs:" and looks like a new field
  1435. in a form. Values are white-space separated on the same
  1436. line or on following lines that start with a tab.
  1437. This does not parse and extract the full git commit message
  1438. like a p4 form. It just sees the Jobs: line as a marker
  1439. to pass everything from then on directly into the p4 form,
  1440. but outside the description section.
  1441. Return a tuple (stripped log message, jobs string)."""
  1442. m = re.search(r'^Jobs:', message, re.MULTILINE)
  1443. if m is None:
  1444. return (message, None)
  1445. jobtext = message[m.start():]
  1446. stripped_message = message[:m.start()].rstrip()
  1447. return (stripped_message, jobtext)
  1448. def prepareLogMessage(self, template, message, jobs):
  1449. """Edits the template returned from "p4 change -o" to insert
  1450. the message in the Description field, and the jobs text in
  1451. the Jobs field."""
  1452. result = ""
  1453. inDescriptionSection = False
  1454. for line in template.split("\n"):
  1455. if line.startswith("#"):
  1456. result += line + "\n"
  1457. continue
  1458. if inDescriptionSection:
  1459. if line.startswith("Files:") or line.startswith("Jobs:"):
  1460. inDescriptionSection = False
  1461. # insert Jobs section
  1462. if jobs:
  1463. result += jobs + "\n"
  1464. else:
  1465. continue
  1466. else:
  1467. if line.startswith("Description:"):
  1468. inDescriptionSection = True
  1469. line += "\n"
  1470. for messageLine in message.split("\n"):
  1471. line += "\t" + messageLine + "\n"
  1472. result += line + "\n"
  1473. return result
  1474. def patchRCSKeywords(self, file, pattern):
  1475. # Attempt to zap the RCS keywords in a p4 controlled file matching the given pattern
  1476. (handle, outFileName) = tempfile.mkstemp(dir='.')
  1477. try:
  1478. outFile = os.fdopen(handle, "w+")
  1479. inFile = open(file, "r")
  1480. regexp = re.compile(pattern, re.VERBOSE)
  1481. for line in inFile.readlines():
  1482. line = regexp.sub(r'$\1$', line)
  1483. outFile.write(line)
  1484. inFile.close()
  1485. outFile.close()
  1486. # Forcibly overwrite the original file
  1487. os.unlink(file)
  1488. shutil.move(outFileName, file)
  1489. except:
  1490. # cleanup our temporary file
  1491. os.unlink(outFileName)
  1492. print("Failed to strip RCS keywords in %s" % file)
  1493. raise
  1494. print("Patched up RCS keywords in %s" % file)
  1495. def p4UserForCommit(self,id):
  1496. # Return the tuple (perforce user,git email) for a given git commit id
  1497. self.getUserMapFromPerforceServer()
  1498. gitEmail = read_pipe(["git", "log", "--max-count=1",
  1499. "--format=%ae", id])
  1500. gitEmail = gitEmail.strip()
  1501. if gitEmail not in self.emails:
  1502. return (None,gitEmail)
  1503. else:
  1504. return (self.emails[gitEmail],gitEmail)
  1505. def checkValidP4Users(self,commits):
  1506. # check if any git authors cannot be mapped to p4 users
  1507. for id in commits:
  1508. (user,email) = self.p4UserForCommit(id)
  1509. if not user:
  1510. msg = "Cannot find p4 user for email %s in commit %s." % (email, id)
  1511. if gitConfigBool("git-p4.allowMissingP4Users"):
  1512. print("%s" % msg)
  1513. else:
  1514. die("Error: %s\nSet git-p4.allowMissingP4Users to true to allow this." % msg)
  1515. def lastP4Changelist(self):
  1516. # Get back the last changelist number submitted in this client spec. This
  1517. # then gets used to patch up the username in the change. If the same
  1518. # client spec is being used by multiple processes then this might go
  1519. # wrong.
  1520. results = p4CmdList("client -o") # find the current client
  1521. client = None
  1522. for r in results:
  1523. if 'Client' in r:
  1524. client = r['Client']
  1525. break
  1526. if not client:
  1527. die("could not get client spec")
  1528. results = p4CmdList(["changes", "-c", client, "-m", "1"])
  1529. for r in results:
  1530. if 'change' in r:
  1531. return r['change']
  1532. die("Could not get changelist number for last submit - cannot patch up user details")
  1533. def modifyChangelistUser(self, changelist, newUser):
  1534. # fixup the user field of a changelist after it has been submitted.
  1535. changes = p4CmdList("change -o %s" % changelist)
  1536. if len(changes) != 1:
  1537. die("Bad output from p4 change modifying %s to user %s" %
  1538. (changelist, newUser))
  1539. c = changes[0]
  1540. if c['User'] == newUser: return # nothing to do
  1541. c['User'] = newUser
  1542. # p4 does not understand format version 3 and above
  1543. input = marshal.dumps(c, 2)
  1544. result = p4CmdList("change -f -i", stdin=input)
  1545. for r in result:
  1546. if 'code' in r:
  1547. if r['code'] == 'error':
  1548. die("Could not modify user field of changelist %s to %s:%s" % (changelist, newUser, r['data']))
  1549. if 'data' in r:
  1550. print("Updated user field for changelist %s to %s" % (changelist, newUser))
  1551. return
  1552. die("Could not modify user field of changelist %s to %s" % (changelist, newUser))
  1553. def canChangeChangelists(self):
  1554. # check to see if we have p4 admin or super-user permissions, either of
  1555. # which are required to modify changelists.
  1556. results = p4CmdList(["protects", self.depotPath])
  1557. for r in results:
  1558. if 'perm' in r:
  1559. if r['perm'] == 'admin':
  1560. return 1
  1561. if r['perm'] == 'super':
  1562. return 1
  1563. return 0
  1564. def prepareSubmitTemplate(self, changelist=None):
  1565. """Run "p4 change -o" to grab a change specification template.
  1566. This does not use "p4 -G", as it is nice to keep the submission
  1567. template in original order, since a human might edit it.
  1568. Remove lines in the Files section that show changes to files
  1569. outside the depot path we're committing into."""
  1570. [upstream, settings] = findUpstreamBranchPoint()
  1571. template = """\
  1572. # A Perforce Change Specification.
  1573. #
  1574. # Change: The change number. 'new' on a new changelist.
  1575. # Date: The date this specification was last modified.
  1576. # Client: The client on which the changelist was created. Read-only.
  1577. # User: The user who created the changelist.
  1578. # Status: Either 'pending' or 'submitted'. Read-only.
  1579. # Type: Either 'public' or 'restricted'. Default is 'public'.
  1580. # Description: Comments about the changelist. Required.
  1581. # Jobs: What opened jobs are to be closed by this changelist.
  1582. # You may delete jobs from this list. (New changelists only.)
  1583. # Files: What opened files from the default changelist are to be added
  1584. # to this changelist. You may delete files from this list.
  1585. # (New changelists only.)
  1586. """
  1587. files_list = []
  1588. inFilesSection = False
  1589. change_entry = None
  1590. args = ['change', '-o']
  1591. if changelist:
  1592. args.append(str(changelist))
  1593. for entry in p4CmdList(args):
  1594. if 'code' not in entry:
  1595. continue
  1596. if entry['code'] == 'stat':
  1597. change_entry = entry
  1598. break
  1599. if not change_entry:
  1600. die('Failed to decode output of p4 change -o')
  1601. for key, value in change_entry.items():
  1602. if key.startswith('File'):
  1603. if 'depot-paths' in settings:
  1604. if not [p for p in settings['depot-paths']
  1605. if p4PathStartsWith(value, p)]:
  1606. continue
  1607. else:
  1608. if not p4PathStartsWith(value, self.depotPath):
  1609. continue
  1610. files_list.append(value)
  1611. continue
  1612. # Output in the order expected by prepareLogMessage
  1613. for key in ['Change', 'Client', 'User', 'Status', 'Description', 'Jobs']:
  1614. if key not in change_entry:
  1615. continue
  1616. template += '\n'
  1617. template += key + ':'
  1618. if key == 'Description':
  1619. template += '\n'
  1620. for field_line in change_entry[key].splitlines():
  1621. template += '\t'+field_line+'\n'
  1622. if len(files_list) > 0:
  1623. template += '\n'
  1624. template += 'Files:\n'
  1625. for path in files_list:
  1626. template += '\t'+path+'\n'
  1627. return template
  1628. def edit_template(self, template_file):
  1629. """Invoke the editor to let the user change the submission
  1630. message. Return true if okay to continue with the submit."""
  1631. # if configured to skip the editing part, just submit
  1632. if gitConfigBool("git-p4.skipSubmitEdit"):
  1633. return True
  1634. # look at the modification time, to check later if the user saved
  1635. # the file
  1636. mtime = os.stat(template_file).st_mtime
  1637. # invoke the editor
  1638. if "P4EDITOR" in os.environ and (os.environ.get("P4EDITOR") != ""):
  1639. editor = os.environ.get("P4EDITOR")
  1640. else:
  1641. editor = read_pipe("git var GIT_EDITOR").strip()
  1642. system(["sh", "-c", ('%s "$@"' % editor), editor, template_file])
  1643. # If the file was not saved, prompt to see if this patch should
  1644. # be skipped. But skip this verification step if configured so.
  1645. if gitConfigBool("git-p4.skipSubmitEditCheck"):
  1646. return True
  1647. # modification time updated means user saved the file
  1648. if os.stat(template_file).st_mtime > mtime:
  1649. return True
  1650. response = prompt("Submit template unchanged. Submit anyway? [y]es, [n]o (skip this patch) ")
  1651. if response == 'y':
  1652. return True
  1653. if response == 'n':
  1654. return False
  1655. def get_diff_description(self, editedFiles, filesToAdd, symlinks):
  1656. # diff
  1657. if "P4DIFF" in os.environ:
  1658. del(os.environ["P4DIFF"])
  1659. diff = ""
  1660. for editedFile in editedFiles:
  1661. diff += p4_read_pipe(['diff', '-du',
  1662. wildcard_encode(editedFile)])
  1663. # new file diff
  1664. newdiff = ""
  1665. for newFile in filesToAdd:
  1666. newdiff += "==== new file ====\n"
  1667. newdiff += "--- /dev/null\n"
  1668. newdiff += "+++ %s\n" % newFile
  1669. is_link = os.path.islink(newFile)
  1670. expect_link = newFile in symlinks
  1671. if is_link and expect_link:
  1672. newdiff += "+%s\n" % os.readlink(newFile)
  1673. else:
  1674. f = open(newFile, "r")
  1675. for line in f.readlines():
  1676. newdiff += "+" + line
  1677. f.close()
  1678. return (diff + newdiff).replace('\r\n', '\n')
  1679. def applyCommit(self, id):
  1680. """Apply one commit, return True if it succeeded."""
  1681. print("Applying", read_pipe(["git", "show", "-s",
  1682. "--format=format:%h %s", id]))
  1683. (p4User, gitEmail) = self.p4UserForCommit(id)
  1684. diff = read_pipe_lines("git diff-tree -r %s \"%s^\" \"%s\"" % (self.diffOpts, id, id))
  1685. filesToAdd = set()
  1686. filesToChangeType = set()
  1687. filesToDelete = set()
  1688. editedFiles = set()
  1689. pureRenameCopy = set()
  1690. symlinks = set()
  1691. filesToChangeExecBit = {}
  1692. all_files = list()
  1693. for line in diff:
  1694. diff = parseDiffTreeEntry(line)
  1695. modifier = diff['status']
  1696. path = diff['src']
  1697. all_files.append(path)
  1698. if modifier == "M":
  1699. p4_edit(path)
  1700. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1701. filesToChangeExecBit[path] = diff['dst_mode']
  1702. editedFiles.add(path)
  1703. elif modifier == "A":
  1704. filesToAdd.add(path)
  1705. filesToChangeExecBit[path] = diff['dst_mode']
  1706. if path in filesToDelete:
  1707. filesToDelete.remove(path)
  1708. dst_mode = int(diff['dst_mode'], 8)
  1709. if dst_mode == 0o120000:
  1710. symlinks.add(path)
  1711. elif modifier == "D":
  1712. filesToDelete.add(path)
  1713. if path in filesToAdd:
  1714. filesToAdd.remove(path)
  1715. elif modifier == "C":
  1716. src, dest = diff['src'], diff['dst']
  1717. all_files.append(dest)
  1718. p4_integrate(src, dest)
  1719. pureRenameCopy.add(dest)
  1720. if diff['src_sha1'] != diff['dst_sha1']:
  1721. p4_edit(dest)
  1722. pureRenameCopy.discard(dest)
  1723. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1724. p4_edit(dest)
  1725. pureRenameCopy.discard(dest)
  1726. filesToChangeExecBit[dest] = diff['dst_mode']
  1727. if self.isWindows:
  1728. # turn off read-only attribute
  1729. os.chmod(dest, stat.S_IWRITE)
  1730. os.unlink(dest)
  1731. editedFiles.add(dest)
  1732. elif modifier == "R":
  1733. src, dest = diff['src'], diff['dst']
  1734. all_files.append(dest)
  1735. if self.p4HasMoveCommand:
  1736. p4_edit(src) # src must be open before move
  1737. p4_move(src, dest) # opens for (move/delete, move/add)
  1738. else:
  1739. p4_integrate(src, dest)
  1740. if diff['src_sha1'] != diff['dst_sha1']:
  1741. p4_edit(dest)
  1742. else:
  1743. pureRenameCopy.add(dest)
  1744. if isModeExecChanged(diff['src_mode'], diff['dst_mode']):
  1745. if not self.p4HasMoveCommand:
  1746. p4_edit(dest) # with move: already open, writable
  1747. filesToChangeExecBit[dest] = diff['dst_mode']
  1748. if not self.p4HasMoveCommand:
  1749. if self.isWindows:
  1750. os.chmod(dest, stat.S_IWRITE)
  1751. os.unlink(dest)
  1752. filesToDelete.add(src)
  1753. editedFiles.add(dest)
  1754. elif modifier == "T":
  1755. filesToChangeType.add(path)
  1756. else:
  1757. die("unknown modifier %s for %s" % (modifier, path))
  1758. diffcmd = "git diff-tree --full-index -p \"%s\"" % (id)
  1759. patchcmd = diffcmd + " | git apply "
  1760. tryPatchCmd = patchcmd + "--check -"
  1761. applyPatchCmd = patchcmd + "--check --apply -"
  1762. patch_succeeded = True
  1763. if verbose:
  1764. print("TryPatch: %s" % tryPatchCmd)
  1765. if os.system(tryPatchCmd) != 0:
  1766. fixed_rcs_keywords = False
  1767. patch_succeeded = False
  1768. print("Unfortunately applying the change failed!")
  1769. # Patch failed, maybe it's just RCS keyword woes. Look through
  1770. # the patch to see if that's possible.
  1771. if gitConfigBool("git-p4.attemptRCSCleanup"):
  1772. file = None
  1773. pattern = None
  1774. kwfiles = {}
  1775. for file in editedFiles | filesToDelete:
  1776. # did this file's delta contain RCS keywords?
  1777. pattern = p4_keywords_regexp_for_file(file)
  1778. if pattern:
  1779. # this file is a possibility...look for RCS keywords.
  1780. regexp = re.compile(pattern, re.VERBOSE)
  1781. for line in read_pipe_lines(["git", "diff", "%s^..%s" % (id, id), file]):
  1782. if regexp.search(line):
  1783. if verbose:
  1784. print("got keyword match on %s in %s in %s" % (pattern, line, file))
  1785. kwfiles[file] = pattern
  1786. break
  1787. for file in kwfiles:
  1788. if verbose:
  1789. print("zapping %s with %s" % (line,pattern))
  1790. # File is being deleted, so not open in p4. Must
  1791. # disable the read-only bit on windows.
  1792. if self.isWindows and file not in editedFiles:
  1793. os.chmod(file, stat.S_IWRITE)
  1794. self.patchRCSKeywords(file, kwfiles[file])
  1795. fixed_rcs_keywords = True
  1796. if fixed_rcs_keywords:
  1797. print("Retrying the patch with RCS keywords cleaned up")
  1798. if os.system(tryPatchCmd) == 0:
  1799. patch_succeeded = True
  1800. print("Patch succeesed this time with RCS keywords cleaned")
  1801. if not patch_succeeded:
  1802. for f in editedFiles:
  1803. p4_revert(f)
  1804. return False
  1805. #
  1806. # Apply the patch for real, and do add/delete/+x handling.
  1807. #
  1808. system(applyPatchCmd)
  1809. for f in filesToChangeType:
  1810. p4_edit(f, "-t", "auto")
  1811. for f in filesToAdd:
  1812. p4_add(f)
  1813. for f in filesToDelete:
  1814. p4_revert(f)
  1815. p4_delete(f)
  1816. # Set/clear executable bits
  1817. for f in filesToChangeExecBit.keys():
  1818. mode = filesToChangeExecBit[f]
  1819. setP4ExecBit(f, mode)
  1820. update_shelve = 0
  1821. if len(self.update_shelve) > 0:
  1822. update_shelve = self.update_shelve.pop(0)
  1823. p4_reopen_in_change(update_shelve, all_files)
  1824. #
  1825. # Build p4 change description, starting with the contents
  1826. # of the git commit message.
  1827. #
  1828. logMessage = extractLogMessageFromGitCommit(id)
  1829. logMessage = logMessage.strip()
  1830. (logMessage, jobs) = self.separate_jobs_from_description(logMessage)
  1831. template = self.prepareSubmitTemplate(update_shelve)
  1832. submitTemplate = self.prepareLogMessage(template, logMessage, jobs)
  1833. if self.preserveUser:
  1834. submitTemplate += "\n######## Actual user %s, modified after commit\n" % p4User
  1835. if self.checkAuthorship and not self.p4UserIsMe(p4User):
  1836. submitTemplate += "######## git author %s does not match your p4 account.\n" % gitEmail
  1837. submitTemplate += "######## Use option --preserve-user to modify authorship.\n"
  1838. submitTemplate += "######## Variable git-p4.skipUserNameCheck hides this message.\n"
  1839. separatorLine = "######## everything below this line is just the diff #######\n"
  1840. if not self.prepare_p4_only:
  1841. submitTemplate += separatorLine
  1842. submitTemplate += self.get_diff_description(editedFiles, filesToAdd, symlinks)
  1843. (handle, fileName) = tempfile.mkstemp()
  1844. tmpFile = os.fdopen(handle, "w+b")
  1845. if self.isWindows:
  1846. submitTemplate = submitTemplate.replace("\n", "\r\n")
  1847. tmpFile.write(encode_text_stream(submitTemplate))
  1848. tmpFile.close()
  1849. submitted = False
  1850. try:
  1851. # Allow the hook to edit the changelist text before presenting it
  1852. # to the user.
  1853. if not run_git_hook("p4-prepare-changelist", [fileName]):
  1854. return False
  1855. if self.prepare_p4_only:
  1856. #
  1857. # Leave the p4 tree prepared, and the submit template around
  1858. # and let the user decide what to do next
  1859. #
  1860. submitted = True
  1861. print("")
  1862. print("P4 workspace prepared for submission.")
  1863. print("To submit or revert, go to client workspace")
  1864. print(" " + self.clientPath)
  1865. print("")
  1866. print("To submit, use \"p4 submit\" to write a new description,")
  1867. print("or \"p4 submit -i <%s\" to use the one prepared by" \
  1868. " \"git p4\"." % fileName)
  1869. print("You can delete the file \"%s\" when finished." % fileName)
  1870. if self.preserveUser and p4User and not self.p4UserIsMe(p4User):
  1871. print("To preserve change ownership by user %s, you must\n" \
  1872. "do \"p4 change -f <change>\" after submitting and\n" \
  1873. "edit the User field.")
  1874. if pureRenameCopy:
  1875. print("After submitting, renamed files must be re-synced.")
  1876. print("Invoke \"p4 sync -f\" on each of these files:")
  1877. for f in pureRenameCopy:
  1878. print(" " + f)
  1879. print("")
  1880. print("To revert the changes, use \"p4 revert ...\", and delete")
  1881. print("the submit template file \"%s\"" % fileName)
  1882. if filesToAdd:
  1883. print("Since the commit adds new files, they must be deleted:")
  1884. for f in filesToAdd:
  1885. print(" " + f)
  1886. print("")
  1887. sys.stdout.flush()
  1888. return True
  1889. if self.edit_template(fileName):
  1890. if not self.no_verify:
  1891. if not run_git_hook("p4-changelist", [fileName]):
  1892. print("The p4-changelist hook failed.")
  1893. sys.stdout.flush()
  1894. return False
  1895. # read the edited message and submit
  1896. tmpFile = open(fileName, "rb")
  1897. message = decode_text_stream(tmpFile.read())
  1898. tmpFile.close()
  1899. if self.isWindows:
  1900. message = message.replace("\r\n", "\n")
  1901. if message.find(separatorLine) != -1:
  1902. submitTemplate = message[:message.index(separatorLine)]
  1903. else:
  1904. submitTemplate = message
  1905. if len(submitTemplate.strip()) == 0:
  1906. print("Changelist is empty, aborting this changelist.")
  1907. sys.stdout.flush()
  1908. return False
  1909. if update_shelve:
  1910. p4_write_pipe(['shelve', '-r', '-i'], submitTemplate)
  1911. elif self.shelve:
  1912. p4_write_pipe(['shelve', '-i'], submitTemplate)
  1913. else:
  1914. p4_write_pipe(['submit', '-i'], submitTemplate)
  1915. # The rename/copy happened by applying a patch that created a
  1916. # new file. This leaves it writable, which confuses p4.
  1917. for f in pureRenameCopy:
  1918. p4_sync(f, "-f")
  1919. if self.preserveUser:
  1920. if p4User:
  1921. # Get last changelist number. Cannot easily get it from
  1922. # the submit command output as the output is
  1923. # unmarshalled.
  1924. changelist = self.lastP4Changelist()
  1925. self.modifyChangelistUser(changelist, p4User)
  1926. submitted = True
  1927. run_git_hook("p4-post-changelist")
  1928. finally:
  1929. # Revert changes if we skip this patch
  1930. if not submitted or self.shelve:
  1931. if self.shelve:
  1932. print ("Reverting shelved files.")
  1933. else:
  1934. print ("Submission cancelled, undoing p4 changes.")
  1935. sys.stdout.flush()
  1936. for f in editedFiles | filesToDelete:
  1937. p4_revert(f)
  1938. for f in filesToAdd:
  1939. p4_revert(f)
  1940. os.remove(f)
  1941. if not self.prepare_p4_only:
  1942. os.remove(fileName)
  1943. return submitted
  1944. # Export git tags as p4 labels. Create a p4 label and then tag
  1945. # with that.
  1946. def exportGitTags(self, gitTags):
  1947. validLabelRegexp = gitConfig("git-p4.labelExportRegexp")
  1948. if len(validLabelRegexp) == 0:
  1949. validLabelRegexp = defaultLabelRegexp
  1950. m = re.compile(validLabelRegexp)
  1951. for name in gitTags:
  1952. if not m.match(name):
  1953. if verbose:
  1954. print("tag %s does not match regexp %s" % (name, validLabelRegexp))
  1955. continue
  1956. # Get the p4 commit this corresponds to
  1957. logMessage = extractLogMessageFromGitCommit(name)
  1958. values = extractSettingsGitLog(logMessage)
  1959. if 'change' not in values:
  1960. # a tag pointing to something not sent to p4; ignore
  1961. if verbose:
  1962. print("git tag %s does not give a p4 commit" % name)
  1963. continue
  1964. else:
  1965. changelist = values['change']
  1966. # Get the tag details.
  1967. inHeader = True
  1968. isAnnotated = False
  1969. body = []
  1970. for l in read_pipe_lines(["git", "cat-file", "-p", name]):
  1971. l = l.strip()
  1972. if inHeader:
  1973. if re.match(r'tag\s+', l):
  1974. isAnnotated = True
  1975. elif re.match(r'\s*$', l):
  1976. inHeader = False
  1977. continue
  1978. else:
  1979. body.append(l)
  1980. if not isAnnotated:
  1981. body = ["lightweight tag imported by git p4\n"]
  1982. # Create the label - use the same view as the client spec we are using
  1983. clientSpec = getClientSpec()
  1984. labelTemplate = "Label: %s\n" % name
  1985. labelTemplate += "Description:\n"
  1986. for b in body:
  1987. labelTemplate += "\t" + b + "\n"
  1988. labelTemplate += "View:\n"
  1989. for depot_side in clientSpec.mappings:
  1990. labelTemplate += "\t%s\n" % depot_side
  1991. if self.dry_run:
  1992. print("Would create p4 label %s for tag" % name)
  1993. elif self.prepare_p4_only:
  1994. print("Not creating p4 label %s for tag due to option" \
  1995. " --prepare-p4-only" % name)
  1996. else:
  1997. p4_write_pipe(["label", "-i"], labelTemplate)
  1998. # Use the label
  1999. p4_system(["tag", "-l", name] +
  2000. ["%s@%s" % (depot_side, changelist) for depot_side in clientSpec.mappings])
  2001. if verbose:
  2002. print("created p4 label for tag %s" % name)
  2003. def run(self, args):
  2004. if len(args) == 0:
  2005. self.master = currentGitBranch()
  2006. elif len(args) == 1:
  2007. self.master = args[0]
  2008. if not branchExists(self.master):
  2009. die("Branch %s does not exist" % self.master)
  2010. else:
  2011. return False
  2012. for i in self.update_shelve:
  2013. if i <= 0:
  2014. sys.exit("invalid changelist %d" % i)
  2015. if self.master:
  2016. allowSubmit = gitConfig("git-p4.allowSubmit")
  2017. if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
  2018. die("%s is not in git-p4.allowSubmit" % self.master)
  2019. [upstream, settings] = findUpstreamBranchPoint()
  2020. self.depotPath = settings['depot-paths'][0]
  2021. if len(self.origin) == 0:
  2022. self.origin = upstream
  2023. if len(self.update_shelve) > 0:
  2024. self.shelve = True
  2025. if self.preserveUser:
  2026. if not self.canChangeChangelists():
  2027. die("Cannot preserve user names without p4 super-user or admin permissions")
  2028. # if not set from the command line, try the config file
  2029. if self.conflict_behavior is None:
  2030. val = gitConfig("git-p4.conflict")
  2031. if val:
  2032. if val not in self.conflict_behavior_choices:
  2033. die("Invalid value '%s' for config git-p4.conflict" % val)
  2034. else:
  2035. val = "ask"
  2036. self.conflict_behavior = val
  2037. if self.verbose:
  2038. print("Origin branch is " + self.origin)
  2039. if len(self.depotPath) == 0:
  2040. print("Internal error: cannot locate perforce depot path from existing branches")
  2041. sys.exit(128)
  2042. self.useClientSpec = False
  2043. if gitConfigBool("git-p4.useclientspec"):
  2044. self.useClientSpec = True
  2045. if self.useClientSpec:
  2046. self.clientSpecDirs = getClientSpec()
  2047. # Check for the existence of P4 branches
  2048. branchesDetected = (len(p4BranchesInGit().keys()) > 1)
  2049. if self.useClientSpec and not branchesDetected:
  2050. # all files are relative to the client spec
  2051. self.clientPath = getClientRoot()
  2052. else:
  2053. self.clientPath = p4Where(self.depotPath)
  2054. if self.clientPath == "":
  2055. die("Error: Cannot locate perforce checkout of %s in client view" % self.depotPath)
  2056. print("Perforce checkout for depot path %s located at %s" % (self.depotPath, self.clientPath))
  2057. self.oldWorkingDirectory = os.getcwd()
  2058. # ensure the clientPath exists
  2059. new_client_dir = False
  2060. if not os.path.exists(self.clientPath):
  2061. new_client_dir = True
  2062. os.makedirs(self.clientPath)
  2063. chdir(self.clientPath, is_client_path=True)
  2064. if self.dry_run:
  2065. print("Would synchronize p4 checkout in %s" % self.clientPath)
  2066. else:
  2067. print("Synchronizing p4 checkout...")
  2068. if new_client_dir:
  2069. # old one was destroyed, and maybe nobody told p4
  2070. p4_sync("...", "-f")
  2071. else:
  2072. p4_sync("...")
  2073. self.check()
  2074. commits = []
  2075. if self.master:
  2076. committish = self.master
  2077. else:
  2078. committish = 'HEAD'
  2079. if self.commit != "":
  2080. if self.commit.find("..") != -1:
  2081. limits_ish = self.commit.split("..")
  2082. for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (limits_ish[0], limits_ish[1])]):
  2083. commits.append(line.strip())
  2084. commits.reverse()
  2085. else:
  2086. commits.append(self.commit)
  2087. else:
  2088. for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, committish)]):
  2089. commits.append(line.strip())
  2090. commits.reverse()
  2091. if self.preserveUser or gitConfigBool("git-p4.skipUserNameCheck"):
  2092. self.checkAuthorship = False
  2093. else:
  2094. self.checkAuthorship = True
  2095. if self.preserveUser:
  2096. self.checkValidP4Users(commits)
  2097. #
  2098. # Build up a set of options to be passed to diff when
  2099. # submitting each commit to p4.
  2100. #
  2101. if self.detectRenames:
  2102. # command-line -M arg
  2103. self.diffOpts = "-M"
  2104. else:
  2105. # If not explicitly set check the config variable
  2106. detectRenames = gitConfig("git-p4.detectRenames")
  2107. if detectRenames.lower() == "false" or detectRenames == "":
  2108. self.diffOpts = ""
  2109. elif detectRenames.lower() == "true":
  2110. self.diffOpts = "-M"
  2111. else:
  2112. self.diffOpts = "-M%s" % detectRenames
  2113. # no command-line arg for -C or --find-copies-harder, just
  2114. # config variables
  2115. detectCopies = gitConfig("git-p4.detectCopies")
  2116. if detectCopies.lower() == "false" or detectCopies == "":
  2117. pass
  2118. elif detectCopies.lower() == "true":
  2119. self.diffOpts += " -C"
  2120. else:
  2121. self.diffOpts += " -C%s" % detectCopies
  2122. if gitConfigBool("git-p4.detectCopiesHarder"):
  2123. self.diffOpts += " --find-copies-harder"
  2124. num_shelves = len(self.update_shelve)
  2125. if num_shelves > 0 and num_shelves != len(commits):
  2126. sys.exit("number of commits (%d) must match number of shelved changelist (%d)" %
  2127. (len(commits), num_shelves))
  2128. if not self.no_verify:
  2129. try:
  2130. if not run_git_hook("p4-pre-submit"):
  2131. print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nYou can skip " \
  2132. "this pre-submission check by adding\nthe command line option '--no-verify', " \
  2133. "however,\nthis will also skip the p4-changelist hook as well.")
  2134. sys.exit(1)
  2135. except Exception as e:
  2136. print("\nThe p4-pre-submit hook failed, aborting the submit.\n\nThe hook failed "\
  2137. "with the error '{0}'".format(e.message) )
  2138. sys.exit(1)
  2139. #
  2140. # Apply the commits, one at a time. On failure, ask if should
  2141. # continue to try the rest of the patches, or quit.
  2142. #
  2143. if self.dry_run:
  2144. print("Would apply")
  2145. applied = []
  2146. last = len(commits) - 1
  2147. for i, commit in enumerate(commits):
  2148. if self.dry_run:
  2149. print(" ", read_pipe(["git", "show", "-s",
  2150. "--format=format:%h %s", commit]))
  2151. ok = True
  2152. else:
  2153. ok = self.applyCommit(commit)
  2154. if ok:
  2155. applied.append(commit)
  2156. if self.prepare_p4_only:
  2157. if i < last:
  2158. print("Processing only the first commit due to option" \
  2159. " --prepare-p4-only")
  2160. break
  2161. else:
  2162. if i < last:
  2163. # prompt for what to do, or use the option/variable
  2164. if self.conflict_behavior == "ask":
  2165. print("What do you want to do?")
  2166. response = prompt("[s]kip this commit but apply the rest, or [q]uit? ")
  2167. elif self.conflict_behavior == "skip":
  2168. response = "s"
  2169. elif self.conflict_behavior == "quit":
  2170. response = "q"
  2171. else:
  2172. die("Unknown conflict_behavior '%s'" %
  2173. self.conflict_behavior)
  2174. if response == "s":
  2175. print("Skipping this commit, but applying the rest")
  2176. if response == "q":
  2177. print("Quitting")
  2178. break
  2179. chdir(self.oldWorkingDirectory)
  2180. shelved_applied = "shelved" if self.shelve else "applied"
  2181. if self.dry_run:
  2182. pass
  2183. elif self.prepare_p4_only:
  2184. pass
  2185. elif len(commits) == len(applied):
  2186. print("All commits {0}!".format(shelved_applied))
  2187. sync = P4Sync()
  2188. if self.branch:
  2189. sync.branch = self.branch
  2190. if self.disable_p4sync:
  2191. sync.sync_origin_only()
  2192. else:
  2193. sync.run([])
  2194. if not self.disable_rebase:
  2195. rebase = P4Rebase()
  2196. rebase.rebase()
  2197. else:
  2198. if len(applied) == 0:
  2199. print("No commits {0}.".format(shelved_applied))
  2200. else:
  2201. print("{0} only the commits marked with '*':".format(shelved_applied.capitalize()))
  2202. for c in commits:
  2203. if c in applied:
  2204. star = "*"
  2205. else:
  2206. star = " "
  2207. print(star, read_pipe(["git", "show", "-s",
  2208. "--format=format:%h %s", c]))
  2209. print("You will have to do 'git p4 sync' and rebase.")
  2210. if gitConfigBool("git-p4.exportLabels"):
  2211. self.exportLabels = True
  2212. if self.exportLabels:
  2213. p4Labels = getP4Labels(self.depotPath)
  2214. gitTags = getGitTags()
  2215. missingGitTags = gitTags - p4Labels
  2216. self.exportGitTags(missingGitTags)
  2217. # exit with error unless everything applied perfectly
  2218. if len(commits) != len(applied):
  2219. sys.exit(1)
  2220. return True
  2221. class View(object):
  2222. """Represent a p4 view ("p4 help views"), and map files in a
  2223. repo according to the view."""
  2224. def __init__(self, client_name):
  2225. self.mappings = []
  2226. self.client_prefix = "//%s/" % client_name
  2227. # cache results of "p4 where" to lookup client file locations
  2228. self.client_spec_path_cache = {}
  2229. def append(self, view_line):
  2230. """Parse a view line, splitting it into depot and client
  2231. sides. Append to self.mappings, preserving order. This
  2232. is only needed for tag creation."""
  2233. # Split the view line into exactly two words. P4 enforces
  2234. # structure on these lines that simplifies this quite a bit.
  2235. #
  2236. # Either or both words may be double-quoted.
  2237. # Single quotes do not matter.
  2238. # Double-quote marks cannot occur inside the words.
  2239. # A + or - prefix is also inside the quotes.
  2240. # There are no quotes unless they contain a space.
  2241. # The line is already white-space stripped.
  2242. # The two words are separated by a single space.
  2243. #
  2244. if view_line[0] == '"':
  2245. # First word is double quoted. Find its end.
  2246. close_quote_index = view_line.find('"', 1)
  2247. if close_quote_index <= 0:
  2248. die("No first-word closing quote found: %s" % view_line)
  2249. depot_side = view_line[1:close_quote_index]
  2250. # skip closing quote and space
  2251. rhs_index = close_quote_index + 1 + 1
  2252. else:
  2253. space_index = view_line.find(" ")
  2254. if space_index <= 0:
  2255. die("No word-splitting space found: %s" % view_line)
  2256. depot_side = view_line[0:space_index]
  2257. rhs_index = space_index + 1
  2258. # prefix + means overlay on previous mapping
  2259. if depot_side.startswith("+"):
  2260. depot_side = depot_side[1:]
  2261. # prefix - means exclude this path, leave out of mappings
  2262. exclude = False
  2263. if depot_side.startswith("-"):
  2264. exclude = True
  2265. depot_side = depot_side[1:]
  2266. if not exclude:
  2267. self.mappings.append(depot_side)
  2268. def convert_client_path(self, clientFile):
  2269. # chop off //client/ part to make it relative
  2270. if not decode_path(clientFile).startswith(self.client_prefix):
  2271. die("No prefix '%s' on clientFile '%s'" %
  2272. (self.client_prefix, clientFile))
  2273. return clientFile[len(self.client_prefix):]
  2274. def update_client_spec_path_cache(self, files):
  2275. """ Caching file paths by "p4 where" batch query """
  2276. # List depot file paths exclude that already cached
  2277. fileArgs = [f['path'] for f in files if decode_path(f['path']) not in self.client_spec_path_cache]
  2278. if len(fileArgs) == 0:
  2279. return # All files in cache
  2280. where_result = p4CmdList(["-x", "-", "where"], stdin=fileArgs)
  2281. for res in where_result:
  2282. if "code" in res and res["code"] == "error":
  2283. # assume error is "... file(s) not in client view"
  2284. continue
  2285. if "clientFile" not in res:
  2286. die("No clientFile in 'p4 where' output")
  2287. if "unmap" in res:
  2288. # it will list all of them, but only one not unmap-ped
  2289. continue
  2290. depot_path = decode_path(res['depotFile'])
  2291. if gitConfigBool("core.ignorecase"):
  2292. depot_path = depot_path.lower()
  2293. self.client_spec_path_cache[depot_path] = self.convert_client_path(res["clientFile"])
  2294. # not found files or unmap files set to ""
  2295. for depotFile in fileArgs:
  2296. depotFile = decode_path(depotFile)
  2297. if gitConfigBool("core.ignorecase"):
  2298. depotFile = depotFile.lower()
  2299. if depotFile not in self.client_spec_path_cache:
  2300. self.client_spec_path_cache[depotFile] = b''
  2301. def map_in_client(self, depot_path):
  2302. """Return the relative location in the client where this
  2303. depot file should live. Returns "" if the file should
  2304. not be mapped in the client."""
  2305. if gitConfigBool("core.ignorecase"):
  2306. depot_path = depot_path.lower()
  2307. if depot_path in self.client_spec_path_cache:
  2308. return self.client_spec_path_cache[depot_path]
  2309. die( "Error: %s is not found in client spec path" % depot_path )
  2310. return ""
  2311. def cloneExcludeCallback(option, opt_str, value, parser):
  2312. # prepend "/" because the first "/" was consumed as part of the option itself.
  2313. # ("-//depot/A/..." becomes "/depot/A/..." after option parsing)
  2314. parser.values.cloneExclude += ["/" + re.sub(r"\.\.\.$", "", value)]
  2315. class P4Sync(Command, P4UserMap):
  2316. def __init__(self):
  2317. Command.__init__(self)
  2318. P4UserMap.__init__(self)
  2319. self.options = [
  2320. optparse.make_option("--branch", dest="branch"),
  2321. optparse.make_option("--detect-branches", dest="detectBranches", action="store_true"),
  2322. optparse.make_option("--changesfile", dest="changesFile"),
  2323. optparse.make_option("--silent", dest="silent", action="store_true"),
  2324. optparse.make_option("--detect-labels", dest="detectLabels", action="store_true"),
  2325. optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
  2326. optparse.make_option("--import-local", dest="importIntoRemotes", action="store_false",
  2327. help="Import into refs/heads/ , not refs/remotes"),
  2328. optparse.make_option("--max-changes", dest="maxChanges",
  2329. help="Maximum number of changes to import"),
  2330. optparse.make_option("--changes-block-size", dest="changes_block_size", type="int",
  2331. help="Internal block size to use when iteratively calling p4 changes"),
  2332. optparse.make_option("--keep-path", dest="keepRepoPath", action='store_true',
  2333. help="Keep entire BRANCH/DIR/SUBDIR prefix during import"),
  2334. optparse.make_option("--use-client-spec", dest="useClientSpec", action='store_true',
  2335. help="Only sync files that are included in the Perforce Client Spec"),
  2336. optparse.make_option("-/", dest="cloneExclude",
  2337. action="callback", callback=cloneExcludeCallback, type="string",
  2338. help="exclude depot path"),
  2339. ]
  2340. self.description = """Imports from Perforce into a git repository.\n
  2341. example:
  2342. //depot/my/project/ -- to import the current head
  2343. //depot/my/project/@all -- to import everything
  2344. //depot/my/project/@1,6 -- to import only from revision 1 to 6
  2345. (a ... is not needed in the path p4 specification, it's added implicitly)"""
  2346. self.usage += " //depot/path[@revRange]"
  2347. self.silent = False
  2348. self.createdBranches = set()
  2349. self.committedChanges = set()
  2350. self.branch = ""
  2351. self.detectBranches = False
  2352. self.detectLabels = False
  2353. self.importLabels = False
  2354. self.changesFile = ""
  2355. self.syncWithOrigin = True
  2356. self.importIntoRemotes = True
  2357. self.maxChanges = ""
  2358. self.changes_block_size = None
  2359. self.keepRepoPath = False
  2360. self.depotPaths = None
  2361. self.p4BranchesInGit = []
  2362. self.cloneExclude = []
  2363. self.useClientSpec = False
  2364. self.useClientSpec_from_options = False
  2365. self.clientSpecDirs = None
  2366. self.tempBranches = []
  2367. self.tempBranchLocation = "refs/git-p4-tmp"
  2368. self.largeFileSystem = None
  2369. self.suppress_meta_comment = False
  2370. if gitConfig('git-p4.largeFileSystem'):
  2371. largeFileSystemConstructor = globals()[gitConfig('git-p4.largeFileSystem')]
  2372. self.largeFileSystem = largeFileSystemConstructor(
  2373. lambda git_mode, relPath, contents: self.writeToGitStream(git_mode, relPath, contents)
  2374. )
  2375. if gitConfig("git-p4.syncFromOrigin") == "false":
  2376. self.syncWithOrigin = False
  2377. self.depotPaths = []
  2378. self.changeRange = ""
  2379. self.previousDepotPaths = []
  2380. self.hasOrigin = False
  2381. # map from branch depot path to parent branch
  2382. self.knownBranches = {}
  2383. self.initialParents = {}
  2384. self.tz = "%+03d%02d" % (- time.timezone / 3600, ((- time.timezone % 3600) / 60))
  2385. self.labels = {}
  2386. # Force a checkpoint in fast-import and wait for it to finish
  2387. def checkpoint(self):
  2388. self.gitStream.write("checkpoint\n\n")
  2389. self.gitStream.write("progress checkpoint\n\n")
  2390. self.gitStream.flush()
  2391. out = self.gitOutput.readline()
  2392. if self.verbose:
  2393. print("checkpoint finished: " + out)
  2394. def isPathWanted(self, path):
  2395. for p in self.cloneExclude:
  2396. if p.endswith("/"):
  2397. if p4PathStartsWith(path, p):
  2398. return False
  2399. # "-//depot/file1" without a trailing "/" should only exclude "file1", but not "file111" or "file1_dir/file2"
  2400. elif path.lower() == p.lower():
  2401. return False
  2402. for p in self.depotPaths:
  2403. if p4PathStartsWith(path, decode_path(p)):
  2404. return True
  2405. return False
  2406. def extractFilesFromCommit(self, commit, shelved=False, shelved_cl = 0):
  2407. files = []
  2408. fnum = 0
  2409. while "depotFile%s" % fnum in commit:
  2410. path = commit["depotFile%s" % fnum]
  2411. found = self.isPathWanted(decode_path(path))
  2412. if not found:
  2413. fnum = fnum + 1
  2414. continue
  2415. file = {}
  2416. file["path"] = path
  2417. file["rev"] = commit["rev%s" % fnum]
  2418. file["action"] = commit["action%s" % fnum]
  2419. file["type"] = commit["type%s" % fnum]
  2420. if shelved:
  2421. file["shelved_cl"] = int(shelved_cl)
  2422. files.append(file)
  2423. fnum = fnum + 1
  2424. return files
  2425. def extractJobsFromCommit(self, commit):
  2426. jobs = []
  2427. jnum = 0
  2428. while "job%s" % jnum in commit:
  2429. job = commit["job%s" % jnum]
  2430. jobs.append(job)
  2431. jnum = jnum + 1
  2432. return jobs
  2433. def stripRepoPath(self, path, prefixes):
  2434. """When streaming files, this is called to map a p4 depot path
  2435. to where it should go in git. The prefixes are either
  2436. self.depotPaths, or self.branchPrefixes in the case of
  2437. branch detection."""
  2438. if self.useClientSpec:
  2439. # branch detection moves files up a level (the branch name)
  2440. # from what client spec interpretation gives
  2441. path = decode_path(self.clientSpecDirs.map_in_client(path))
  2442. if self.detectBranches:
  2443. for b in self.knownBranches:
  2444. if p4PathStartsWith(path, b + "/"):
  2445. path = path[len(b)+1:]
  2446. elif self.keepRepoPath:
  2447. # Preserve everything in relative path name except leading
  2448. # //depot/; just look at first prefix as they all should
  2449. # be in the same depot.
  2450. depot = re.sub("^(//[^/]+/).*", r'\1', prefixes[0])
  2451. if p4PathStartsWith(path, depot):
  2452. path = path[len(depot):]
  2453. else:
  2454. for p in prefixes:
  2455. if p4PathStartsWith(path, p):
  2456. path = path[len(p):]
  2457. break
  2458. path = wildcard_decode(path)
  2459. return path
  2460. def splitFilesIntoBranches(self, commit):
  2461. """Look at each depotFile in the commit to figure out to what
  2462. branch it belongs."""
  2463. if self.clientSpecDirs:
  2464. files = self.extractFilesFromCommit(commit)
  2465. self.clientSpecDirs.update_client_spec_path_cache(files)
  2466. branches = {}
  2467. fnum = 0
  2468. while "depotFile%s" % fnum in commit:
  2469. raw_path = commit["depotFile%s" % fnum]
  2470. path = decode_path(raw_path)
  2471. found = self.isPathWanted(path)
  2472. if not found:
  2473. fnum = fnum + 1
  2474. continue
  2475. file = {}
  2476. file["path"] = raw_path
  2477. file["rev"] = commit["rev%s" % fnum]
  2478. file["action"] = commit["action%s" % fnum]
  2479. file["type"] = commit["type%s" % fnum]
  2480. fnum = fnum + 1
  2481. # start with the full relative path where this file would
  2482. # go in a p4 client
  2483. if self.useClientSpec:
  2484. relPath = decode_path(self.clientSpecDirs.map_in_client(path))
  2485. else:
  2486. relPath = self.stripRepoPath(path, self.depotPaths)
  2487. for branch in self.knownBranches.keys():
  2488. # add a trailing slash so that a commit into qt/4.2foo
  2489. # doesn't end up in qt/4.2, e.g.
  2490. if p4PathStartsWith(relPath, branch + "/"):
  2491. if branch not in branches:
  2492. branches[branch] = []
  2493. branches[branch].append(file)
  2494. break
  2495. return branches
  2496. def writeToGitStream(self, gitMode, relPath, contents):
  2497. self.gitStream.write(encode_text_stream(u'M {} inline {}\n'.format(gitMode, relPath)))
  2498. self.gitStream.write('data %d\n' % sum(len(d) for d in contents))
  2499. for d in contents:
  2500. self.gitStream.write(d)
  2501. self.gitStream.write('\n')
  2502. def encodeWithUTF8(self, path):
  2503. try:
  2504. path.decode('ascii')
  2505. except:
  2506. encoding = 'utf8'
  2507. if gitConfig('git-p4.pathEncoding'):
  2508. encoding = gitConfig('git-p4.pathEncoding')
  2509. path = path.decode(encoding, 'replace').encode('utf8', 'replace')
  2510. if self.verbose:
  2511. print('Path with non-ASCII characters detected. Used %s to encode: %s ' % (encoding, path))
  2512. return path
  2513. # output one file from the P4 stream
  2514. # - helper for streamP4Files
  2515. def streamOneP4File(self, file, contents):
  2516. file_path = file['depotFile']
  2517. relPath = self.stripRepoPath(decode_path(file_path), self.branchPrefixes)
  2518. if verbose:
  2519. if 'fileSize' in self.stream_file:
  2520. size = int(self.stream_file['fileSize'])
  2521. else:
  2522. size = 0 # deleted files don't get a fileSize apparently
  2523. sys.stdout.write('\r%s --> %s (%i MB)\n' % (file_path, relPath, size/1024/1024))
  2524. sys.stdout.flush()
  2525. (type_base, type_mods) = split_p4_type(file["type"])
  2526. git_mode = "100644"
  2527. if "x" in type_mods:
  2528. git_mode = "100755"
  2529. if type_base == "symlink":
  2530. git_mode = "120000"
  2531. # p4 print on a symlink sometimes contains "target\n";
  2532. # if it does, remove the newline
  2533. data = ''.join(decode_text_stream(c) for c in contents)
  2534. if not data:
  2535. # Some version of p4 allowed creating a symlink that pointed
  2536. # to nothing. This causes p4 errors when checking out such
  2537. # a change, and errors here too. Work around it by ignoring
  2538. # the bad symlink; hopefully a future change fixes it.
  2539. print("\nIgnoring empty symlink in %s" % file_path)
  2540. return
  2541. elif data[-1] == '\n':
  2542. contents = [data[:-1]]
  2543. else:
  2544. contents = [data]
  2545. if type_base == "utf16":
  2546. # p4 delivers different text in the python output to -G
  2547. # than it does when using "print -o", or normal p4 client
  2548. # operations. utf16 is converted to ascii or utf8, perhaps.
  2549. # But ascii text saved as -t utf16 is completely mangled.
  2550. # Invoke print -o to get the real contents.
  2551. #
  2552. # On windows, the newlines will always be mangled by print, so put
  2553. # them back too. This is not needed to the cygwin windows version,
  2554. # just the native "NT" type.
  2555. #
  2556. try:
  2557. text = p4_read_pipe(['print', '-q', '-o', '-', '%s@%s' % (decode_path(file['depotFile']), file['change'])], raw=True)
  2558. except Exception as e:
  2559. if 'Translation of file content failed' in str(e):
  2560. type_base = 'binary'
  2561. else:
  2562. raise e
  2563. else:
  2564. if p4_version_string().find('/NT') >= 0:
  2565. text = text.replace(b'\r\n', b'\n')
  2566. contents = [ text ]
  2567. if type_base == "apple":
  2568. # Apple filetype files will be streamed as a concatenation of
  2569. # its appledouble header and the contents. This is useless
  2570. # on both macs and non-macs. If using "print -q -o xx", it
  2571. # will create "xx" with the data, and "%xx" with the header.
  2572. # This is also not very useful.
  2573. #
  2574. # Ideally, someday, this script can learn how to generate
  2575. # appledouble files directly and import those to git, but
  2576. # non-mac machines can never find a use for apple filetype.
  2577. print("\nIgnoring apple filetype file %s" % file['depotFile'])
  2578. return
  2579. # Note that we do not try to de-mangle keywords on utf16 files,
  2580. # even though in theory somebody may want that.
  2581. pattern = p4_keywords_regexp_for_type(type_base, type_mods)
  2582. if pattern:
  2583. regexp = re.compile(pattern, re.VERBOSE)
  2584. text = ''.join(decode_text_stream(c) for c in contents)
  2585. text = regexp.sub(r'$\1$', text)
  2586. contents = [ text ]
  2587. if self.largeFileSystem:
  2588. (git_mode, contents) = self.largeFileSystem.processContent(git_mode, relPath, contents)
  2589. self.writeToGitStream(git_mode, relPath, contents)
  2590. def streamOneP4Deletion(self, file):
  2591. relPath = self.stripRepoPath(decode_path(file['path']), self.branchPrefixes)
  2592. if verbose:
  2593. sys.stdout.write("delete %s\n" % relPath)
  2594. sys.stdout.flush()
  2595. self.gitStream.write(encode_text_stream(u'D {}\n'.format(relPath)))
  2596. if self.largeFileSystem and self.largeFileSystem.isLargeFile(relPath):
  2597. self.largeFileSystem.removeLargeFile(relPath)
  2598. # handle another chunk of streaming data
  2599. def streamP4FilesCb(self, marshalled):
  2600. # catch p4 errors and complain
  2601. err = None
  2602. if "code" in marshalled:
  2603. if marshalled["code"] == "error":
  2604. if "data" in marshalled:
  2605. err = marshalled["data"].rstrip()
  2606. if not err and 'fileSize' in self.stream_file:
  2607. required_bytes = int((4 * int(self.stream_file["fileSize"])) - calcDiskFree())
  2608. if required_bytes > 0:
  2609. err = 'Not enough space left on %s! Free at least %i MB.' % (
  2610. os.getcwd(), required_bytes/1024/1024
  2611. )
  2612. if err:
  2613. f = None
  2614. if self.stream_have_file_info:
  2615. if "depotFile" in self.stream_file:
  2616. f = self.stream_file["depotFile"]
  2617. # force a failure in fast-import, else an empty
  2618. # commit will be made
  2619. self.gitStream.write("\n")
  2620. self.gitStream.write("die-now\n")
  2621. self.gitStream.close()
  2622. # ignore errors, but make sure it exits first
  2623. self.importProcess.wait()
  2624. if f:
  2625. die("Error from p4 print for %s: %s" % (f, err))
  2626. else:
  2627. die("Error from p4 print: %s" % err)
  2628. if 'depotFile' in marshalled and self.stream_have_file_info:
  2629. # start of a new file - output the old one first
  2630. self.streamOneP4File(self.stream_file, self.stream_contents)
  2631. self.stream_file = {}
  2632. self.stream_contents = []
  2633. self.stream_have_file_info = False
  2634. # pick up the new file information... for the
  2635. # 'data' field we need to append to our array
  2636. for k in marshalled.keys():
  2637. if k == 'data':
  2638. if 'streamContentSize' not in self.stream_file:
  2639. self.stream_file['streamContentSize'] = 0
  2640. self.stream_file['streamContentSize'] += len(marshalled['data'])
  2641. self.stream_contents.append(marshalled['data'])
  2642. else:
  2643. self.stream_file[k] = marshalled[k]
  2644. if (verbose and
  2645. 'streamContentSize' in self.stream_file and
  2646. 'fileSize' in self.stream_file and
  2647. 'depotFile' in self.stream_file):
  2648. size = int(self.stream_file["fileSize"])
  2649. if size > 0:
  2650. progress = 100*self.stream_file['streamContentSize']/size
  2651. sys.stdout.write('\r%s %d%% (%i MB)' % (self.stream_file['depotFile'], progress, int(size/1024/1024)))
  2652. sys.stdout.flush()
  2653. self.stream_have_file_info = True
  2654. # Stream directly from "p4 files" into "git fast-import"
  2655. def streamP4Files(self, files):
  2656. filesForCommit = []
  2657. filesToRead = []
  2658. filesToDelete = []
  2659. for f in files:
  2660. filesForCommit.append(f)
  2661. if f['action'] in self.delete_actions:
  2662. filesToDelete.append(f)
  2663. else:
  2664. filesToRead.append(f)
  2665. # deleted files...
  2666. for f in filesToDelete:
  2667. self.streamOneP4Deletion(f)
  2668. if len(filesToRead) > 0:
  2669. self.stream_file = {}
  2670. self.stream_contents = []
  2671. self.stream_have_file_info = False
  2672. # curry self argument
  2673. def streamP4FilesCbSelf(entry):
  2674. self.streamP4FilesCb(entry)
  2675. fileArgs = []
  2676. for f in filesToRead:
  2677. if 'shelved_cl' in f:
  2678. # Handle shelved CLs using the "p4 print file@=N" syntax to print
  2679. # the contents
  2680. fileArg = f['path'] + encode_text_stream('@={}'.format(f['shelved_cl']))
  2681. else:
  2682. fileArg = f['path'] + encode_text_stream('#{}'.format(f['rev']))
  2683. fileArgs.append(fileArg)
  2684. p4CmdList(["-x", "-", "print"],
  2685. stdin=fileArgs,
  2686. cb=streamP4FilesCbSelf)
  2687. # do the last chunk
  2688. if 'depotFile' in self.stream_file:
  2689. self.streamOneP4File(self.stream_file, self.stream_contents)
  2690. def make_email(self, userid):
  2691. if userid in self.users:
  2692. return self.users[userid]
  2693. else:
  2694. return "%s <a@b>" % userid
  2695. def streamTag(self, gitStream, labelName, labelDetails, commit, epoch):
  2696. """ Stream a p4 tag.
  2697. commit is either a git commit, or a fast-import mark, ":<p4commit>"
  2698. """
  2699. if verbose:
  2700. print("writing tag %s for commit %s" % (labelName, commit))
  2701. gitStream.write("tag %s\n" % labelName)
  2702. gitStream.write("from %s\n" % commit)
  2703. if 'Owner' in labelDetails:
  2704. owner = labelDetails["Owner"]
  2705. else:
  2706. owner = None
  2707. # Try to use the owner of the p4 label, or failing that,
  2708. # the current p4 user id.
  2709. if owner:
  2710. email = self.make_email(owner)
  2711. else:
  2712. email = self.make_email(self.p4UserId())
  2713. tagger = "%s %s %s" % (email, epoch, self.tz)
  2714. gitStream.write("tagger %s\n" % tagger)
  2715. print("labelDetails=",labelDetails)
  2716. if 'Description' in labelDetails:
  2717. description = labelDetails['Description']
  2718. else:
  2719. description = 'Label from git p4'
  2720. gitStream.write("data %d\n" % len(description))
  2721. gitStream.write(description)
  2722. gitStream.write("\n")
  2723. def inClientSpec(self, path):
  2724. if not self.clientSpecDirs:
  2725. return True
  2726. inClientSpec = self.clientSpecDirs.map_in_client(path)
  2727. if not inClientSpec and self.verbose:
  2728. print('Ignoring file outside of client spec: {0}'.format(path))
  2729. return inClientSpec
  2730. def hasBranchPrefix(self, path):
  2731. if not self.branchPrefixes:
  2732. return True
  2733. hasPrefix = [p for p in self.branchPrefixes
  2734. if p4PathStartsWith(path, p)]
  2735. if not hasPrefix and self.verbose:
  2736. print('Ignoring file outside of prefix: {0}'.format(path))
  2737. return hasPrefix
  2738. def findShadowedFiles(self, files, change):
  2739. # Perforce allows you commit files and directories with the same name,
  2740. # so you could have files //depot/foo and //depot/foo/bar both checked
  2741. # in. A p4 sync of a repository in this state fails. Deleting one of
  2742. # the files recovers the repository.
  2743. #
  2744. # Git will not allow the broken state to exist and only the most recent
  2745. # of the conflicting names is left in the repository. When one of the
  2746. # conflicting files is deleted we need to re-add the other one to make
  2747. # sure the git repository recovers in the same way as perforce.
  2748. deleted = [f for f in files if f['action'] in self.delete_actions]
  2749. to_check = set()
  2750. for f in deleted:
  2751. path = decode_path(f['path'])
  2752. to_check.add(path + '/...')
  2753. while True:
  2754. path = path.rsplit("/", 1)[0]
  2755. if path == "/" or path in to_check:
  2756. break
  2757. to_check.add(path)
  2758. to_check = ['%s@%s' % (wildcard_encode(p), change) for p in to_check
  2759. if self.hasBranchPrefix(p)]
  2760. if to_check:
  2761. stat_result = p4CmdList(["-x", "-", "fstat", "-T",
  2762. "depotFile,headAction,headRev,headType"], stdin=to_check)
  2763. for record in stat_result:
  2764. if record['code'] != 'stat':
  2765. continue
  2766. if record['headAction'] in self.delete_actions:
  2767. continue
  2768. files.append({
  2769. 'action': 'add',
  2770. 'path': record['depotFile'],
  2771. 'rev': record['headRev'],
  2772. 'type': record['headType']})
  2773. def commit(self, details, files, branch, parent = "", allow_empty=False):
  2774. epoch = details["time"]
  2775. author = details["user"]
  2776. jobs = self.extractJobsFromCommit(details)
  2777. if self.verbose:
  2778. print('commit into {0}'.format(branch))
  2779. files = [f for f in files
  2780. if self.hasBranchPrefix(decode_path(f['path']))]
  2781. self.findShadowedFiles(files, details['change'])
  2782. if self.clientSpecDirs:
  2783. self.clientSpecDirs.update_client_spec_path_cache(files)
  2784. files = [f for f in files if self.inClientSpec(decode_path(f['path']))]
  2785. if gitConfigBool('git-p4.keepEmptyCommits'):
  2786. allow_empty = True
  2787. if not files and not allow_empty:
  2788. print('Ignoring revision {0} as it would produce an empty commit.'
  2789. .format(details['change']))
  2790. return
  2791. self.gitStream.write("commit %s\n" % branch)
  2792. self.gitStream.write("mark :%s\n" % details["change"])
  2793. self.committedChanges.add(int(details["change"]))
  2794. committer = ""
  2795. if author not in self.users:
  2796. self.getUserMapFromPerforceServer()
  2797. committer = "%s %s %s" % (self.make_email(author), epoch, self.tz)
  2798. self.gitStream.write("committer %s\n" % committer)
  2799. self.gitStream.write("data <<EOT\n")
  2800. self.gitStream.write(details["desc"])
  2801. if len(jobs) > 0:
  2802. self.gitStream.write("\nJobs: %s" % (' '.join(jobs)))
  2803. if not self.suppress_meta_comment:
  2804. self.gitStream.write("\n[git-p4: depot-paths = \"%s\": change = %s" %
  2805. (','.join(self.branchPrefixes), details["change"]))
  2806. if len(details['options']) > 0:
  2807. self.gitStream.write(": options = %s" % details['options'])
  2808. self.gitStream.write("]\n")
  2809. self.gitStream.write("EOT\n\n")
  2810. if len(parent) > 0:
  2811. if self.verbose:
  2812. print("parent %s" % parent)
  2813. self.gitStream.write("from %s\n" % parent)
  2814. self.streamP4Files(files)
  2815. self.gitStream.write("\n")
  2816. change = int(details["change"])
  2817. if change in self.labels:
  2818. label = self.labels[change]
  2819. labelDetails = label[0]
  2820. labelRevisions = label[1]
  2821. if self.verbose:
  2822. print("Change %s is labelled %s" % (change, labelDetails))
  2823. files = p4CmdList(["files"] + ["%s...@%s" % (p, change)
  2824. for p in self.branchPrefixes])
  2825. if len(files) == len(labelRevisions):
  2826. cleanedFiles = {}
  2827. for info in files:
  2828. if info["action"] in self.delete_actions:
  2829. continue
  2830. cleanedFiles[info["depotFile"]] = info["rev"]
  2831. if cleanedFiles == labelRevisions:
  2832. self.streamTag(self.gitStream, 'tag_%s' % labelDetails['label'], labelDetails, branch, epoch)
  2833. else:
  2834. if not self.silent:
  2835. print("Tag %s does not match with change %s: files do not match."
  2836. % (labelDetails["label"], change))
  2837. else:
  2838. if not self.silent:
  2839. print("Tag %s does not match with change %s: file count is different."
  2840. % (labelDetails["label"], change))
  2841. # Build a dictionary of changelists and labels, for "detect-labels" option.
  2842. def getLabels(self):
  2843. self.labels = {}
  2844. l = p4CmdList(["labels"] + ["%s..." % p for p in self.depotPaths])
  2845. if len(l) > 0 and not self.silent:
  2846. print("Finding files belonging to labels in %s" % self.depotPaths)
  2847. for output in l:
  2848. label = output["label"]
  2849. revisions = {}
  2850. newestChange = 0
  2851. if self.verbose:
  2852. print("Querying files for label %s" % label)
  2853. for file in p4CmdList(["files"] +
  2854. ["%s...@%s" % (p, label)
  2855. for p in self.depotPaths]):
  2856. revisions[file["depotFile"]] = file["rev"]
  2857. change = int(file["change"])
  2858. if change > newestChange:
  2859. newestChange = change
  2860. self.labels[newestChange] = [output, revisions]
  2861. if self.verbose:
  2862. print("Label changes: %s" % self.labels.keys())
  2863. # Import p4 labels as git tags. A direct mapping does not
  2864. # exist, so assume that if all the files are at the same revision
  2865. # then we can use that, or it's something more complicated we should
  2866. # just ignore.
  2867. def importP4Labels(self, stream, p4Labels):
  2868. if verbose:
  2869. print("import p4 labels: " + ' '.join(p4Labels))
  2870. ignoredP4Labels = gitConfigList("git-p4.ignoredP4Labels")
  2871. validLabelRegexp = gitConfig("git-p4.labelImportRegexp")
  2872. if len(validLabelRegexp) == 0:
  2873. validLabelRegexp = defaultLabelRegexp
  2874. m = re.compile(validLabelRegexp)
  2875. for name in p4Labels:
  2876. commitFound = False
  2877. if not m.match(name):
  2878. if verbose:
  2879. print("label %s does not match regexp %s" % (name,validLabelRegexp))
  2880. continue
  2881. if name in ignoredP4Labels:
  2882. continue
  2883. labelDetails = p4CmdList(['label', "-o", name])[0]
  2884. # get the most recent changelist for each file in this label
  2885. change = p4Cmd(["changes", "-m", "1"] + ["%s...@%s" % (p, name)
  2886. for p in self.depotPaths])
  2887. if 'change' in change:
  2888. # find the corresponding git commit; take the oldest commit
  2889. changelist = int(change['change'])
  2890. if changelist in self.committedChanges:
  2891. gitCommit = ":%d" % changelist # use a fast-import mark
  2892. commitFound = True
  2893. else:
  2894. gitCommit = read_pipe(["git", "rev-list", "--max-count=1",
  2895. "--reverse", ":/\[git-p4:.*change = %d\]" % changelist], ignore_error=True)
  2896. if len(gitCommit) == 0:
  2897. print("importing label %s: could not find git commit for changelist %d" % (name, changelist))
  2898. else:
  2899. commitFound = True
  2900. gitCommit = gitCommit.strip()
  2901. if commitFound:
  2902. # Convert from p4 time format
  2903. try:
  2904. tmwhen = time.strptime(labelDetails['Update'], "%Y/%m/%d %H:%M:%S")
  2905. except ValueError:
  2906. print("Could not convert label time %s" % labelDetails['Update'])
  2907. tmwhen = 1
  2908. when = int(time.mktime(tmwhen))
  2909. self.streamTag(stream, name, labelDetails, gitCommit, when)
  2910. if verbose:
  2911. print("p4 label %s mapped to git commit %s" % (name, gitCommit))
  2912. else:
  2913. if verbose:
  2914. print("Label %s has no changelists - possibly deleted?" % name)
  2915. if not commitFound:
  2916. # We can't import this label; don't try again as it will get very
  2917. # expensive repeatedly fetching all the files for labels that will
  2918. # never be imported. If the label is moved in the future, the
  2919. # ignore will need to be removed manually.
  2920. system(["git", "config", "--add", "git-p4.ignoredP4Labels", name])
  2921. def guessProjectName(self):
  2922. for p in self.depotPaths:
  2923. if p.endswith("/"):
  2924. p = p[:-1]
  2925. p = p[p.strip().rfind("/") + 1:]
  2926. if not p.endswith("/"):
  2927. p += "/"
  2928. return p
  2929. def getBranchMapping(self):
  2930. lostAndFoundBranches = set()
  2931. user = gitConfig("git-p4.branchUser")
  2932. if len(user) > 0:
  2933. command = "branches -u %s" % user
  2934. else:
  2935. command = "branches"
  2936. for info in p4CmdList(command):
  2937. details = p4Cmd(["branch", "-o", info["branch"]])
  2938. viewIdx = 0
  2939. while "View%s" % viewIdx in details:
  2940. paths = details["View%s" % viewIdx].split(" ")
  2941. viewIdx = viewIdx + 1
  2942. # require standard //depot/foo/... //depot/bar/... mapping
  2943. if len(paths) != 2 or not paths[0].endswith("/...") or not paths[1].endswith("/..."):
  2944. continue
  2945. source = paths[0]
  2946. destination = paths[1]
  2947. ## HACK
  2948. if p4PathStartsWith(source, self.depotPaths[0]) and p4PathStartsWith(destination, self.depotPaths[0]):
  2949. source = source[len(self.depotPaths[0]):-4]
  2950. destination = destination[len(self.depotPaths[0]):-4]
  2951. if destination in self.knownBranches:
  2952. if not self.silent:
  2953. print("p4 branch %s defines a mapping from %s to %s" % (info["branch"], source, destination))
  2954. print("but there exists another mapping from %s to %s already!" % (self.knownBranches[destination], destination))
  2955. continue
  2956. self.knownBranches[destination] = source
  2957. lostAndFoundBranches.discard(destination)
  2958. if source not in self.knownBranches:
  2959. lostAndFoundBranches.add(source)
  2960. # Perforce does not strictly require branches to be defined, so we also
  2961. # check git config for a branch list.
  2962. #
  2963. # Example of branch definition in git config file:
  2964. # [git-p4]
  2965. # branchList=main:branchA
  2966. # branchList=main:branchB
  2967. # branchList=branchA:branchC
  2968. configBranches = gitConfigList("git-p4.branchList")
  2969. for branch in configBranches:
  2970. if branch:
  2971. (source, destination) = branch.split(":")
  2972. self.knownBranches[destination] = source
  2973. lostAndFoundBranches.discard(destination)
  2974. if source not in self.knownBranches:
  2975. lostAndFoundBranches.add(source)
  2976. for branch in lostAndFoundBranches:
  2977. self.knownBranches[branch] = branch
  2978. def getBranchMappingFromGitBranches(self):
  2979. branches = p4BranchesInGit(self.importIntoRemotes)
  2980. for branch in branches.keys():
  2981. if branch == "master":
  2982. branch = "main"
  2983. else:
  2984. branch = branch[len(self.projectName):]
  2985. self.knownBranches[branch] = branch
  2986. def updateOptionDict(self, d):
  2987. option_keys = {}
  2988. if self.keepRepoPath:
  2989. option_keys['keepRepoPath'] = 1
  2990. d["options"] = ' '.join(sorted(option_keys.keys()))
  2991. def readOptions(self, d):
  2992. self.keepRepoPath = ('options' in d
  2993. and ('keepRepoPath' in d['options']))
  2994. def gitRefForBranch(self, branch):
  2995. if branch == "main":
  2996. return self.refPrefix + "master"
  2997. if len(branch) <= 0:
  2998. return branch
  2999. return self.refPrefix + self.projectName + branch
  3000. def gitCommitByP4Change(self, ref, change):
  3001. if self.verbose:
  3002. print("looking in ref " + ref + " for change %s using bisect..." % change)
  3003. earliestCommit = ""
  3004. latestCommit = parseRevision(ref)
  3005. while True:
  3006. if self.verbose:
  3007. print("trying: earliest %s latest %s" % (earliestCommit, latestCommit))
  3008. next = read_pipe("git rev-list --bisect %s %s" % (latestCommit, earliestCommit)).strip()
  3009. if len(next) == 0:
  3010. if self.verbose:
  3011. print("argh")
  3012. return ""
  3013. log = extractLogMessageFromGitCommit(next)
  3014. settings = extractSettingsGitLog(log)
  3015. currentChange = int(settings['change'])
  3016. if self.verbose:
  3017. print("current change %s" % currentChange)
  3018. if currentChange == change:
  3019. if self.verbose:
  3020. print("found %s" % next)
  3021. return next
  3022. if currentChange < change:
  3023. earliestCommit = "^%s" % next
  3024. else:
  3025. if next == latestCommit:
  3026. die("Infinite loop while looking in ref %s for change %s. Check your branch mappings" % (ref, change))
  3027. latestCommit = "%s^@" % next
  3028. return ""
  3029. def importNewBranch(self, branch, maxChange):
  3030. # make fast-import flush all changes to disk and update the refs using the checkpoint
  3031. # command so that we can try to find the branch parent in the git history
  3032. self.gitStream.write("checkpoint\n\n");
  3033. self.gitStream.flush();
  3034. branchPrefix = self.depotPaths[0] + branch + "/"
  3035. range = "@1,%s" % maxChange
  3036. #print "prefix" + branchPrefix
  3037. changes = p4ChangesForPaths([branchPrefix], range, self.changes_block_size)
  3038. if len(changes) <= 0:
  3039. return False
  3040. firstChange = changes[0]
  3041. #print "first change in branch: %s" % firstChange
  3042. sourceBranch = self.knownBranches[branch]
  3043. sourceDepotPath = self.depotPaths[0] + sourceBranch
  3044. sourceRef = self.gitRefForBranch(sourceBranch)
  3045. #print "source " + sourceBranch
  3046. branchParentChange = int(p4Cmd(["changes", "-m", "1", "%s...@1,%s" % (sourceDepotPath, firstChange)])["change"])
  3047. #print "branch parent: %s" % branchParentChange
  3048. gitParent = self.gitCommitByP4Change(sourceRef, branchParentChange)
  3049. if len(gitParent) > 0:
  3050. self.initialParents[self.gitRefForBranch(branch)] = gitParent
  3051. #print "parent git commit: %s" % gitParent
  3052. self.importChanges(changes)
  3053. return True
  3054. def searchParent(self, parent, branch, target):
  3055. parentFound = False
  3056. for blob in read_pipe_lines(["git", "rev-list", "--reverse",
  3057. "--no-merges", parent]):
  3058. blob = blob.strip()
  3059. if len(read_pipe(["git", "diff-tree", blob, target])) == 0:
  3060. parentFound = True
  3061. if self.verbose:
  3062. print("Found parent of %s in commit %s" % (branch, blob))
  3063. break
  3064. if parentFound:
  3065. return blob
  3066. else:
  3067. return None
  3068. def importChanges(self, changes, origin_revision=0):
  3069. cnt = 1
  3070. for change in changes:
  3071. description = p4_describe(change)
  3072. self.updateOptionDict(description)
  3073. if not self.silent:
  3074. sys.stdout.write("\rImporting revision %s (%s%%)" % (change, cnt * 100 / len(changes)))
  3075. sys.stdout.flush()
  3076. cnt = cnt + 1
  3077. try:
  3078. if self.detectBranches:
  3079. branches = self.splitFilesIntoBranches(description)
  3080. for branch in branches.keys():
  3081. ## HACK --hwn
  3082. branchPrefix = self.depotPaths[0] + branch + "/"
  3083. self.branchPrefixes = [ branchPrefix ]
  3084. parent = ""
  3085. filesForCommit = branches[branch]
  3086. if self.verbose:
  3087. print("branch is %s" % branch)
  3088. self.updatedBranches.add(branch)
  3089. if branch not in self.createdBranches:
  3090. self.createdBranches.add(branch)
  3091. parent = self.knownBranches[branch]
  3092. if parent == branch:
  3093. parent = ""
  3094. else:
  3095. fullBranch = self.projectName + branch
  3096. if fullBranch not in self.p4BranchesInGit:
  3097. if not self.silent:
  3098. print("\n Importing new branch %s" % fullBranch);
  3099. if self.importNewBranch(branch, change - 1):
  3100. parent = ""
  3101. self.p4BranchesInGit.append(fullBranch)
  3102. if not self.silent:
  3103. print("\n Resuming with change %s" % change);
  3104. if self.verbose:
  3105. print("parent determined through known branches: %s" % parent)
  3106. branch = self.gitRefForBranch(branch)
  3107. parent = self.gitRefForBranch(parent)
  3108. if self.verbose:
  3109. print("looking for initial parent for %s; current parent is %s" % (branch, parent))
  3110. if len(parent) == 0 and branch in self.initialParents:
  3111. parent = self.initialParents[branch]
  3112. del self.initialParents[branch]
  3113. blob = None
  3114. if len(parent) > 0:
  3115. tempBranch = "%s/%d" % (self.tempBranchLocation, change)
  3116. if self.verbose:
  3117. print("Creating temporary branch: " + tempBranch)
  3118. self.commit(description, filesForCommit, tempBranch)
  3119. self.tempBranches.append(tempBranch)
  3120. self.checkpoint()
  3121. blob = self.searchParent(parent, branch, tempBranch)
  3122. if blob:
  3123. self.commit(description, filesForCommit, branch, blob)
  3124. else:
  3125. if self.verbose:
  3126. print("Parent of %s not found. Committing into head of %s" % (branch, parent))
  3127. self.commit(description, filesForCommit, branch, parent)
  3128. else:
  3129. files = self.extractFilesFromCommit(description)
  3130. self.commit(description, files, self.branch,
  3131. self.initialParent)
  3132. # only needed once, to connect to the previous commit
  3133. self.initialParent = ""
  3134. except IOError:
  3135. print(self.gitError.read())
  3136. sys.exit(1)
  3137. def sync_origin_only(self):
  3138. if self.syncWithOrigin:
  3139. self.hasOrigin = originP4BranchesExist()
  3140. if self.hasOrigin:
  3141. if not self.silent:
  3142. print('Syncing with origin first, using "git fetch origin"')
  3143. system("git fetch origin")
  3144. def importHeadRevision(self, revision):
  3145. print("Doing initial import of %s from revision %s into %s" % (' '.join(self.depotPaths), revision, self.branch))
  3146. details = {}
  3147. details["user"] = "git perforce import user"
  3148. details["desc"] = ("Initial import of %s from the state at revision %s\n"
  3149. % (' '.join(self.depotPaths), revision))
  3150. details["change"] = revision
  3151. newestRevision = 0
  3152. fileCnt = 0
  3153. fileArgs = ["%s...%s" % (p,revision) for p in self.depotPaths]
  3154. for info in p4CmdList(["files"] + fileArgs):
  3155. if 'code' in info and info['code'] == 'error':
  3156. sys.stderr.write("p4 returned an error: %s\n"
  3157. % info['data'])
  3158. if info['data'].find("must refer to client") >= 0:
  3159. sys.stderr.write("This particular p4 error is misleading.\n")
  3160. sys.stderr.write("Perhaps the depot path was misspelled.\n");
  3161. sys.stderr.write("Depot path: %s\n" % " ".join(self.depotPaths))
  3162. sys.exit(1)
  3163. if 'p4ExitCode' in info:
  3164. sys.stderr.write("p4 exitcode: %s\n" % info['p4ExitCode'])
  3165. sys.exit(1)
  3166. change = int(info["change"])
  3167. if change > newestRevision:
  3168. newestRevision = change
  3169. if info["action"] in self.delete_actions:
  3170. # don't increase the file cnt, otherwise details["depotFile123"] will have gaps!
  3171. #fileCnt = fileCnt + 1
  3172. continue
  3173. for prop in ["depotFile", "rev", "action", "type" ]:
  3174. details["%s%s" % (prop, fileCnt)] = info[prop]
  3175. fileCnt = fileCnt + 1
  3176. details["change"] = newestRevision
  3177. # Use time from top-most change so that all git p4 clones of
  3178. # the same p4 repo have the same commit SHA1s.
  3179. res = p4_describe(newestRevision)
  3180. details["time"] = res["time"]
  3181. self.updateOptionDict(details)
  3182. try:
  3183. self.commit(details, self.extractFilesFromCommit(details), self.branch)
  3184. except IOError as err:
  3185. print("IO error with git fast-import. Is your git version recent enough?")
  3186. print("IO error details: {}".format(err))
  3187. print(self.gitError.read())
  3188. def importRevisions(self, args, branch_arg_given):
  3189. changes = []
  3190. if len(self.changesFile) > 0:
  3191. with open(self.changesFile) as f:
  3192. output = f.readlines()
  3193. changeSet = set()
  3194. for line in output:
  3195. changeSet.add(int(line))
  3196. for change in changeSet:
  3197. changes.append(change)
  3198. changes.sort()
  3199. else:
  3200. # catch "git p4 sync" with no new branches, in a repo that
  3201. # does not have any existing p4 branches
  3202. if len(args) == 0:
  3203. if not self.p4BranchesInGit:
  3204. raise P4CommandException("No remote p4 branches. Perhaps you never did \"git p4 clone\" in here.")
  3205. # The default branch is master, unless --branch is used to
  3206. # specify something else. Make sure it exists, or complain
  3207. # nicely about how to use --branch.
  3208. if not self.detectBranches:
  3209. if not branch_exists(self.branch):
  3210. if branch_arg_given:
  3211. raise P4CommandException("Error: branch %s does not exist." % self.branch)
  3212. else:
  3213. raise P4CommandException("Error: no branch %s; perhaps specify one with --branch." %
  3214. self.branch)
  3215. if self.verbose:
  3216. print("Getting p4 changes for %s...%s" % (', '.join(self.depotPaths),
  3217. self.changeRange))
  3218. changes = p4ChangesForPaths(self.depotPaths, self.changeRange, self.changes_block_size)
  3219. if len(self.maxChanges) > 0:
  3220. changes = changes[:min(int(self.maxChanges), len(changes))]
  3221. if len(changes) == 0:
  3222. if not self.silent:
  3223. print("No changes to import!")
  3224. else:
  3225. if not self.silent and not self.detectBranches:
  3226. print("Import destination: %s" % self.branch)
  3227. self.updatedBranches = set()
  3228. if not self.detectBranches:
  3229. if args:
  3230. # start a new branch
  3231. self.initialParent = ""
  3232. else:
  3233. # build on a previous revision
  3234. self.initialParent = parseRevision(self.branch)
  3235. self.importChanges(changes)
  3236. if not self.silent:
  3237. print("")
  3238. if len(self.updatedBranches) > 0:
  3239. sys.stdout.write("Updated branches: ")
  3240. for b in self.updatedBranches:
  3241. sys.stdout.write("%s " % b)
  3242. sys.stdout.write("\n")
  3243. def openStreams(self):
  3244. self.importProcess = subprocess.Popen(["git", "fast-import"],
  3245. stdin=subprocess.PIPE,
  3246. stdout=subprocess.PIPE,
  3247. stderr=subprocess.PIPE);
  3248. self.gitOutput = self.importProcess.stdout
  3249. self.gitStream = self.importProcess.stdin
  3250. self.gitError = self.importProcess.stderr
  3251. if bytes is not str:
  3252. # Wrap gitStream.write() so that it can be called using `str` arguments
  3253. def make_encoded_write(write):
  3254. def encoded_write(s):
  3255. return write(s.encode() if isinstance(s, str) else s)
  3256. return encoded_write
  3257. self.gitStream.write = make_encoded_write(self.gitStream.write)
  3258. def closeStreams(self):
  3259. if self.gitStream is None:
  3260. return
  3261. self.gitStream.close()
  3262. if self.importProcess.wait() != 0:
  3263. die("fast-import failed: %s" % self.gitError.read())
  3264. self.gitOutput.close()
  3265. self.gitError.close()
  3266. self.gitStream = None
  3267. def run(self, args):
  3268. if self.importIntoRemotes:
  3269. self.refPrefix = "refs/remotes/p4/"
  3270. else:
  3271. self.refPrefix = "refs/heads/p4/"
  3272. self.sync_origin_only()
  3273. branch_arg_given = bool(self.branch)
  3274. if len(self.branch) == 0:
  3275. self.branch = self.refPrefix + "master"
  3276. if gitBranchExists("refs/heads/p4") and self.importIntoRemotes:
  3277. system("git update-ref %s refs/heads/p4" % self.branch)
  3278. system("git branch -D p4")
  3279. # accept either the command-line option, or the configuration variable
  3280. if self.useClientSpec:
  3281. # will use this after clone to set the variable
  3282. self.useClientSpec_from_options = True
  3283. else:
  3284. if gitConfigBool("git-p4.useclientspec"):
  3285. self.useClientSpec = True
  3286. if self.useClientSpec:
  3287. self.clientSpecDirs = getClientSpec()
  3288. # TODO: should always look at previous commits,
  3289. # merge with previous imports, if possible.
  3290. if args == []:
  3291. if self.hasOrigin:
  3292. createOrUpdateBranchesFromOrigin(self.refPrefix, self.silent)
  3293. # branches holds mapping from branch name to sha1
  3294. branches = p4BranchesInGit(self.importIntoRemotes)
  3295. # restrict to just this one, disabling detect-branches
  3296. if branch_arg_given:
  3297. short = self.branch.split("/")[-1]
  3298. if short in branches:
  3299. self.p4BranchesInGit = [ short ]
  3300. else:
  3301. self.p4BranchesInGit = branches.keys()
  3302. if len(self.p4BranchesInGit) > 1:
  3303. if not self.silent:
  3304. print("Importing from/into multiple branches")
  3305. self.detectBranches = True
  3306. for branch in branches.keys():
  3307. self.initialParents[self.refPrefix + branch] = \
  3308. branches[branch]
  3309. if self.verbose:
  3310. print("branches: %s" % self.p4BranchesInGit)
  3311. p4Change = 0
  3312. for branch in self.p4BranchesInGit:
  3313. logMsg = extractLogMessageFromGitCommit(self.refPrefix + branch)
  3314. settings = extractSettingsGitLog(logMsg)
  3315. self.readOptions(settings)
  3316. if ('depot-paths' in settings
  3317. and 'change' in settings):
  3318. change = int(settings['change']) + 1
  3319. p4Change = max(p4Change, change)
  3320. depotPaths = sorted(settings['depot-paths'])
  3321. if self.previousDepotPaths == []:
  3322. self.previousDepotPaths = depotPaths
  3323. else:
  3324. paths = []
  3325. for (prev, cur) in zip(self.previousDepotPaths, depotPaths):
  3326. prev_list = prev.split("/")
  3327. cur_list = cur.split("/")
  3328. for i in range(0, min(len(cur_list), len(prev_list))):
  3329. if cur_list[i] != prev_list[i]:
  3330. i = i - 1
  3331. break
  3332. paths.append ("/".join(cur_list[:i + 1]))
  3333. self.previousDepotPaths = paths
  3334. if p4Change > 0:
  3335. self.depotPaths = sorted(self.previousDepotPaths)
  3336. self.changeRange = "@%s,#head" % p4Change
  3337. if not self.silent and not self.detectBranches:
  3338. print("Performing incremental import into %s git branch" % self.branch)
  3339. # accept multiple ref name abbreviations:
  3340. # refs/foo/bar/branch -> use it exactly
  3341. # p4/branch -> prepend refs/remotes/ or refs/heads/
  3342. # branch -> prepend refs/remotes/p4/ or refs/heads/p4/
  3343. if not self.branch.startswith("refs/"):
  3344. if self.importIntoRemotes:
  3345. prepend = "refs/remotes/"
  3346. else:
  3347. prepend = "refs/heads/"
  3348. if not self.branch.startswith("p4/"):
  3349. prepend += "p4/"
  3350. self.branch = prepend + self.branch
  3351. if len(args) == 0 and self.depotPaths:
  3352. if not self.silent:
  3353. print("Depot paths: %s" % ' '.join(self.depotPaths))
  3354. else:
  3355. if self.depotPaths and self.depotPaths != args:
  3356. print("previous import used depot path %s and now %s was specified. "
  3357. "This doesn't work!" % (' '.join (self.depotPaths),
  3358. ' '.join (args)))
  3359. sys.exit(1)
  3360. self.depotPaths = sorted(args)
  3361. revision = ""
  3362. self.users = {}
  3363. # Make sure no revision specifiers are used when --changesfile
  3364. # is specified.
  3365. bad_changesfile = False
  3366. if len(self.changesFile) > 0:
  3367. for p in self.depotPaths:
  3368. if p.find("@") >= 0 or p.find("#") >= 0:
  3369. bad_changesfile = True
  3370. break
  3371. if bad_changesfile:
  3372. die("Option --changesfile is incompatible with revision specifiers")
  3373. newPaths = []
  3374. for p in self.depotPaths:
  3375. if p.find("@") != -1:
  3376. atIdx = p.index("@")
  3377. self.changeRange = p[atIdx:]
  3378. if self.changeRange == "@all":
  3379. self.changeRange = ""
  3380. elif ',' not in self.changeRange:
  3381. revision = self.changeRange
  3382. self.changeRange = ""
  3383. p = p[:atIdx]
  3384. elif p.find("#") != -1:
  3385. hashIdx = p.index("#")
  3386. revision = p[hashIdx:]
  3387. p = p[:hashIdx]
  3388. elif self.previousDepotPaths == []:
  3389. # pay attention to changesfile, if given, else import
  3390. # the entire p4 tree at the head revision
  3391. if len(self.changesFile) == 0:
  3392. revision = "#head"
  3393. p = re.sub ("\.\.\.$", "", p)
  3394. if not p.endswith("/"):
  3395. p += "/"
  3396. newPaths.append(p)
  3397. self.depotPaths = newPaths
  3398. # --detect-branches may change this for each branch
  3399. self.branchPrefixes = self.depotPaths
  3400. self.loadUserMapFromCache()
  3401. self.labels = {}
  3402. if self.detectLabels:
  3403. self.getLabels();
  3404. if self.detectBranches:
  3405. ## FIXME - what's a P4 projectName ?
  3406. self.projectName = self.guessProjectName()
  3407. if self.hasOrigin:
  3408. self.getBranchMappingFromGitBranches()
  3409. else:
  3410. self.getBranchMapping()
  3411. if self.verbose:
  3412. print("p4-git branches: %s" % self.p4BranchesInGit)
  3413. print("initial parents: %s" % self.initialParents)
  3414. for b in self.p4BranchesInGit:
  3415. if b != "master":
  3416. ## FIXME
  3417. b = b[len(self.projectName):]
  3418. self.createdBranches.add(b)
  3419. p4_check_access()
  3420. self.openStreams()
  3421. err = None
  3422. try:
  3423. if revision:
  3424. self.importHeadRevision(revision)
  3425. else:
  3426. self.importRevisions(args, branch_arg_given)
  3427. if gitConfigBool("git-p4.importLabels"):
  3428. self.importLabels = True
  3429. if self.importLabels:
  3430. p4Labels = getP4Labels(self.depotPaths)
  3431. gitTags = getGitTags()
  3432. missingP4Labels = p4Labels - gitTags
  3433. self.importP4Labels(self.gitStream, missingP4Labels)
  3434. except P4CommandException as e:
  3435. err = e
  3436. finally:
  3437. self.closeStreams()
  3438. if err:
  3439. die(str(err))
  3440. # Cleanup temporary branches created during import
  3441. if self.tempBranches != []:
  3442. for branch in self.tempBranches:
  3443. read_pipe("git update-ref -d %s" % branch)
  3444. os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
  3445. # Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
  3446. # a convenient shortcut refname "p4".
  3447. if self.importIntoRemotes:
  3448. head_ref = self.refPrefix + "HEAD"
  3449. if not gitBranchExists(head_ref) and gitBranchExists(self.branch):
  3450. system(["git", "symbolic-ref", head_ref, self.branch])
  3451. return True
  3452. class P4Rebase(Command):
  3453. def __init__(self):
  3454. Command.__init__(self)
  3455. self.options = [
  3456. optparse.make_option("--import-labels", dest="importLabels", action="store_true"),
  3457. ]
  3458. self.importLabels = False
  3459. self.description = ("Fetches the latest revision from perforce and "
  3460. + "rebases the current work (branch) against it")
  3461. def run(self, args):
  3462. sync = P4Sync()
  3463. sync.importLabels = self.importLabels
  3464. sync.run([])
  3465. return self.rebase()
  3466. def rebase(self):
  3467. if os.system("git update-index --refresh") != 0:
  3468. die("Some files in your working directory are modified and different than what is in your index. You can use git update-index <filename> to bring the index up to date or stash away all your changes with git stash.");
  3469. if len(read_pipe("git diff-index HEAD --")) > 0:
  3470. die("You have uncommitted changes. Please commit them before rebasing or stash them away with git stash.");
  3471. [upstream, settings] = findUpstreamBranchPoint()
  3472. if len(upstream) == 0:
  3473. die("Cannot find upstream branchpoint for rebase")
  3474. # the branchpoint may be p4/foo~3, so strip off the parent
  3475. upstream = re.sub("~[0-9]+$", "", upstream)
  3476. print("Rebasing the current branch onto %s" % upstream)
  3477. oldHead = read_pipe("git rev-parse HEAD").strip()
  3478. system("git rebase %s" % upstream)
  3479. system("git diff-tree --stat --summary -M %s HEAD --" % oldHead)
  3480. return True
  3481. class P4Clone(P4Sync):
  3482. def __init__(self):
  3483. P4Sync.__init__(self)
  3484. self.description = "Creates a new git repository and imports from Perforce into it"
  3485. self.usage = "usage: %prog [options] //depot/path[@revRange]"
  3486. self.options += [
  3487. optparse.make_option("--destination", dest="cloneDestination",
  3488. action='store', default=None,
  3489. help="where to leave result of the clone"),
  3490. optparse.make_option("--bare", dest="cloneBare",
  3491. action="store_true", default=False),
  3492. ]
  3493. self.cloneDestination = None
  3494. self.needsGit = False
  3495. self.cloneBare = False
  3496. def defaultDestination(self, args):
  3497. ## TODO: use common prefix of args?
  3498. depotPath = args[0]
  3499. depotDir = re.sub("(@[^@]*)$", "", depotPath)
  3500. depotDir = re.sub("(#[^#]*)$", "", depotDir)
  3501. depotDir = re.sub(r"\.\.\.$", "", depotDir)
  3502. depotDir = re.sub(r"/$", "", depotDir)
  3503. return os.path.split(depotDir)[1]
  3504. def run(self, args):
  3505. if len(args) < 1:
  3506. return False
  3507. if self.keepRepoPath and not self.cloneDestination:
  3508. sys.stderr.write("Must specify destination for --keep-path\n")
  3509. sys.exit(1)
  3510. depotPaths = args
  3511. if not self.cloneDestination and len(depotPaths) > 1:
  3512. self.cloneDestination = depotPaths[-1]
  3513. depotPaths = depotPaths[:-1]
  3514. for p in depotPaths:
  3515. if not p.startswith("//"):
  3516. sys.stderr.write('Depot paths must start with "//": %s\n' % p)
  3517. return False
  3518. if not self.cloneDestination:
  3519. self.cloneDestination = self.defaultDestination(args)
  3520. print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
  3521. if not os.path.exists(self.cloneDestination):
  3522. os.makedirs(self.cloneDestination)
  3523. chdir(self.cloneDestination)
  3524. init_cmd = [ "git", "init" ]
  3525. if self.cloneBare:
  3526. init_cmd.append("--bare")
  3527. retcode = subprocess.call(init_cmd)
  3528. if retcode:
  3529. raise CalledProcessError(retcode, init_cmd)
  3530. if not P4Sync.run(self, depotPaths):
  3531. return False
  3532. # create a master branch and check out a work tree
  3533. if gitBranchExists(self.branch):
  3534. system([ "git", "branch", "master", self.branch ])
  3535. if not self.cloneBare:
  3536. system([ "git", "checkout", "-f" ])
  3537. else:
  3538. print('Not checking out any branch, use ' \
  3539. '"git checkout -q -b master <branch>"')
  3540. # auto-set this variable if invoked with --use-client-spec
  3541. if self.useClientSpec_from_options:
  3542. system("git config --bool git-p4.useclientspec true")
  3543. return True
  3544. class P4Unshelve(Command):
  3545. def __init__(self):
  3546. Command.__init__(self)
  3547. self.options = []
  3548. self.origin = "HEAD"
  3549. self.description = "Unshelve a P4 changelist into a git commit"
  3550. self.usage = "usage: %prog [options] changelist"
  3551. self.options += [
  3552. optparse.make_option("--origin", dest="origin",
  3553. help="Use this base revision instead of the default (%s)" % self.origin),
  3554. ]
  3555. self.verbose = False
  3556. self.noCommit = False
  3557. self.destbranch = "refs/remotes/p4-unshelved"
  3558. def renameBranch(self, branch_name):
  3559. """ Rename the existing branch to branch_name.N
  3560. """
  3561. found = True
  3562. for i in range(0,1000):
  3563. backup_branch_name = "{0}.{1}".format(branch_name, i)
  3564. if not gitBranchExists(backup_branch_name):
  3565. gitUpdateRef(backup_branch_name, branch_name) # copy ref to backup
  3566. gitDeleteRef(branch_name)
  3567. found = True
  3568. print("renamed old unshelve branch to {0}".format(backup_branch_name))
  3569. break
  3570. if not found:
  3571. sys.exit("gave up trying to rename existing branch {0}".format(sync.branch))
  3572. def findLastP4Revision(self, starting_point):
  3573. """ Look back from starting_point for the first commit created by git-p4
  3574. to find the P4 commit we are based on, and the depot-paths.
  3575. """
  3576. for parent in (range(65535)):
  3577. log = extractLogMessageFromGitCommit("{0}~{1}".format(starting_point, parent))
  3578. settings = extractSettingsGitLog(log)
  3579. if 'change' in settings:
  3580. return settings
  3581. sys.exit("could not find git-p4 commits in {0}".format(self.origin))
  3582. def createShelveParent(self, change, branch_name, sync, origin):
  3583. """ Create a commit matching the parent of the shelved changelist 'change'
  3584. """
  3585. parent_description = p4_describe(change, shelved=True)
  3586. parent_description['desc'] = 'parent for shelved changelist {}\n'.format(change)
  3587. files = sync.extractFilesFromCommit(parent_description, shelved=False, shelved_cl=change)
  3588. parent_files = []
  3589. for f in files:
  3590. # if it was added in the shelved changelist, it won't exist in the parent
  3591. if f['action'] in self.add_actions:
  3592. continue
  3593. # if it was deleted in the shelved changelist it must not be deleted
  3594. # in the parent - we might even need to create it if the origin branch
  3595. # does not have it
  3596. if f['action'] in self.delete_actions:
  3597. f['action'] = 'add'
  3598. parent_files.append(f)
  3599. sync.commit(parent_description, parent_files, branch_name,
  3600. parent=origin, allow_empty=True)
  3601. print("created parent commit for {0} based on {1} in {2}".format(
  3602. change, self.origin, branch_name))
  3603. def run(self, args):
  3604. if len(args) != 1:
  3605. return False
  3606. if not gitBranchExists(self.origin):
  3607. sys.exit("origin branch {0} does not exist".format(self.origin))
  3608. sync = P4Sync()
  3609. changes = args
  3610. # only one change at a time
  3611. change = changes[0]
  3612. # if the target branch already exists, rename it
  3613. branch_name = "{0}/{1}".format(self.destbranch, change)
  3614. if gitBranchExists(branch_name):
  3615. self.renameBranch(branch_name)
  3616. sync.branch = branch_name
  3617. sync.verbose = self.verbose
  3618. sync.suppress_meta_comment = True
  3619. settings = self.findLastP4Revision(self.origin)
  3620. sync.depotPaths = settings['depot-paths']
  3621. sync.branchPrefixes = sync.depotPaths
  3622. sync.openStreams()
  3623. sync.loadUserMapFromCache()
  3624. sync.silent = True
  3625. # create a commit for the parent of the shelved changelist
  3626. self.createShelveParent(change, branch_name, sync, self.origin)
  3627. # create the commit for the shelved changelist itself
  3628. description = p4_describe(change, True)
  3629. files = sync.extractFilesFromCommit(description, True, change)
  3630. sync.commit(description, files, branch_name, "")
  3631. sync.closeStreams()
  3632. print("unshelved changelist {0} into {1}".format(change, branch_name))
  3633. return True
  3634. class P4Branches(Command):
  3635. def __init__(self):
  3636. Command.__init__(self)
  3637. self.options = [ ]
  3638. self.description = ("Shows the git branches that hold imports and their "
  3639. + "corresponding perforce depot paths")
  3640. self.verbose = False
  3641. def run(self, args):
  3642. if originP4BranchesExist():
  3643. createOrUpdateBranchesFromOrigin()
  3644. cmdline = "git rev-parse --symbolic "
  3645. cmdline += " --remotes"
  3646. for line in read_pipe_lines(cmdline):
  3647. line = line.strip()
  3648. if not line.startswith('p4/') or line == "p4/HEAD":
  3649. continue
  3650. branch = line
  3651. log = extractLogMessageFromGitCommit("refs/remotes/%s" % branch)
  3652. settings = extractSettingsGitLog(log)
  3653. print("%s <= %s (%s)" % (branch, ",".join(settings["depot-paths"]), settings["change"]))
  3654. return True
  3655. class HelpFormatter(optparse.IndentedHelpFormatter):
  3656. def __init__(self):
  3657. optparse.IndentedHelpFormatter.__init__(self)
  3658. def format_description(self, description):
  3659. if description:
  3660. return description + "\n"
  3661. else:
  3662. return ""
  3663. def printUsage(commands):
  3664. print("usage: %s <command> [options]" % sys.argv[0])
  3665. print("")
  3666. print("valid commands: %s" % ", ".join(commands))
  3667. print("")
  3668. print("Try %s <command> --help for command specific help." % sys.argv[0])
  3669. print("")
  3670. commands = {
  3671. "debug" : P4Debug,
  3672. "submit" : P4Submit,
  3673. "commit" : P4Submit,
  3674. "sync" : P4Sync,
  3675. "rebase" : P4Rebase,
  3676. "clone" : P4Clone,
  3677. "rollback" : P4RollBack,
  3678. "branches" : P4Branches,
  3679. "unshelve" : P4Unshelve,
  3680. }
  3681. def main():
  3682. if len(sys.argv[1:]) == 0:
  3683. printUsage(commands.keys())
  3684. sys.exit(2)
  3685. cmdName = sys.argv[1]
  3686. try:
  3687. klass = commands[cmdName]
  3688. cmd = klass()
  3689. except KeyError:
  3690. print("unknown command %s" % cmdName)
  3691. print("")
  3692. printUsage(commands.keys())
  3693. sys.exit(2)
  3694. options = cmd.options
  3695. cmd.gitdir = os.environ.get("GIT_DIR", None)
  3696. args = sys.argv[2:]
  3697. options.append(optparse.make_option("--verbose", "-v", dest="verbose", action="store_true"))
  3698. if cmd.needsGit:
  3699. options.append(optparse.make_option("--git-dir", dest="gitdir"))
  3700. parser = optparse.OptionParser(cmd.usage.replace("%prog", "%prog " + cmdName),
  3701. options,
  3702. description = cmd.description,
  3703. formatter = HelpFormatter())
  3704. try:
  3705. (cmd, args) = parser.parse_args(sys.argv[2:], cmd);
  3706. except:
  3707. parser.print_help()
  3708. raise
  3709. global verbose
  3710. verbose = cmd.verbose
  3711. if cmd.needsGit:
  3712. if cmd.gitdir == None:
  3713. cmd.gitdir = os.path.abspath(".git")
  3714. if not isValidGitDir(cmd.gitdir):
  3715. # "rev-parse --git-dir" without arguments will try $PWD/.git
  3716. cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
  3717. if os.path.exists(cmd.gitdir):
  3718. cdup = read_pipe("git rev-parse --show-cdup").strip()
  3719. if len(cdup) > 0:
  3720. chdir(cdup);
  3721. if not isValidGitDir(cmd.gitdir):
  3722. if isValidGitDir(cmd.gitdir + "/.git"):
  3723. cmd.gitdir += "/.git"
  3724. else:
  3725. die("fatal: cannot locate git repository at %s" % cmd.gitdir)
  3726. # so git commands invoked from the P4 workspace will succeed
  3727. os.environ["GIT_DIR"] = cmd.gitdir
  3728. if not cmd.run(args):
  3729. parser.print_help()
  3730. sys.exit(2)
  3731. if __name__ == '__main__':
  3732. main()