1*4b7eee81SChris Kay#!/bin/sh 2*4b7eee81SChris Kay# From Gerrit Code Review 2.14.20 3*4b7eee81SChris Kay# 4*4b7eee81SChris Kay# Part of Gerrit Code Review (https://www.gerritcodereview.com/) 5*4b7eee81SChris Kay# 6*4b7eee81SChris Kay# Copyright (C) 2009 The Android Open Source Project 7*4b7eee81SChris Kay# 8*4b7eee81SChris Kay# Licensed under the Apache License, Version 2.0 (the "License"); 9*4b7eee81SChris Kay# you may not use this file except in compliance with the License. 10*4b7eee81SChris Kay# You may obtain a copy of the License at 11*4b7eee81SChris Kay# 12*4b7eee81SChris Kay# http://www.apache.org/licenses/LICENSE-2.0 13*4b7eee81SChris Kay# 14*4b7eee81SChris Kay# Unless required by applicable law or agreed to in writing, software 15*4b7eee81SChris Kay# distributed under the License is distributed on an "AS IS" BASIS, 16*4b7eee81SChris Kay# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17*4b7eee81SChris Kay# See the License for the specific language governing permissions and 18*4b7eee81SChris Kay# limitations under the License. 19*4b7eee81SChris Kay# 20*4b7eee81SChris Kay 21*4b7eee81SChris Kayunset GREP_OPTIONS 22*4b7eee81SChris Kay 23*4b7eee81SChris KayCHANGE_ID_AFTER="Bug|Depends-On|Issue|Test|Feature|Fixes|Fixed" 24*4b7eee81SChris KayMSG="$1" 25*4b7eee81SChris Kay 26*4b7eee81SChris Kay# Check for, and add if missing, a unique Change-Id 27*4b7eee81SChris Kay# 28*4b7eee81SChris Kayadd_ChangeId() { 29*4b7eee81SChris Kay clean_message=`sed -e ' 30*4b7eee81SChris Kay /^diff --git .*/{ 31*4b7eee81SChris Kay s/// 32*4b7eee81SChris Kay q 33*4b7eee81SChris Kay } 34*4b7eee81SChris Kay /^Signed-off-by:/d 35*4b7eee81SChris Kay /^#/d 36*4b7eee81SChris Kay ' "$MSG" | git stripspace` 37*4b7eee81SChris Kay if test -z "$clean_message" 38*4b7eee81SChris Kay then 39*4b7eee81SChris Kay return 40*4b7eee81SChris Kay fi 41*4b7eee81SChris Kay 42*4b7eee81SChris Kay # Do not add Change-Id to temp commits 43*4b7eee81SChris Kay if echo "$clean_message" | head -1 | grep -q '^\(fixup\|squash\)!' 44*4b7eee81SChris Kay then 45*4b7eee81SChris Kay return 46*4b7eee81SChris Kay fi 47*4b7eee81SChris Kay 48*4b7eee81SChris Kay if test "false" = "`git config --bool --get gerrit.createChangeId`" 49*4b7eee81SChris Kay then 50*4b7eee81SChris Kay return 51*4b7eee81SChris Kay fi 52*4b7eee81SChris Kay 53*4b7eee81SChris Kay # Does Change-Id: already exist? if so, exit (no change). 54*4b7eee81SChris Kay if grep -i '^Change-Id:' "$MSG" >/dev/null 55*4b7eee81SChris Kay then 56*4b7eee81SChris Kay return 57*4b7eee81SChris Kay fi 58*4b7eee81SChris Kay 59*4b7eee81SChris Kay id=`_gen_ChangeId` 60*4b7eee81SChris Kay T="$MSG.tmp.$$" 61*4b7eee81SChris Kay AWK=awk 62*4b7eee81SChris Kay if [ -x /usr/xpg4/bin/awk ]; then 63*4b7eee81SChris Kay # Solaris AWK is just too broken 64*4b7eee81SChris Kay AWK=/usr/xpg4/bin/awk 65*4b7eee81SChris Kay fi 66*4b7eee81SChris Kay 67*4b7eee81SChris Kay # Get core.commentChar from git config or use default symbol 68*4b7eee81SChris Kay commentChar=`git config --get core.commentChar` 69*4b7eee81SChris Kay commentChar=${commentChar:-#} 70*4b7eee81SChris Kay 71*4b7eee81SChris Kay # How this works: 72*4b7eee81SChris Kay # - parse the commit message as (textLine+ blankLine*)* 73*4b7eee81SChris Kay # - assume textLine+ to be a footer until proven otherwise 74*4b7eee81SChris Kay # - exception: the first block is not footer (as it is the title) 75*4b7eee81SChris Kay # - read textLine+ into a variable 76*4b7eee81SChris Kay # - then count blankLines 77*4b7eee81SChris Kay # - once the next textLine appears, print textLine+ blankLine* as these 78*4b7eee81SChris Kay # aren't footer 79*4b7eee81SChris Kay # - in END, the last textLine+ block is available for footer parsing 80*4b7eee81SChris Kay $AWK ' 81*4b7eee81SChris Kay BEGIN { 82*4b7eee81SChris Kay if (match(ENVIRON["OS"], "Windows")) { 83*4b7eee81SChris Kay RS="\r?\n" # Required on recent Cygwin 84*4b7eee81SChris Kay } 85*4b7eee81SChris Kay # while we start with the assumption that textLine+ 86*4b7eee81SChris Kay # is a footer, the first block is not. 87*4b7eee81SChris Kay isFooter = 0 88*4b7eee81SChris Kay footerComment = 0 89*4b7eee81SChris Kay blankLines = 0 90*4b7eee81SChris Kay } 91*4b7eee81SChris Kay 92*4b7eee81SChris Kay # Skip lines starting with commentChar without any spaces before it. 93*4b7eee81SChris Kay /^'"$commentChar"'/ { next } 94*4b7eee81SChris Kay 95*4b7eee81SChris Kay # Skip the line starting with the diff command and everything after it, 96*4b7eee81SChris Kay # up to the end of the file, assuming it is only patch data. 97*4b7eee81SChris Kay # If more than one line before the diff was empty, strip all but one. 98*4b7eee81SChris Kay /^diff --git / { 99*4b7eee81SChris Kay blankLines = 0 100*4b7eee81SChris Kay while (getline) { } 101*4b7eee81SChris Kay next 102*4b7eee81SChris Kay } 103*4b7eee81SChris Kay 104*4b7eee81SChris Kay # Count blank lines outside footer comments 105*4b7eee81SChris Kay /^$/ && (footerComment == 0) { 106*4b7eee81SChris Kay blankLines++ 107*4b7eee81SChris Kay next 108*4b7eee81SChris Kay } 109*4b7eee81SChris Kay 110*4b7eee81SChris Kay # Catch footer comment 111*4b7eee81SChris Kay /^\[[a-zA-Z0-9-]+:/ && (isFooter == 1) { 112*4b7eee81SChris Kay footerComment = 1 113*4b7eee81SChris Kay } 114*4b7eee81SChris Kay 115*4b7eee81SChris Kay /]$/ && (footerComment == 1) { 116*4b7eee81SChris Kay footerComment = 2 117*4b7eee81SChris Kay } 118*4b7eee81SChris Kay 119*4b7eee81SChris Kay # We have a non-blank line after blank lines. Handle this. 120*4b7eee81SChris Kay (blankLines > 0) { 121*4b7eee81SChris Kay print lines 122*4b7eee81SChris Kay for (i = 0; i < blankLines; i++) { 123*4b7eee81SChris Kay print "" 124*4b7eee81SChris Kay } 125*4b7eee81SChris Kay 126*4b7eee81SChris Kay lines = "" 127*4b7eee81SChris Kay blankLines = 0 128*4b7eee81SChris Kay isFooter = 1 129*4b7eee81SChris Kay footerComment = 0 130*4b7eee81SChris Kay } 131*4b7eee81SChris Kay 132*4b7eee81SChris Kay # Detect that the current block is not the footer 133*4b7eee81SChris Kay (footerComment == 0) && (!/^\[?[a-zA-Z0-9-]+:/ || /^[a-zA-Z0-9-]+:\/\//) { 134*4b7eee81SChris Kay isFooter = 0 135*4b7eee81SChris Kay } 136*4b7eee81SChris Kay 137*4b7eee81SChris Kay { 138*4b7eee81SChris Kay # We need this information about the current last comment line 139*4b7eee81SChris Kay if (footerComment == 2) { 140*4b7eee81SChris Kay footerComment = 0 141*4b7eee81SChris Kay } 142*4b7eee81SChris Kay if (lines != "") { 143*4b7eee81SChris Kay lines = lines "\n"; 144*4b7eee81SChris Kay } 145*4b7eee81SChris Kay lines = lines $0 146*4b7eee81SChris Kay } 147*4b7eee81SChris Kay 148*4b7eee81SChris Kay # Footer handling: 149*4b7eee81SChris Kay # If the last block is considered a footer, splice in the Change-Id at the 150*4b7eee81SChris Kay # right place. 151*4b7eee81SChris Kay # Look for the right place to inject Change-Id by considering 152*4b7eee81SChris Kay # CHANGE_ID_AFTER. Keys listed in it (case insensitive) come first, 153*4b7eee81SChris Kay # then Change-Id, then everything else (eg. Signed-off-by:). 154*4b7eee81SChris Kay # 155*4b7eee81SChris Kay # Otherwise just print the last block, a new line and the Change-Id as a 156*4b7eee81SChris Kay # block of its own. 157*4b7eee81SChris Kay END { 158*4b7eee81SChris Kay unprinted = 1 159*4b7eee81SChris Kay if (isFooter == 0) { 160*4b7eee81SChris Kay print lines "\n" 161*4b7eee81SChris Kay lines = "" 162*4b7eee81SChris Kay } 163*4b7eee81SChris Kay changeIdAfter = "^(" tolower("'"$CHANGE_ID_AFTER"'") "):" 164*4b7eee81SChris Kay numlines = split(lines, footer, "\n") 165*4b7eee81SChris Kay for (line = 1; line <= numlines; line++) { 166*4b7eee81SChris Kay if (unprinted && match(tolower(footer[line]), changeIdAfter) != 1) { 167*4b7eee81SChris Kay unprinted = 0 168*4b7eee81SChris Kay print "Change-Id: I'"$id"'" 169*4b7eee81SChris Kay } 170*4b7eee81SChris Kay print footer[line] 171*4b7eee81SChris Kay } 172*4b7eee81SChris Kay if (unprinted) { 173*4b7eee81SChris Kay print "Change-Id: I'"$id"'" 174*4b7eee81SChris Kay } 175*4b7eee81SChris Kay }' "$MSG" > "$T" && mv "$T" "$MSG" || rm -f "$T" 176*4b7eee81SChris Kay} 177*4b7eee81SChris Kay_gen_ChangeIdInput() { 178*4b7eee81SChris Kay echo "tree `git write-tree`" 179*4b7eee81SChris Kay if parent=`git rev-parse "HEAD^0" 2>/dev/null` 180*4b7eee81SChris Kay then 181*4b7eee81SChris Kay echo "parent $parent" 182*4b7eee81SChris Kay fi 183*4b7eee81SChris Kay echo "author `git var GIT_AUTHOR_IDENT`" 184*4b7eee81SChris Kay echo "committer `git var GIT_COMMITTER_IDENT`" 185*4b7eee81SChris Kay echo 186*4b7eee81SChris Kay printf '%s' "$clean_message" 187*4b7eee81SChris Kay} 188*4b7eee81SChris Kay_gen_ChangeId() { 189*4b7eee81SChris Kay _gen_ChangeIdInput | 190*4b7eee81SChris Kay git hash-object -t commit --stdin 191*4b7eee81SChris Kay} 192*4b7eee81SChris Kay 193*4b7eee81SChris Kay 194*4b7eee81SChris Kayadd_ChangeId 195