update_package.sh

这是一个用于从Git仓库实时更新和切换指定分支的bash脚本。它首先检查是否为Git仓库,然后获取或设定默认分支,并实现强制更新。如果分支不匹配,脚本会进行切换;如果分支已匹配且未禁止更新,则执行快速合并或硬重置。
摘要由CSDN通过智能技术生成

2021SC@SDUSC

该模块主要用来做到实时更新,从git上push包。

其代码如下:

#!/bin/bash

script_dir="$(dirname "$0")"
package_dir="$1"
branch="$2"

option_no_update="${no_update:+1}"

source "${script_dir}"/bootstrap.sh
require 'styles'

if [[ -z "${package_dir}" ]]; then
    echo "Usage: $(basename "$0") <package-dir> [<branch>]"
    exit 1
fi

git_current_branch() {
    if ! command git rev-parse 2> /dev/null
    then
        # not a git repository
        return 2
    fi
    local ref="$(command git symbolic-ref HEAD 2> /dev/null)"
    if [[ -n "$ref" ]]
    then
        echo "${ref#refs/heads/}"
        return 0
    else
        return 1
    fi
}

git_default_branch() {
    if ! command git rev-parse 2> /dev/null
    then
        return 2
    fi
    local ref="$(command git symbolic-ref refs/remotes/origin/HEAD 2> /dev/null)"
    if [[ -n "$ref" ]]
    then
        echo "${ref#refs/remotes/origin/}"
        return 0
    else
        return 1
    fi
}

fetch_all_branches() {
    local fetch_all_pattern='\+refs/heads/\*:'
    if ! [[ "$(git config --get remote.origin.fetch)" =~ $fetch_all_pattern ]]; then
        if [[ -n "${option_no_update}" ]]; then
            echo $(warning 'WARNING:') 'forced update for switching branch to' $(print_option "${target_branch}")
        fi
        git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
    elif [[ -n "${option_no_update}" ]]; then
        return
    fi
    git fetch origin --depth 1
}

switch_branch() {
    local target_branch="$1"
    if [[ -z "${branch}" ]]; then
        echo $(warning 'WARNING:') "'${package_dir}' was on" \
             $(print_option "${current_branch:-(detached HEAD)}") 'instead of' $(print_option "${target_branch}")
    fi
    fetch_all_branches
    git checkout "${target_branch}" || exit 1
}

pushd "${package_dir}" &> /dev/null

current_branch="$(git_current_branch)"
if [[ $? -gt 1 ]]; then
    echo $(warning 'WARNING:') "not a git repository, skipped updating '${package_dir}'"
    exit
fi
if [[ -z "${branch}" ]]; then
    target_branch="$(git_default_branch)"
else
    target_branch="${branch}"
fi
if [[ "${current_branch}" != "${target_branch}" ]]; then
    switch_branch "${target_branch}"
elif [[ -z "${option_no_update}" ]]; then
    git fetch --recurse-submodules && (
        git merge --ff-only "origin/${target_branch}" || (
            echo $(warning 'WARNING:') 'fast-forward failed;' \
                 'doing a hard reset to' $(print_option "origin/${target_branch}")
            git reset --hard "origin/${target_branch}"
        )
    ) || exit 1
fi

popd &> /dev/null

每段最后都将不需要的版块pop进入了空设备文件中。以做到实时更新。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值