From f171dfbb3103de7802013471aac7ba3082ee2618 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sat, 17 Aug 2024 22:12:05 +0300 Subject: git-request-pull: Split commit checks to a separate function Create a new check_commit() function to make the code more readable. Signed-off-by: Laurent Pinchart --- git-request-pull.sh | 87 ++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 41 deletions(-) (limited to 'git-request-pull.sh') diff --git a/git-request-pull.sh b/git-request-pull.sh index 6a5afea..1058d2e 100755 --- a/git-request-pull.sh +++ b/git-request-pull.sh @@ -50,56 +50,61 @@ parse_options() { 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 +} + check_commits() { if [[ -z "$baserev" || -z "$headrev" ]] ; then # git request-pull will catch this issue return fi - local author local commit - local committer - local errors=0 - local sob + errors=0 for commit in $(git rev-list ^$baserev $headrev) ; do - 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 + check_commit $baserev $commit done if [[ $errors != 0 ]] ; then -- cgit v1.2.3