Skip to content

Instantly share code, notes, and snippets.

@soraxas
Last active July 1, 2020 16:46
Show Gist options
  • Save soraxas/735c512a785b8278b9faffdbb303b37b to your computer and use it in GitHub Desktop.
Save soraxas/735c512a785b8278b9faffdbb303b37b to your computer and use it in GitHub Desktop.
Git utilities! :) It's now being tracked in a standalone repository https://github.com/soraxas/git-utils
#!/bin/bash
# test git is working in cwd
# git || exit 1
# declare a wrapper function for git
git() {
command git "$@" || exit 1
}
BRANCH=`git show-ref | grep $(git show-ref -s -- HEAD) | sed 's|.*/\(.*\)|\1|' | grep -v HEAD | sort | uniq`
HASH=`git rev-parse $BRANCH`
PREV=`git rev-list --topo-order HEAD..$HASH | tail -1`
echo $BRANCH
echo $HASH
echo $PREV
git checkout $PREV
#!/bin/bash
# test git is working in cwd
# git log || exit 1
# declare a wrapper function for git
git() {
command git "$@" || exit 1
}
git checkout HEAD~
#!/bin/sh
#set -x
WIP_TOKEN="[FIXME]: local wip DIRTY files commit (to be reverted)"
WIP_HAS_STAGED_TOKEN="[FIXME]: local wip STAGED files commit (to be reverted)"
GREEN='\033[0;32m'
RED='\033[0;31m'
MAGENTA='\033[0;35m'
NC='\033[0m'
say_action() {
printf "$MAGENTA>> ${@}${NC}\n"
}
say() {
printf "$GREEN>> ${@}${NC}\n"
}
failed() {
printf "$RED>> Something went wrong. ${@}${NC}\n"
exit 1
}
check_has_wip() {
git update-index --refresh
git diff-index --quiet HEAD --
}
check_staged() {
x="$(git diff --name-only --cached)"
if [ "$x" != "" ]; then
say_action "There are staged files, adding WIP staged files commit"
# non-empty means there is files to be staged.
git commit --no-verify -m "${WIP_HAS_STAGED_TOKEN}" || failed
fi
}
# https://stackoverflow.com/questions/45352246/how-to-switch-and-save-without-commit-in-git
main() {
x="$(git log -1 --pretty=%B)"
if [ "$x" = "${WIP_TOKEN}" ]; then
say_action "Undoing WIP commit"
git reset --soft HEAD^ || failed
git reset
x="$(git log -1 --pretty=%B)"
if [ "$x" = "${WIP_HAS_STAGED_TOKEN}" ]; then
say_action "Undoing staged commit"
git reset --soft HEAD^ || failed
fi
else
if [ "$(check_has_wip)" = "" ]; then
# nothing to add to wip! :)
say "No WIP work found, exiting."
exit 0
fi
check_staged
say_action "Adding WIP commit with all files"
git add -u || failed
git commit --no-verify -am "${WIP_TOKEN}" || failed
fi
say "Done"
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment