为什么在使用git add .命令时会出现“Assertion failed”错误?

21

我fork了一个repo,然后将它克隆到我的Mac上的/YATC目录中。我之前创建的Xcode项目(TwitterTimeline)在另一个目录中,我将其复制到了/YATC directory中。我在/YATC目录中执行了git add .命令,但只添加了一个空的TwitterTimeline目录到repo中,没有其他文件被添加。后来我发现TwitterTimeline目录中已经有了.git目录。我认为是Xcode创建了这个目录,尽管我不记得曾经要求过Xcode这样做。

无论如何,我删除了TwitterTimeline/.git目录。我返回到/YATC目录并尝试在那里执行git add .命令,但什么也没发生。也就是说,我立即执行了git status命令,结果显示没有需要提交的内容。然后我进入了TwitterTimeline目录并执行了git add .命令,得到了以下结果:

Assertion failed: (item->nowildcard_len <= item->len && item->prefix <= item->len), function prefix_pathspec, file pathspec.c, line 308.
Abort trap: 6

这是什么?

4个回答

34

不确定发生了什么,但我曾处于相同的情况。

  1. 我将一个 git 仓库移动到另一个仓库中
  2. 误删了 .git 目录,以为它会变成一个普通子目录
  3. 尝试执行 git add -A .
  4. 得到了奇怪的错误信息

我通过重命名子目录,从父目录进行 git add,然后将子目录重新命名回原始名称来解决这个问题。不知何故,这似乎使 git 恢复了正常工作状态。


在我的情况下,我忘记了我要添加的空目录是一个子模块(在克隆后没有运行git submodule update)。当我将内容复制到空文件夹并尝试git add .时,我得到了相同的错误。 - Dilawar

2
基本上这个问题是已知的,通常会影响子模块。这不是Git的错误,而是应该看到特定 pathspec 是子模块的正确错误提示,而不是断言。
以下来自Stefan Beller的Git 补丁解决了这个问题:
--- a/pathspec.c
+++ b/pathspec.c
@@ -313,8 +313,23 @@ static unsigned prefix_pathspec(struct pathspec_item *item,
    }

    /* sanity checks, pathspec matchers assume these are sane */
-   assert(item->nowildcard_len <= item->len &&
-          item->prefix         <= item->len);
+   if (item->nowildcard_len > item->len ||
+       item->prefix         > item->len) {
+       /* Historically this always was a submodule issue */
+       for (i = 0; i < active_nr; i++) {
+           struct cache_entry *ce = active_cache[i];
+           int ce_len = ce_namelen(ce);
+           int len = ce_len < item->len ? ce_len : item->len;
+           if (!S_ISGITLINK(ce->ce_mode))
+               continue;
+           if (!memcmp(ce->name, item->match, len))
+               die (_("Pathspec '%s' is in submodule '%.*s'"),
+                   item->original, ce_len, ce->name);
+       }
+       /* The error is a new unknown bug */
+       die ("BUG: item->nowildcard_len > item->len || item->prefix > item->len)");
+   }
+
    return magic;
 }

可能的原因之一是您添加文件的目录仍然在索引中注册为子模块,但实际上它不是有效的git存储库(例如,缺少.git目录)。

因此,您应该执行以下操作之一:

  • initialize and update your submodules by:

     git submodule init
     git submodule update
    

    Run from the parent directory where you had the error.

    Make sure all non-initialized submodules have empty directories, so move any files out of there temporary.

  • or if you don't need this submodule, remove it:

    git rm -f --cached subrepo
    

    Run from the parent directory where you had the error.

然后尝试重新添加文件。
另请参见:

1
我也遇到了这个问题,以下是我的解决方法:
  1. 删除子模块文件夹中的 .git 文件夹(如果之前已经删除过,跳过此步骤)
  2. 在子模块文件夹中执行以下命令,directory 指的是你的子模块文件夹: git rm --cached directory git add directory
  3. 现在你可以像上传其他文件夹一样上传这个文件夹了

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接