如何检查当前存储库是否是顶级git repo

时间:2018-08-27 21:22:39

标签: git

我正在尝试检查当前仓库不是子模块而是顶层 git存储库。我尝试了以下命令:git submodule init

输出:

You need to run this command from the toplevel of the working tree.

但是它将初始化子模块。我该如何检查当前存储库(如果它不是子模块而是顶级git存储库),而无需在存储库中进行更改?

2 个答案:

答案 0 :(得分:1)

您可能希望使用git rev-parse --show-toplevel,如果您不在子模块中,那么会回退到toplevel=$(git rev-parse --show-toplevel) superproject=$(git rev-parse --show-superproject-working-tree) if [[ -z "$superproject" ]]; then echo "submodule in $superproject" else echo "toplevel is $toplevel" fi 。例如:

k=10
t=20
r=8.5

我目前还不知道可以在两种情况下提供多合一答案的内置函数。

答案 1 :(得分:0)

如果要做使用git rev-parse --show-superproject-working-tree,请确保使用Git 2.20(2018年第四季度),以避免出现错误消息“ returned path string doesn't match cwd

请参见commit c5cbb27Sam McKelvie (sammck)(2018年9月27日)。
(由Junio C Hamano -- gitster --commit d152a74中合并,2018年10月19日)

  

rev-parse:--show-superproject-working-tree应该在合并期间起作用

     

调用'git rev-parse --show-superproject-working-tree'退出

"fatal: BUG: returned path string doesn't match cwd?"
     

当超级项目具有当前子模块的未合并条目时,而不是显示超级项目的工作树。

(那是first reported here

  

该问题归因于以下事实:合并子模块引用时   正在进行中时,“ git ls-files --stage —full-name <submodule-relative-path>”为子模块返回三个单独的条目(每个阶段一个),而不是单个条目;例如,

$ git ls-files --stage --full-name submodule-child-test
160000 dbbd2766fa330fa741ea59bb38689fcc2d283ac5 1       submodule-child-test
160000 f174d1dbfe863a59692c3bdae730a36f2a788c51 2       submodule-child-test
160000 e6178f3a58b958543952e12824aa2106d560f21d 3       submodule-child-test
     

get_superproject_working_tree()中的代码预期将恰好返回一个条目;
  如果返回多个条目,this patch将使其使用第一个条目。