Created
September 3, 2021 03:21
-
-
Save doitian/149539874141557d6f6421c984e0f494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.