Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is git erroring with 'Assertion failed' on git add .?

Tags:

git

xcode

I forked a repo, then cloned it to my Mac into a /YATC directory. I had a previously-created Xcode project (TwitterTimeline) in another directory, which I copied into the /YATC directory. I did git add . in the /YATC directory, and only an empty TwitterTimeline directory was added to the repo. No other files were added. I later found out that there was already a .git directory in TwitterTimeline. I think Xcode must have created that, although I don't recall ever asking it to.

Anyway, I deleted the TwitterTimeline/.git directory. I went back up to /YATC and tried to do git add . there, and nothing happened. Meaning I immediately did git status, and it said there was nothing to commit. Then I went down to the TwitterTimeline directory and did git add ., and got the following:

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

What is this?

like image 491
user26270 Avatar asked Apr 21 '14 21:04

user26270


3 Answers

Not sure exactly what's happening but I was in the same situation

  1. I moved one git repo inside another
  2. deleted the .git dir thinking it would become a regular subdir
  3. tried to git add -A .
  4. got the weird error message

I got around it by renaming the subdir, doing git add from a parent dir, then renaming the subdir back to the original name. Somehow that seemed to get git back in a working state.

like image 128
Patrick Canfield Avatar answered Oct 07 '22 20:10

Patrick Canfield


Basically the problem is known and it usually affects submodules. It's not Git bug, but basically instead of assert you should see the proper error that specific pathspec is part of the submodule.

The following Git patch from Stefan Beller fixes that problem:

--- 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;
 }

One of the reason could be that the directory where you're adding the files is still registered as a submodule in the index, but actually it's not a valid git repository (e.g. it's missing .git directory).

So you should either:

  • 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.

Then try adding the files again.


See also:

  • [PATCH] pathspec: give better message for submodule related pathspec error
  • Re: [PATCH] pathspec: adjust prefixlen after striping trailing slash
  • assert failed in submodule edge case,
  • "item->nowildcard_len" in Google.
like image 23
kenorb Avatar answered Oct 07 '22 21:10

kenorb


I got this problem too,Here is my way to fix this:

  1. delete the .git folder in the submodule folder(if you did this before, go 2)
  2. then excute follow command, directory is your submodule folder git rm --cached directory git add directory
  3. then you can upload this folder like others
like image 1
F.Anne Avatar answered Oct 07 '22 22:10

F.Anne