aboutsummaryrefslogtreecommitdiffstats
path: root/amlook
blob: 1fcbd347fa4230c7dfa11531ccb5b2b4d892edca (plain)
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
#!/bin/sh
# Usage:
# Meta/amlook <mbox (for single message from MUA) or
# Meta/amlook id1 id2... (from the command line)
# Meta/amlook --gc

MASTER=master

find_commit () {
	in= commits=

	if test -z "$commits"
	then
		# I know I know there should be "notes grep" command...
		commits=$(
			git grep -l -e "$1" notes/amlog |
			sed -e 's|^notes/amlog:||' -e 's|/||g'
		)
	fi

	if test -z "$commits"
	then
		commits=$(sed -ne "s|^\([0-9a-f]\{40\}\) $1|\1|p" .git/am.log)
	fi

	if test -z "$commits"
	then
		echo "Never applied"
		return
	fi

	echo "$commits"

	found=$(
		echo "$commits" |
		while read commit
		do
			git branch --with $commit
		done | sed -e 's|^..||' |
		sort -u |
		tr '\012' ' '
	)
	if test -z "$found"
	then
		echo "Not merged ($commits)"
		return
	fi
	case " $found " in
	*' maint '*) in=maint ;;
	*" $MASTER "*) in=$MASTER ;;
	*' next '*) in=next ;;
	esac
	if test -n "$in"
	then
		echo "Found in $in"
	else
		echo "Found in $found"
	fi
}

garbage_collect () {
	cutoff_days=${1-"180"} &&
	git notes --ref amlog list |
	sed -e 's/.* //' |
	xargs -n 1 git show -s --format="%ci %H" 2>/dev/null |
	perl -e '
		my @time = localtime(time() - $ARGV[0] * 24 * 3600);
		my $cutoff = sprintf("%04d-%02d-%02d 00:00:00",
				$time[5]+1900, $time[4]+1, $time[3]);
		while (<STDIN>) {
			if ($_ le $cutoff) {
				s/.* //;
				print;
			}
		}
	' "$cutoff_days" >..gcinput

: <<\INVALID
	: (
		GIT_INDEX_FILE=/tmp/amlook.$$.tmp &&
		export GIT_INDEX_FILE &&
		rm -f "$GIT_INDEX_FILE" &&
		git read-tree refs/notes/amlog &&
		xargs git rm -f &&
		T=$(git write-tree) &&
		C=$(echo Prune amlog | git commit-tree $T -p refs/notes/amlog) &&
		git update-ref -m "Prune amlog" refs/notes/amlog $C
	)
INVALID
}

if test $# = 0
then
	msg=$(sed -ne '
		/^[ 	]/{
			# Append continuation line
			H
			x
			s/\n//
			x
			n
		}
		# Hold this new line, and look at what is in the hold space
		x
		# Is it the Message-ID line?  If so, spit out and finish.
		/^[Mm][Ee][Ss][Ss][Aa][Gg][Ee]-[Ii][Dd]:[ 	]*/{
			s///p
			q
		}
		# Otherwise, check if this new line is empty
		x
		# Is it?  Then we are done with the header
		/^$/b end
		# Otherwise we need to hold onto this header line
		x
		# And start the next cycle
		b
	: end
		q
	') &&
	find_commit "$msg"
elif test "$1" = "--gc"
then
	shift
	garbage_collect "$@"
elif test "$1" == "--squash"
then
	L=notes/amlog &&
	git notes --ref=$L prune &&
	C=$(echo amlog | git commit-tree refs/$L^{tree}) &&
	git update-ref refs/$L $C
else
	for msg
	do
		find_commit "$msg"
	done
fi