Skip to content

Create shell script for import git repositories automatically

astrapi69 edited this page Apr 27, 2016 · 7 revisions

I install linux often on my virtual boxes and start a clean install.

For my projects i use git and therefore i create a script for import my github repositories automatically, and saves me a lot of typing and time.

So here is the code:

#!/bin/zsh
# import remote git repositories to local git dir

# Echoes all commands before executing.
set -o verbose
# settings 
remoteGithubHost=github.com
remoteOrgUser=lightblueseas
localCodeDir="${HOME}/git/"
# Here is the array with the repositories names that will be used for cloning
# Change this array to your git repositories names 
githubRepositoryNames=(
"jfugue-core" 
"mvn-parent-projects" 
"linuxstuff" 
"knockout-doc-examples" 
"payment-system-data" 
"order-management-data" 
"user-management-data"
"file-worker"
"swing-components"
"silly-collections"
"email-tails"
"gen-tree"
"jcommons-lang"
"net-extensions"
"test-objects"
"vintage-time"
"xml-extensions"
"resource-system-data"
"address-book-data"
"resource-bundles-data"
"message-system-data"
"phone-data-management-system"
"scheduler-system-data"
"dating-system-data"
"event-system-data"
"rating-system-data"
"persistence-api"
"server-configurations"
"wicket-js-addons"
"mvn-less-css"
"design-patterns"
"wicket-application-template"
"freelancerdocs"
"sitedocs")

cd $localCodeDir
pwd
for gitRepo in $githubRepositoryNames

do
  localRepoDir=$(echo ${localCodeDir}${gitRepo}|cut -d'.' -f1)

   if [[ -d $localRepoDir ]]; then 
		echo -e "Directory $localRepoDir already exits, skipping ...\n"
	else   
		echo -e "Directory $localRepoDir does not exits, start clone repository ...\n"
		echo -e "Running clone command: $cloneCmd \n"
		git clone https://$remoteGithubHost/$remoteOrgUser/$gitRepo.git
		cd $gitRepo 
		git checkout develop
		cd $localCodeDir
		
  fi
done

Wouldn't it be nice to be able to read the settings from a file. So lets write one shell script that does exactly this. Lets create first the settings script with the name 'impgitrep-settings.sh' that contains the variables:

# settings 
remoteHost=github.com
remoteUser=lightblueseas
localCodeDir="${HOME}/git/"
gitRepositoryNames=(
"jfugue-core" 
"mvn-parent-projects" 
"linuxstuff" 
"knockout-doc-examples" 
"payment-system-data" 
"order-management-data" 
"user-management-data"
"file-worker"
"swing-components"
"silly-collections"
"email-tails"
"gen-tree"
"jcommons-lang"
"net-extensions"
"test-objects"
"vintage-time"
"xml-extensions"
"resource-system-data"
"address-book-data"
"resource-bundles-data"
"message-system-data"
"phone-data-management-system"
"scheduler-system-data"
"dating-system-data"
"event-system-data"
"rating-system-data"
"persistence-api"
"server-configurations"
"wicket-js-addons"
"mvn-less-css"
"design-patterns"
"wicket-application-template"
"freelancerdocs")

Note: you can alter this script to your needs.

Now we have to create a new shell script with the name 'impgitrep-from-file.sh' for read the variables from file:

#!/bin/zsh
# import remote git repositories to local git dir

# import variables from file.
source impgitrep-settings.sh

# Echoes all commands before executing.
set -o verbose

# change to git directory
cd $localCodeDir
pwd
# iterate over the repository names
for gitRepositoryName in $gitRepositoryNames

do
	# create a string from the local repository directory for checks
	localRepoDir=$(echo ${localCodeDir}${gitRepositoryName}|cut -d'.' -f1)
	# check if the local repository directory already exists
	if [[ -d $localRepoDir ]]; then 
		# if yes skip and continue the iteration
		echo -e "Directory $localRepoDir already exits, skipping ...\n"
	else 
		# if not then clone it and check out the develop branch  
		echo -e "Directory $localRepoDir does not exits, start clone repository ...\n"
		echo -e "Running clone command: $cloneCmd \n"
		git clone https://$remoteHost/$remoteUser/$gitRepositoryName.git
		cd $gitRepositoryName 
		git checkout develop
		cd $localCodeDir			
	fi
done

Note: both files are in the same directory. If not you have to write the full path from the settings script.

To run now the script you can execute it in a shell like this:

$ chmod +x impgitrep-from-file.sh && ./impgitrep-from-file.sh

When you have to give a password for every repository and the password is the same than it is a good idea to add a cache for the credentials.

You do that in git under linux with this command:

$ git config --global credential.helper cache
# Set git to use the credential memory cache

If your repository names have dots than you have to adapt the script like the following:

#!/bin/zsh
# import remote git repositories to local git dir

# import variables from file.
source impgitrep-settings-bitbucket.sh

# Echoes all commands before executing.
set -o verbose

# change to git directory
cd $localCodeDir
pwd
# iterate over the repository names
for gitRepositoryName in $gitRepositoryNames

do
	# create a string from the local repository directory for checks
	localRepoDir=$(echo ${localCodeDir}${gitRepositoryName})
	# check if the local repository directory already exists
	if [[ -d $localRepoDir ]]; then 
		# if yes skip and continue the iteration
		echo -e "Directory $localRepoDir already exits, skipping ...\n"
	else 
		# if not then clone it and check out the develop branch  
		echo -e "Directory $localRepoDir does not exits, start clone repository ...\n"
		echo -e "Running clone command: $cloneCmd \n"
		# git clone 
		git clone https://$remoteUser@$remoteHost/$remoteUser/$gitRepositoryName.git
		cd $gitRepositoryName 
		git checkout develop
		cd $localCodeDir			
	fi
done

What is changed is this:

from

	localRepoDir=$(echo ${localCodeDir}${gitRepositoryName}|cut -d'.' -f1)

to

	localRepoDir=$(echo ${localCodeDir}${gitRepositoryName})
Clone this wiki locally