这个 Ruby 代码是什么意思?

What does this Ruby code mean?

我正在尝试对 gitlab 进行备份恢复,它有点工作,但命令行总是说存储库恢复失败。我想我在代码中找到了负责 [failed] 语句的条件语句。有人知道这是在做什么或知道我应该去寻找错误的方向吗?

 if Kernel.system("git clone --bare #{path_to_bundle} #{project.repository.path_to_repo} > /dev/null 2>&1")
puts "[DONE]".green                                                                                                                                                               
          else                                                                                                                                                                                
            puts "[FAILED]".red                                                                                                                                                               
          end 

Kernel.system 调用给定的 shell 命令。当它失败时,它 return 一个 false 值。

在您的情况下,这意味着 git clone --bare #{path_to_bundle} #{project.repository.path_to_repo} > /dev/null 2>&1 失败。

您可以在没有 > /dev/null 2>&1 的情况下在命令行上手动执行此命令时检查失败的原因。

要获取命令,您可以在命令前进行调试打印

if Kernel.system(pp("git clone --bare #{path_to_bundle} #{project.repository.path_to_repo} > /dev/null 2>&1"))

来自docs

system returns true if the command gives zero exit status, false for non zero exit status. Returns nil if command execution fails. An error status is available in $?.

也就是说:不断陷入失败状态是指系统命令返回falsenil。您可能需要检查 $? 原因:

command = Kernel.system("git clone --bare #{path_to_bundle} #{project.repository.path_to_repo} > /dev/null 2>&1")
if command
  puts "[DONE]".green
else
  puts "[FAILED]".red
  puts "Reason:"
  puts $?
end