Skip to content

Instantly share code, notes, and snippets.

@gheift
Created March 28, 2020 15:12
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 gheift/dc77b83f5f9d7989d5fa1a29292965dd to your computer and use it in GitHub Desktop.
Save gheift/dc77b83f5f9d7989d5fa1a29292965dd to your computer and use it in GitHub Desktop.
#!/usr/bin/env sh
# autocmd BufWritePost * execute ":silent ! git-commit-file %"
FILE="$1"
test -e "$FILE" || exit
if [ "${FILE#/}" = "${FILE}" ]; then
FILE="./${FILE}"
fi
cd "${FILE%/*}"
FILE="${FILE##*/}"
if [ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1; then
GITDIR="$(git rev-parse --git-dir 2> /dev/null)"
WORKBASE="${GITDIR%/.git}"
# check if rebase is in progress
if test -d "$(git rev-parse --git-path rebase-merge 2> /dev/null)" || test -d "$(git rev-parse --git-path rebase-apply 2> /dev/null)"; then
# do nothing
true
elif test -f "$(git rev-parse --git-path BISECT_START 2> /dev/null)"; then
# do nothing
true
elif test -n "$(git check-ignore "./$FILE" 2> /dev/null)"; then
# do nothing
true
else
FULL_NAME="$(git ls-files --full-name "$FILE")"
if [ -n "$FULL_NAME" ]; then
git commit -o "$FILE" -m "saved file $FULL_NAME" > /dev/null 2>&1
else
git add "$FILE"
git commit -o "$FILE" -m "new file ${PWD#$WORKBASE/}/$FILE" > /dev/null 2>&1
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment