-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg
executable file
·55 lines (46 loc) · 1.77 KB
/
commit-msg
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
#!/usr/bin/env ruby
MIN_COMMIT_LENGTH = 20
PREFIX = "#"
PREFIX_LOW = PREFIX.downcase
$currentBranch = `git rev-parse --abbrev-ref HEAD`
$commitFileName = ARGV[0]
def checkTicketNumbers(branch, commit)
if branch != commit
puts "Ticket number of commit does not match the branch name (#{commit} != #{branch})"
exit 1
end
end
def rewriteCommitMsg(lines, ticketNumber, commitMsg)
newCommitMsg = "[#{PREFIX}#{ticketNumber}] #{commitMsg}"
lines[0] = newCommitMsg
newFileContents = lines.join("")
IO.write $commitFileName, newFileContents
puts "Commit message corrected to: '#{newCommitMsg.delete("\n")}'"
end
if /(?:#{PREFIX}|#{PREFIX_LOW})(\d+)-.+/ =~ $currentBranch
# Branch name does start with ticket number, check commit message
branchTicketNumber = $1 # Ticket number from branch
commitFileLines = IO.readlines($commitFileName)
commitMessage = commitFileLines[0]
unless commitMessage.start_with?("Merge")
# Check if commit message starts with valid ticket number and commit is > 20 chars
if /\[#{PREFIX}(\d+)\] .{#{MIN_COMMIT_LENGTH},}$/ =~ commitMessage
# Commit message does match the format, check the ticket numbers match and error if not
checkTicketNumbers($1, branchTicketNumber)
else
# Commit message does not match the format
# If the prefix is in lowercase or there are extra spaces, we can correct it
if /\[(?:#{PREFIX}|#{PREFIX_LOW})(\d+)\] *(.{#{MIN_COMMIT_LENGTH},})$/ =~ commitMessage
checkTicketNumbers($1, branchTicketNumber)
rewriteCommitMsg(commitFileLines, $1, $2)
else
if commitMessage.length < MIN_COMMIT_LENGTH
# Commit message too short, error..
puts "Commit message must be > #{MIN_COMMIT_LENGTH} chars"
exit 1
end
rewriteCommitMsg(commitFileLines, branchTicketNumber, commitMessage)
end
end
end
end