sscirrus
sscirrus

Reputation: 56719

Corrupted file and tree in git

I just came back to a repo I haven't touched in a couple of months and it's giving me strange issues.

Here are the results of a number of actions:

>>> git status
[Works as expected] On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  deleted:    app/views/steps/_self_service_code.html.erb
no changes added to commit (use "git add" and/or "git commit -a")

>>> git fsck --name-objects
Checking object directories: 100% (256/256), done.
Checking objects: 100% (11080/11080), done.
broken link from    tree 13f6a8bd586b1d5a80a3e67610fc1103fdd827ad (HEAD@{1595970321}^:app/)
              to    tree 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb (HEAD@{1595970321}^:app/views/)
missing tree 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb (HEAD@{1595970321}^:app/views/)
missing blob 9241fc61b7d8ede2f9c1081d97db7f4ae2dd81a7 (:app/views/steps/_self_service_code.html.erb)
...

>>> git restore app/views/steps/_self_service_code.html.erb
error: unable to read sha1 file of app/views/steps/_self_service_code.html.erb (9241fc61b7d8ede2f9c1081d97db7f4ae2dd81a7)

>>> git log --raw --all --full-history | grep 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb
fatal: unable to read tree 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb

>>> git log --raw --all --full-history | grep 9241fc61b7d8ede2f9c1081d97db7f4ae2dd81a7
fatal: unable to read tree 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb

I've tried everything I've read about online, including pulling from a Heroku repo and running 'First Aid' on my disk (it's a mac recently upgraded to Catalina from High Sierra). Nothing has fixed these issues so far.

Git version 2.28.0.

Upvotes: 0

Views: 2133

Answers (1)

Darek Kay
Darek Kay

Reputation: 16669

I have reproduced the issue. I've created a new repository and ran git ls-tree master:

$ git ls-tree master
100644 blob 5cca282c0449f01cf317c3b4b7ed9f7d65125095    file-31e422aa.txt
100644 blob 03a9272a34f3085006cab8d7f1d6a45225a95a75    file-981ec394.txt
100644 blob 12c194b0694b456102f3ceaec027606b3b0b3edd    file-bf32de9c.txt
100644 blob 45aa64b2dfa1d0647be0d776c810eb493e882b23    file-f46b47e4.txt
040000 tree e560d2adfdbb678d625ca5de6f20abbc929f7092    moo

I deleted the file .git/objects/e5/60d2adfdbb678d625ca5de6f20abbc929f7092 and ran the same command as you did:

$ git fsck --name-objects
Checking object directories: 100% (256/256), done.
error: e560d2adfdbb678d625ca5de6f20abbc929f7092: invalid sha1 pointer in cache-tree
broken link from    tree d0f8939f8d20cf252c1dd584526bcc6d7c3090e8 (:)
              to    tree e560d2adfdbb678d625ca5de6f20abbc929f7092 (:moo/)
missing tree e560d2adfdbb678d625ca5de6f20abbc929f7092 (:moo/)

As cloning the project solves your issue, it seems that the mentioned git objects are missing/corrupt in your .git/objects folder.

Option 1: Move your branches/stashes to your cloned repository.

Option 2: Move the broken git objects from your cloned repository into your broken repository.

Option 2 should work just fine. You can either move only the two objects mentioned in your CLI output or just copy&paste the entire .git/objects folder and merge (not overwrite!) it with your broken repository's .git/objects content. In any way, I suggest creating a full copy of your current broken repository before you attempt to solve the issue.

Note: be aware of the folder structure of your .git/objects folder. It contains two-char folders, where the two characters stand for the beginning of your object hash. So for example, your broken 9916bab22f21c3e8d77a0c4bb2e633ec26e45edb tree will be in .git/objects/99/16bab22f21c3e8d77a0c4bb2e633ec26e45edb of your cloned repository (and missing or corrupt in your broken repository).

Upvotes: 1

Related Questions