-
Notifications
You must be signed in to change notification settings - Fork 4
/
snotes-open
executable file
·165 lines (140 loc) · 3.52 KB
/
snotes-open
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/sh
# snotes-1.1 by v4hn / 2009-2022
# See LICENSE file for copyright and license details.
info(){
echo "$0: Info: $1" >&2
}
die(){
echo "$0: Error: $1" >&2
exit 1
}
newtemp(){
local tmp
tmp="$(mktemp -t snotes.tmp.XXXXXX)"
[ -e "$tmp" ] || die "Could not create temporary file"
echo "$tmp"
}
cancreate(){
[ ! -e "$1" -o -f "$1" ] && (touch "$1" && rm -f "$1") > /dev/null 2>&1
}
validnote(){
echo $1 | grep -q -E -v \
-e "^/" \
-e "(^|/)\.\./" \
-e "(^|/)\./" \
-e "^~[^/]*/" \
-e "^\.git"
}
type git >/dev/null 2>&1 || die "git not in PATH"
. "$HOME/.snotes/config" || die "config ~/.snotes/config messed up"
if [ "${SNOTES_CLEANUP}" = "yes" ] && ! type perl >/dev/null 2>&1; then
info "cleanup mode requires perl"
SNOTES_CLEANUP="no"
fi
[ -n "$1" ] || die "nothing to do"
cd "${SNOTES_DB}"
unset snotes_file
unset snotes_file_flags
blame(){
local tmp
tmp="$(newtemp)"
git blame --relative-date -- "$1" | sed 's/^\^\?/commit:/' > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
snotes_file_flags=temp
}
commit(){
local tmp
tmp="$(newtemp)"
git show --color=never "$1" > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
snotes_file_flags=temp
}
changed(){
local tmp
local diffcommit
tmp="$(newtemp)"
diffcommit="$(git log --oneline --color=never --max-count=${SNOTES_CHANGED_COMMITS} -- "$1" | tail -n1 | cut -d' ' -f1)"
git diff --color=never "${diffcommit}^" -- "$1" > "$tmp"
chmod 0400 "$tmp"
snotes_file="$tmp"
snotes_file_flags=temp
}
cleanup(){
local tmp
tmp="$(newtemp)"
# cleanup
cat "$1" | perl -e '
while(<>){
next if(/^[ \t\n]*$/);
last;
}
my $tmp="";
do {
s/^(.*?)[ \t\n]*$/\1/;
if(/^[ \t\n]*$/){ $tmp= $tmp."\n" }
else { print $tmp.$_."\n"; $tmp="" };
} while(<>);' > "$tmp"
cat "$tmp" > "$1"
rm "$tmp"
}
snotes_file="${1##*:}"
[ -n "$snotes_file" ] || die "command with an empty argument"
validnote "$snotes_file" || die "note name is invalid. please don't try anything fancy"
case "${1%:*}" in
note)
if [ ! -f "$snotes_file" ]; then
cancreate "$snotes_file" && snotes_file_flags=created || die "could not create file '$(pwd)/$snotes_file'"
fi
;;
blame)
[ -f "$snotes_file" ] || die "'$(pwd)/$snotes_file' does not exist"
blame "$snotes_file"
;;
commit)
commit "$snotes_file"
;;
changed)
[ -f "$snotes_file" ] || die "'$(pwd)/$snotes_file' does not exist"
changed "$snotes_file"
;;
remove)
snotes_file="${1##*:}"
snotes_file_flags=deleted
;;
*)
die "unknown prefix '${1%:*}'"
;;
esac
if [ "$snotes_file_flags" != "deleted" ]; then
${SNOTES_EDITOR} "$snotes_file"
if [ "${SNOTES_CLEANUP}" = "yes" -a "$snotes_file_flags" != "temp" -a -f "$snotes_file" \
-a "$(echo "$snotes_file" | grep -E -c "$SNOTES_DONTCLEAN_REGEXP")" -eq 0 ]; then
cleanup "$snotes_file"
fi
[ ! -s "$snotes_file" -a -z "$snotes_file_flags" ] && snotes_file_flags=deleted
fi
case "$snotes_file_flags" in
created)
if [ -s "$snotes_file" ]; then
git add -- "$snotes_file"
git commit -q -m "created note '$snotes_file'"
else
rm -f "$snotes_file"
fi
;;
temp)
rm -f "$snotes_file"
;;
deleted)
git rm -q -f -- "$snotes_file"
git commit -q -m "removed note '$snotes_file'"
;;
*)
if ! git diff --quiet -- "$snotes_file"; then
git add -- "$snotes_file"
git commit -q -m "changed note '$snotes_file'"
fi
;;
esac