
    Vh                        d Z ddlmZ ddlZddlZddlZddlZddlm	Z	 ddZ
 G d d      Z G d	 d
      Z G d d      Zy)z#Diff parsing functions and classes.    )annotationsN   )ApplicationErrorc                ,    t        |       j                  S )zgParse the given diff lines and return a list of FileDiff objects representing the changes of each file.)
DiffParserfiles)liness    K/home/dcms/DCMS/lib/python3.12/site-packages/ansible_test/_internal/diff.py
parse_diffr      s    e"""    c                  2    e Zd ZdZddZddZedd       Zy)	FileDiffzParsed diff for a single file.c                h    t        |d      | _        t        |d      | _        g | _        d| _        y )NF)newT)DiffSideoldr   headersbinary)selfold_pathnew_paths      r
   __init__zFileDiff.__init__   s,    H%0H$/"$r   c                :    | j                   j                  |       y)z;Append the given line to the list of headers for this file.N)r   append)r   lines     r
   append_headerzFileDiff.append_header   s    D!r   c                ^    | j                   j                  xr | j                  j                  S ).True if the diff is complete, otherwise False.)r   is_completer   r   s    r
   r   zFileDiff.is_complete    s#     xx##<(<(<<r   N)r   strr   r!   returnNoner   r!   r"   r#   r"   bool)__name__
__module____qualname____doc__r   r   propertyr    r   r
   r   r      s#    (" = =r   r   c                  D    e Zd ZdZddZd	dZd
dZedd       ZdddZ	y)r   z1Parsed diff for a single 'side' of a single file.c                    || _         || _        | j                  rdnd| _        d| _        d| _        g | _        g | _        g | _        d| _        d| _	        d| _
        y )N+-Tr   )pathr   prefixeof_newlineexistsr	   lines_and_contextranges_next_line_number_lines_remaining_range_start)r   r1   r   s      r
   r   zDiffSide.__init__)   s\    	!XXc3,.
8:-/!" !r   c                .    || _         || _        d| _        y)z%Set the starting line and line count.r   N)r7   r8   r9   )r   
line_start
line_counts      r
   	set_startzDiffSide.set_start8   s    !+ *r   c                   | j                   dk  rt        d      | j                  |f}|j                  d      rn_|j                  | j                        r9| j
                  j                  |       | j                  s| j                  | _        nt        d      | j                  j                  |       | xj                   dz  c_         | j                  rl| j                  r| j                  }n#|j                  d      r| j                  dz
  }nd}|r.| j                  j                  | j                  |f       d| _        | xj                  dz  c_        y)zAppend the given line.r   zDiff range overflow. zUnexpected diff content prefix.r   N)r8   	Exceptionr7   
startswithr2   r	   r   r9   r5   r   r6   )r   r   entry	range_ends       r
   r   zDiffSide.append>   s     A%233&&,??3__T[[)JJe$$$$($:$:!=>>%%e," 22	% 22Q6		""D$5$5y#AB$%!!#r   c                     | j                   dk(  S )r   r   )r8   r    s    r
   r   zDiffSide.is_completea   s     $$))r   c                    |r| j                   }n| j                  }|D cg c]  }d| j                  |d   |d   fz   c}S c c}w )zIFormat the diff and return a list of lines, optionally including context.z	%s:%4d %sr   r   )r5   r	   r1   )r   contextr	   r   s       r
   format_lineszDiffSide.format_linesf   sE    **EJJEHMNtyy$q'47;;NNNs   AN)r1   r!   r   r&   r"   r#   )r;   intr<   rH   r"   r#   r$   r%   )T)rF   r&   r"   	list[str])
r'   r(   r)   r*   r   r=   r   r+   r   rG   r,   r   r
   r   r   &   s/    ;!$F * *Or   r   c                  H    e Zd ZdZd
dZddZddZddZddZddZ	ddZ
y	)r   zParse diff lines.c           	     P   || _         g | _        | j                  | _        d| _        d | _        d | _        d | _        | j                   D ]>  | _        | xj                  dz  c_        	 | j                          | j                  | _        @ | j                          y # t        $ r}t        j                  d      j                         || j                  | j
                  xs d| j                  xs dt        j                         fz  }t        |j                               d d }~ww xY w)Nr   r   z
                %s

                     Line: %d
                 Previous: %s
                  Current: %s
                %s
                 )r	   r   process_startactionline_numberprevious_liner   filer@   textwrapdedentstrip	traceback
format_excr   complete_file)r   r	   exmessages       r
   r   zDiffParser.__init__s   s   
%'
((.2%)	*.	 	+DI!B& "&D/	+2 	)  B"// +  UW$$&&,"IIO((*  'w}}7TA!Bs   &B	D%#A=D  D%c                    | j                          t        j                  d| j                        }|st	        d      t        |j                  d      |j                  d            | _        | j                  | _	        y)zProcess a diff start line.zD^diff --git "?(?:a/)?(?P<old_path>.*)"? "?(?:b/)?(?P<new_path>.*)"?$zUnexpected diff start line.r   r   N)
rW   researchr   r@   r   grouprQ   process_continuerN   r   matchs     r
   rM   zDiffParser.process_start   sb    		acgclclm9::U[[4ekk*6MN	++r   c                   t        j                  d| j                        }|st        d      | j                  j
                  j                  t        |j                  d      xs d      t        |j                  d                   | j                  j                  j                  t        |j                  d      xs d      t        |j                  d                   | j                  | _        y)	zProcess a diff range line.zg^@@ -((?P<old_start>[0-9]+),)?(?P<old_count>[0-9]+) \+((?P<new_start>[0-9]+),)?(?P<new_count>[0-9]+) @@zUnexpected diff range line.	old_startr   	old_count	new_start	new_countN)r[   r\   r   r@   rQ   r   r=   rH   r]   r   process_contentrN   r_   s     r
   process_rangezDiffParser.process_range   s    		  E  GK  GP  GP  Q9::		EKK$<$A BCT_H`Dab		EKK$<$A BCT_H`Dab**r   c                    | j                   j                  d      r| j                          y| j                   j                  d      r| j                          y| j	                          y)z+Process a diff start, range or header line.zdiff z@@ N)r   rA   rM   rg   process_headerr    s    r
   r^   zDiffParser.process_continue   sJ    99( YY!!%( !r   c                T   | j                   j                  d      rd| j                  _        y| j                   dk(  rd| j                  j                  _        y| j                   dk(  rd| j                  j                  _        y| j                  j                  | j                          y)zProcess a diff header line.zBinary files Tz--- /dev/nullFz+++ /dev/nullN)r   rA   rQ   r   r   r4   r   r   r    s    r
   ri   zDiffParser.process_header   sn    990#DIIYY/)#(DIIMM YY/)#(DIIMM II##DII.r   c                :   | j                   dk(  r| j                  j                  d      r7d| j                  j                  _        d| j                  j                  _        y| j                  j                  d      rd| j                  j                  _        y| j                  j                  d      rd| j                  j                  _        yt        d      | j                  j                  r| j                          y| j                   j                  d      r_| j                  j                  j                  | j                          | j                  j                  j                  | j                          y| j                   j                  d      r0| j                  j                  j                  | j                          y| j                   j                  d      r0| j                  j                  j                  | j                          yt        d      )	zProcess a diff content line.z\ No newline at end of filer?   Fr0   r/   z&Unexpected previous diff content line.NzUnexpected diff content line.)r   rP   rA   rQ   r   r3   r   r@   r   r^   r   r    s    r
   rf   zDiffParser.process_content   st   9966!!,,S1,1		),1		)  ##..s3,1		)  ##..s3,1		)    HII 99  !!#99$IIMM  +IIMM  +YY!!#&IIMM  +YY!!#&IIMM  +;<<r   c                h    | j                   sy| j                  j                  | j                          y)z0Complete processing of the current file, if any.N)rQ   r   r   r    s    r
   rW   zDiffParser.complete_file   s"    yy

$))$r   N)r	   rI   r"   r#   )r"   r#   )r'   r(   r)   r*   r   rM   rg   r^   ri   rf   rW   r,   r   r
   r   r   p   s)    #J
,	+"	/=:%r   r   )r	   rI   r"   zlist[FileDiff])r*   
__future__r   r[   rR   rU   typingtutilr   r   r   r   r   r,   r   r
   <module>rq      sF    ) " 	   
#
= =&GO GOTu% u%r   