[리눅스/윈도] git clone 응용 스크립트

2023. 5. 6. 18:33버전관리 및 빌드/Git

작성일 : 2021. 9. 15.

 

IDE 를 활용하지 않고 커맨드라인으로 git 을 활용할 때 유용하게 쓸 수 있다.

소스 작업 도중에 로컬 작업 디렉토리를 삭제했을 때 다시 가져오는 방법을 스크립트로 작성하였다.

 

BACKEND_URL 은 git REPO URL 이고

BACKEND 에는 백엔드(또는 관리자 사이트)와 관련된 git 프로젝트 각각을 지정한다.

FRONTEND_URL, $FRONT 도 마찬가지이다.

 

사용법)

# 사용법을 보여준다.
git_clone.sh
# 본 스크립트에서 지원하는 백엔드 프로젝트(.git 이 존재하는 디렉토리) 목록을 보여준다.
git_clone.sh -backend -l
# 백엔드 프로젝트를 모두 가져온다.
git_clone.sh -backend -a
(또는)
git_clone.sh -backend
# 백엔드 프로젝트 중에서 repoPrj3 만 내려 받는다.
git_clone.sh -backend repoPrj3

 

스크립트 소스)

$ cat ~/bin/git_clone.sh

#!/bin/bash
# Copyright [2021] [한정훈 & blog.daum.net/debianizer & 종이와 쉼터]
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ------------------------------------------------------------
# 본 BASH 스크립트는 아파치 라이선스 버전 2.0(라이선스)에 따라 라이선스가 부여됩니다.
# 라이선스를 준수하지 않는 한 이 파일을 사용할 수 없습니다.
# 다음에서 라이선스 사본을 얻을 수 있습니다.
# 
#  http://www.apache.org/licenses/LICENSE-2.0
# 
# 관련 법률에서 요구하거나 서면으로 동의하지 않는 한 소프트웨어
# 라이선스에 따라 배포되는 것은 "있는 그대로" 배포되며,
# 명시적이든 묵시적이든 어떠한 종류의 보증이나 조건도 제공하지 않습니다.
# 라이선스에 따른 권한 및 제한 사항을 관리하는 특정 언어는 라이선스를 참조하십시오. 
# 
# 퍼갈 때는 출처를 남겨주십시오.
BACKEND_URL=bitbucket.ANY_DOMAIN.com:8888/scm/back
BACKEND=(
repoPrj0
repoPrj1
repoPrj2
repoPrj3
)
FRONTEND_URL=bitbucket.ANY_DOMAIN.com:8888/scm/front
FRONTEND=(
repoPrj5
repoPrj6
repoPrj7
repoPrj8
repoPrj9
)
GIT_ID=$(awk -F':' '{print $1}' ~/.ssh/git)
GIT_PW=$(awk -F':' '{print $2}' ~/.ssh/git)
clone_all() {
	for i in ${REPO[@]};do
		git clone http://$GIT_ID:$GIT_PW@URL/$i.git;
		echo -e '\n'
	done
}
usage() {
	echo -e '-h\t this message.' \
			'\n-help\t this message.' \
			'\n' \
			'\n-backend\t Backend projects' \
			'\n-front\t Frontend projects' \
			'\n' \
			'\n-- use examples --' \
			'\n-backend -l\t\t\t\t list of Backend projects' \
			'\n-backend -a\t\t\t\t clone all projects of Backend' \
			'\n-backend [proj0] [proj1] [proj2]\t clone matching projects only' \
			'\n'
	exit 0
}
[[ $# -eq 0 ]] && usage && exit 1
TARGET=''
URL=''
REPO=''
for param in $@;do
	case $param in
		-backend)
			TARGET=$param
			URL=${BACKEND_URL}
			read -a REPO <<< ${BACKEND[@]}
			;;
		-frontend)
			TARGET=$param
			URL=${FRONTEND_URL}
			read -a REPO <<< ${FRONTEND[@]}
			;;
	esac
done
# '-backend' | '-frontend' was not specified
[[ -z $TARGET ]] && usage && exit 1
# -h or -help
for param in $@;do
	if [[ $param == '-l' || $param == '-list' ]];then
		echo -e '('${#REPO[*]}') projects: '${REPO[@]}'\n'
		exit 0
	fi
done
# -a
for param in $@;do
	if [ $param == '-a' ];then
		echo -e 'with -a, gettting all ('${#REPO[*]}') projects from '${TARGET}'...\n'
		clone_all
		exit 0
	fi
done
if [$# -eq 1 ];then
	echo -e 'getting all ('${#REPO[*]}') projects from '${TARGET}'...\n'
	clone_all
	exit 0
fi
# without '-a', just download matching projects only.
for item in ${REPO[@]};do
	for param1 in $@;do
		if [ $param1 == $item ];then
			git clone http://$GIT_ID:$GIT_PW@$URL/$param1.git;
			echo -e '\n'
		fi
	done
done