简体   繁体   English

Git hook prepare-commit-msg 以防止合并禁止的分支 - 将 Ruby 转换为 Bash

[英]Git hook prepare-commit-msg to prevent merging forbidden branches - translate Ruby to Bash

I'm trying to implement a git hook to prevent users from merging our environment branches (other than main) into ticket branches.我正在尝试实现 git 挂钩,以防止用户将我们的环境分支(除主分支外)合并到票证分支中。 This hook is exactly what I need, except for that it's written in Ruby.这个钩子正是我需要的,除了它是用 Ruby 编写的。 I need my hook to be in bash or shell so that it's easy for all of our devs to adopt without having to modify their machines.我需要我的钩子在 bash 或 shell 中,这样我们所有的开发人员都可以轻松采用而无需修改他们的机器。 The issue is I'm struggling with figuring out how to translate it since I'm not experienced with bash scripting for git.问题是我正在努力弄清楚如何翻译它,因为我对 git 的 bash 脚本没有经验。

Here is the script in Ruby:这是 Ruby 中的脚本:

#!/usr/bin/env ruby
FORBIDDEN_BRANCHES = ["uat", "support"]

def merge?
  ARGV[1] == "merge"
end

def merge_msg
  @msg ||= `cat .git/MERGE_MSG`
end

def from_branch
  @from_branch = merge_msg.match(/Merge branch '(.*?)'/)[1]
end

def from_forbidden_branch?
  FORBIDDEN_BRANCHES.include?(from_branch)
end

if merge? && from_forbidden_branch?
  out = `git reset --merge`
  puts
  puts " STOP THE PRESSES!"
  puts " You are trying to merge #{from_branch} into your branch."
  puts " Surely you don't mean that?"
  puts
  puts " run the following command now to discard your working tree changes:"
  puts
  puts " git reset --merge"
  puts
  exit 1
end

This is what I've got so far... I've figured out how to set the FORBIDDEN_BRANCHES array, and check if the current action being executed is a merge.这就是我到目前为止所得到的......我已经弄清楚了如何设置 FORBIDDEN_BRANCHES 数组,并检查当前正在执行的操作是否是合并。 What I'm missing is how to get the actual FROM_BRANCH (it's currently hardcoded to "support")我缺少的是如何获得实际的 FROM_BRANCH (它目前被硬编码为“支持”)

#!/bin/bash
FORBIDDEN_BRANCHES=("uat" "support" "develop")
FROM_BRANCH="support"
FROM_FORBIDDEN=0

for i in ${!FORBIDDEN_BRANCHES[@]}; do
  if test ${FORBIDDEN_BRANCHES[$i]} = $FROM_BRANCH
  then
    echo "Merging from $FROM_BRANCH is forbidden"
    FROM_FORBIDDEN=1
  fi
done

echo $FROM_FORBIDDEN

if test "$2" = "merge"
then
    if test $FROM_FORBIDDEN = 1
    then
        echo "STOP!"
        exit 1
    else
        echo "FROM_FORBIDDEN != 1, continuing"
    fi
else
    echo "Not merging"
fi

echo "Got to the end without errors..."

I didn't get an answer but I did come up with a solution of my own, if anyone else needs this hook.我没有得到答案,但我确实想出了自己的解决方案,如果其他人需要这个钩子的话。 It could surely be better, since I'm not experienced in bash scripting but it accomplishes the goal of preventing merging from a forbidden branch into any other branch它肯定会更好,因为我没有 bash 脚本的经验,但它实现了防止从禁止分支合并到任何其他分支的目标

#!/bin/bash
FORBIDDEN_BRANCHES=("uat" "support" "develop" "origin/uat" "origin/support" "origin/develop")
FROM_BRANCH="null"

MSG=`cat .git/MERGE_MSG`

[[ $MSG =~ ^'Merge branch '(.*) ]] && FROM_BRANCH=${BASH_REMATCH[1]}

if test $FROM_BRANCH != "null"
then
    FROM_BRANCH=$(echo $FROM_BRANCH | cut -f 1 -d " ")
    eval BRANCH=$FROM_BRANCH
else
    [[ $MSG =~ ^'Merge remote-tracking branch '(.*) ]] && FROM_BRANCH=${BASH_REMATCH[1]}
    FROM_BRANCH=$(echo $FROM_BRANCH | cut -f 1 -d " ")
    eval BRANCH=$FROM_BRANCH
fi

if test $FROM_BRANCH != "null"
then
    for i in ${!FORBIDDEN_BRANCHES[@]}; do
      if test ${FORBIDDEN_BRANCHES[$i]} = $BRANCH
      then
        echo "Merging from $BRANCH is forbidden"
        echo "Reset your branch using \"git reset --merge\""
        exit 1
      fi
    done
else
    "NOT COMITTING: FROM_BRANCH = $FROM_BRANCH"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM