censor.py
100 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
/ hgext / censor.py
Mike Edgar
|
r24347 | # Copyright (C) 2015 - Mike Edgar <adgar@google.com> | ||
# | ||||
# This extension enables removal of file content at a given revision, | ||||
# rewriting the data/metadata of successive revisions to preserve revision log | ||||
# integrity. | ||||
"""erase file content at a given revision | ||||
The censor command instructs Mercurial to erase all content of a file at a given | ||||
revision *without updating the changeset hash.* This allows existing history to | ||||
remain valid while preventing future clones/pulls from receiving the erased | ||||
data. | ||||
Typical uses for censor are due to security or legal requirements, including:: | ||||
Mads Kiilerich
|
r26781 | * Passwords, private keys, cryptographic material | ||
Mike Edgar
|
r24347 | * Licensed data/code/libraries for which the license has expired | ||
* Personally Identifiable Information or other private data | ||||
Censored nodes can interrupt mercurial's typical operation whenever the excised | ||||
data needs to be materialized. Some commands, like ``hg cat``/``hg revert``, | ||||
simply fail when asked to produce censored data. Others, like ``hg verify`` and | ||||
``hg update``, must be capable of tolerating censored data to continue to | ||||
function in a meaningful way. Such commands only tolerate censored file | ||||
FUJIWARA Katsunori
|
r24890 | revisions if they are allowed by the "censor.policy=ignore" config option. | ||
Mike Edgar
|
r24347 | """ | ||
Gregory Szorc
|
r28092 | from __future__ import absolute_import | ||
from mercurial.i18n import _ | ||||
Mike Edgar
|
r24347 | from mercurial.node import short | ||
Gregory Szorc
|
r28092 | |||
from mercurial import ( | ||||
error, | ||||
Yuya Nishihara
|
r32337 | registrar, | ||
Gregory Szorc
|
r28092 | scmutil, | ||
) | ||||
Mike Edgar
|
r24347 | |||
cmdtable = {} | ||||
Yuya Nishihara
|
r32337 | command = registrar.command(cmdtable) | ||
Augie Fackler
|
r29841 | # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for | ||
Augie Fackler
|
r25186 | # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should | ||
# be specifying the version(s) of Mercurial they are tested with, or | ||||
# leave the attribute unspecified. | ||||
Augie Fackler
|
r29841 | testedwith = 'ships-with-hg-core' | ||
Mike Edgar
|
r24347 | |||
@command('censor', | ||||
[('r', 'rev', '', _('censor file from specified revision'), _('REV')), | ||||
('t', 'tombstone', '', _('replacement tombstone data'), _('TEXT'))], | ||||
rdamazio@google.com
|
r40329 | _('-r REV [-t TEXT] [FILE]'), | ||
helpcategory=command.CATEGORY_MAINTENANCE) | ||||
Mike Edgar
|
r24347 | def censor(ui, repo, path, rev='', tombstone='', **opts): | ||
Matt Harbison
|
r38460 | with repo.wlock(), repo.lock(): | ||
FUJIWARA Katsunori
|
r27290 | return _docensor(ui, repo, path, rev, tombstone, **opts) | ||
def _docensor(ui, repo, path, rev='', tombstone='', **opts): | ||||
Mike Edgar
|
r24347 | if not path: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('must specify file path to censor')) | ||
Mike Edgar
|
r24347 | if not rev: | ||
Pierre-Yves David
|
r26587 | raise error.Abort(_('must specify revision to censor')) | ||
Mike Edgar
|
r24347 | |||
FUJIWARA Katsunori
|
r25806 | wctx = repo[None] | ||
m = scmutil.match(wctx, (path,)) | ||||
if m.anypats() or len(m.files()) != 1: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('can only specify an explicit filename')) | ||
FUJIWARA Katsunori
|
r25806 | path = m.files()[0] | ||
Mike Edgar
|
r24347 | flog = repo.file(path) | ||
if not len(flog): | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot censor file with no history')) | ||
Mike Edgar
|
r24347 | |||
rev = scmutil.revsingle(repo, rev, rev).rev() | ||||
try: | ||||
ctx = repo[rev] | ||||
except KeyError: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('invalid revision identifier %s') % rev) | ||
Mike Edgar
|
r24347 | |||
try: | ||||
fctx = ctx.filectx(path) | ||||
except error.LookupError: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('file does not exist at revision %s') % rev) | ||
Mike Edgar
|
r24347 | |||
fnode = fctx.filenode() | ||||
Valentin Gatien-Baron
|
r39651 | heads = [] | ||
for headnode in repo.heads(): | ||||
Yuya Nishihara
|
r39697 | hc = repo[headnode] | ||
if path in hc and hc.filenode(path) == fnode: | ||||
heads.append(hc) | ||||
Mike Edgar
|
r24347 | if heads: | ||
headlist = ', '.join([short(c.node()) for c in heads]) | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot censor file in heads (%s)') % headlist, | ||
Mike Edgar
|
r24347 | hint=_('clean/delete and commit first')) | ||
wp = wctx.parents() | ||||
if ctx.node() in [p.node() for p in wp]: | ||||
Pierre-Yves David
|
r26587 | raise error.Abort(_('cannot censor working directory'), | ||
Mike Edgar
|
r24347 | hint=_('clean/delete/update first')) | ||
Gregory Szorc
|
r39814 | with repo.transaction(b'censor') as tr: | ||
flog.censorrevision(tr, fnode, tombstone=tombstone) | ||||