aboutsummaryrefslogtreecommitdiff
path: root/bin/td
blob: e4b0669ad2d75639a4a9fcca103e8fb7ac312525 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash

#
# td(1) -- Manage to-do files with just $EDITOR and git(1), because I've
# completely given up on finding a more useful way to do it.
#
# Author: Tom Ryder <tom@sanctum.geek.nz>
# Copyright: 2016
# License: Public domain
#
self=td

# Specify the path to the file; you can override this with environment
# variables
dir=${TODO_DIR:-$HOME/Todo}
file=${1:-${TODO_NAME:-todo}}
path="$dir"/"$file"

# Check we've got the tools we need
hash git || exit

# If the directory doesn't exist, create it
if [[ ! -d $dir ]] ; then
    mkdir -p -- "$dir" || exit
fi

# Change into the directory
cd -- "$dir" || exit

# Quick function to determine if a directory is a Git repository
isrepo() {
    { git symbolic-ref --quiet HEAD || \
        git rev-parse --short HEAD 
    } >/dev/null 2>&1
}

# If the current directory isn't a Git repository, try to create one
if ! isrepo ; then
    git init || exit
fi

# If the to-do file doesn't exist yet, create it
if ! [[ -e $file ]] ; then
    touch -- "$file" || exit
fi

# Launch $VISUAL (or $EDITOR (or vi(1))) to edit the appropriate to-do file
"${VISUAL:-${EDITOR:-vi}}" "$file"

# Add the file to the changeset
git add -- "$file"

# If there are changes to commit, commit them
message=$(printf 'Changed by %s(1)' "$self")
if ! git diff-index --quiet HEAD ; then
    git commit --message "$message" --quiet
fi