Last active
December 23, 2015 11:59
-
-
Save kjdev/6632246 to your computer and use it in GitHub Desktop.
Simple git task command
This file contains 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
#!/usr/bin/env bash | |
set -e | |
VERSION="0.0.1" | |
TASKFILE=".task" | |
_git="/usr/bin/git" | |
_grep="/usr/bin/grep" | |
_sed="/usr/bin/sed" | |
_touch="/usr/bin/touch" | |
_cat="/usr/bin/cat" | |
_cut="/usr/bin/cut" | |
_tail="/usr/bin/tail" | |
git-task-filepath() | |
{ | |
path=$(${_git} rev-parse --show-toplevel || true) | |
echo $path"/${TASKFILE}" | |
} | |
git-task-add() | |
{ | |
if [ -z "$1" ]; then | |
return 1 | |
fi | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
${_touch} ${taskfile} | |
fi | |
echo "[ ] $@" >> ${taskfile} | |
return 0 | |
} | |
git-task-done() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
if [ -z "$1" ]; then | |
${_grep} -s -n -T "\[x\]" ${taskfile} | ${_sed} -e "s/\[x\]//g" | |
return 0 | |
fi | |
id=$(echo "$1" | ${_sed} -e "s/[^0-9]//g") | |
if [ "${id}" = "$1" -a ${id} -gt 0 ]; then | |
${_sed} -i -e "${id}s/\[ \]/\[x\]/" ${taskfile} | |
fi | |
return 0 | |
} | |
git-task-rollback() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
id=$(echo "$1" | ${_sed} -e "s/[^0-9]//g") | |
if [ "${id}" = "$1" -a ${id} -gt 0 ]; then | |
check=$(${_sed} -n "${id}p" ${taskfile}) | |
if [ ! -z "${check}" ]; then | |
echo -n "task '${check}' rollback? [y/N]: " | |
read _result | |
if [ "${_result}" = "Y" -o "${_result}" = "y" ]; then | |
${_sed} -i -e "${id}s/\[x\]/\[ \]/" ${taskfile} | |
fi | |
fi | |
fi | |
return 0 | |
} | |
git-task-remove() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
id=$(echo "$1" | ${_sed} -e "s/[^0-9]//g") | |
if [ "${id}" = "$1" -a ${id} -gt 0 ]; then | |
check=$(${_sed} -n "${id}p" ${taskfile}) | |
if [ ! -z "${check}" ]; then | |
echo -n "task '${check}' remove? [y/N]: " | |
read _result | |
if [ "${_result}" = "Y" -o "${_result}" = "y" ]; then | |
${_sed} -i -e "${id}d" ${taskfile} | |
fi | |
fi | |
fi | |
return 0 | |
} | |
git-task-info() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
id=$(echo "$1" | ${_sed} -e "s/[^0-9]//g") | |
if [ "${id}" = "$1" -a ${id} -gt 0 ]; then | |
ids=$(${_grep} -s -n "\[ \]" ${taskfile} | ${_cut} -d ":" -f 1 | ${_sed} -e "s/[\t\n ]*//g") | |
for i in ${ids}; do | |
if [ ! "${n}" = "" ]; then | |
m=${i} | |
break | |
elif [ "${i}" = "${id}" ]; then | |
n=${i} | |
fi | |
done | |
if [ ! "${n}" = "" ]; then | |
if [ "${m}" = "" ]; then | |
${_tail} -n +${n} ${taskfile} | |
else | |
${_tail} -n +${n} ${taskfile} | head -$(expr ${m} - ${n}) | |
fi | |
fi | |
fi | |
return 0 | |
} | |
git-task-list() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
${_grep} -s -n -T "\[ \]" ${taskfile} | ${_sed} -e "s/\[ \]//g" | |
return 0 | |
} | |
git-task-show() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
${_cat} ${taskfile} | |
return 0 | |
} | |
git-task-edit() | |
{ | |
taskfile=`git-task-filepath` | |
if [ ! -f "${taskfile}" ]; then | |
return 1 | |
fi | |
editor=$(${_git} config --get core.editor || true) | |
if [ -z "${editor}" ]; then | |
editor="vi" | |
fi | |
${editor} ${taskfile} | |
return 0 | |
} | |
git-task-help() | |
{ | |
echo "usage: git task <command> [args] | |
commands are: | |
add <comment> : add task list | |
done [<id>] : done task list | |
remove <id> : remove task list | |
rollback <id> : done rollback task list | |
info <id> : show task information | |
list : show task list | |
show : show task list file | |
edit : edit task list file | |
" | |
return 0 | |
} | |
case "$1" in | |
"add") | |
cmd="git-task-add" | |
shift 1 | |
;; | |
"done") | |
cmd="git-task-done" | |
shift 1 | |
;; | |
"rollback") | |
cmd="git-task-rollback" | |
shift 1 | |
;; | |
"remove"|"rm") | |
cmd="git-task-remove" | |
shift 1 | |
;; | |
"info") | |
cmd="git-task-info" | |
shift 1 | |
;; | |
"show") | |
cmd="git-task-show" | |
;; | |
"edit") | |
cmd="git-task-edit" | |
;; | |
"help") | |
cmd="git-task-help" | |
;; | |
*) | |
cmd="git-task-list" | |
;; | |
esac | |
${cmd} "$@" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment