/
git-vault-export
executable file
·50 lines (40 loc) · 1.35 KB
/
git-vault-export
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
trace () {
echo "`basename $0`: ${@:1}" 1>&2;
}
error() {
trace $@
exit 1
}
[ $# -eq 2 ] || error "Usage: $0 snapshot dst_dir"
SNAP=$1
DST=$2
if [ "${SNAP:0:1}" != ">" ]; then
SNAP=">$SNAP"
fi
git rev-parse --show-toplevel || error "Not a git dir $(pwd)"
git rev-parse "$SNAP" >/dev/null || error "There is no snapshot $SNAP"
[ -d $DST ] || error "There is no destination dir $DST"
# only $DST itself is expected
COUNT=$(find $DST -maxdepth 1 -not -name '.' | wc -l)
if [ "x$COUNT" != "x1" ]; then
error "Dst dir $DST should be empty, found $COUNT entries"
fi
trace "Export $SNAP to the $DST"
GIT_DIR=$(pwd)
(git archive --format=tar "$SNAP" | tar -C $DST -xf -) || \
error "Export of $SNAP to the $DST is failed"
find $DST -type l | grep '/blobs/' | while read -r LINK
do
NAME=$(readlink "$LINK")
# match minimal relative blob ref (path inside the tree is unit/blobs/link)
if [[ $NAME =~ \.\./\.\./\.git/blobs/ ]]; then
FNAME=$(basename $NAME)
DNAME="$GIT_DIR/.git/blobs/$(basename $(dirname $NAME))"
[ -d $DNAME ] || error "There is no blob dir $DNAME for $LINK"
FNAME="$DNAME/$FNAME"
[ -f $FNAME ] || error "There is no blob file $FNAME for $LINK"
unlink "$LINK" || error "Can't unlink $LINK"
cp $FNAME "$LINK" || error "Can't copy $FNAME to $LINK"
fi
done