如果我像这样克隆一个 git 存储库
git clone <some-repository>
git 创建一个目录,命名为源存储库的“人性化”部分。(根据手册页)。
现在我想创建一个 bash 脚本,将存储库克隆到新创建的目录中,并执行一些操作。bash 脚本是否有一种简单的方法可以知道创建的目录的名称,而无需为“git clone”命令显式提供目录?
要添加到 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
您需要获取 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
....