Meta/amlook: wip gc
Junio C Hamano committed
May 18, 2011 at 23:35 UTC
91c921be5b849786def82f258c97b9eb234a56bf
1 file changed
+116
amlook
new
+116
@@ -0,0 +1,116 @@
1
+#!/bin/sh
2
+# Usage:
3
+# Meta/amlook <mbox (for single message from MUA) or
4
+# Meta/amlook id1 id2... (from the command line)
5
+# Meta/amlook --gc
6
+
7
+find_commit () {
8
+ in=
9
+ # I know I know there should be "notes grep" command...
10
+ commits=$(
11
+ git grep -l -e "$1" notes/amlog |
12
+ sed -e 's|^notes/amlog:||' -e 's|/||g'
13
+ )
14
+ if test -z "$commits"
15
+ then
16
+ echo "Never applied"
17
+ return
18
+ fi
19
+ found=$(
20
+ echo "$commits" |
21
+ while read commit
22
+ do
23
+ git branch --with $commit
24
+ done | sed -e 's|^..||' |
25
+ sort -u |
26
+ tr '\012' ' '
27
+ )
28
+ if test -z "$found"
29
+ then
30
+ echo "Not merged ($commits)"
31
+ return
32
+ fi
33
+ case " $found " in
34
+ *' maint '*) in=maint ;;
35
+ *' master '*) in=master ;;
36
+ *' next '*) in=next ;;
37
+ esac
38
+ if test -n "$in"
39
+ then
40
+ echo "Found in $in"
41
+ else
42
+ echo "Found in $found"
43
+ fi
44
+}
45
+
46
+garbage_collect () {
47
+ cutoff_days=${1-"180"} &&
48
+ git notes --ref amlog list |
49
+ sed -e 's/.* //' |
50
+ xargs -n 1 git show -s --format="%ci %H" 2>/dev/null |
51
+ perl -e '
52
+ my @time = localtime(time() - $ARGV[0] * 24 * 3600);
53
+ my $cutoff = sprintf("%04d-%02d-%02d 00:00:00",
54
+ $time[5]+1900, $time[4]+1, $time[3]);
55
+ while (<STDIN>) {
56
+ if ($_ le $cutoff) {
57
+ s/.* //;
58
+ print;
59
+ }
60
+ }
61
+ ' "$cutoff_days" >..gcinput
62
+
63
+: <<\INVALID
64
+ : (
65
+ GIT_INDEX_FILE=/tmp/amlook.$$.tmp &&
66
+ export GIT_INDEX_FILE &&
67
+ rm -f "$GIT_INDEX_FILE" &&
68
+ git read-tree refs/notes/amlog &&
69
+ xargs git rm -f &&
70
+ T=$(git write-tree) &&
71
+ C=$(echo Prune amlog | git commit-tree $T -p refs/notes/amlog) &&
72
+ git update-ref -m "Prune amlog" refs/notes/amlog $C
73
+ )
74
+INVALID
75
+}
76
+
77
+if test $# = 0
78
+then
79
+ msg=$(sed -ne '
80
+ /^[ ]/{
81
+ # Append continuation line
82
+ H
83
+ x
84
+ s/\n//
85
+ x
86
+ n
87
+ }
88
+ # Hold this new line, and look at what is in the hold space
89
+ x
90
+ # Is it the Message-ID line? If so, spit out and finish.
91
+ /^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:[ ]*/{
92
+ s///p
93
+ q
94
+ }
95
+ # Otherwise, check if this new line is empty
96
+ x
97
+ # Is it? Then we are done with the header
98
+ /^$/b end
99
+ # Otherwise we need to hold onto this header line
100
+ x
101
+ # And start the next cycle
102
+ b
103
+ : end
104
+ q
105
+ ') &&
106
+ find_commit "$msg"
107
+elif test "$1" = "--gc"
108
+then
109
+ shift
110
+ garbage_collect "$@"
111
+else
112
+ for msg
113
+ do
114
+ find_commit "$msg"
115
+ done
116
+fi