
    	^cx                         d Z ddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZ dZdZdZd	Zd
ZdZdZdZdZdZdZdZ ej        d          Z ej        d          Zd Zd ZddZd Zd ZdS )aa  core code for "sidedata" support

The "sidedata" are stored alongside the revision without actually being part of
its content and not affecting its hash. It's main use cases is to cache
important information related to a changesets.

The current implementation is experimental and subject to changes. Do not rely
on it in production.

Sidedata are stored in the revlog itself, thanks to a new version of the
revlog. The following format is currently used::

    initial header:
        <number of sidedata; 2 bytes>
    sidedata (repeated N times):
        <sidedata-key; 2 bytes>
        <sidedata-entry-length: 4 bytes>
        <sidedata-content-sha1-digest: 20 bytes>
        <sidedata-content; X bytes>
    normal raw text:
        <all bytes remaining in the rawtext>

This is a simple and effective format. It should be enough to experiment with
the concept.
    N   )errorrequirements)	constantsflagutil)hashutil                     	   
         z>Hz>HL20sc           	         t          |                                           } |                                  t                              t          |                     g}| D ]g\  }}t          j        |                                          }|	                    t                              |t          |          |                     h| D ]\  }}|	                    |           d                    |          }|S )N    )listitemssortSIDEDATA_HEADERpacklenr   sha1digestappendSIDEDATA_ENTRYjoin)sidedatabufkeyvaluer   s        @/usr/lib/python3/dist-packages/mercurial/revlogutils/sidedata.pyserialize_sidedatar&   ?   s    HNN$$%%HMMOOOH..
/C A A
Uu%%,,..

>&&sCJJ??@@@@  
U

5
((3--CJr   c                    i }d}t                               | d t           j                           \  }|t           j        z  }t           j        t          j        |z  z   }t	          |          D ]}|t          j        z   }t                              | ||                   \  }}}	|}||z   }
t          | ||
                   }t          j        |                                          }|	|k    rt          j
        ||	|          |||<   |
}|S )Nr   )r   unpacksizer   rangebytesr   r   r   r   SidedataHashError)blobr!   offsetnbentry
dataoffseti
nextoffsetr#   r)   storeddigestnextdataoffset	entrytext
readdigests                r%   deserialize_sidedatar7   L   s   HF ''-C/C-C(DEEJW
o""F %)<w)FGJ7^^ $ $n11
"0"7"7VJ=N8O"P"PT<#d*$z.89::	]9--4466
:%%)#|ZHHH!#

Or   Fc                    t          j        t                    }t          j        t                    }|| j        z
  }| j        |z
  }|r||}}| j                                        D ]]\  }}|                                D ]C\  }	}
|	|v r||                             |
           |	|v r||                             |
           D^| ||f}|S )a  
    Returns a dictionary mapping revlog types to tuples of
    `(repo, computers, removers)`:
        * `repo` is used as an argument for computers
        * `computers` is a list of `(category, (keys, computer, flags)` that
           compute the missing sidedata categories that were asked:
           * `category` is the sidedata category
           * `keys` are the sidedata keys to be affected
           * `flags` is a bitmask (an integer) of flags to remove when
              removing the category.
           * `computer` is the function `(repo, store, rev, sidedata)` that
             returns a tuple of
             `(new sidedata dict, (flags to add, flags to remove))`.
             For example, it will return `({}, (0, 1 << 15))` to return no
             sidedata, with no flags to add and one flag to remove.
        * `removers` will remove the keys corresponding to the categories
          that are present, but not needed.
        If both `computers` and `removers` are empty, sidedata will simply not
        be transformed.
    )collectionsdefaultdictr   _wanted_sidedata_sidedata_computersr   r   )reporemote_sd_categoriespullsd_computerssd_removersto_generate	to_removerevlog_kind	computerscategorycomputersidedata_helperss               r%   get_sidedata_helpersrI   a   s    , *400L)$//K&)>>K%(<<I 8!*KY"&":"@"@"B"B : :Y"+//"3"3 	: 	:Hh;&&[)00:::9$$K(//999		: lK8r   c                 2   |\  }}}| j         }d}d}	|                    |g           D ]-\  }
}} ||| ||          \  }}||d         z  }|	|d         z  }	.|                    |g           D ]&\  }}}|D ]}|                    |d           |	|z  }	'|||	ffS )aO  Returns the sidedata for the given revision after running through
    the given helpers.
    - `store`: the revlog this applies to (changelog, manifest, or filelog
      instance)
    - `sidedata_helpers`: see `get_sidedata_helpers`
    - `sidedata`: previous sidedata at the given rev, if any
    - `rev`: affected rev of `store`
    r   r	   N)rD   getpop)storerH   r!   revr=   r@   rA   kindflags_to_addflags_to_remove_keyssd_computer_flagsflagskeys	_computerr#   s                    r%   run_sidedata_helpersrX      s     '7#D,DLO&2&6&6tR&@&@ $ $"{F%+dE3AA%a 58#"-//$";"; ! !i 	$ 	$CLLd####5 lO444r   c                     ddl m} t          j        | j        v r|                     t                     |                     t          j	        t          t          f|j
        t          j                   d S )Nr   )metadata) rZ   requirementsmodCOPIESSDC_REQUIREMENTr   register_wanted_sidedataSD_FILESregister_sidedata_computerr   KIND_CHANGELOGcopies_sidedata_computerr   REVIDX_HASCOPIESINFO)r=   rZ   s     r%   set_sidedata_spec_for_repord      su    ,0AAA%%h///## 	)%    r   )F) __doc__r9   structr[   r   r   r\   revlogutilsr   r   utilsr   SD_TEST1SD_TEST2SD_TEST3SD_TEST4SD_TEST5SD_TEST6SD_TEST7SD_P1COPIESSD_P2COPIESSD_FILESADDEDSD_FILESREMOVEDr_   Structr   r   r&   r7   rI   rX   rd    r   r%   <module>rv      s'   6      5 5 5 5 5 5 5 5 - - - - - - - -          &-%%x((
 
 
  *& & & &R5 5 50    r   