Skip to content

Instantly share code, notes, and snippets.

@isaac-ped
Created August 7, 2023 00:16
Show Gist options
  • Save isaac-ped/8752a652355ec5b890d46634e6db496e to your computer and use it in GitHub Desktop.
Save isaac-ped/8752a652355ec5b890d46634e6db496e to your computer and use it in GitHub Desktop.
git-trash
#!/usr/bin/env bash
REALPATH="$(nix-shell -p coreutils -p which --pure --run 'which realpath')"
#echo $REALPATH
usage() {
echo "Usage: $0 files..."
echo "Move files into a datestamped .trash/ directory at the root of the git repo"
}
GIT_ROOT="$(git rev-parse --show-toplevel)"
DATELABEL="$(date "+%Y-%m-%d")"
TRASHROOT="$GIT_ROOT/.trash/$DATELABEL"
relpath() {
$REALPATH "$1" -m --relative-to "$GIT_ROOT"
}
announce() {
src="$(relpath $1)"
dst="$(trashloc $1)"
echo "$src -> $(relpath $dst)"
}
trashloc() {
TO_TRASH="$1"
DEST_BASE="$TRASHROOT/$(relpath $1)"
DEST="$DEST_BASE"
i=0
while [[ -e $DEST ]]; do
i=$(( i + 1 ))
DEST="${DEST_BASE}_$i"
done
echo "$DEST"
}
set -eu
for TRASHFILE in "$@"; do
announce $TRASHFILE
DEST="$(trashloc $TRASHFILE)"
mkdir -p "$(dirname "$DEST")"
mv "$TRASHFILE" "$(trashloc $TRASHFILE)"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment