Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 62 additions & 50 deletions git-task.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
# FIXME: git-stash changes the ctime of files. This causes vim to think that
# files have changes. Find a way to avoid this, if possible.

_TASKBRANCH="git-task"
_TASKBRANCH="${TASKBRANCH:-tasks}"
_DEBUG="${DEBUG:-false}"

log () {
echo $* >&1
}

error () {
echo $* >&2
exit 1
echo $* >&2
exit 1
}

branch_exists () {
Expand All @@ -28,59 +33,64 @@ current_branch () {

# stash the current branch and remember its name
prepare () {
#echo "Preparing transaction..."
#echo "Checking for .git directory..."
# TODO: currently only works in the git root directory.
if [[ ! -d .git ]]; then
error "Git dir not found. Is this the root directory of the repository?"
fi

# if this fails, something is horribly wrong.
#echo "Stashing current branch..."
git stash save --include-untracked \
"git-task stash. You should never see this." &>/dev/null
if [[ $? -ne 0 ]]; then
error "[FATAL] Stashing failed, bailing out. Your working directory might be dirty."
fi

#echo "Checking out task-branch..."
git checkout -q ${_TASKBRANCH}
if [[ $? -ne 0 ]]; then
#echo "No task branch. Creating new orphan branch..."
git checkout -q --orphan "${_TASKBRANCH}" HEAD || rollback 1
#echo "Unstaging everything..."
git rm -q --cached -r "*" || rollback 1
fi

#echo "Done preparing."
$_DEBUG && log "Preparing transaction..."
$_DEBUG && log "Checking for .git directory..."
# TODO: currently only works in the git root directory.
_OLDDIR="$PWD"
cd $( git rev-parse --show-toplevel )
if [[ ! -d .git ]]; then
error "Git dir not found. Is this the root directory of the repository?"
exit 1
fi

# source env file if exists
[ -f ${_TASKBRANCH}.config ] && source ./${_TASKBRANCH}.config
# if this fails, something is horribly wrong.
$_DEBUG && log "Stashing current branch..."
git stash save --include-untracked \
"git-task stash. You should never see this." &>/dev/null
if [[ $? -ne 0 ]]; then
error "[FATAL] Stashing failed, bailing out. Your working directory might be dirty."
fi

$_DEBUG && log "Checking out task-branch..."
git checkout -q ${_TASKBRANCH}
if [[ $? -ne 0 ]]; then
$_DEBUG && log "No task branch. Creating new orphan branch..."
git checkout -q --orphan "${_TASKBRANCH}" HEAD || rollback 1
$_DEBUG && log "Unstaging everything..."
git rm -q --cached -r "*" || rollback 1
fi

cd $_OLDDIR
$_DEBUG && log "Done preparing."
}

task_commit () {
#echo "Starting task transaction..."
#echo "Recording task..."
TASKDATA=.task task $* || rollback 1

# add and commit the changes
#echo "Adding task to git..."
git add .task || rollback 1
#echo "Committing task..."
git commit -q -m "$*" || rollback 1
#echo "Transaction done."
$_DEBUG && log "Starting task transaction..."
$_DEBUG && log "Recording task..."
TASKDATA=.task task $* || rollback 1
# add and commit the changes
$_DEBUG && log "Adding task to git..."
git add .task || rollback 1
$_DEBUG && log "Committing task..."
git commit -q -m "$*" || rollback 1
$_DEBUG && log "Transaction done."
}

rollback () {
#echo "Rolling back..."
# Since we stashed, there should™ be nothing that could go wrong here.
#echo "Checking out working branch..."
git checkout -f ${_CURRENT} &>/dev/null
if [[ $? -ne 0 ]]; then
error "[FATAL] Couldn't rollback to previous state: checkout to ${_CURRENT} failed. There should be a stash with your uncommited changes."
fi
#echo "Applying the stash..."
git stash pop -q
#echo "Done rolling back."

exit $1
$_DEBUG && log "Rolling back..."
# Since we stashed, there should™ be nothing that could go wrong here.
$_DEBUG && log "Checking out working branch..."
git checkout -f ${_CURRENT} &>/dev/null
if [[ $? -ne 0 ]]; then
error "[FATAL] Couldn't rollback to previous state: checkout to ${_CURRENT} failed. There should be a stash with your uncommited changes."
fi
$_DEBUG && log "Applying the stash..."
git stash pop -q
$_DEBUG && log "Done rolling back."

exit $1
}

# BEGIN SCRIPT
Expand All @@ -92,3 +102,5 @@ task_commit $*
rollback

exit 0

# vim: ts=2 sw=2 sts=2 et :