Skip to content

git: Recover branch refs when metadata lookup fails#57285

Merged
cole-miller merged 9 commits into
zed-industries:mainfrom
GoldStrikeArch:fix/git-stale-state
May 25, 2026
Merged

git: Recover branch refs when metadata lookup fails#57285
cole-miller merged 9 commits into
zed-industries:mainfrom
GoldStrikeArch:fix/git-stale-state

Conversation

@GoldStrikeArch

@GoldStrikeArch GoldStrikeArch commented May 20, 2026

Copy link
Copy Markdown
Contributor

cc @cole-miller

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Release Notes:

  • Fixed branch enumeration when a broken Git ref prevents commit metadata from being read.

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 20, 2026
@MrSubidubi MrSubidubi added the area:integrations/git Git integration feedback label May 20, 2026
@MrSubidubi

This comment was marked as resolved.

@GoldStrikeArch GoldStrikeArch marked this pull request as draft May 20, 2026 18:47
@GoldStrikeArch

Copy link
Copy Markdown
Contributor Author

@MrSubidubi

We generally discourage pinging maintainers unless there was a prior discussion/agreement about this. Feel free to educate me should that have been the case, but just as an FYI

I had the pairing session with Cole on this one, so this PR was created for the pairing session and I will adjust it based on the conclusion.

@MrSubidubi

Copy link
Copy Markdown
Member

Appreciate the context, was obviously unaware of that.

@GoldStrikeArch GoldStrikeArch force-pushed the fix/git-stale-state branch 4 times, most recently from ccb6a96 to 99fd825 Compare May 20, 2026 21:11
@GoldStrikeArch GoldStrikeArch marked this pull request as ready for review May 20, 2026 21:12
@GoldStrikeArch

GoldStrikeArch commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

@cole-miller
Alright, so I updated the branch as per our conclusion. But! It still does run the git for-each-ref --format ... command the second time and I can explain why (despite that we talked about a simple case -> if it failed the for-each-ref command the first time you just parse what you can and continue).

I investigated the original git source code here for the actual implementation of for-each-ref and how it decides whether or not it should load/parse the actual git object (from the .git/objects folder or things like packs/alternates)

If you look here at the "valid atoms" ("fields" in our terminology) you can see that objectname is SOURCE_OTHER andHEAD + refname are both SOURCE_NONE and everything else: parent, authorname, committerdate, contents, etc are SOURCE_OBJ (well, upstream is also SOURCE_NONE but problematic because of upstream:track, but let's not really dig into it).

There is a logic in which if it is SOURCE_OBJ it sets those fields here

if (*atom == '*')
     oi_deref.info.contentp = &oi_deref.content;
else
     oi.info.contentp = &oi.content;
}

And then it will decide based on those fields whether it should call get_object():

if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
	    !memcmp(&oi_deref.info, &empty, sizeof(empty)))
		return 0;

So, those 3 fields does not require the same object-loading path: HEAD, refname, objectname. And by doing that we will make sure that we iterate on all refs because if we simply "collect what we can" we might end up being broken on the first branch and parse 0 branches.

If you still think we shouldn't do this second command I can remove it but IMO it makes the fix robust and is "safe enough" to use (as your original concern was "why it will not fail again if the first one failed" -> because it will not go into the same path in the git's code 😄 )

@GoldStrikeArch GoldStrikeArch changed the title git: Fix git stale state git: Recover branch refs when metadata lookup fails May 20, 2026
@cole-miller

Copy link
Copy Markdown
Member

Thanks for the additional research @GoldStrikeArch. However, I still think it's better here not to call a fallback command. I think if a user has some git refs that are broken in this way (which seems rare), it's okay for the branch picker to be degraded, and not worth the complexity of a second command invocation to try to recover some of the refs we couldn't get before (especially because interacting with those refs might not work for the same reason we couldn't process them here). I think a better path forward would be to try to surface errors on this codepath better in the UI, so that people don't have to check their logs to see what went wrong. (And parsing whatever lines we can from the output of the existing command still seems reasonable too, since we can do that without much additional complexity.)

@GoldStrikeArch

Copy link
Copy Markdown
Contributor Author

@cole-miller Alright, so I reverted the changes and now we are doing exactly that:

  • running git for-each-ref --format... once and parse what we can (the parsing logic didn't change as it was already robust enough)
  • and then show this error in "branch picker UI" if the status code was not 0 and there were any errors:
Screenshot 2026-05-21 at 13 19 51

@cole-miller

Copy link
Copy Markdown
Member

@GoldStrikeArch Thanks! I tweaked the UI slightly but this looks good otherwise--merging.

@cole-miller cole-miller enabled auto-merge May 25, 2026 14:20
@cole-miller cole-miller added this pull request to the merge queue May 25, 2026
Merged via the queue into zed-industries:main with commit 786eb24 May 25, 2026
32 checks passed
TomPlanche pushed a commit to TomPlanche/zed that referenced this pull request Jun 2, 2026
…7285)

cc @cole-miller 

Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Fixed branch enumeration when a broken Git ref prevents commit
metadata from being read.

---------

Co-authored-by: Cole Miller <cole@zed.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:integrations/git Git integration feedback cla-signed The user has signed the Contributor License Agreement

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants