summaryrefslogtreecommitdiffstats
path: root/quilt/annotate.in
blob: a6235618b261f26b67c1ae6adedbfa28b42d9a13 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#! @BASH@

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r $QUILT_DIR/scripts/patchfns ]
	then
		echo "Cannot read library $QUILT_DIR/scripts/patchfns" >&2
		exit 1
	fi
	. $QUILT_DIR/scripts/patchfns
fi

usage()
{
	printf $"Usage: quilt annotate [-p patch] {file}\n"
	if [ x$1 = x-h ]
	then
		printf $"
Print an annotated listing of the specified file showing which
patches modify which lines. Only applied patches are included.

-p patch
	Stop checking for changes at the specified rather than the
	topmost patch.
"
		exit 0
	else
		exit 1
	fi
}

empty_file()
{
	local file=$1
	[ -s "$file" ] \
	&& sed -e 's:.*::' "$file"
}

annotation_for()
{
	local old_file=$1 new_file=$2 annotation=$3
	[ -s "$old_file" ] || old_file=/dev/null
	[ -s "$new_file" ] || new_file=/dev/null
	diff -Ne "$old_file" "$new_file" \
	| perl -e '
	while (<>) {
	    if (/^\d+(?:,\d+)?[ac]$/) {
		print;
		while (<>) {
		    last if /^\.$/;
		    print "'"$annotation"'\n";
		}
		print;
		next;
	    }
	    print;
	}
	'
}

merge_files()
{
	local a b saved_IFS="$IFS"
	local template=$1 file=$2

	[ -e "$file" ] || file=/dev/null

	exec 3< "$template"
	exec 4< "$file"
	IFS=
	while read -r a <&3
	do
		read -r b <&4
		echo "$a"$'\t'"$b"
	done
	IFS="$saved_IFS"
	exec 3<&-
	exec 4<&-
}

options=`getopt -o p:h -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-p)
		if ! opt_patch=$(find_patch "$2")
		then
			printf $"Patch %s is not in series\n" "$2" >&2
			exit 1
		fi
		shift 2 ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -ne 1 ]
then
	usage
fi
opt_file="$SUBDIR$1"

if [ -n "$opt_patch" ] && ! is_applied "$opt_patch"
then
	printf $"Patch %s is not applied\n" \
	       "$(print_patch "$opt_patch")" >&2
	exit 1
fi

for patch in $(applied_patches); do
	old_file="$(backup_file_name "$patch" "$opt_file")"
	if [ -f "$old_file" ]
	then
		patches[${#patches[@]}]="$patch"
		files[${#files[@]}]="$old_file"
	fi
	if [ "$opt_patch" = "$patch" ]
	then
		# We also need to know the next patch, if any
		next_patch="$(next_patch_for_file "$opt_patch" "$opt_file")"
		break
	fi
done

if [ -z "$next_patch" ]
then
	files[${#files[@]}]="$opt_file"
else
	files[${#files[@]}]="$(backup_file_name "$next_patch" "$opt_file")"
fi

if [ ${#patches[@]} = 0 ]
then
	sed -e 's:^:'$'\t'':' "${files[${#files[@]}-1]}"
	exit 0
fi

template=$(gen_tempfile)

trap "rm -f $template" EXIT

# The annotated listing is generated as follows: A file of annotations
# is created based on a file that contains the same number of lines as
# the source file, but all lines are empty.
#
# Then, for each patch that modifies the source file, an ed-style diff
# (which has no context, and removes lines that are removed without
# caring for the line's contents) is generated. In that diff, all line
# additions are replaced with the identifier of the patch (1, 2, ...).
# These patches are then applied to the empty file. 
#
# Finally, the annotations listing is merged with the source file line
# by line.

empty_file ${files[0]} > $template
for ((n = 0; n < ${#patches[@]}; n++))
do
	annotation_for "${files[n]}" "${files[n+1]}" $((n+1))
done \
| patch $template
merge_files $template "${files[${#files[@]}-1]}"

echo
for ((n = 0; n < ${#patches[@]}; n++))
do
	echo "$((n+1))"$'\t'"$(print_patch ${patches[n]})"
done

### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh