技术解析

写了个 git push 脚本,容易崩溃,有大神给点意见么
0
2021-07-06 10:42:32
idczone
# !/bin/bash
# maybe you are using bash,fix the first line.
# Program:
# THIS PROGRAM IS FOR GIT PUSH
# BEFORE USE THIS SCRIPT, YOU MUST ADD YOU GIT PRIVATE KEY,LIKE FOLLOW SHELL COMMAND.
# ssh-add you_ssh_private_key_path,like ssh-add ~/.ssh_github/id_rsa
# History:
# 2015/10 wikinee First release
# ENV
# ubuntu 14.04 LTS
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH

echo "===========git config========="
echo "Ignore Permission"
git config core.fileMode false
echo "===========git branch========="
git branch
echo "===========git fetch==========="
echo "Input destination branch, must be already exist in remote.Enter use master:"
read to_branch
echo "Add tmp branch to store diff"
git fetch origin ${to_branch:=master}:tmp
echo "This time will modify following:"
git diff tmp
read -p "Merge it, y or Y?" yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ] ;then
echo $yn
echo "now merge it."
git merge tmp
else echo "Illeagal Character."
exit
fi

echo "===========git status========="
git status
echo "Input git add parameter,like -A"
read input1
git add $input1
echo "===========git commit========="
read -p "Git commit this changes?(y/n)" yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ] ;then
echo $yn
echo "Pow you can input commit message:"
elif [ "$yn" == "N" ]||[ "$yn" == "n" ] ;then
echo $yn
echo "Not Committed."
exit
else echo "illeagal character."
exit
fi
read input2
git commit -m "$input2" -a
echo "===========git push==========="
echo "Push to git?(y/n)"
read push_choice
if [ "$push_choice" == "y" ]||[ "$push_choice" == "Y" ] ;then
echo "Now push files,you can give parameter 1,default 'origin'."
read input3
echo "Now push files,you can give parameter 2,default 'master'."
read input4
git push -u $input3 $input4
else
echo "Already Commit, But Not Pushed"
fi
我建议手打这些命令,可以更自由地调整流程,出了问题也容易发现和修改,合并 PR 之前检查清楚。

目前在使用 alias 简化命令, 脚本是更进一步简化

太长 太乱 不看 (:教导主任的表情
哈哈

崩溃是什么情况?有出错信息吗?
感觉合并和提交可以分开两个脚本来写。另外没必要每次执行的时候都设置 PATH 和`git config`。

至少每步执行后需要判断返回值 $? 是不是 0 才继续下一步吧,不然一步没成,后面都在做无用功

bash -x 调试吧

轻轻地说:楼主英语……不敢恭维……蹩脚……如:“ Enter use master ” —— Type Enter to use 'master':"
另,输入 n N 就是非法字符?
—— read -p "Merge it, y or Y?" yn
if [ "$yn" == "Y" ]||[ "$yn" == "y" ] ;then
echo $yn
echo "now merge it."
git merge tmp
else echo "Illeagal Character."

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin/:~/bin
export PATH
我觉得开头的这两行语句的写法,要不得~~
人家电脑上面还有 /opt/bin 在 path 的怎么办?都被你洗了。
你要添加~/bin ,可以用字符串连接的方式来添加。。。

好建议
我现在改成回车就继续,不然就推出
我说的崩溃是指的情况好复杂,太多种可能了
能看懂就好了, enlish 不好,又戳我伤心事
path 这两句鸟哥私房菜上抄来的,你一说我都对人生产生怀疑,又去看了一眼,就是这么写的。
估计应该 PATH=xxx:$PATH

自己撸的一个发布代码工具,增量更新代码
代码: https://github.com/yzimhao/shellcode/blob/master/issuecode
说明: https://github.com/yzimhao/shellcode#1-issuecode

建议写成半自动的,经常需要思考的手打,然后固定的操作写成脚本。
一般而言,当我启动一些脚本时,就等它自动完成就好了。顺便喝口水。

数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服