
    6h3                    2   S SK Jr  S SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
JrJr  SSKJr  \R                  " 5       rSrS	\-  rS
 rSr\(       a&  \R*                  \R,                  -  \R.                  -  rSrS rS rSS jrS S 4S jrS S 4S jrg)    )annotationsN)_)sha1nodeconstants)encodingerrorpycompat   )gitutil   a  
CREATE TABLE refs (
  -- node and name are unique together. There may be more than one name for
  -- a given node, and there may be no name at all for a given node (in the
  -- case of an anonymous hg head).
  node TEXT NOT NULL,
  name TEXT
);

-- The "possible heads" of the repository, which we use to figure out
-- if we need to re-walk the changelog.
CREATE TABLE possible_heads (
  node TEXT NOT NULL
);

CREATE UNIQUE INDEX possible_heads_idx ON possible_heads(node);

-- The topological heads of the changelog, which hg depends on.
CREATE TABLE heads (
  node TEXT NOT NULL
);

-- A total ordering of the changelog
CREATE TABLE changelog (
  rev INTEGER NOT NULL PRIMARY KEY,
  node TEXT NOT NULL,
  p1 TEXT,
  p2 TEXT,
  synthetic TEXT,
  changedfiles BOOLEAN
);

CREATE UNIQUE INDEX changelog_node_idx ON changelog(node);
CREATE UNIQUE INDEX changelog_node_rev_idx ON changelog(rev, node);

-- Changed files for each commit, which lets us dynamically build
-- filelogs.
CREATE TABLE changedfiles (
  node TEXT NOT NULL,
  filename TEXT NOT NULL,
  -- 40 zeroes for deletions
  filenode TEXT NOT NULL,
-- to handle filelog parentage:
  p1node TEXT,
  p1filenode TEXT,
  p2node TEXT,
  p2filenode TEXT
);

CREATE INDEX changedfiles_nodes_idx
  ON changedfiles(node);

-- Cached values to improve performance
CREATE TABLE cache (
  ncommits INTEGER
);
INSERT INTO cache (ncommits) VALUES (NULL);

PRAGMA user_version=%d
c                   [         R                  " [        R                  " U 5      5      n[        Ul        UR                  S5      R                  5       S   nUS:X  aL  [        R                  S5       H"  nUR                  UR                  5       5        M$     UR                  5         O*U[        :X  a  O[        R                  " [        S5      5      eUR                  S5        U$ )NzPRAGMA user_versionr   ;s(   sqlite database has unrecognized versionzPRAGMA journal_mode=WAL)sqlite3connectr   strfromlocalbytestext_factoryexecutefetchone_SCHEMAsplitstripcommit_CURRENT_SCHEMA_VERSIONr   Abortr   )pathdbres	statements       1/usr/lib/python3/dist-packages/hgext/git/index.py	_createdbr    W   s     
..t4	5BBO
***
+
4
4
6q
9C ax s+IJJy() , 				'	' kk!GHIIJJ()I     i    c                   [        U[        5      (       d   S[        U5      -  5       e[        U[        5      (       d   S[        U5      -  5       eU R                  SX$45       Vs1 s H  nUS   R	                  S5      iM     nnUR                  U[        5      nU H5  nUR                  R                  U;   d  M  UR                  R                  s  $    [        R                  " SX$U45      es  snf )az  Find the nearest ancestor that introduces a file node.

Args:
  db: a handle to our sqlite database.
  gitrepo: A pygit2.Repository instance.
  file_path: the path of a file in the repo
  walk_start: a pygit2.Oid that is a commit where we should start walking
              for our nearest ancestor.

Returns:
  A hexlified SHA that is the commit ID of the next-nearest parent.
zfile_path must be str, got %rzfilenode must be str, got %rzASELECT node FROM changedfiles WHERE filename = ? AND filenode = ?r   asciiz8Unable to find introducing commit for %s node %s from %s)
isinstancestrtyper   decodewalk
_OUR_ORDERidhexr   ProgrammingError)	r   gitrepo	file_path
walk_startfilenoderowparent_optionsinner_walkerws	            r   '_find_nearest_ancestor_introducing_noder6   }   s    i%% 'FJ ( % h$$ &DtH ' $
 ::2!

C 	Ag
   <<
J7L4488~%4488O  
 
 B	j) s   &C0c           
     n   [        U[        5      (       d   S[        U5      -  5       e[        U[        5      (       d   S[        U5      -  5       e[        R                  " X$4/5      nU(       Ga  UR                  5       u  pgX   n/ n	UR                   H  n
U
R                  nUR                  S5       H  n XU   R                     nM     [        XX:R                  UR                  R                  5      nU	R                  XR                  R                  45        M     [        R                  =n=n=nnU	 HU  u  nn[!        UR#                  SUUU45      R%                  5       S   5      nUS:X  d  M=  Uc   eUR                  UU45        MW     U	(       a  U	S   u  p['        U	5      S:X  a  U	S   u  nn['        U	5      S:  a  [(        R*                  " S	5      eUR#                  S
XUUUR                  R                  X745        U(       a  GM  UR-                  5         g! [         a       GM  f = f)ar  Given a starting commit and path, fill in a filelog's parent pointers.

Args:
  gitrepo: a pygit2.Repository
  db: a handle to our sqlite database
  startcommit: a hexlified node id for the commit to start at
  path: the path of the file whose parent pointers we should fill in.
  filenode: the hexlified node id of the file at startcommit

TODO: make filenode optional
zstartcommit must be str, got %rz!startfilenode must be str, got %r/zfSELECT COUNT(*) FROM changedfiles WHERE node = ? AND filename = ? AND filenode = ? AND p1node NOT NULLr   N   r	   s'   git support can't handle octopus mergeszUPDATE changedfiles SET p1node = ?, p1filenode = ?, p2node = ?, p2filenode = ? WHERE node = ? AND filename = ? AND filenode = ?)r%   r&   r'   collectionsdequepopleftparentstreer   r+   KeyErrorr6   r,   appendr
   nullgitintr   r   lenr   r-   r   )r.   r   startcommitr   startfilenodevisitcnoder1   r   r=   parenttcomp
introducerp1nodep1fnodep2nodep2fnodeparparfnodefounds                        r   fill_in_filelogrS      s0    S  =(4+<<=  s  A*T--@@A  ;<=E
--/nnFA

3$

+A ( Eyy!$$((
 
DDHH56 % /6oo===6G$MC

& $)	
 (*Q E z&c8_- % %ajOFw<1%ajOFGw<!((:  	

? fgvyy}}dM		
K %V IIKE   s   9H$$
H43H4c                B   UR                  SU45      R                  5       S   nU(       a  g X   n0 nUR                  (       a#  UR                  S   R                  R                  OS nUb)  U R                  XcR                  R                  [        S9nOUR                  R                  S[        S9nS U 5       nU V	s0 s HN  n	U	R                  R                  [        R                  :w  d  M-  U	R                  U	R                  R                  _MP     nn	UR                  5        H1  u  pUR                  SUR                  R                  XS S S S 45        M3     UR                  SUR                  R                  45        U(       a  UR                  5         g g s  sn	f )	Nz/SELECT changedfiles FROM changelog WHERE node=?r   )flagsT)swaprU   c              3  L   #    U  H  oR                   R                  v   M     g 7fN)deltanew_file).0ps     r   	<genexpr>%_index_repo_commit.<locals>.<genexpr>   s     48a!!8s   "$zwINSERT INTO changedfiles (node, filename, filenode, p1node, p1filenode, p2node, p2filenode) VALUES(?, ?, ?, ?, ?, ?, ?)z3UPDATE changelog SET changedfiles=TRUE WHERE node=?)r   r   r=   r+   r,   diff_DIFF_FLAGSr>   diff_to_treerawr   nullidr   itemsr   )r.   r   noder   already_donefilesp1patchgen	new_filesnfr\   ns               r   _index_repo_commitrm      sb   ::9D7hjL ]FE
 &,^^				!	!B	~<<IIMM<E;;++[+I484I B5599)000 	 
 
  	

6 YY]]A$dD9		
	  JJ=		?O 
		 's    ,F0%Fc                    g rX   r"   xs    r   <lambda>rq     s    Dr!   c                     g rX   r"   argskwargss     r   rq   rq     s    Tr!   c           	     	   U R                  5       n[        5       nU" S5      n[        U5       H  u  pxUb  UR                  U5        UR	                  S5      (       dD  UR	                  S5      (       d.  UR	                  S5      (       d  UR	                  S5      (       d  Ms   U R                  U5      R                  [        R                  5      n	UR                  U	R                  5        M     UR                  S5       V
s1 s H  n
[        R                  " U
S   5      iM     nn
S nU Vs1 s H  oR                  iM     nnX:X  a  g U" S5        U H/  n	Uc  U R!                  U	["        5      nM  UR%                  U	5        M1     UR                  S	5        Ub  UR'                  5         U" S
5      nSnU GH  nUb  UR                  U5        [(        R*                  =nn[-        UR.                  5      S::  a  UR.                  (       a#  UR.                  S   R                  R                  n[-        UR.                  5      S:X  a#  UR.                  S   R                  R                  nUS-  nUR                  SXR                  R                  UU45        M  [1        UR.                  5      nUR3                  S5      R                  R                  nU(       d  GM'  US-  n[-        U5      S:X  a  UR                  R                  nS nOSU-  nUR                  R                  nUR3                  S5      R                  R                  nUR                  SUUUUU45        UnU(       a  M  GM     UR                  S5        UR                  S5        UR5                  SU Vs/ s H  nUR                  4PM     sn5        UR                  S5        UR                  S5        Ub  UR'                  5         U" S5      nUR                  S5       Vs/ s H  nUS   R7                  S5      PM     nn[        U5       H%  u  p}Ub  UR                  U5        [9        XU5        M'     UR                  S5        UR;                  5         Ub  UR'                  5         g g ! [         a     GM|  f = fs  sn
f s  snf s  snf s  snf )Ns   indexing refszrefs/heads/z
refs/tags/zrefs/remotes/zrefs/hg/zSELECT node FROM possible_headsr   s$   heads mismatch, rebuilding dagcache
zDELETE FROM changelogs   indexing commitsr9   r	   zbINSERT INTO changelog (rev, node, p1, p2, synthetic, changedfiles) VALUES(?, ?, ?, ?, NULL, FALSE)z%040xz_INSERT INTO changelog (rev, node, p1, p2, synthetic, changedfiles) VALUES(?, ?, ?, ?, ?, FALSE)zDELETE FROM headszDELETE FROM possible_headsz+INSERT INTO possible_heads (node) VALUES(?)aB  
    INSERT INTO heads (node)
        SELECT node FROM possible_heads WHERE
            node NOT IN (
                SELECT DISTINCT possible_heads.node FROM changelog, possible_heads WHERE
                    changelog.p1 = possible_heads.node OR
                    changelog.p2 = possible_heads.node
            )
    zu
    UPDATE changelog SET changedfiles=TRUE WHERE node IN (
        SELECT DISTINCT node FROM changedfiles
    )
    s   indexing head fileszSELECT * FROM headsr$   zN
    UPDATE cache SET
        ncommits = (SELECT COUNT(1) FROM changelog)
    )listall_referencesset	enumerateupdate
startswithlookup_referencepeelpygit2GIT_OBJ_COMMIT
ValueErroraddr+   r   r   sysstrr,   r)   r*   pushcompleter
   rA   rC   r=   listpopexecutemanyr(   rm   r   )r.   r   logfnprogress_factoryall_refspossible_headsprogposrefstartrp   cache_headswalkerhcur_cache_headsr   rh   p2r=   thissynthhidr2   headss                           r   _index_repor     s)    ))+HUN,-Dh'KKNN=))~~l++~~o..~~j))	,,S166v7L7LME 	588$ (4 =>>A 	!>   F&45nuunO5%	
23>\\%4FKK	   JJ&'/0D CKK//!Rv~~!#~~^^A&))--6>>"a'^^A&))--1HCJJtiimmR,
 6>>*GQ""&&B'qw<1$!99==D E"S=D"IIMME[[^&&**

u$B.
 # '% J JJ"#JJ+,NN5-.~#''~. JJ	
 JJ	  23D*,**5J*K*K3Ag*K 
  E"KK7* #
 JJ	 IIK u  		
 6| 	/6s*   ".R#R.-R3$R8
R=
R+*R+c                    g rX   r"   ro   s    r   rq   rq     s    Tr!   c                     g rX   r"   rs   s     r   rq   rq     s    Dr!   c                h   [         R                  R                  [        R                  " U R                  5      SSS5      n[         R                  R                  U5      (       d  [         R                  " U5        [         R                  R                  US5      n[        U5      n[        XX5        U$ )Ns   ..s   .hgs   caches   git-commits.sqlite)	osr   joinr   fsencodeexistsmakedirsr    r   )r.   r   r   	cachepathdbpathr   s         r   	get_indexr     s     ',,'I 77>>)$$
IWW\\)%:;F	6	B U5Ir!   )F)
__future__r   r:   r   r   mercurial.i18nr   mercurial.noder   	mercurialr   r   r    r
   
get_pygit2r   r   r   r    r*   GIT_SORT_TOPOLOGICALGIT_SORT_TIMEGIT_SORT_REVERSEr`   r6   rS   rm   r   r   r"   r!   r   <module>r      s    "  	   ,   
			 ;x y< B6 
	##


	

!
!	"  $N>B'Z 1	Qj "4Pr!   