forked from v4hn/snotes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snotes-open
executable file
·138 lines (118 loc) · 2.73 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
#!/bin/sh
# snotes-1.0 by v4hn / 2009-2013
# 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
}
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"
}
case "${1%:*}" in
note)
snotes_file="${1##*:}"
if [ ! -f "$snotes_file" ]; then
touch "$snotes_file" && snotes_file_flags=created || die "could not create file '`pwd`/$snotes_file'"
fi
;;
blame)
blame "${1##*:}"
;;
commit)
commit "${1##*:}"
;;
changed)
changed "${1##*:}"
;;
*)
die "unknown prefix '${1%:*}'"
;;
esac
${SNOTES_EDITOR} "$snotes_file"
if [ "${SNOTES_CLEANUP}" = "yes" -a "$snotes_file_flags" != "temp" ]; then
cleanup "$snotes_file"
fi
[ ! -s "$snotes_file" -a -z "$snotes_file_flags" ] && snotes_file_flags=deleted
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