0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

git の特定のディレクトリを取ってくるbashスクリプト

Last updated at Posted at 2024-01-18
  1. 以下を適当な名前で保存する。
    function usage_git_subdir_clone() {
        cat << EOF
    usage:    
        git_subdir_clone [URL] [subdir]
    EOF
        exit 0
    }
    
    function git_subdir_clone() {
        URL=$1
        DIRS=$2
    
        if [ ""${URL} = "" ] ; then
            usage_git_subdir_clone
        fi
    
        if [ ""${DIRS} = "" ] ; then
            usage_git_subdir_clone
        fi
    
        GITNAME=$(basename ${URL} .git)
        TARGET_DIR=${GITNAME}/${DIRS}
    
        if [ -d ${TARGET_DIR} ] ; then
            echo "exist ${TARGET_DIR}"
            exit 0
        fi
    
        CWD=$(readlink -f .)
        mkdir -p ${GITNAME}
    
        cd ${GITNAME}
        git init
    
        git config core.sparsecheckout true
    
        git remote add origin ${URL}
    
        git sparse-checkout set ${DIRS}
        git fetch --depth=1
        BRANCH=$(git remote show origin | grep 'HEAD branch' | awk '{print $NF}')
        git checkout origin/${BRANCH}
        git branch -D ${BRANCH} 2>/dev/null
        git checkout -b ${BRANCH}
        git branch -u origin/${BRANCH}
    
        cd ${CWD}
    }
    
  2. .bashrcから読みこむ
    例えば ~/.bin/git_subdir_cloneに保存した場合
    ~/.bashrc
    ...<略>
    . ${HOME}/.bin/git_subdir_clone
    
  3. あとはコマンドとして呼ぶだけ
    例)
    git_subdir_clone https://github.com/unicode-org/icu.git icu4c/source
    

参考

https://qiita.com/progrhyme/items/994f738be668bf3dc452
https://itosae.com/archives/7642

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?