Pārlūkot izejas kodu

feat(git-template): Update the git hooks

Signed-off-by: Jeremy MAURO <jeremy.mauro@gmail.com>
Jeremy MAURO 2 gadi atpakaļ
vecāks
revīzija
899fed639c

+ 11 - 42
dotfiles/git-templates/hooks/jira_check_lib.sh

@@ -1,5 +1,6 @@
 #!/bin/bash
 
+# --[ function
 function get_sed () {
   OS=$(uname  -s)
   if [ "${OS}" == "Linux" ]; then
@@ -15,54 +16,22 @@ function get_sed () {
   echo "${SED}"
 }
 
-function not_include_ticket () {
-  set -o pipefail
-  if $(cat "$*" | grep -q "^JIRA: "); then
-    return 1
-  fi
-  return 0
-}
-
 function get_branch_name () {
     echo "$(git branch --show-current)"
 }
 
 # Function to extract the ticket number:
-# Ex: AVXSRE-12324-toto -> AVXSRE-1234
-#     AVXSRE-12134/titi -> AVXSRE-1234
+# Ex: 12324-toto -> AVXSRE-1234
+#     12134/titi -> AVXSRE-1234
 function clean_branch_name () {
-  echo "$1" | grep -E -o '[A-Z][A-Z0-9]+-[0-9]+'
+  echo "$1" | grep -E -o '[0-9][0-9]+'
 }
 
-function add_jira_ref () {
-    # hook arguments
-    COMMIT_MSG_FILE=$1
-    # Possible values are none (git commit), message (git commit -m <msg>), template, merge, squash, or commit
-    COMMIT_SOURCE=$2
-
-    SED="$(get_sed)"
-    BRANCH="$(get_branch_name)"
-    CLEAN_BRANCH="$(clean_branch_name $BRANCH)"
+# --[ Env
+TRAILER=$1
+SED="$(get_sed)"
+BRANCH="$(get_branch_name)"
+CLEAN_BRANCH="$(clean_branch_name $BRANCH)"
 
-    # check branch name isn’t empty (typical e.g. during rebase)
-    if not_include_ticket "${COMMIT_MSG_FILE}" && [ -n "${CLEAN_BRANCH}" ]; then
-      # check that this is a "message": if a -m or -F option was given
-      if [ "${COMMIT_SOURCE}" == "message" ]; then
-        echo >> "${COMMIT_MSG_FILE}"
-        echo "JIRA: ${CLEAN_BRANCH}" >> "${COMMIT_MSG_FILE}"
-        return 0
-      fi
-
-      # check that this is a "commit"
-      if [ -z "${COMMIT_SOURCE}" ]; then
-        ${SED} -i "1s@^@\n\nJIRA: ${CLEAN_BRANCH}@" "${COMMIT_MSG_FILE}"
-        return 0
-      fi
-
-      # check that this is an "amend"
-      if [ "${COMMIT_SOURCE}" == "commit" ]; then
-        ${SED} -i "2s@^@\n\nJIRA: ${CLEAN_BRANCH}\n@" "${COMMIT_MSG_FILE}"
-        return 0
-      fi
-    fi
-}
+# --[ Main function
+echo "${1}-${CLEAN_BRANCH}"

+ 52 - 0
dotfiles/git-templates/hooks/jira_trailer.sh

@@ -0,0 +1,52 @@
+#!/bin/bash
+
+# --[ function
+function get_sed () {
+	OS=$(uname  -s)
+	if [ "${OS}" == "Linux" ]; then
+		SED=$(command -v sed)
+	else
+		SED=$(command -v gsed)
+	fi
+	if [ -z "${SED}" ]; then
+		echo ">> GNU 'sed' was not found on the system."
+		echo ">> Please install it"
+		exit 1
+	fi
+	echo "${SED}"
+}
+
+function rebasing-branch() {
+for location in rebase-merge rebase-apply; do
+	path=$(git rev-parse --git-path ${location})
+	if test -d ${path}; then
+		revision=$(<${path}/head-name)
+		echo ${revision##refs/heads/}
+		return 0
+	fi
+done
+}
+
+function get_branch_name () {
+  BRANCH_NAME="$(git branch --show-current)"
+  if [ -z "${BRANCH_NAME}" ]; then
+    BRANCH_NAME="$(rebasing-branch)"
+  fi
+  echo "${BRANCH_NAME}"
+}
+
+# Function to extract the ticket number:
+# Ex: 12324-toto -> AVXSRE-1234
+#     12134/titi -> AVXSRE-1234
+function clean_branch_name () {
+	echo "$1" | grep -E -o '[0-9][0-9]+'
+}
+
+# --[ Env
+TRAILER=$1
+SED="$(get_sed)"
+BRANCH="$(get_branch_name)"
+CLEAN_BRANCH="$(clean_branch_name $BRANCH)"
+
+# --[ Main function
+echo "${1}-${CLEAN_BRANCH}"

+ 6 - 0
dotfiles/git-templates/hooks/post-checkout

@@ -0,0 +1,6 @@
+#!/usr/bin/env bash
+
+# Do some actions each time branch is switched
+
+# --[ First refresh the repository
+hub sync

+ 20 - 0
dotfiles/git-templates/hooks/pre-commit

@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+# File generated by pre-commit: https://pre-commit.com
+# ID: 138fd403232d2ddd5efb44317e38bf03
+
+# start templated
+INSTALL_PYTHON=/usr/bin/python3
+ARGS=(hook-impl --config=.pre-commit-config.yaml --hook-type=pre-commit --skip-on-missing-config)
+# end templated
+
+HERE="$(cd "$(dirname "$0")" && pwd)"
+ARGS+=(--hook-dir "$HERE" -- "$@")
+
+if [ -x "$INSTALL_PYTHON" ]; then
+    exec "$INSTALL_PYTHON" -mpre_commit "${ARGS[@]}"
+elif command -v pre-commit > /dev/null; then
+    exec pre-commit "${ARGS[@]}"
+else
+    echo '`pre-commit` not found.  Did you forget to activate your virtualenv?' 1>&2
+    exit 1
+fi

+ 0 - 70
dotfiles/git-templates/hooks/prepare-commit-msg

@@ -1,70 +0,0 @@
-#!/usr/bin/env bash
-# Description:  Git hook to check the JIRA reference in the commit message
-# Usage:        Copy this file on your hooksPath with the name "commit-msg"
-# Ref:          https://aviatrix.atlassian.net/l/c/ARC3NMy1#Git-client-hook-for-automatic-Jira-ref-insertion
-
-set -e
-
-[ -z "${GITHOOK_DEBUG}" ] || set -x
-
-# hook arguments
-COMMIT_MSG_FILE=$1
-# Possible values are none (git commit), message (git commit -m <msg>), template, merge, squash, or commit
-COMMIT_SOURCE=$2
-
-BRANCH=$(git branch --show-current)
-
-function get_sed () {
-  OS=$(uname  -s)
-  if [ "${OS}" == "Linux" ]; then
-    SED=$(command -v sed)
-  else
-    SED=$(command -v gsed)
-  fi
-  if [ -z "${SED}" ]; then
-    echo ">> GNU 'sed' was not found on the system."
-    echo ">> Please install it"
-    exit 1
-  fi
-  echo "${SED}"
-}
-
-function not_include_ticket () {
-  set -o pipefail
-  if $(cat "$*" | grep -q "^JIRA: "); then
-    return 1
-  fi
-  return 0
-}
-
-# Function to extract the ticket number:
-# Ex: AVXSRE-12324-toto -> AVXSRE-1234
-#     AVXSRE-12134/titi -> AVXSRE-1234
-function get_branch_name () {
-  echo "$*" | ${SED} -e 's@\([[:alpha:]]\+\).*-\([[:digit:]]\+\).*@\1-\2@g'
-}
-
-SED="$(get_sed)"
-BRANCH="$(get_branch_name $(git branch --show-current))"
-
-# check branch name isn’t empty (typical e.g. during rebase)
-if not_include_ticket "${COMMIT_MSG_FILE}" && [ -n "${BRANCH}" ]; then
-  # check that this is a "message": if a -m or -F option was given
-  if [ "${COMMIT_SOURCE}" == "message" ]; then
-    echo >> ${COMMIT_MSG_FILE}
-    echo "JIRA: ${BRANCH}" >> ${COMMIT_MSG_FILE}
-    exit 0
-  fi
-
-  # check that this is a "commit"
-  if [ -z "${COMMIT_SOURCE}" ]; then
-    ${SED} -i "1s@^@\n\nJIRA: ${BRANCH}@" ${COMMIT_MSG_FILE}
-    exit 0
-  fi
-
-  # check that this is an "amend"
-  if [ "${COMMIT_SOURCE}" == "commit" ]; then
-    ${SED} -i "2s@^@\n\nJIRA: ${BRANCH}\n@" ${COMMIT_MSG_FILE}
-    exit 0
-  fi
-fi