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

joh*_*anv 6 git scripting

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

git clone <some-repository>
Run Code Online (Sandbox Code Playgroud)

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

现在我想创建一个 bash 脚本来克隆一个存储库,'cds' 到新创建的目录中,并做一些事情。bash 脚本是否有一种简单的方法可以知道创建的目录的名称,而无需向“git clone”命令明确提供目录?

Von*_*onC 5

要添加 Hiery 的答案,您将在 git 存储库本身中找到一个完整的 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
Run Code Online (Sandbox Code Playgroud)

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


Hie*_*mus 1

您需要获取 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
....
Run Code Online (Sandbox Code Playgroud)

  • 我最终做了类似的事情: `echo $giturl|sed 's/.*[:/]\([^:/]*\)\.git$/\1/'` (2认同)