A script for downloading git library code, git library script
Because of the daily building requirement, you need to use a script to download the code for automatic compilation. This script is a small part of the function of the entire system.
#!/bin/bash#@author Liuyang#@date 2015-06-23function help() { echo "Usage: $0" echo " First argument should be the git repository's address" echo " for example: git@192.168.1.52:android/xiaomeidaojia.git" echo " Second argument should be the branch you want to checkout" echo " for example: dev" echo " If the second argument is not supplied, master will be used as default"}# Whether the given branch is in local branches.function is_in_local_branch() { git branch | grep $1 2>&1 > /dev/null return $?}# Whether the given branch is in remote branches.function is_in_remote_branch() { git branch -r | grep origin/$1 2>&1 > /dev/null return $?}if [[ $# != 1 && $# != 2 ]]; then help exit 1fi# Judge whether the repository's address is valid.if [[ $1 != *.git ]]; then help exit 1fi# Split the project's nameproject_name=`echo $(basename $1) | cut -d . -f 1`if [[ ! -d $project_name ]]; then git clone $1else cd $project_name git reset HEAD --hard git pull if [[ $2 == "" ]]; then exit fi is_in_local_branch $2 if [[ $? == 0 ]]; then git checkout $2 exit fi is_in_remote_branch $2 if [[ $? == 0 ]]; then git checkout -b $2 origin/$2 fifi
In the script, first determine whether the git library exists. If it does not exist, clone the repository.
Otherwise, all changes will be rolled back, the pull operation will be executed, and the given branch will be switched.