| 1 |
#!/bin/sh |
| 2 |
"""": # -*-python-*- |
| 3 |
bup_python="$(dirname "$0")/bup-python" || exit $? |
| 4 |
exec "$bup_python" "$0" ${1+"$@"} |
| 5 |
""" |
| 6 |
|
| 7 |
from argparse import ArgumentParser |
| 8 |
from glob import glob |
| 9 |
from os import fsencode, unlink |
| 10 |
from os.path import splitext |
| 11 |
from subprocess import PIPE, run |
| 12 |
from sys import stderr, stdin |
| 13 |
from tempfile import TemporaryDirectory |
| 14 |
import os, re, sys |
| 15 |
|
| 16 |
# Currently does nothing bup-specific, i.e. it's up to the caller to |
| 17 |
# reset midx, bloom, etc. if that's relevant. |
| 18 |
|
| 19 |
script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 20 |
bup = os.path.realpath(f'{script_dir}/../bup') |
| 21 |
|
| 22 |
def runc(*args, **kwargs): |
| 23 |
kwargs['check'] = True |
| 24 |
return run(*args, **kwargs) |
| 25 |
|
| 26 |
def log(*args, **kwargs): |
| 27 |
if 'file' not in kwargs: |
| 28 |
kwargs['file'] = stderr |
| 29 |
if 'end' not in kwargs: |
| 30 |
kwargs['end'] = '' |
| 31 |
print(*args, **kwargs) |
| 32 |
|
| 33 |
def logn(*args, **kwargs): |
| 34 |
kwargs['end'] = '\n' |
| 35 |
log(*args, **kwargs) |
| 36 |
|
| 37 |
def all_objects(repo): |
| 38 |
git = runc(('git', '--git-dir', repo, 'cat-file', '--batch-all-objects', |
| 39 |
f'--batch-check=%(objectname) %(objecttype)'), |
| 40 |
stdout=PIPE) |
| 41 |
for line in git.stdout.splitlines(): |
| 42 |
yield line.split() |
| 43 |
|
| 44 |
parser = ArgumentParser() |
| 45 |
add_arg = parser.add_argument |
| 46 |
add_arg('--drop-oids', action='store_true', |
| 47 |
help='drop (hex ASCII) oids given on standard input') |
| 48 |
add_arg('repo', metavar='<repository>', nargs=1, help='victim repository') |
| 49 |
opt = parser.parse_args() |
| 50 |
|
| 51 |
repo = opt.repo[0] |
| 52 |
|
| 53 |
if not opt.drop_oids: |
| 54 |
logn(f'error: --drop-oids is currently required') |
| 55 |
sys.exit(2) |
| 56 |
|
| 57 |
packdir = f'{repo}/objects/pack' |
| 58 |
if not os.path.isdir(packdir): |
| 59 |
logn(f'error: {repo!r} does not appear to be a git repository') |
| 60 |
sys.exit(2) |
| 61 |
|
| 62 |
objs = list(all_objects(repo)) |
| 63 |
packs = glob(f'{packdir}/*.pack') |
| 64 |
|
| 65 |
with TemporaryDirectory(prefix='perforate-', dir=repo) as tmpdir: |
| 66 |
tmprepo = f'{tmpdir}/repo' |
| 67 |
runc((bup, '-d', tmprepo, 'init')) |
| 68 |
for pack in packs: |
| 69 |
with open(pack, 'rb') as pf: |
| 70 |
runc(('git', '--git-dir', tmprepo, 'unpack-objects'), stdin=pf) |
| 71 |
|
| 72 |
newobjs = list(all_objects(tmprepo)) |
| 73 |
if not set(x[0] for x in newobjs) == set(x[0] for x in objs): |
| 74 |
logn(f'error: does the source repo have loose objects?') |
| 75 |
sys.exit(2) |
| 76 |
|
| 77 |
# For now assumes only one subdir level, and that locale decoding is fine |
| 78 |
oid_rx = re.compile(b'[0-9A-Za-z]{40}') |
| 79 |
for oidx in stdin.buffer: |
| 80 |
oidx = oidx.rstrip() |
| 81 |
if not oid_rx.fullmatch(oidx): |
| 82 |
logn(f'error: oid {oidx} is not a 40-char hash') |
| 83 |
unlink(b'%s/objects/%s/%s' % (fsencode(tmprepo), oidx[:2], oidx[2:])) |
| 84 |
|
| 85 |
for pack in packs: |
| 86 |
stem, _ = splitext(pack) |
| 87 |
for pack_related in glob(f'{stem}.*'): |
| 88 |
unlink(pack_related) |
| 89 |
|
| 90 |
new_oids = (x[0] for x in all_objects(tmprepo)) |
| 91 |
runc(('git', '--git-dir', tmprepo, 'pack-objects', f'{packdir}/pack'), |
| 92 |
input=b'\n'.join(new_oids)) |