V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
wikinee
V2EX  ›  Linux

写了个 git push 脚本,容易崩溃,有大神给点意见么

  •  
  •   wikinee · 2015-12-10 11:21:32 +08:00 · 3527 次点击
    这是一个创建于 3065 天前的主题,其中的信息可能已经有所发展或是发生改变。
    # !/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
    11 条回复    2015-12-11 17:07:09 +08:00
    wlsnx
        1
    wlsnx  
       2015-12-10 12:07:44 +08:00
    我建议手打这些命令,可以更自由地调整流程,出了问题也容易发现和修改,合并 PR 之前检查清楚。
    wikinee
        2
    wikinee  
    OP
       2015-12-10 12:33:30 +08:00
    @wlsnx 目前在使用 alias 简化命令, 脚本是更进一步简化
    500miles
        3
    500miles  
       2015-12-10 12:55:36 +08:00
    太长 太乱 不看 (:教导主任的表情

    哈哈
    spacewander
        4
    spacewander  
       2015-12-10 13:14:48 +08:00
    崩溃是什么情况?有出错信息吗?
    感觉合并和提交可以分开两个脚本来写。另外没必要每次执行的时候都设置 PATH 和`git config`。
    KyleMeow
        5
    KyleMeow  
       2015-12-10 16:52:47 +08:00
    至少每步执行后需要判断返回值 $? 是不是 0 才继续下一步吧,不然一步没成,后面都在做无用功
    jackysc
        6
    jackysc  
       2015-12-10 16:53:54 +08:00
    bash -x 调试吧
    GNiux
        7
    GNiux  
       2015-12-10 17:45:15 +08:00 via iPhone
    轻轻地说:楼主英语……不敢恭维……蹩脚……如:“ 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."
    darkbill
        8
    darkbill  
       2015-12-10 17:53:28 +08:00
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin/:~/bin
    export PATH

    我觉得开头的这两行语句的写法,要不得~~
    人家电脑上面还有 /opt/bin 在 path 的怎么办?都被你洗了。
    你要添加~/bin ,可以用字符串连接的方式来添加。。。
    wikinee
        9
    wikinee  
    OP
       2015-12-10 20:19:03 +08:00   ❤️ 1
    @spacewander 好建议
    @KyleMeow 我现在改成回车就继续,不然就推出
    @jackysc 我说的崩溃是指的情况好复杂,太多种可能了
    @GNiux 能看懂就好了, enlish 不好,又戳我伤心事
    @darkbill path 这两句鸟哥私房菜上抄来的,你一说我都对人生产生怀疑,又去看了一眼,就是这么写的。
    估计应该 PATH=xxx:$PATH
    yzimhao
        10
    yzimhao  
       2015-12-11 11:08:19 +08:00
    自己撸的一个发布代码工具,增量更新代码
    代码: https://github.com/yzimhao/shellcode/blob/master/issuecode
    说明: https://github.com/yzimhao/shellcode#1-issuecode
    zhouyg
        11
    zhouyg  
       2015-12-11 17:07:09 +08:00
    建议写成半自动的,经常需要思考的手打,然后固定的操作写成脚本。

    一般而言,当我启动一些脚本时,就等它自动完成就好了。顺便喝口水。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2308 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:39 · PVG 11:39 · LAX 20:39 · JFK 23:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.