Skip to content

Instantly share code, notes, and snippets.

@blueforesticarus
Last active March 2, 2021 17:54
Show Gist options
  • Save blueforesticarus/e28a13a29ef8fba036c12415533d566e to your computer and use it in GitHub Desktop.
Save blueforesticarus/e28a13a29ef8fba036c12415533d566e to your computer and use it in GitHub Desktop.
shell script to produce a tar representing the difference between two directories or one directory and a base tar. (can be used as a filesystem patch)
#!/bin/bash
BASE=$2
OUT=diff.tar
if test -z "$TMP"; then
TMP=`mktemp -d -t tardiff-XXXXXXXX`
else
TMP=`realpath $TMP`
fi
if [ -f "$BASE" ]; then
tar -tf $BASE > $TMP/tar.list
rg --passthru '(^\.?/)|(/$)' -r '' $TMP/tar.list | sort | uniq | rg -N . > $TMP/files.base
else
(cd $BASE && fd -uu ) | sort | uniq > $TMP/files.base
fi
(cd $1 && fd -uu | sort | uniq) > $TMP/files.1
cat $TMP/files.1 $TMP/files.base | sort | uniq -d > $TMP/files.common
if [ -f "$BASE" ]; then
#foo $TMP/files.common.t $TMP/tar.list > $TMP/files.common.t
sed -e 's/^/.\//' $TMP/files.common > $TMP/files.common.t
tar -f "$BASE" --diff --directory $1 --no-recursion -T $TMP/files.common.t
else
( cd $BASE && tar -c --no-recursion -T $TMP/files.common ) | tee asdf | tar --diff --directory $1
fi | grep -v 'Mod time differs' | rg --passthru ': .*differs?$' -r '' \
| rg --passthru '^\./' -r '' | sort | uniq > $TMP/files.changed
cat $TMP/files.1 $TMP/files.common | sort | uniq -u > $TMP/files.added
cat $TMP/files.base $TMP/files.common | sort | uniq -u > $TMP/.deleted
(tar -c -C $1 --no-recursion -T $CWD/$TMP/files.changed -T $CWD/$TMP/files.added) > $OUT
tar -rf $OUT -C $TMP .deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment