From b9a6650ba8b88d2e9ac005284f757b94e398a46e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20J=C3=BCrges?= <jurges@astron.nl>
Date: Tue, 2 Jul 2019 16:07:59 +0200
Subject: [PATCH] ROADMT-103:  Add git client side hooks

The user has to install one of the two client side hooks
him|herself.  There's no way this can be enforced.
---
 support/hooks/commit-msg | 14 ++++++++++++++
 support/hooks/pre-commit | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)
 create mode 100644 support/hooks/commit-msg
 create mode 100644 support/hooks/pre-commit

diff --git a/support/hooks/commit-msg b/support/hooks/commit-msg
new file mode 100644
index 00000000000..9e2e4a454b5
--- /dev/null
+++ b/support/hooks/commit-msg
@@ -0,0 +1,14 @@
+#! /usr/bin/env ruby
+
+message_file = ARGV[0]
+message = File.read(message_file)
+$regex = /^((fixes|task|story|issue)\s)?(#(\d+)|[a-zA-Z]+-(\d)+):/i
+if !$regex.match(message)
+    STDERR.puts("[commit-msg hook] Your commit #{ rev } was rejected!\n"\
+        "[commit-msg hook] A commit message must start with a JIRA task ID,\n"\
+        " or \"task\", \"story\", \"issue\" or \"fixes\", followed by an issue ID.\n"\
+        "[commit-msg hook] For example: \"Task #42: Fixed bug\" or \"SW-22334: Fixed bug\"\n"\
+        "[commit-msg hook] Your commit message was: " + message)
+    exit 1
+end
+
diff --git a/support/hooks/pre-commit b/support/hooks/pre-commit
new file mode 100644
index 00000000000..826a1395c16
--- /dev/null
+++ b/support/hooks/pre-commit
@@ -0,0 +1,36 @@
+#! /usr/bin/env ruby
+
+$regex = /^((fixes|task|story|issue)\s)?(#(\d+)|[a-zA-Z]+-(\d)+):/i
+
+# Simple function, just for ease of testing
+def parse_message(msg,re)
+    if not re.match(msg)
+        return false
+    else
+        return true
+    end
+end
+
+# enforced custom commit message format
+def check_message_format
+    missed_revs = `git rev-list #{$rev_old}..#{$rev_new}`.split("\n")
+    missed_revs.each do |rev|
+        message = `git cat-file commit #{rev} | sed '1,/^$/d'`
+        if not parse_message(message,$regex)
+            STDERR.puts("[pre-receive hook] Your commit #{ rev } was rejected.")
+            STDERR.puts("[pre-receive hook] Commit message must start with a JIRA task ID,"\
+                " or \"Task\", \"Story\", \"Issue\" or \"Fixes\", followed by an issue ID.")
+            STDERR.puts("[pre-receive hook] For example: \"Task #42: Fixed bug\" or \"SW-22334: Fixed bug\"")
+            STDERR.puts("[pre-receive hook] Your commit message was: " +message)
+            exit 1
+        end 
+    end
+end
+
+# The "main" method ... when executing this file:
+# Only run this if the file itself is being executed
+if __FILE__ == $0
+    $rev_old, $rev_new, $ref = STDIN.read.split(" ")
+    check_message_format
+end
+
-- 
GitLab