Make a shell script to update 3 git repos

前端 未结 4 1921
心在旅途
心在旅途 2021-01-31 04:09

I am working with 5 repos that I have cloned in my development environment. When I want to update a git repo, I enter the folder /home/adrian/repo1/ and do:

git checkout

4条回答
  •  后悔当初
    2021-01-31 04:36

    I would suggest to manage the update of all the repos with a cron script.
    Here is an example script for auto update base to their upstream.

    #!/bin/bash
    
    repo_update() {
        echo -e "\nUpdating $1" && cd $1
        if [[ `git rev-parse --abbrev-ref HEAD` != master ]]; then git checkout master; fi
        GIT_URL=$(git config --get remote.origin.url) && REMOTE=${GIT_URL##*:}
        REMOTE=https://api.github.com/repos/${REMOTE%.*}
    
        UPSTREAM=$(curl -s $REMOTE | jq -r '.parent.ssh_url')
        if [[ $UPSTREAM == null ]]; then return 1; fi
        if grep -q $UPSTREAM << EOF
        `git remote -v`
    EOF
        then
            git remote set-url upstream $UPSTREAM
        else
            git remote add upstream $UPSTREAM
        fi
    
        git fetch --prune upstream
        if [[ `git rev-list HEAD...upstream/master --count` == 0 ]]
        then
            echo -e "all the same, do nothing"
        else
            echo -e "update exist, let's checking!"
            git pull --rebase upstream master
            git reset --hard upstream/master
            push $GIT_URL
        fi
    }
    
    # Check connection
    ssh-add -l &>/dev/null
    if [[ "$?" == 2 ]]; then eval `ssh-agent` > /dev/null; fi
    
    # Check identity
    ssh-add -l &>/dev/null
    if [[ "$?" == 1 ]]; then expect $HOME/.ssh/agent > /dev/null && ssh-add -l; fi
    
    # Update repositories
    find ~/.gits -maxdepth 1 -mindepth 1 -type d | while read repo; do repo_update $repo; done
    

提交回复
热议问题