Skip to content

Instantly share code, notes, and snippets.

@jasagredo
Last active March 28, 2023 15:32
Show Gist options
  • Save jasagredo/3e2bc11bc46ef0ef6e69c4650589ec6f to your computer and use it in GitHub Desktop.
Save jasagredo/3e2bc11bc46ef0ef6e69c4650589ec6f to your computer and use it in GitHub Desktop.
Diffs the revisions in chap
#!/usr/bin/env sh
# This script requires difftastic and toml-cli, which can be installed by `cargo install`
pkg=$2
version=$3
what_to_do=$1
if [ ! -d _sources/$pkg/$version ]; then
echo "Package $pkg and/or version $version doesn't exist"
exit 1
fi
ghub=$(toml get _sources/$pkg/$version/meta.toml github.repo | cut -d "\"" -f 2)
repo_name=$(echo $ghub | cut -d'/' -f 2)
subdir=$(toml get _sources/$pkg/$version/meta.toml subdir | cut -d "\"" -f 2)
grev=$(toml get _sources/$pkg/$version/meta.toml github.rev | cut -d "\"" -f 2)
revs=$(toml get _sources/$pkg/$version/meta.toml revisions | jq ".[] | .number" | sort)
checkout () {
if [ ! -d "$repo_name" ]; then
git clone "https://github.com/$ghub"
fi
( (cd "$repo_name" && git fetch --all && git checkout $grev) >/dev/null 2>&1)
echo "Checked out $repo_name at commit $grev"
}
apply_rev () {
cp "_sources/$pkg/$version/revisions/$rev.cabal" "$repo_name/$subdir/$pkg.cabal"
( cd "$repo_name" && git add $subdir/$pkg.cabal && git commit -m "rev $rev")
}
read_a_key() (
exec </dev/tty
t=$(stty -g)
trap 'stty "$t"; return' EXIT INT QUIT TERM
stty raw -echo isig
dd count=1 2>/dev/null
)
apply_rev_and_diff () {
rev=$1
echo "Diffing revision $rev, press something to continue..."
read_a_key >/dev/null
cp "_sources/$pkg/$version/revisions/$rev.cabal" "$repo_name/$subdir/$pkg.cabal"
( cd "$repo_name" && GIT_EXTERNAL_DIFF=difft git diff && git add $subdir/$pkg.cabal && git commit -m "rev $rev" >/dev/null 2>&1)
}
apply_up_to () {
for rev in $revs; do
if [ $rev -eq $1 ]; then
break
fi
apply_rev $rev
done
}
apply_and_diff_starting () {
for rev in $revs; do
if [ $rev -lt $1 ]; then
continue
else
apply_rev_and_diff $rev
fi
done
}
case $what_to_do in
last)
checkout
last=$(echo "$revs" | tail -n1)
apply_up_to $last
apply_and_diff_starting $last
;;
diff_from)
checkout
apply_up_to $4
apply_and_diff_starting $4
;;
diff_all)
checkout
apply_and_diff_starting 1
;;
*)
printf "Options are:\n\t- last: diffs the last revision\n\t- diff_from: diffs from the provided revision\n\t- diff_all: diffs all the revisions\n"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment