| 1 |
# Copyright (C) 2015 - Mike Edgar <[email protected]> |
| 2 |
# |
| 3 |
# This extension enables removal of file content at a given revision, |
| 4 |
# rewriting the data/metadata of successive revisions to preserve revision log |
| 5 |
# integrity. |
| 6 |
|
| 7 |
"""erase file content at a given revision |
| 8 |
|
| 9 |
The censor command instructs Mercurial to erase all content of a file at a given |
| 10 |
revision *without updating the changeset hash.* This allows existing history to |
| 11 |
remain valid while preventing future clones/pulls from receiving the erased |
| 12 |
data. |
| 13 |
|
| 14 |
Typical uses for censor are due to security or legal requirements, including:: |
| 15 |
|
| 16 |
* Passwords, private keys, cryptographic material |
| 17 |
* Licensed data/code/libraries for which the license has expired |
| 18 |
* Personally Identifiable Information or other private data |
| 19 |
|
| 20 |
Censored nodes can interrupt mercurial's typical operation whenever the excised |
| 21 |
data needs to be materialized. Some commands, like ``hg cat``/``hg revert``, |
| 22 |
simply fail when asked to produce censored data. Others, like ``hg verify`` and |
| 23 |
``hg update``, must be capable of tolerating censored data to continue to |
| 24 |
function in a meaningful way. Such commands only tolerate censored file |
| 25 |
As having a censored version in a checkout is impractical. The current head |
| 26 |
revisions of the repository are checked. If the revision to be censored is in |
| 27 |
any of them the command will abort. You can configure this behavior using the |
| 28 |
following option: |
| 29 |
|
| 30 |
`censor.policy` |
| 31 |
:config-doc:`censor.policy` |
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import annotations |
| 35 |
|
| 36 |
from mercurial.i18n import _ |
| 37 |
from mercurial.node import short |
| 38 |
|
| 39 |
from mercurial import ( |
| 40 |
error, |
| 41 |
registrar, |
| 42 |
scmutil, |
| 43 |
) |
| 44 |
|
| 45 |
cmdtable = {} |
| 46 |
command = registrar.command(cmdtable) |
| 47 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 48 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 49 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 50 |
# leave the attribute unspecified. |
| 51 |
testedwith = b'ships-with-hg-core' |
| 52 |
|
| 53 |
|
| 54 |
@command( |
| 55 |
b'censor', |
| 56 |
[ |
| 57 |
( |
| 58 |
b'r', |
| 59 |
b'rev', |
| 60 |
[], |
| 61 |
_(b'censor file from specified revision'), |
| 62 |
_(b'REV'), |
| 63 |
), |
| 64 |
( |
| 65 |
b'', |
| 66 |
b'check-heads', |
| 67 |
True, |
| 68 |
_(b'check that repository heads are not affected'), |
| 69 |
), |
| 70 |
(b't', b'tombstone', b'', _(b'replacement tombstone data'), _(b'TEXT')), |
| 71 |
], |
| 72 |
_(b'-r REV [-t TEXT] [FILE]'), |
| 73 |
helpcategory=command.CATEGORY_MAINTENANCE, |
| 74 |
) |
| 75 |
def censor(ui, repo, path, rev=(), tombstone=b'', check_heads=True, **opts): |
| 76 |
with repo.wlock(), repo.lock(): |
| 77 |
return _docensor( |
| 78 |
ui, |
| 79 |
repo, |
| 80 |
path, |
| 81 |
rev, |
| 82 |
tombstone, |
| 83 |
check_heads=check_heads, |
| 84 |
**opts, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def _docensor(ui, repo, path, revs=(), tombstone=b'', check_heads=True, **opts): |
| 89 |
if not path: |
| 90 |
raise error.Abort(_(b'must specify file path to censor')) |
| 91 |
if not revs: |
| 92 |
raise error.Abort(_(b'must specify revisions to censor')) |
| 93 |
|
| 94 |
wctx = repo[None] |
| 95 |
|
| 96 |
m = scmutil.match(wctx, (path,)) |
| 97 |
if m.anypats() or len(m.files()) != 1: |
| 98 |
raise error.Abort(_(b'can only specify an explicit filename')) |
| 99 |
path = m.files()[0] |
| 100 |
flog = repo.file(path) |
| 101 |
if not len(flog): |
| 102 |
raise error.Abort(_(b'cannot censor file with no history')) |
| 103 |
|
| 104 |
revs = scmutil.revrange(repo, revs) |
| 105 |
if not revs: |
| 106 |
raise error.Abort(_(b'no matching revisions')) |
| 107 |
file_nodes = set() |
| 108 |
for r in revs: |
| 109 |
try: |
| 110 |
ctx = repo[r] |
| 111 |
file_nodes.add(ctx.filectx(path).filenode()) |
| 112 |
except error.LookupError: |
| 113 |
raise error.Abort(_(b'file does not exist at revision %s') % ctx) |
| 114 |
|
| 115 |
if check_heads: |
| 116 |
heads = [] |
| 117 |
repo_heads = repo.heads() |
| 118 |
msg = b'checking for the censored content in %d heads\n' |
| 119 |
msg %= len(repo_heads) |
| 120 |
ui.status(msg) |
| 121 |
for headnode in repo_heads: |
| 122 |
hc = repo[headnode] |
| 123 |
if path in hc and hc.filenode(path) in file_nodes: |
| 124 |
heads.append(hc) |
| 125 |
if heads: |
| 126 |
headlist = b', '.join([short(c.node()) for c in heads]) |
| 127 |
raise error.Abort( |
| 128 |
_(b'cannot censor file in heads (%s)') % headlist, |
| 129 |
hint=_(b'clean/delete and commit first'), |
| 130 |
) |
| 131 |
|
| 132 |
msg = b'checking for the censored content in the working directory\n' |
| 133 |
ui.status(msg) |
| 134 |
wp = wctx.parents() |
| 135 |
if ctx.node() in [p.node() for p in wp]: |
| 136 |
raise error.Abort( |
| 137 |
_(b'cannot censor working directory'), |
| 138 |
hint=_(b'clean/delete/update first'), |
| 139 |
) |
| 140 |
|
| 141 |
msg = b'censoring %d file revisions\n' |
| 142 |
msg %= len(file_nodes) |
| 143 |
ui.status(msg) |
| 144 |
with repo.transaction(b'censor') as tr: |
| 145 |
flog.censorrevision(tr, file_nodes, tombstone=tombstone) |