如何确定git存储库的“人性化”部分

时间:2012-12-12 12:32:45

标签: git scripting

如果我像这样克隆一个git存储库

git clone <some-repository>

git创建一个目录,命名为源存储库的“人性化”部分。 (根据手册页)。

现在我想创建一个bash脚本来克隆存储库,'cds'到新创建的目录中,并做一些事情。有没有一种简单的方法让bash脚本知道所创建目录的名称,而无需为'git clone'命令显式提供目录?

2 个答案:

答案 0 :(得分:1)

要添加Hiery的答案,您将在git repo中找到一个完整的shell脚本示例:
contrib/examples/git-clone.shrelevant extract

# Decide the directory name of the new repository
if test -n "$2"
then
    dir="$2"
    test $# = 2 || die "excess parameter to git-clone"
else
    # Derive one from the repository name
    # Try using "humanish" part of source repo if user didn't specify one
    if test -f "$repo"
    then
        # Cloning from a bundle
        dir=$(echo "$repo" | sed -e 's|/*\.bundle$||' -e 's|.*/||g')
    else
        dir=$(echo "$repo" |
            sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
    fi
fi

请注意,它会考虑克隆 bundle (这是一个repo作为一个文件,在使用repository in the cloud时非常有用)

答案 1 :(得分:0)

你需要获取url并解析它以获取最新的部分并剥离.git。

#!/bin/bash
URL=$1

# Strip off everything from the beginning up to and including the last slash
REPO_DIR=${URL##*/}
# Strip off the .git part from the end of the REPO_DIR
REPO_DIR=${REPO_DIR%%.git}

git clone $URL
cd $REPO_DIR
....