What's cooking (2015/08 #04)
[alt-git.git] / amlook
blob0544f402eec1c6c52c31bdacb92f3ef10566caff
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
7 find_commit () {
8 in= commits=
10 if test -z "$commits"
11 then
12 blob=$(echo "Message-Id: $1" | git hash-object --stdin)
13 commits=$(git notes --ref amlog show $blob | sed -e '/^$/d')
16 if test -z "$commits"
17 then
18 commits=$(sed -ne "s|^\([0-9a-f]\{40\}\) $1|\1|p" .git/am.log)
21 if test -z "$commits"
22 then
23 # I know I know there should be "notes grep" command...
24 commits=$(
25 git grep -l -e "$1" notes/amlog |
26 sed -e 's|^notes/amlog:||' -e 's|/||g'
30 if test -z "$commits"
31 then
32 echo "Never applied"
33 return
36 found=$(
37 echo "$commits" |
38 while read commit
40 git branch --with $commit
41 done | sed -e 's|^..||' |
42 sort -u |
43 tr '\012' ' '
45 if test -z "$found"
46 then
47 echo "Not merged ($commits)"
48 return
50 case " $found " in
51 *' maint '*) in=maint ;;
52 *' master '*) in=master ;;
53 *' next '*) in=next ;;
54 esac
55 if test -n "$in"
56 then
57 echo "Found in $in"
58 else
59 echo "Found in $found"
63 garbage_collect () {
64 cutoff_days=${1-"180"} &&
65 git notes --ref amlog list |
66 sed -e 's/.* //' |
67 xargs -n 1 git show -s --format="%ci %H" 2>/dev/null |
68 perl -e '
69 my @time = localtime(time() - $ARGV[0] * 24 * 3600);
70 my $cutoff = sprintf("%04d-%02d-%02d 00:00:00",
71 $time[5]+1900, $time[4]+1, $time[3]);
72 while (<STDIN>) {
73 if ($_ le $cutoff) {
74 s/.* //;
75 print;
78 ' "$cutoff_days" >..gcinput
80 : <<\INVALID
81 : (
82 GIT_INDEX_FILE=/tmp/amlook.$$.tmp &&
83 export GIT_INDEX_FILE &&
84 rm -f "$GIT_INDEX_FILE" &&
85 git read-tree refs/notes/amlog &&
86 xargs git rm -f &&
87 T=$(git write-tree) &&
88 C=$(echo Prune amlog | git commit-tree $T -p refs/notes/amlog) &&
89 git update-ref -m "Prune amlog" refs/notes/amlog $C
91 INVALID
94 if test $# = 0
95 then
96 msg=$(sed -ne '
97 /^[ ]/{
98 # Append continuation line
101 s/\n//
105 # Hold this new line, and look at what is in the hold space
107 # Is it the Message-ID line? If so, spit out and finish.
108 /^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:[ ]*/{
109 s///p
112 # Otherwise, check if this new line is empty
114 # Is it? Then we are done with the header
115 /^$/b end
116 # Otherwise we need to hold onto this header line
118 # And start the next cycle
120 : end
122 ') &&
123 find_commit "$msg"
124 elif test "$1" = "--gc"
125 then
126 shift
127 garbage_collect "$@"
128 else
129 for msg
131 find_commit "$msg"
132 done