6

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

git clone <some-repository>

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

现在我想创建一个 bash 脚本,将存储库克隆到新创建的目录中,并执行一些操作。bash 脚本是否有一种简单的方法可以知道创建的目录的名称,而无需为“git clone”命令显式提供目录?

4

2 回答 2

4

要添加到 Hiery 的答案,您将在 git repo 本身中找到一个完整的 shell 脚本示例:
contrib/examples/git-clone.sh,以及相关的提取

# 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

请注意,它考虑了克隆捆绑包(这是一个作为一个文件的存储库,在使用云中的存储库时很有用)。

于 2012-12-12T13:13:08.780 回答
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
....
于 2012-12-12T12:40:51.267 回答