summaryrefslogtreecommitdiffstats
path: root/debian/changelog.upstream.sh
blob: afaec0fcfd0b0385e365753a8f35215b41b250bd (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
#!/bin/sh
# Generate debian/changelog.upstream from debian/changelog and
# the git revision log.  Inspired by Gerrit Pape’s
# debian/changelog.upstream.sh, from the git-core Debian package.

set -e

# If argument matches /^Version: /, output remaining text.
# Result is true if and only if argument matches.
version_line() {
	local line result
	line=$1
	result=${line#Version: }

	if test "$result" = "$line"
	then
		return 1
	else
		printf "%s\n" "$result"
		return 0
	fi
}

# If argument matches /^\* New.*snapshot.*commit /,
# output remaining text.
# Result is true if and only if argument matches.
commit_id_line() {
	local line result
	line=$1
	result=${line#\* New*snapshot*commit }

	if test "$result" = "$line"
	then
		return 1
	else
		printf "%s\n" "$result"
		return 0
	fi
}

# Read standard input, scanning for a changelog entry of the
# form “New snapshot, taken from upstream commit <blah>.”
# Output is <blah>.
# Fails and writes a message to standard error if no such entry is
# found before the next Version: line with a different upstream
# version (or EOF).
# $1 is the upstream version sought.
read_commit_id() {
	local upstream_version line version cid
	upstream_version=$1

	while read line
	do
		if
			version=$(version_line "$line") &&
			test "${version%-*}" != "$upstream_version"
		then
			break
		fi

		if cid=$(commit_id_line "$line")
		then
			printf "%s\n" "${cid%.}"
			return 0
		fi
	done

	echo >&2 "No commit id for $upstream_version"
	return 1
}

last=none
last_cid=none
# Add a list of all revisions up to $last to debian/changelog.upstream
# and set last=$2.
# $1 is a user-readable name for the commit $2
add_version() {
	local new new_cid limiter
	new=$1
	new_cid=$2

	if test "$last" = none
	then
		: > debian/changelog.upstream
		last=$new
		last_cid=$new_cid
		return 0
	fi

	exec >> debian/changelog.upstream
	if test "$new" = none
	then
		echo "Version $last:"
		echo "Version $last:" | tr "[:print:]" -
		limiter=
	elif test "$new" = "$last"
	then
		return 0
	else
		echo "Version $last; changes since $new:"
		echo "Version $last; changes since $new:" | tr "[:print:]" -
		limiter="$new_cid.."
	fi
	echo
	git rev-list --no-merges "$limiter$last_cid" |
	git diff-tree --date=iso --stat --format=medium --stdin
	test "$new" = none || echo

	last=$new
	last_cid=$new_cid
}

dpkg-parsechangelog --format rfc822 --all | {
while read line
do
	if version=$(version_line "$line")
	then
		# strip Debian revision
		upstream_version=${version%-*}

		if git rev-parse --verify -q "v$upstream_version" > /dev/null
		then
			# upstream release
			add_version "$upstream_version" "v$upstream_version"
		else
			# snapshot
			cid=$(read_commit_id "$upstream_version") || exit 1
			add_version "$upstream_version" "$cid"
		fi
	fi
done
add_version none none
}