summaryrefslogtreecommitdiff
path: root/git-request-pull.sh
blob: 957a760f917fb8c0d6a5f83bdd59b2584538c20b (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
# SPDX-License-Identifier: GPL-2.0+
#
# git-request-pull wrapper with sanity checks
#

baserev=
headrev=

parse_options() {
	# The code below if borrowed from git-request-pull

	# Skip all options
	while [[ $# != 0 ]] ; do
		case "$1" in
		-p)
			;;
		--)
			shift
			break
			;;
		-*)
			return
			;;
		*)
			break
			;;
		esac
		shift
	done

	local base=$1
	local head=

	baserev=$(git rev-parse --verify --quiet "$base"^0)
	if [[ -z "$baserev" ]] ; then
		return
	fi

	local=${3%:*}
	local=${local:-HEAD}
	head=$(git symbolic-ref -q "$local")
	head=${head:-$(git show-ref --heads --tags "$local" | cut -d' ' -f2)}
	head=${head:-$(git rev-parse --quiet --verify "$local")}

	if [[ -z "$head" ]] ; then
		return
	fi

	headrev=$(git rev-parse --verify --quiet "$head"^0)
}

check_commit()
{
	local baserev=$1
	local commit=$2

	local msg=$(git cat-file commit "$commit")
	local summary=$(git show --pretty='format:%h ("%s")' -s $commit)

	# 1. The commit message shall not contain a local changelog.
	if echo -E "$msg" | grep -q '^--- *$'
	then
		echo "Found local changelog in commit $commit"
		errors=$((errors+1))
	fi

	# 2. The commit message shall have Signed-off-by lines
	# corresponding the committer and the author.
	local committer=$(echo "$msg" | grep '^committer ' | head -1 | \
			cut -d ' ' -f 2- | rev | cut -d ' ' -f 3- | rev)
	if ! echo -E "$msg" | grep -Fqx "Signed-off-by: ${committer}"
	then
		echo "Commit $summary is not signed off by committer"
		errors=$((errors+1))
	fi

	local author=$(echo "$msg" | grep '^author ' | head -1 | \
			cut -d ' ' -f 2- | rev | cut -d ' ' -f 3- | rev)
	if ! echo -E "$msg" | grep -Fqx "Signed-off-by: ${author}"
	then
		echo "Commit $summary is not signed off by author"
		errors=$((errors+1))
	fi

	# 3. Fixes: tags shall reference an ancestor commit.
	local fixes=$(echo "$msg" | grep '^Fixes: ')
	if [ -n "$fixes" ] ; then
		fixes=$(echo "$fixes" | sed 's/^Fixes: \([0-9a-f]\+\) (.*)$/\1/')
		git merge-base --is-ancestor $fixes $baserev >/dev/null 2>&1 || {
			echo "Commit $summary contains a Fixes: tag referencing $fixes not present in history"
			errors=$((errors+1))
		}
	fi

	# 4. The commit should not contain any file matching .gitignore
	# patterns.
	local files=$(git show --format= --name-only $commit | \
		      git check-ignore --stdin --no-index)
	if [ -n "$files" ] ; then
		echo "Commit $summary contains files matching .gitignore patterns:"
		local file
		for file in $files ; do
			echo "- $file"
		done

		errors=$((errors+1))
	fi
}

check_commits() {
	if [[ -z "$baserev" || -z "$headrev" ]] ; then
		# git request-pull will catch this issue
		return
	fi

	local commit
	errors=0

	for commit in $(git rev-list ^$baserev $headrev) ; do
		check_commit $baserev $commit
	done

	if [[ $errors != 0 ]] ; then
		echo "$errors errors found, please fix"
		exit 1
	fi
}

parse_options $*
check_commits

git request-pull $*