获取先前分支名称的Git钩子

时间:2022-07-03 23:43:56

I'm working on .git/hooks/post-checkout and having trouble either sourcing/exporting the branch name, or getting the prior branch name. I want to restart the server when switching to or from s3 branch.

我正在处理.git / hooks / post-checkout,无法获取/导出分支名称或获取先前的分支名称。我想在切换到s3分支或从s3分支切换时重新启动服务器。

I couldn't figure out how to source the env var in bash, so I tried using git to get the prior branch, but the closest I got was git checkout -/git checkout @{-1}, tho I'm not sure simply how to retrieve the prior branch name without the call to checkout.

我无法弄清楚如何在bash中获取env var,所以我尝试使用git来获取前一个分支,但是我得到的最接近的是git checkout - / git checkout @ { - 1},所以我不确定简单地说明如何在不调用checkout的情况下检索先前的分支名称。

Should I be using Git env vars instead of shell?

我应该使用Git env vars而不是shell吗?

Current file just restarts the server on each checkout

#!/bin/bash

touch tmp/restart.txt
echo " *** restarting puma-dev"

current_branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
if [ "$current_branch" = "s3" ]
then
  echo " *** please don't upload any files"
  echo
fi

3 个解决方案

#1


1  

You should be able to use this line to grab the previous branch name:

您应该能够使用此行来获取以前的分支名称:

git rev-parse --abbrev-ref @{-1}

git rev-parse --abbrev-ref @ { - 1}

and to get the current branch name:

并获取当前分支名称:

git rev-parse --abbrev-ref HEAD

git rev-parse --abbrev-ref HEAD

#2


1  

Git passes the previous and current ref names to the post-checkout hook, so you should be able to do something like:

Git将前一个和当前的引用名称传递给post-checkout钩子,所以你应该可以做类似的事情:

#!/bin/sh

oldref="$1"
newref="$2"
branch_update="$3"

[ "$branch_update" = '1' ] || exit  # exit if branch didn't change

[ "$oldref" = 'refs/heads/s3' ] && oldref_was_s3=1
[ "$newref" = 'refs/heads/s3' ] && newref_is_s3=1

if [ -z "$oldref_was_s3" -a -n "$newref_is_s3" ]; then
    echo " *** please don't upload any files"
fi

Totally untested, but it should be close.

完全未经测试,但它应该是接近的。

#3


0  

Thanks in part to Chris, whose method I couldn't interpret or get working, but found the information helpful, and thanks to Keif Kraken, whose method I did get working.

部分地感谢Chris,他的方法我无法解释或工作,但发现这些信息很有帮助,并且感谢Keif Kraken,他的方法让我开始工作。

Restart server when changing to or from a specific branch (s3)

更改到特定分支或从特定分支更改时重新启动服务器(s3)

.git/hooks/post-checkout script

.git / hooks / post-checkout脚本

#!/bin/bash
oldref=$(git rev-parse --abbrev-ref @{-1})
newref=$(git rev-parse --abbrev-ref head)

if [[ ( "$oldref" = "s3" || "$newref" = "s3" ) && "$oldref" != "$newref" ]]
then
  touch tmp/restart.txt
  echo " *** restarting puma-dev"
  echo " *** please don't upload any files"
fi

#1


1  

You should be able to use this line to grab the previous branch name:

您应该能够使用此行来获取以前的分支名称:

git rev-parse --abbrev-ref @{-1}

git rev-parse --abbrev-ref @ { - 1}

and to get the current branch name:

并获取当前分支名称:

git rev-parse --abbrev-ref HEAD

git rev-parse --abbrev-ref HEAD

#2


1  

Git passes the previous and current ref names to the post-checkout hook, so you should be able to do something like:

Git将前一个和当前的引用名称传递给post-checkout钩子,所以你应该可以做类似的事情:

#!/bin/sh

oldref="$1"
newref="$2"
branch_update="$3"

[ "$branch_update" = '1' ] || exit  # exit if branch didn't change

[ "$oldref" = 'refs/heads/s3' ] && oldref_was_s3=1
[ "$newref" = 'refs/heads/s3' ] && newref_is_s3=1

if [ -z "$oldref_was_s3" -a -n "$newref_is_s3" ]; then
    echo " *** please don't upload any files"
fi

Totally untested, but it should be close.

完全未经测试,但它应该是接近的。

#3


0  

Thanks in part to Chris, whose method I couldn't interpret or get working, but found the information helpful, and thanks to Keif Kraken, whose method I did get working.

部分地感谢Chris,他的方法我无法解释或工作,但发现这些信息很有帮助,并且感谢Keif Kraken,他的方法让我开始工作。

Restart server when changing to or from a specific branch (s3)

更改到特定分支或从特定分支更改时重新启动服务器(s3)

.git/hooks/post-checkout script

.git / hooks / post-checkout脚本

#!/bin/bash
oldref=$(git rev-parse --abbrev-ref @{-1})
newref=$(git rev-parse --abbrev-ref head)

if [[ ( "$oldref" = "s3" || "$newref" = "s3" ) && "$oldref" != "$newref" ]]
then
  touch tmp/restart.txt
  echo " *** restarting puma-dev"
  echo " *** please don't upload any files"
fi