martinvonz / jj

A Git-compatible VCS that is both simple and powerful
https://martinvonz.github.io/jj/
Apache License 2.0
8.81k stars 303 forks source link

FR: restore missing `.git/HEAD` file #4699

Open BatmanAoD opened 1 day ago

BatmanAoD commented 1 day ago

Is your feature request related to a problem? Please describe.

In general, .git/HEAD should never be missing in a colocated repo. But it can be useful to overwrite it manually, and sometimes people trying to make a manual change make mistakes.

If .git/HEAD is missing, but the .git repository is otherwise uncorrupted, jj has all the information it needs to restore the repository to a working state with jj git export, because jj git export overwrites HEAD with whatever jj thinks it should be.

This, however, is not what happens:

Internal error: The repository appears broken or inaccessible
Caused by:
1: Failed to open git repository
2: "/Users/kstrand/sandbox/git/.git" does not appear to be a git repository
3: Missing HEAD at '.git/HEAD'

Describe the solution you'd like In the case where the .git/HEAD file is missing, but .jj/ and .git/ exist, I would like jj git export to restore the last known git-head from the operation log. This would bring the repository back to a usable state.

The message above should be modified to indicate that jj git export may be able to fix the broken state.

Describe alternatives you've considered

Additional context

As far as I can tell, there is no git command that will restore .git/HEAD, even though the reflog should provide the means to do so. I don't know why git doesn't provide tools for basic fix-ups.

arxanas commented 1 day ago

Does Git automatically restore a missing .git/HEAD?

yuja commented 1 day ago

because jj git export overwrites HEAD with whatever jj thinks it should be.

That describes the current git::reset_head() behavior, but it's kinda bug. jj should update HEAD only if it matches the internal view.git_head record. https://github.com/martinvonz/jj/issues/3754

I'm not against adding git fsck-like command for recovery, but I wouldn't expect jj git export fixes corrupted repository.

BatmanAoD commented 1 day ago

Does Git automatically restore a missing .git/HEAD?

No; I mentioned this at the very end of my comment.