c? `dZddlZddlZddlZddlZddlZddlmZddlm Z m Z m Z ddl m Z ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZdZ iZ!ej"e!Z"iZ#ej$e#Z$dddddd d d Z%e%&D]\Z'Z(e$d d e'ze(d e$d dde$d ddd:dZ)dddedfZ*ddgededfZ+ddgededfZ,ddgededfZ-d d!ded"fZ.dd#ded$fZ/ed%Z0e"d e*e+e,e-e.e/ge0e"j1&d'Z2d(Z3d)Z4d*Z5d+Z6d,Z7d-Z8d.Z9d/Z:d0Z;d1Zd4Z?d5Z@d6ZAd7ZBGd8d9ZCdS);a"rewrite file content in changesets or working copy (EXPERIMENTAL) Provides a command that runs configured tools on the contents of modified files, writing back any fixes to the working copy or replacing changesets. Here is an example configuration that causes :hg:`fix` to apply automatic formatting fixes to modified lines in C++ code:: [fix] clang-format:command=clang-format --assume-filename={rootpath} clang-format:linerange=--lines={first}:{last} clang-format:pattern=set:**.cpp or **.hpp The :command suboption forms the first part of the shell command that will be used to fix a file. The content of the file is passed on standard input, and the fixed file content is expected on standard output. Any output on standard error will be displayed as a warning. If the exit status is not zero, the file will not be affected. A placeholder warning is displayed if there is a non-zero exit status but no standard error output. Some values may be substituted into the command:: {rootpath} The path of the file being fixed, relative to the repo root {basename} The name of the file being fixed, without the directory path If the :linerange suboption is set, the tool will only be run if there are changed lines in a file. The value of this suboption is appended to the shell command once for every range of changed lines in the file. Some values may be substituted into the command:: {first} The 1-based line number of the first line in the modified range {last} The 1-based line number of the last line in the modified range Deleted sections of a file will be ignored by :linerange, because there is no corresponding line range in the version being fixed. By default, tools that set :linerange will only be executed if there is at least one changed line range. This is meant to prevent accidents like running a code formatter in such a way that it unexpectedly reformats the whole file. If such a tool needs to operate on unchanged files, it should set the :skipclean suboption to false. The :pattern suboption determines which files will be passed through each configured tool. See :hg:`help patterns` for possible values. However, all patterns are relative to the repo root, even if that text says they are relative to the current working directory. If there are file arguments to :hg:`fix`, the intersection of these patterns is used. There is also a configurable limit for the maximum size of file that will be processed by :hg:`fix`:: [fix] maxfilesize = 2MB Normally, execution of configured tools will continue after a failure (indicated by a non-zero exit status). It can also be configured to abort after the first such failure, so that no files will be affected if any tool fails. This abort will also cause :hg:`fix` to exit with a non-zero status:: [fix] failure = abort When multiple tools are configured to affect a file, they execute in an order defined by the :priority suboption. The priority suboption has a default value of zero for each tool. Tools are executed in order of descending priority. The execution order of tools with equal priority is unspecified. For example, you could use the 'sort' and 'head' utilities to keep only the 10 smallest numbers in a text file by ensuring that 'sort' runs before 'head':: [fix] sort:command = sort -n head:command = head -n 10 sort:pattern = numbers.txt head:pattern = numbers.txt sort:priority = 2 head:priority = 1 To account for changes made by each tool, the line numbers used for incremental formatting are recomputed before executing the next tool. So, each tool may see different values for the arguments added by the :linerange suboption. Each fixer tool is allowed to return some metadata in addition to the fixed file content. The metadata must be placed before the file content on stdout, separated from the file content by a zero byte. The metadata is parsed as a JSON value (so, it should be UTF-8 encoded and contain no zero bytes). A fixer tool is expected to produce this metadata encoding if and only if the :metadata suboption is true:: [fix] tool:command = tool --prepend-json-metadata tool:metadata = true The metadata values are passed to hooks, which can be used to print summaries or perform other post-fixing work. The supported hooks are:: "postfixfile" Run once for each file in each revision where any fixer tools made changes to the file content. Provides "$HG_REV" and "$HG_PATH" to identify the file, and "$HG_METADATA" with a map of fixer names to metadata values from fixer tools that affected the file. Fixer tools that didn't affect the file have a value of None. Only fixer tools that executed are present in the metadata. "postfix" Run once after all files and revisions have been handled. Provides "$HG_REPLACEMENTS" with information about what revisions were created and made obsolete. Provides a boolean "$HG_WDIRWRITTEN" to indicate whether any files in the working copy were updated. Provides a list "$HG_METADATA" mapping fixer tool names to lists of metadata values returned from executions that modified a file. This aggregates the same metadata previously passed to the "postfixfile" hook. Fixer tools are run in the repository's root directory. This allows them to read configuration files from the working copy, or even write to the working copy. The working copy is not updated to match the revision being fixed. In fact, several revisions may be fixed in parallel. Writes to the working copy are not amended into the revision being fixed; fixer tools should always write fixed file content back to stdout as documented above. N)_)nullidnullrevwdirrev)procutil)cmdutilcontextcopieserror logcmdutilmatchmdiffmerge mergestatepycompat registrar rewriteutilscmutilutilworkersships-with-hg-coreFT)scommands linerangespatternsprioritymetadatas skipcleansenabledfixs.*:%s$)defaultgeneric maxfilesizes2MB)rfailurecontinuec|dd}|dvr4tjtd|fztd|dkrtj||dS) z)Abort with 'message' if fix.failure=abortrr)rabortsunknown fix.failure action: %ssuse "continue" or "abort"hintrN)configr Abortr)uimessager!actions +/usr/lib/python3/dist-packages/hgext/fix.pychecktoolfailureactionr(s YYvz * *F ,,,k / 0 0F9 </00    k'----r)alls)fix all non-public non-obsolete revisionsbasesdrevisions to diff against (overrides automatic selection, and applies to every revision being fixed)sREVrrevsrevisions to fix (ADVANCED)ssources1fix the specified revisions and their descendantsws working-dirsfix the working directorywholesalways fix every line of a files[OPTION]... [FILE]...) helpcategoryc tjtjdddtjdddg55d5t}t|t||\}}t|tt|fd}tjd|t|d }t!jt$} t!jt&} i} d} t)|d } t-d t-d t/|5}|D])\}}}}|||D] \}}| ||!|3|| ||<|||d}j ddditj|||xxdzcc<| r|| ds| }|}|t>kr.tA|| || tC| |} ntE|| || | |=| r|| d+ dddn #1swxYwYtG| | | | | d}jddd itj|dddn #1swxYwYdddn #1swxYwYddddS#1swxYwYdS)arewrite file content in changesets or working directory Runs any configured tools to fix the content of files. Only affects files with changes, unless file arguments are provided. Only affects changed lines of files, unless the --whole flag is used. Some tools may always affect the whole file regardless of --whole. If --working-dir is used, files with uncommitted changes in the working copy will be fixed. Note that no backup are made. If revisions are specified with --source, those revisions and their descendants will be checked, and they may be replaced with new revisions that have fixed file content. By automatically including the descendants, no merging, rebasing, or evolution will be required. If an ancestor of the working copy is included, then the working copy itself will also be fixed, and the working copy will be updated to the fixed parent. When determining what lines of each file to fix at each revision, the whole set of revisions being fixed is considered, so that fixes to earlier revisions are not forgotten in later ones. The --base flag can be used to override this default behavior, though it is not usually desirable to do so. r*r/r- working_dirrc 3K|D]^\}}}|}||}t || |\}}||kr|nd}|D] } | |||fV _dSN)datafixfile)itemssrcrevpathdstrevsctxolddatametadatanewdatar7dstrevbasectxs basepathsfixersoptsrepor$s r'getfixeszfix..getfixess). 9 9%g6ld)..**$+V$ % %!'"#*W"4"4ww$%99F!44888889+ 9 9r)g?F) threadsafeT)reversesfixingsfiles)topicunittotalitemN)r-spathr postfixfilethrow)s replacementss wdirwrittenrpostfix)rO)rS)$r byteskwargsrcheck_at_most_one_argcheck_incompatible_argumentswlocklock transaction getrevstofix getbasectxs getworkqueue getbasepaths getfixers_prefetchfilesrtuple collections defaultdictdictlistsorted makeprogressrsumvalues incrementr9appendhook strkwargspoprwriteworkingdirbool replacerevcleanup)r$rFpatsrE revstofix workqueuenumitemsrGresultsfiledataaggregatemetadata replacements wdirwritten commitorderprogressrevr;filerevmetadatar@ fixername fixermetadatahookargsr=rBrCrDs`` ` @@@r'fixrs:   % %D !$ 6BBB ( nvy1 `J`Jtyy{{`J`JD$4$4V$<$<`J`J T400 tT955* dD)X   8!tYAA 2 tY 222 9 9 9 9 9 9 9 9 9 92- Xuww e   *400'3D99  Y555 __I,,Qx[[HOO & &3T?G"""---0?0E0E0G0GGG,I}%i077 FFFF&*1HSM$' #!%%4  H DI&##,X66  " "&(;r?*C&%//++Cs)Cg~~'c8C=,OOO&*8C=&9&9 "2tS(3-NNN  "&(;r?*C&+ & & & & & & & & & & & & & & & &D lK000)'*    IIDIH,>x,H,HIIIA`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`J`JsM63M D4M=D.K8 , M8K< <M?K< ;M; MM MM M M6M" "M6%M" &M66M:=M:crd|D}tj||dddS)aCalls scmutil.cleanupnodes() with the given replacements. "replacements" is a dict from nodeid to nodeid, with one key and one value for every revision that was affected by fixing. This is slightly different from cleanupnodes(). "wdirwritten" is a bool which tells whether the working copy was affected by fixing, since it has no entry in "replacements". Useful as a hook point for extending "hg fix" with output summarizing the effects of the command, though we choose not to output anything here. ci|] \}}||g Sr).0precsuccs r' zcleanup..|s HHHZT4D4&HHHr)rT)fixphaseN)r9r cleanupnodes)rFryrzs r'rqrqosDIH<3E3E3G3GHHHL |VdCCCCCCr)c Ttjt}tjt}|dd}t |D]} || } t j| ||} t t||||| || | D]} | | } | r| |kr:| tdtj|| fzqtd|| D}|| || f| || xxdz cc<ѐd|D}|||fS)aConstructs a list of files to fix and which revisions each fix applies to To avoid duplicating work, there is usually only one work item for each file revision that might need to be fixed. There can be multiple work items per file revision if the same file needs to be fixed in multiple changesets with different baserevs. Each work item also contains a list of changesets where the file's data should be replaced with the fixed data. The work items for earlier changesets come earlier in the work queue, to improve pipelining by allowing the first changeset to be replaced while fixes are still being computed for later changesets. Also returned is a map from changesets to the count of work items that might affect each changeset. This is used later to count when all of a changeset's work items have been finished, without having to inspect the remaining work queue in each worker subprocess. The example work item (1, "foo/bar.txt", (1, 2, 3)) means that the data of bar.txt should be read from revision 1, then fixed, and written back to revisions 1, 2 and 3. Revision 1 is called the "srcrev" and the list of revisions is called the "dstrevs". In practice the srcrev is always one of the dstrevs, and we make that choice when constructing the work item so that the choice can't be made inconsistently later on. The dstrevs should all have the same file revision for the given path, so the choice of srcrev is arbitrary. The wdirrev can be a dstrev and a srcrev. rrs!ignoring file larger than %s: %s c3>K|]}|VdSr6)r})rr=s r' zgetworkqueue..s*@@3SWWYY@@@@@@r)rQc@g|]\\}}}}t|||fSr)min)r_filerev _baserevsr;r<s r' z getworkqueue..s@ 0 'Xy$ WtW%r))rarbrdint configbytesrerr pathstofixislinksizewarnrr bytecountr`filerevrjr9sort)r$rFrrrErsrB dstrevmapru maxfilesizer}fixctxr r;fctxbaserevsrts r'r\r\s4'--I&s++H..88Ki  c fdD11 r4tUHSM6 J J    D$ >s C C C SMMMQ MMMM 4=OO4E4EINN h r)c|dr|d}n|drtj||d}t|d|}t|vr|t|d|vr|tnWttj||d}|dr|tt|dkr t|vs*tj |tj ||d t|vrGtj|rt#jd d |st#jd d |S)z8Returns the set of revision numbers that should be fixedr*s+(not public() and not obsolete()) or wdir()r/s(%ld::) - obsolete().r-r4rQrsunresolved conflictssuse 'hg resolve'r sno changesets specifiedsuse --source or --working-dir)revsr revrangesetraddr}getlenrcheckunfinishedrprecheck mergestatemodrreadunresolvedcountr r#)r$rFrEr source_revss r'rZrZs F| yyGHH i  )$Y@@ 4994kBBCC k ! ! HHW    :>>  t # # HHW   :&tT&\::;; 88N # #  HHW    IINNw$%%%T40004  $ ) )$ / / ? ? A A k18KLLLL  k &-M     Kr)c (t}|D]}|||t|t|} |tt j| j| j| j| j |S)zReturns the set of files that should be fixed in a context The result depends on the base contexts; we include any file that has changed relative to any of the base contexts. Base contexts should be ancestors of the context being fixed. )r listclean listunknown) rstatusroupdate itertoolschainaddedmodifiedcleanunknown) r$rFrrrEr rBrfilesbasectxstats r'rrs EEE   ~~ %4::4::    J tz4<       Lr)cz|drtd|Sg}|D]}||||f|}||vr||} nd} |t| |t |S)aReturns the set of line ranges that should be fixed in a file Of the form [(10, 20), (30, 40)]. This depends on the given base contexts; we must consider lines that have changed versus any of the base contexts, and whether the file has been renamed versus any of them. Another way to understand this is that we exclude line ranges that are common to the file in all base contexts. r1r))rdifflinerangesr}r7extend unionranges) rEr;rCrBrcontent2 rangeslistrbasepathcontent1s r' linerangesrs xx-c8,,,J>>=='++--t!DdKK w  x(--//HHH.8<<==== z " ""r)c"|driSi}|D]r\}}}||}||D][} tj| |||} | | vr,| || ||f<\s|S)Nr1)rr pathcopiesr}) rFrErtrBrCr:r;_dstrevsrrrs r'r]r]s xx I"+JJhf' J JG(&99==dDIIH7""AI 7;;==&**,,=> J r)ctt|}g}|r|dg|dd}}|D]F\}}|d\}}||dzkr|||f1|t||f|d<G|S)aReturn the union of some closed intervals >>> unionranges([]) [] >>> unionranges([(1, 100)]) [(1, 100)] >>> unionranges([(1, 100), (1, 100)]) [(1, 100)] >>> unionranges([(1, 100), (2, 100)]) [(1, 100)] >>> unionranges([(1, 99), (1, 100)]) [(1, 100)] >>> unionranges([(1, 100), (40, 60)]) [(1, 100)] >>> unionranges([(1, 49), (50, 100)]) [(1, 100)] >>> unionranges([(1, 48), (50, 100)]) [(1, 48), (50, 100)] >>> unionranges([(1, 2), (3, 4), (5, 6)]) [(1, 6)] rrQNrR)rerrjmax)runionedabcds r'rr s,J((JG>)!}oz!""~))1r{1 q1u99 NNAq6 " " " "c!Qii.GBKK Nr)cg}tj||D]8\}}|dd\}}|dkr ||kr||dz|f9|S)aReturn list of line number ranges in content2 that differ from content1. Line numbers are 1-based. The numbers are the first and last line contained in the range. Single-line ranges have the same line number for the first and last line. Excludes any empty ranges that result from lines that are only present in content1. Relies on mdiff's idea of where the line endings are in the string. >>> from mercurial import pycompat >>> lines = lambda s: b'\n'.join([c for c in pycompat.iterbytestr(s)]) >>> difflineranges2 = lambda a, b: difflineranges(lines(a), lines(b)) >>> difflineranges2(b'', b'') [] >>> difflineranges2(b'a', b'') [] >>> difflineranges2(b'', b'A') [(1, 1)] >>> difflineranges2(b'a', b'a') [] >>> difflineranges2(b'a', b'A') [(1, 1)] >>> difflineranges2(b'ab', b'') [] >>> difflineranges2(b'', b'AB') [(1, 2)] >>> difflineranges2(b'abc', b'ac') [] >>> difflineranges2(b'ab', b'aCb') [(2, 2)] >>> difflineranges2(b'abc', b'aBc') [(2, 2)] >>> difflineranges2(b'ab', b'AB') [(1, 2)] >>> difflineranges2(b'abcde', b'aBcDe') [(2, 2), (4, 4)] >>> difflineranges2(b'abcde', b'aBCDe') [(2, 4)] !rQ)r allblocksrj)rrrangeslineskind firstlinelastlines r'rrCsoNFx::55 t#AaCj 8 4<zgetbasectxs..s222#DI222r)ci|]}|Srr)rr}rBs r'rzgetbasectxs..s333#X333r)) rrr rrrarbreparentsr}rr)rFrErsrr}r=pctxrBs` @r'r[r[rs- xx4z*4'1B1BCCDD !yH222222233333333&s++Hi  ((3iKKMM ( (DxxzzX%% $$Xdhhjj%9:::: !!$''''  ( Or)c0t}|D])\}}}|tkr|||f*|D] \\}}}} ||| f!|r#t jfd|DdSdS)NcFg|]\}}|tj|gfSr)r matchfiles)rr}r;rFs r'rz"_prefetchfiles..sA   Cg(v667   r))rrrr9r prefetchfiles) rFrtrC toprefetchr:r;rbaserevfixrevrs ` r'r_r_sJ#,''h W   ~&&&&.7__->->,,)&$*++++      !+          r)c i}||} |D] \} } | |||rt|||||| } | ||| } | Ld}||| |fz}|||d| fzt jtj | dtj |j t j t j t j }| | \}}|r$t||| ||}| ri |dd\}}t#j||| <n<#t&$r*|t+d| fzYwxYwd|| <|jd kr|} |sPPGr6::<<GDDD&1223 W s+0F0GGctjd|D]t}|rp|d|%|tddn|d|zd|d||fzudS) a9Writes the lines of the stderr string as warnings on the ui Uses the revision number and fixername to give more context to each line of the error message. Doesn't include file names, since those take up a lot of space and would tend to be included in the error message if they were relevant. s[ ]+[Nswdirs evolve.rev)labels%ds ] %s: %s )rerrr)r$r}rrlines r'rrsV,,77  7 GGDMMM{' -8888 =999 GGMY$55 6 6 677r)c|D]5\}}||}|||6|j}|||}||krs|jtksJ|j5tj |||ddddS#1swxYwYdSdS)aWrite new content to the working copy and check out the new p1 if any We check out a new revision if and only if we fixed something in both the working directory and its parent revision. This avoids the need for a full update/merge, and means that the working directory simply isn't affected unless the --working-dir flag is given. Directly updates the dirstate for the affected files. N) r9writeflagsdirstatep1rp2r parentchanger movedirstate) rFr=rwryr;r7roldp1newp1s r'rnrns/nn&&'' d4y 4&&&& M    E   UE * *E ~~}!!V++++ ] ' ' ) ) 4 4  tE{ 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4~s=C&&C*-C*c &|j|\}}||||}}|||} |||} |s.||vr||vrdS|} || d<tj |} | || tj || tj| |||D]f} || }|}| | || ||r| | |gt+j||d|D}| ||| || | f|}|}|}||kr,|d|zdS|||<dS)aCommit a new revision like the given one, but with file content changes "ctx" is the original revision to be replaced by a modified one. "filedata" is a dict that maps paths to their new file content. All other paths will be recreated from the original revision without changes. "filedata" may contain paths that didn't exist in the original revision; they will be added. "replacements" is a dict that maps a single node to a single node, and it is updated to indicate the original revision is replaced by the newly created one. No entry is added if the replacement's node already exists. The new revision has the same parents as the old one, unless those parents have already been replaced, in which case those replacements are the parents of this new revision. Thus, if revisions are replaced in topological order, there is no need to rebase them into the original topology later. Ns fix_source)wc)r ci|] \}}||g Srr)roldnodenewnodes r'rzreplacerev..Js!III 07)IIIr))textbranchextradaterusersnode %s already existed ) changelog parentrevsr}rnodercopyhexr overlayworkingctxsetbaser revert_tor graftcopiesr keys copysourcer r  markcopiedrupdate_hash_refs descriptionr9tomemctxrrrcommitr)r$rFr=rwryp1revp2revp1ctxp2ctx newp1node newp2noderwctxr;rr&descmemctxsucnodeprenodes r'rprps(>,,SWWYY77LE5;U 5E  uzz||<zgetfixers..s Q0Ar)T)keyrI) fixernames configboolr" configintrrrFixerra OrderedDictrer9) r$rDnameenabledrpattern linerangepriorityr? skipcleans r'r^r^^s F2--{(:;;))FD;$677))FD;$677IIfd]&:;; <<|(;<<== )<==MM&$*>?? ? GGBCCtgM    _ GGBCCtgM      HH:dWD E E E E )XxF4LL  "v||~~#A#A4PPP  r)ct}|dD]8\}}d|vr/||ddd9|S)z>Returns the names of [fix] config options that have suboptionsr:rQr)r configitemsrr)r$nameskvs r'r<r<s^ EEEv&&++1 199 IIaggdA&&q) * * * Lr)c*eZdZdZdZdZdZdZdS)r?z4Wraps the raw config values for a fixer with methodscZ||_||_||_||_||_||_dSr6)_command_pattern _lineranger9 _metadata _skipclean)selfrrCrDrEr?rFs r'__init__zFixer.__init__s2   #!!#r)c|}tj|j|j|jg|}||S)z@Should this fixer run on the file at the given path and context?)r=)rFmatchmodr rrP)rTrErr;rFmatchers r'rz Fixer.affectssF{{}}. Ity4=/v   wt}}r)c|jS)z@Should the stdout of this fixer start with JSON and a null byte?)rR)rTs r'rzFixer.shouldoutputmetadatas ~r)c .tj}|||j|tj|dg}|jr;|jr|sdS|D]-\}}||||j||d.d |S)zA shell command to use to invoke this fixer on the given file/lines May return None if there is no appropriate command to run for the given parameters. )srootpathsbasenameN)sfirstslast ) rrendercommandtemplaterOosr;basenamerQrSrjjoin)rTr$r;rexpandpartsfirstlasts r'rz Fixer.commands . F "1A1A$1G1GHH    ?  v t%   t FDO-M-M yyr)N)__name__ __module__ __qualname____doc__rUrrrrr)r'r?r?sV>>$$$     r)r?r6)Drgrarr]rrmercurial.i18nrmercurial.noderrrmercurial.utilsr mercurialrr r r r r rWrrrrrrrrrr testedwithcmdtabler configtable configitem FIXER_ATTRSr9r;rr(alloptbaseoptrevopt sourceoptwdiroptwholeoptusageCATEGORY_FILE_CONTENTSrrqr\rZrrr]rrr[r_r8rrnrpr^r<r?rr)r'rysttn  %$$$$$,#  ) H % % !Y !+ . .   %%''GGLCJvy3FFFFF  6>62222  6:{3333 . . . . vuaa LMM N A  AfII  AA<==qqyy I A:;;AfII    *F(G(G H 5!!$F"G"G H  "##   Wfi(; /  }J}J  }J@DDD";;;|@,###:      F,,,^<   .@@@F777$444,I+I+I+X###L1 1 1 1 1 1 1 1 1 1 r)