2011-05-21 56 views
6

如果我添加一个当前不存在的子模块,则不会将子模块信息添加到.git/config为什么添加现有的回购作为子模块修改.git/config?

$ mkdir testing 
$ cd testing 
$ git init 
$ git submodule add [email protected]:submodule.git 
$ cat .git/config 
[core] 
    repositoryformatversion = 0 
    filemode = true 
    bare = false 
    logallrefupdates = true 
    ignorecase = true 

不过,如果我补充一点,目前存在的一个子模块回购,该URL添加到.git/config

$ mkdir testing 
$ cd testing 
$ git init 
$ git clone [email protected]:submodule.git 
$ git submodule add [email protected]:submodule.git 
$ cat .git/config 
[core] 
    repositoryformatversion = 0 
    filemode = true 
    bare = false 
    logallrefupdates = true 
    ignorecase = true 
[submodule "submodule"] 
    url = [email protected]:submodule.git 

我都会以为,在这两种情况下,git submodule add将只能修改.gitmodules ,并且git submodule init本来会更新该项目的.git/config

为什么.git/config在第二种情况下被修改,但不是第一种?有人可以解释这种行为的理性吗?

回答

4

这看起来很奇怪。这种行为被引入in this commit

commit c2f939170c65173076bbd752bb3c764536b3b09b 
Author: Mark Levedahl <[email protected]> 
Date: Wed Jul 9 21:05:41 2008 -0400 

    git-submodule - register submodule URL if adding in place 

    When adding a new submodule in place, meaning the user created the 
    submodule as a git repo in the superproject's tree first, we don't go 
    through "git submodule init" to register the module. Thus, the 
    submodule's origin repository URL is not stored in .git/config, and no 
    subsequent submodule operation will ever do so. In this case, assume the 
    URL the user supplies to "submodule add" is the one that should be 
    registered, and do so. 

    Signed-off-by: Mark Levedahl <[email protected]> 
    Signed-off-by: Junio C Hamano <[email protected]> 

更新:你指出了低于我原来此提交信息的解释并没有任何意义的意见,所以我已删除的文字现在以避免对他人的困惑。

如在下面的评论中提到,cdwilson发布到git的邮件列表来问这个矛盾,并因此延斯·莱曼正在努力解决这个问题 - 该线程可以在这里找到:

+0

马克,感谢您的指针指向提交(至少它表明这种行为是故意的)。但是,当您说“在子模块更新之前不克隆”,我仍然感到困惑。在上面的第一种情况下,'git submodule add [email protected]:submodule.git'实际上将'submodule.git'克隆到超级项目中。在上面的示例中,我运行的唯一子模块命令是'git submodule add',在这两种情况下,我都在我的超级项目中使用了克隆的submodule.git。唯一的区别是'.git/config'事后的样子,case#1也需要'git submodule init'来注册。 – cdwilson 2011-05-22 16:37:26

+0

如果在第一种情况下'git submodule add'在注册子模块('git submodule init')和更新('git submodule update')之前没有克隆存储库,那么这种行为对我来说是有意义的。但是,由于上面的case#1确实克隆了'submodule.git',并且需要'git submodule init'来注册,所以我希望情况#2也需要'git submodule init'来执行注册。我确信这里有一些我错过的东西,但现在我不明白为什么会有差异。 – cdwilson 2011-05-22 16:51:15

+0

@cdwilson:好的一点是,我误解了'git submodule add'在我急于回答问题时的行为。我会纠正我的答案。你可以在git邮件列表上询问这个问题的正确答案 - 我怀疑这是一个错误,但也许我错过了一些东西。 – 2011-05-22 17:01:13