Shell:如果文件夹已经存在,则 git clone 不会失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36498981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:55:54  来源:igfitidea点击:

Shell: don't fail if git clone if folder already exists

gitshell

提问by Augustin Riedinger

I'm writing an automated script.

我正在编写一个自动化脚本。

At some point, I'm calling git clone <repo>. But there is a chance this operation is already done.

在某些时候,我打电话给git clone <repo>. 但是有可能这个操作已经完成了。

How can I call git clone --no-failso that it ignores if repository exists?

我如何调用git clone --no-fail以便它忽略存储库是否存在?

Thanks

谢谢

采纳答案by hek2mgl

The most stable solution would be to simply let it fail and print the error message. If you think that's too ugly for your scenario, you may redirect it to /dev/null:

最稳定的解决方案是简单地让它失败并打印错误消息。如果您认为这对您的场景来说太难看,您可以将其重定向到 /dev/null:

folder="foo"
if ! git clone "${url}" "${folder}" 2>/dev/null && [ -d "${folder}" ] ; then
    echo "Clone failed because the folder ${folder} exists"
fi


Otherwise you can do something like this:

否则,您可以执行以下操作:

if [ ! -d "$FOLDER" ] ; then
    git clone "$URL" "$FOLDER"
fi

but that would be vulnerable to race conditions.

但这很容易受到竞争条件的影响。

回答by spannerj

A bit late to the party but I'm doing something similar but I want to ensure the repo is up to date if it already exists so to expand on the above answer if the repo already existed you could then pull to ensure it's up to date.

聚会有点晚了,但我正在做类似的事情,但我想确保回购是最新的,如果它已经存在,那么如果回购已经存在,请扩展上述答案,然后您可以拉动以确保它是最新的.

if [ ! -d "$FOLDER" ] ; then
    git clone $URL $FOLDER
else
    cd "$FOLDER"
    git pull $URL
fi

回答by kostix

git clone $url || truewould make shell runing with set -enot fail on git clonehaving had failed.

git clone $url || true将使 shell 在set -e失败后运行并不会git clone失败。

If this is not what you want then the suggested solutions with explicit testing for the target directory being already there should work.

如果这不是您想要的,那么建议的对已经存在的目标目录进行显式测试的解决方案应该可以工作。

The only problem with them is that you'd need to simulate Git's approach to figuring out its name.

它们的唯一问题是您需要模拟 Git 的方法来找出其名称。

Something like this should work:

这样的事情应该工作:

url=git://host/path/name.git
last=${url##*/}
bare=${last%%.git}
test -e "$bare" || git clone "$url"

Those "tricks" with ##and %%are standard featues of the "parameter expansion" rules of POSIX shells.

这些“招数”以##%%的POSIX壳的“参数扩展”规则标准featues

回答by Shital Shah

There are no options in git cloneto avoid cloning if clone already exist. Your best bet is:

git clone如果克隆已经存在,则没有避免克隆的选项。你最好的选择是:

[ ! -d 'MyRepo' ] && git clone https://github.com/Myorg/MyRepo.git

回答by AnoE

You don't tell us why it could already be there, or any context.

你没有告诉我们为什么它可能已经存在,或任何上下文。

I would very like add a [ -e $folder ] && rm -rf $folder(making sure $folderis sane) before the git clonebecause I would not want my software to use any old folder which just happens to lie around there in who knows which state...

我非常想在之前添加一个[ -e $folder ] && rm -rf $folder(确保$folder正常),git clone因为我不希望我的软件使用任何旧文件夹,而这些文件夹恰好位于那里,谁知道处于哪个状态...