gpt4 book ai didi

git - 如何编写 Git 本地分支检查脚本?

转载 作者:太空狗 更新时间:2023-10-29 13:53:45 25 4
gpt4 key购买 nike

寻求解决方案以实现以下目标:

  • 如果分支不是在本地创建的当前创建
  • 如果它已经存在提示用户并移动到下一条语句

到目前为止,我已经让它工作了,但还不够。我的问题确实是后者,但我想花点时间重新思考整个事情,并就如何更合理地编写这篇文章获得一些反馈。

当分支存在时,变量 existing_branch 提供 SHArefs/heads/branchName 否则 git 会控制并提供预期的 致命的:

check_for_branch() {
args=("$@")
echo `$branch${args[0]}`
existing_branch=$?
}

create_branch() {
current="git rev-parse --abbrev-ref HEAD"
branch="git show-ref --verify refs/heads/"

args=("$@")
branch_present=$(check_for_branch ${args[0]})
echo $branch_present
read -p "Do you really want to create branch $1 " ans
case $ans in
y | Y | yes | YES | Yes)
if [ ! -z branch_present ]; then
echo "Branch already exists"
else
`git branch ${args[0]}`
echo "Created ${args[0]} branch"
fi
;;
n | N | no | NO | No)
echo "exiting"
;;
*)
echo "Enter something I can work with y or n."
;;
esac
}

最佳答案

如果分支已经存在,你可以避免提示,并稍微缩短脚本,像这样:

create_branch() {
branch="${1:?Provide a branch name}"

if git show-ref --verify --quiet "refs/heads/$branch"; then
echo >&2 "Branch '$branch' already exists."
else
read -p "Do you really want to create branch $1 " ans
...
fi
}

关于git - 如何编写 Git 本地分支检查脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14412972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com