Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3090 and #2575 - Treats .git/gitdir as cwd on "empty" bare clones #3092

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hwangh95
Copy link

@hwangh95 hwangh95 commented Jan 7, 2024

Description

There are cases for having bare clones with multiple worktrees in the same folder to manage various versions in a workspace

Currently, however, the only supported way to do this is to have the .git/ files such as HEAD, branches/, refs/, worktrees/, etc in the same folder as your actual worktrees. This makes your workspace very cluttered and is counterproductive to the structured way of multiple worktrees

What many of us prefer is to have an "empty" bare clone with the normal bare clone files within the .git/ folder or another folder labeled .bare and use a .git file with a gitdir reference

This fix will allow for the worktrees view to function the same way it would show on a git worktree list command

Checklist

  • I have followed the guidelines in the Contributing document
  • My changes follow the coding style of this project
  • My changes build without any errors or warnings
  • My changes have been formatted and linted
  • My changes include any required corresponding changes to the documentation (including CHANGELOG.md and README.md)
  • My changes have been rebased and squashed to the minimal number (typically 1) of relevant commits
  • My changes have a descriptive commit message with a short title, including a Fixes $XXX - or Closes #XXX - prefix to auto-close the issue that your PR addresses

Refs: #3090, #2575

@hwangh95 hwangh95 force-pushed the fix-empty-bare-clone-parsing branch from 5df0c1b to 8a0a323 Compare January 7, 2024 00:21
@hwangh95 hwangh95 force-pushed the fix-empty-bare-clone-parsing branch 2 times, most recently from fa54bdf to fc0ffd4 Compare January 26, 2024 09:45
@hwangh95
Copy link
Author

@eamodio Hello, could you please have a look at this PR?

@hwangh95
Copy link
Author

@d13 Hey sorry for the ping but could you look at this PR? Or is there someone else who can? I've been waiting for feedback for about a month now

@hwangh95
Copy link
Author

@d13 Still waiting for feedback on this

@hwangh95 hwangh95 force-pushed the fix-empty-bare-clone-parsing branch from 5ab5ff8 to c15d92d Compare June 20, 2024 08:35
@hwangh95
Copy link
Author

@axosoft-ramint Sorry for the ping, but could I get you to look into this? I've been manually compiling the source every version to fix this issue...

@hwangh95 hwangh95 force-pushed the fix-empty-bare-clone-parsing branch from c15d92d to 0eb5f1f Compare June 20, 2024 08:40
@axosoft-ramint axosoft-ramint self-requested a review June 20, 2024 19:24
@axosoft-ramint axosoft-ramint self-assigned this Jun 20, 2024
@axosoft-ramint
Copy link
Contributor

@hwangh95 Thanks! It may be a while before I get to this, but it's on my list now.

…n "empty" bare clones

There are cases for having bare clones with multiple worktrees in the
same folder to manage various versions in a workspace

Currently, however, the only supported way to do this is to have the
.git/ files such as HEAD, branches/, refs/, worktrees/, etc in the same
folder as your actual worktrees. This makes your workspace very
cluttered and is counterproductive to the structured way of multiple
worktrees

What many of us prefer is to have an "empty" bare clone with the normal
bare clone files within the .git/ folder or another folder labeled .bare
and use a .git file with a gitdir reference

This fix will allow for the worktrees view to function the same way it
would show on a `git worktree list` command
@hwangh95 hwangh95 force-pushed the fix-empty-bare-clone-parsing branch from 0eb5f1f to ff729b7 Compare October 8, 2024 09:39
@hwangh95
Copy link
Author

hwangh95 commented Oct 8, 2024

@axosoft-ramint Are there any updates on this?

Copy link
Member

@eamodio eamodio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR and sorry for the LONG delay here. Please test things out with the following suggestions and if it works, we should be able to get this in ASAP.

@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- Fixes [#3090](https://github.com/gitkraken/vscode-gitlens/issues/3090) - Manually created empty bare clone repositories in a trusted directory crash worktree view since LocalGitProvider.findRepositoryUri returns just ".git" *and* Fixes [#2575](https://github.com/gitkraken/vscode-gitlens/issues/2575) - Existing worktrees recognized, but incorrectly organized — thanks to [PR #3092](https://github.com/gitkraken/vscode-gitlens/pull/3092)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please split into 2 "Fixes" lines

Comment on lines +1817 to +1827
// We can assume rev_parse__git_dir will always return a value since we know we are in a repo
({ commonPath: data } = (await this.rev_parse__git_dir(cwd)) as {
path: string;
commonPath?: string;
});

// If the git dir is .git or a path pointed to by gitdir: ...,
// we should treat it as an "empty" version of a bare clone
// This is the case when git dir == git common dir, or when commonPath is undefined
data = data ?? '.';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace this with the following (assuming it works in your testing). In my testing this seems to resolve the issue and takes advantage of the path normalization that rev_parse__git_dir is already doing. It also avoids returning the cwd if rev_parse__git_dir really returns no data (which shouldn't happen), but keeps this safer.

Suggested change
// We can assume rev_parse__git_dir will always return a value since we know we are in a repo
({ commonPath: data } = (await this.rev_parse__git_dir(cwd)) as {
path: string;
commonPath?: string;
});
// If the git dir is .git or a path pointed to by gitdir: ...,
// we should treat it as an "empty" version of a bare clone
// This is the case when git dir == git common dir, or when commonPath is undefined
data = data ?? '.';
const result = await this.rev_parse__git_dir(cwd);
const repoPath = result?.commonPath ?? result?.path;
if (repoPath?.length) return [true, repoPath];

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm the issue is this doesn't solve the case where it's a bare clone with a .git folder and worktrees in it. Something like

repo/
repo/.git
repo/worktree1
repo/worktree2

doesn't get collapsed into one repo, but several repos with repeating worktrees. I think we should check when dotGitPath == commonDotGitPath but I don't know if we should change rev_parse__git_dir or just write a custom check like this

Suggested change
// We can assume rev_parse__git_dir will always return a value since we know we are in a repo
({ commonPath: data } = (await this.rev_parse__git_dir(cwd)) as {
path: string;
commonPath?: string;
});
// If the git dir is .git or a path pointed to by gitdir: ...,
// we should treat it as an "empty" version of a bare clone
// This is the case when git dir == git common dir, or when commonPath is undefined
data = data ?? '.';
data = await this.git<string>(
{ cwd: cwd, errors: GitErrorHandling.Ignore },
'rev-parse',
'--git-dir',
'--git-common-dir',
);
data = data.trim();
if (data.length) {
const [path, commonPath] = data.split('\n').map(r => r.trim());
return [true, normalizePath((commonPath === '.' || commonPath === path ? cwd : commonPath).trimStart().replace(/[\r|\n]+$/, ''))];
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants