0
0
Gitdevops~20 mins

commit-msg hook for message validation in Git - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Commit Message Validator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Output of a commit-msg hook script
Given this commit-msg hook script, what will be the output if the commit message is "Fix bug in login"?
Git
#!/bin/sh
MESSAGE_FILE=$1
MESSAGE=$(cat "$MESSAGE_FILE")
if echo "$MESSAGE" | grep -qE '^\[JIRA-[0-9]+\]'; then
  exit 0
else
  echo "Error: Commit message must start with a JIRA ticket ID like [JIRA-123]"
  exit 1
fi
AError: Commit message is too short
BNo output, commit succeeds
CSyntax error in script
DError: Commit message must start with a JIRA ticket ID like [JIRA-123]
Attempts:
2 left
💡 Hint
Check if the commit message starts with the required pattern.
🧠 Conceptual
intermediate
1:00remaining
Purpose of commit-msg hook
What is the main purpose of a commit-msg hook in Git?
ATo check and validate the commit message before allowing the commit
BTo automatically push commits to remote repositories
CTo merge branches after commit
DTo delete untracked files before commit
Attempts:
2 left
💡 Hint
Think about what happens right after you write a commit message.
Troubleshoot
advanced
1:30remaining
Why does the commit-msg hook not run?
You created a commit-msg hook script in .git/hooks/ but it does not run when committing. What is the most likely reason?
AThe repository is not initialized
BThe script file is not executable
CThe hook script is named commit-msg.txt
DThe commit message is empty
Attempts:
2 left
💡 Hint
Check file permissions for scripts in .git/hooks/
🔀 Workflow
advanced
2:00remaining
Implementing commit-msg hook for multiple rules
You want a commit-msg hook that enforces these rules:
1. Commit message must start with [PROJ-123]
2. Commit message must be at least 15 characters long
Which script correctly implements both checks?
A
#!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE '^\[PROJ-[0-9]+\]'; then
  echo "Error: Message must start with [PROJ-123]"
  exit 1
fi
if [ ${#MSG} -lt 15 ]; then
  echo "Error: Message must be at least 15 characters"
  exit 1
fi
exit 0
B
#!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -qE '^\[PROJ-[0-9]+\]' || [ ${#MSG} -ge 15 ]; then
  exit 0
else
  echo "Error: Commit message invalid"
  exit 1
fi
C
#!/bin/sh
MSG=$(cat "$1")
if echo "$MSG" | grep -qE '^\[PROJ-[0-9]+\]' && [ ${#MSG} -ge 15 ]; then
  exit 1
else
  echo "Error: Commit message invalid"
  exit 0
fi
D
#!/bin/sh
MSG=$(cat "$1")
if ! echo "$MSG" | grep -qE '^\[PROJ-[0-9]+\]' || [ ${#MSG} -lt 15 ]; then
  echo "Error: Commit message invalid"
  exit 1
fi
exit 0
Attempts:
2 left
💡 Hint
Both conditions must be true to pass validation.
Best Practice
expert
2:00remaining
Best practice for sharing commit-msg hooks in a team
What is the best way to ensure all team members use the same commit-msg hook for message validation?
AAdd the hook script to .gitignore so it is not tracked
BAsk each member to manually create the hook in their .git/hooks/ folder
CStore the hook script in the repository and use a setup script to copy it to .git/hooks/
DUse a global Git hook configured on each developer's machine
Attempts:
2 left
💡 Hint
Think about version controlling the hook and automating installation.