1
votes

I have a pre-commit hook script to check for TaskID in log message. I'm unable to make out the logic for same. In Highlighted **if** statement i need a logic to check if first letter of first line is TaskID:(multiple digits)-(space)(log message)

Pre-commit hook:

REPOS="$1"
TXN="$2"

# Check log message for proper task/bug identification

if [ -x ${REPOS}/hooks/check_log_message.sh ]; then
${REPOS}/hooks/check_log_message.sh "${REPOS}" "${TXN}" 1>&2 || exit 1
fi
exit 0

=======>>>>>check_log_message.sh

#!/bin/bash

REPOS="${1}"
TXN="${2}"

SVNLOOK=/usr/bin/svnlook

LOG_MSG_LINE1=`${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1`

**if (echo "${LOG_MSG_LINE1}" | egrep '^[T][a][s][k][I][D][:]?[-][1-9]
[\s]*.*$' > /dev/null;) \
|| (echo "${LOG_MSG_LINE1}" | egrep '^[a-zA-Z]+[-][1-9][0-9]*[:]?[\s]*.*$' 
> /dev/null;)**
then
exit 0
else
echo ""
echo "Your log message does not contain a TaskID(or bad format used)"
echo "The TaskID must be the first item on the first line of the log 
message."
echo ""
echo "Proper TaskID format--> TaskID:xxx- 'Your commit message'  "
exit 1
fi
2
I'm also looking for a way to implement this only on svn trunk and not on branches/tags. I also want a way out in case there are no TaskID associated then it should also accept [TaskID:0000(four zeros)- commit message]Himanshu

2 Answers

1
votes

I guess your question concerns using conditionals in Bash. You can work with the exit codes of programs directly. For example, if egrep matches something, it exits with code 0 which means success, otherwise it exits with non-zero, which means failure. And you can use this is in conditions, for example:

if command; then
    echo success
else
    echo failure
fi

Where command can be a pipeline, for example:

if ${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q ^TaskID:
then
    exit 0
fi

This means if the first line of the log starts with TaskID:, then exit with 0. Instead of an if statement, you could also use a shorter form with && like this:

${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q ^TaskID: && exit 0

In both examples I used -q with egrep, to suppress the output (the matched line), as I guess you probably don't need it.

The full script with the more complete pattern:

#!/bin/bash

REPOS="${1}"
TXN="${2}"

SVNLOOK=/usr/bin/svnlook

${SVNLOOK} log -t "${TXN}" "${REPOS}" | head -n1 | egrep -q '^TaskID:[0-9][0-9]*- ' && exit 0

echo ""
echo "Your log message does not contain a TaskID(or bad format used)"
echo "The TaskID must be the first item on the first line of the log 
message."
echo ""
echo "Proper TaskID format--> TaskID:xxx- 'Your commit message'  "
exit 1
0
votes

Alternatively, I modified my script with help of a colleague.

REPOS="$1"
TXN="$2"

# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook

LOGMSG=$($SVNLOOK log -t "$TXN" "$REPOS")

# check if any comment has supplied by the commiter
if [ -z "$LOGMSG" ]; then
echo "Your commit was blocked because it have no comments." 1>&2
exit 1
fi

#check minimum size of text
if [ ${#LOGMSG} -lt 15 ]; then
echo "Your Commit was blocked because the comments does not meet minimum length requirements (15 letters)." 1>&2
exit 1
fi

# get TaskID by regex
TaskID=$(expr "$LOGMSG" : '\([#][0-9]\{1,9\}[:][" "]\)[A-Za-z0-9]*')

# Check if task id was found. 
if [ -z "$TaskID" ]; then

echo ""  1>&2
echo "No Task id found in log message \"$LOGMSG\"" 1>&2
echo ""  1>&2
echo "The TaskID must be the first item on the first line of the log message."  1>&2
echo ""  1>&2
echo "Proper TaskID format--> #123- 'Your commit message'  " 1>&2
exit 1
fi