-
Notifications
You must be signed in to change notification settings - Fork 1
/
todo.md.sh
executable file
·101 lines (89 loc) · 2.55 KB
/
todo.md.sh
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
SEARCHTEXT="TODO"
ROOT=$(git rev-parse --show-toplevel)
OUTFILE="todo.md"
EXCLUDE=''
while getopts "ohlf:t:e:i:" option; do
case $option in
'o' )
STDOUT=true
;;
'h' )
echo "$(basename $0) - generate a todo file based on your code"
echo "---"
echo " -h - display this help"
echo -n " -l - LIVE MODE this will add the generated file to your "
echo "staged changes."
echo -n " Use this in the git hook. Off by default "
echo "for playing around in the terminal"
echo " -o - print output to STDOUT"
echo -n " -f - write to this file (defaults to todo.md and path "
echo "starts at the repo's root)"
echo " -t - text to search for (defaults to TODO)"
echo " -e - exclude pattern"
echo " -i - include patter"
exit
;;
'l' )
LIVEMODE=true
;;
'f' )
OUTFILE=$OPTARG
;;
't' )
SEARCHTEXT=$OPTARG
;;
'e' )
EXCLUDE="$EXCLUDE$OPTARG|"
;;
'i' )
if [[ -z $INCLUDE ]]; then
INCLUDE=$OPTARG
else
INCLUDE="$INCLUDE|$OPTARG"
fi
;;
esac
done
if [[ $INCLUDE ]] && [[ $EXCLUDE ]]; then
echo "-i and -e can't be used together!"
exit 1
fi
EXCLUDE=$EXCLUDE$OUTFILE
if [ $STDOUT ] && [ $LIVEMODE ]; then
echo "-o and -l can't be used together! Ignoring -l" >&2
unset LIVEMODE
fi
OUTFILE="$ROOT/$OUTFILE"
if [ -z $STDOUT ]; then
exec 1>$OUTFILE
fi
echo "## To Do"
current_file=''
if [[ -z $INCLUDE ]]; then
IFS=$'\r\n' tasks=($(git grep -In --full-name $SEARCHTEXT $ROOT | egrep -v "($EXCLUDE)"))
else
IFS=$'\r\n' tasks=($(git grep -In --full-name $SEARCHTEXT $ROOT | egrep "($INCLUDE)"))
fi
# -I - skip binaries
# -n - include line numbers
# --full-name - the full path (starting from the repo root)
for task in ${tasks[@]}; do
file=$(echo $task | cut -f1 -d':')
line=$(echo $task | cut -f2 -d':')
item=$(echo $task | cut -f3- -d':' | sed "s/.*$SEARCHTEXT *//g")
if [[ $file != $current_file ]]; then
if [ $current_file ]; then
echo
fi
current_file=$file
echo "### \`\`$current_file\`\`"
fi
echo "(line $line) $item"
echo
done
echo "######Generated using [todo.md](https://github.com/charlesthomas/todo.md)"
if [ -z $LIVEMODE ]; then
exit
fi
$(git add $OUTFILE)