Skip to content
Snippets Groups Projects

Convert git repo to use git.soton.ac.uk

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Ed Rogers

    A script to allow automated switch of the origin repo from another hosting provider (say gitlab.com) to git.soton.ac.uk.

    It assumes you have already created a repo on git.soton.ac.uk with the same name as the repo you are moving from. It assumes you are connecting to both old and new repos using SSH.

    Use with caution on repos with subtrees. You may end up pulling things you don't expect. This could probably be fixed, but hasn't been yet...

    Note that it does not necessarily preserve the state of the local repository, but you could (if you want) run it on a fresh clone of the original repo and delete afterwards.

    You have to tell it (as variables at the start of the script) your Soton username soton and the name of the remote you are replacing from (default 'origin')

    Only tested on my repos, so use at own risk...

    Algorithm

    1. Fetch from repo.
    2. For each branch on repo from.
      1. If a local branch exists, set the local branch with the same name to track the remote branch.
      2. If not, create a local branch that tracks the remote branch.
    3. Pull all branches Affects state of local repo.
    4. Rename from with a label pulled from the hostname of the old repo (for example for gitlab.com you would get a label gitlab).
    5. Add a new remote on git.soton.ac.uk with the same name as the old repo - this repo must already have been created.
    6. Push all branches to the new repo, and set them to track the new repo.
    Edited
    port_git.sh 1.86 KiB
    #!/bin/bash
    
    #pull and checks out all branches - note it will merge all remotes with local branches!
    
    #assumes you want to replace your current origin and you want to make git.soton.ac.uk the new origin with "gitlab/github" as a remote called "gitlab/github"
    #assumes you have set up a repo on git.soton.ac.uk with the same name as the original repo on another server
    
    sotonuser="etfr"
    from="origin"
    to="git.soton.ac.uk"
    
    usage()
    {
        echo "usage: port_git.sh [[[-f file ] [-i]] | [-h]]"
    }
    
    pull=1
    push=1
    while [ "$1" != "" ]; do
        case $1 in
            --no-push )           	push=0
                                    ;;
            --no-pull )           	pull=0
                                    ;;								
            -s | --simple )    		pull=0
    								push=0
                                    ;;
            -h | --help )           usage
                                    exit
                                    ;;
            * )                     usage
                                    exit 1
        esac
        shift
    done
    
    reponame=$(git remote get-url $from | awk -F'/' '{ print $NF }')
    old_label=$(git remote get-url $from | sed 's/.*@//' | sed 's/\..*//')
    
    
    #make sure all remote branches are tracked locally
    git fetch $from
    if [ "$pull" = "1" ]; then
    	for remote in $(git branch -r | grep -v '\->'); do 
    		git branch --track "${remote#$from/}" "$remote" 2>/dev/null || 
    		git branch --set-upstream-to="$remote" "${remote#$from/}"; 
    	done
    	git pull --all
    fi
    
    git remote rename "$from" "$old_label"
    
    git remote add $from "git@$to:$sotonuser/$reponame"
    git fetch $from
    if [ "$push" = "1" ]; then
    	git push -u --all $from
    else
    	for branch in  $(git for-each-ref refs/heads --format='%(refname)'); do
    		bare_branch="${branch##refs/heads/}"
    		echo "Setting up branch $bare_branch"
    		git branch --track "$bare_branch" "$from/$bare_branch" 2>/dev/null || 
    		git branch --set-upstream-to="$from/$bare_branch" "$bare_branch"; 
    	done	
    fi
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment