From 4d2fb6f564cd367bd979ecf2885f07e874aa2ea7 Mon Sep 17 00:00:00 2001 From: SClo Date: Wed, 9 May 2018 21:07:16 +0200 Subject: [PATCH 1/3] refactor : add _DEBUG, preserve OLDDIR, use git's root local directory --- git-task.sh | 106 ++++++++++++++++++++++++++++------------------------ 1 file changed, 58 insertions(+), 48 deletions(-) diff --git a/git-task.sh b/git-task.sh index 0b88d86..21d302d 100755 --- a/git-task.sh +++ b/git-task.sh @@ -8,10 +8,15 @@ # files have changes. Find a way to avoid this, if possible. _TASKBRANCH="git-task" +_DEBUG="true" + +log () { + echo $* >&1 +} error () { - echo $* >&2 - exit 1 + echo $* >&2 + exit 1 } branch_exists () { @@ -28,59 +33,62 @@ 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?" + fi + + # 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 .taskrc || rollback 1 + $_DEBUG && log "Committing task..." + git commit -q -m "$*" || rollback 1 + $_DEBUG && log "Transaction done." } rollback () { - #echo "Rolling back..." + $_DEBUG && log "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 "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 @@ -92,3 +100,5 @@ task_commit $* rollback exit 0 + +# vim: ts=2 sw=2 sts=2 et : From 10390a76187b0a2d9ea6ee70a0f7aa37aabdfab8 Mon Sep 17 00:00:00 2001 From: SClo Date: Wed, 9 May 2018 22:55:45 +0200 Subject: [PATCH 2/3] add TASKBRANCH env + local config (optional) --- git-task.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/git-task.sh b/git-task.sh index 21d302d..92e47ef 100755 --- a/git-task.sh +++ b/git-task.sh @@ -7,8 +7,9 @@ # 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" -_DEBUG="true" +_TASKBRANCH="${TASKBRANCH:-tasks}" +_DEBUG="false" +TASK="/usr/local/bin/task" log () { echo $* >&1 @@ -40,8 +41,11 @@ prepare () { 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 \ @@ -66,11 +70,10 @@ prepare () { task_commit () { $_DEBUG && log "Starting task transaction..." $_DEBUG && log "Recording task..." - TASKDATA=.task task $* || rollback 1 - + TASKDATA=.task $TASK $* || rollback 1 # add and commit the changes $_DEBUG && log "Adding task to git..." - git add .task .taskrc || rollback 1 + git add .task || rollback 1 $_DEBUG && log "Committing task..." git commit -q -m "$*" || rollback 1 $_DEBUG && log "Transaction done." @@ -78,7 +81,7 @@ task_commit () { rollback () { $_DEBUG && log "Rolling back..." - # Since we stashed, there should™ be nothing that could go wrong here. + # 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 From 78e185dcc52be59b18006375afecac9dec0edffd Mon Sep 17 00:00:00 2001 From: SClo Date: Thu, 10 May 2018 10:55:42 +0200 Subject: [PATCH 3/3] remove fixed path for 'task' command use DEBUG env variable if exists --- git-task.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/git-task.sh b/git-task.sh index 92e47ef..98110b8 100755 --- a/git-task.sh +++ b/git-task.sh @@ -8,8 +8,7 @@ # files have changes. Find a way to avoid this, if possible. _TASKBRANCH="${TASKBRANCH:-tasks}" -_DEBUG="false" -TASK="/usr/local/bin/task" +_DEBUG="${DEBUG:-false}" log () { echo $* >&1 @@ -70,7 +69,7 @@ prepare () { task_commit () { $_DEBUG && log "Starting task transaction..." $_DEBUG && log "Recording task..." - TASKDATA=.task $TASK $* || rollback 1 + TASKDATA=.task task $* || rollback 1 # add and commit the changes $_DEBUG && log "Adding task to git..." git add .task || rollback 1