Last active
July 1, 2020 16:46
-
-
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
This file contains hidden or 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 | |
# 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 |
This file contains hidden or 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 | |
# test git is working in cwd | |
# git log || exit 1 | |
# declare a wrapper function for git | |
git() { | |
command git "$@" || exit 1 | |
} | |
git checkout HEAD~ |
This file contains hidden or 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/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