mob: Use 4 decimals in SI output
[sunny256-utils.git] / t
blob4e132cd14e3d200c27f1ff4a62fa4bf0f8b69c4d
1 #!/usr/bin/env bash
3 #=======================================================================
4 # t
5 # File ID: e2469b62-dc83-11e0-9dfc-9f1b7346cb92
7 # Shortcut to task(1) or timew(1) and commit changes to Git
9 # Author: Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 2 or later.
11 #=======================================================================
13 progname=t
14 VERSION=0.6.0
16 if test "$1" = "--version"; then
17 echo $progname $VERSION
18 exit 0
21 T_BOLD=$(tput bold)
22 T_GREEN=$(tput setaf 2)
23 T_RED=$(tput setaf 1)
24 T_RESET=$(tput sgr0)
26 lockdir=$HOME/.t-task.LOCK
27 taskdir=$HOME/src/git/task
29 if test "$1" = "-h" -o "$1" = "--help"; then
30 cat <<END
32 Frontend to task(1) (Taskwarrior) or timew(1) (Timewarrior), depending
33 on what the script is called. If the file name of the script is "t",
34 call Taskwarrior, if it's "tw", call Timewarrior. Other names result in
35 an error.
37 Commits the result of every command to Git.
39 Usage: $progname [options] TASK_COMMAND
40 $progname --is-active
41 $progname angre
43 Options:
45 -h, --help
46 Show this help.
47 --is-active
48 Check that the task repository is marked as active, that the t.active Git
49 config value is set to "true". If it's anything else, the script will abort
50 with exit value 1.
51 --version
52 Print version information.
54 "$progname angre" assumes that $taskdir is version controlled by Git and
55 will throw away the newest commit. If the repository is modified in any
56 way, the operation aborts.
58 END
59 exit 0
62 myexit() {
63 rmdir $lockdir || echo $progname: $lockdir: Cannot remove lockdir >&2
64 exit $1
67 cd $taskdir || { echo $progname: $taskdir: Cannot chdir >&2; exit 1; }
69 if test "$1" = "--is-active"; then
70 if test "$(git config --get t.active)" != "true"; then
71 echo >&2
72 echo "The task repository ($taskdir) is not marked as active," >&2
73 echo "set the Git config variable t.active to \"true\"." >&2
74 echo >&2
75 exit 1
77 exit 0
80 base="$(basename "$0")"
81 if test "$base" = "t"; then
82 cmd=task
83 elif test "$base" = "tw"; then
84 cmd=timew
85 else
86 echo $progname: $base: Unknown script name, must be \"t\" or \"tw\" >&2
87 exit 1
90 t --is-active || exit 1
92 mkdir $lockdir || {
93 echo $progname: $lockdir: Cannot create lockdir >&2
94 exit 1
96 trap "myexit 1" INT TERM
98 if [ ! -f $taskdir/.taskrc -o ! -d $taskdir/.task ]; then
99 echo $progname: Missing files in $taskdir/ >&2
100 myexit 1
103 if [ "$1" = "angre" ]; then
104 if [ ! -d .git ]; then
105 echo $progname: $taskdir/.git not found >&2
106 myexit 1
108 if ! (git-wait-until-clean -e); then
109 echo $progname: $taskdir has modifications, aborting >&2
110 myexit 1
112 echo -n "$T_BOLD$T_RED"
113 GIT_PAGER=cat git log -1 --format='Deleting rev: %h ("%s", %cd)'
114 echo -n "$T_RESET"
115 git reset --hard HEAD^
116 echo -n "$T_BOLD$T_GREEN"
117 GIT_PAGER=cat git log -1 --format='New rev: %h ("%s", %cd)'
118 echo -n "$T_RESET"
119 git dangling
120 myexit
123 $cmd "$@"
124 oldcommit=$(git rev-parse HEAD)
125 ciall -d -y -- $cmd "$@" >/tmp/t-output.txt 2>&1 || {
126 echo $progname: git commit error >&2
127 exit 1
130 $cmd &>/dev/null
131 ciall -d -y -- Finish previous command >/tmp/t-output-2.txt 2>&1 || {
132 echo $progname: git commit 2 error >&2
133 exit 1
135 newcommit=$(git rev-parse HEAD)
136 GIT_PAGER=cat git log --abbrev-commit --format=oneline --decorate=short \
137 $oldcommit..$newcommit
139 myexit 0