Skip to content

Instantly share code, notes, and snippets.

@doitian
Created September 3, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doitian/149539874141557d6f6421c984e0f494 to your computer and use it in GitHub Desktop.
Save doitian/149539874141557d6f6421c984e0f494 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
ACTION="${1:-init}"
TMPDIR="$(cd "${TMPDIR:-/tmp}" && pwd)"
escape() {
sed 's;[\\/];%;g'
}
case "$ACTION" in
init)
if git rev-parse --is-inside-work-tree &> /dev/null; then
echo 'Error: already in a git work tree' >&2
exit 1
fi
git init --separate-git-dir "$TMPDIR/$(pwd | escape).git" > /dev/null
git config user.email 'tmp@localhost' > /dev/null
git add --all > /dev/null
git commit -n -m 'initial commit' > /dev/null
git status
;;
done)
GIT_DIR="$(git rev-parse --git-dir)"
TMP_GIT_DIR="$TMPDIR/$(git rev-parse --show-toplevel | escape).git"
if ! [ "${GIT_DIR#/private}" = "${TMP_GIT_DIR#/private}" ]; then
echo 'Error: this repo is not created by git tmp' >&2
echo "Expected: ${TMP_GIT_DIR}" >&2
echo "Real: ${GIT_DIR}" >&2
exit 1
fi
DOT_GIT_FILE="$(git rev-parse --show-toplevel)/.git"
echo "rm -f '$DOT_GIT_FILE'"
rm -f "$DOT_GIT_FILE"
echo "rm -rf '$GIT_DIR'"
rm -rf "$GIT_DIR"
;;
*)
echo "ckb tmp init|done"
;;
esac
@doitian
Copy link
Author

doitian commented Sep 3, 2021

Sometimes, I want to do batch replace in the directory not tracked by git.

This script helps me to preview the changes and rollback when things go wrong by creating a temporary git repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment