-
Notifications
You must be signed in to change notification settings - Fork 360
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
Colocated workspaces, minimal #4644
Open
cormacrelf
wants to merge
8
commits into
main
Choose a base branch
from
workspace-colocate-minimal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
92bbf56
workspace: Make backend factories aware of the workspace root
cormacrelf 7e34182
workspace: Reload the repo from disk in `workspace add`
cormacrelf 0d72f0b
git: box all `gix::open::Error` as clippy keeps warning about it
cormacrelf 6c62985
git: Add MaybeColocatedGitRepo helper
cormacrelf 899ac30
git: make GitBackend colocation-aware
cormacrelf 64adf64
cli: warn when workspace/.git exists, but is not colocated
cormacrelf 3e4b0c1
workspace: add stopgap test helper to create a colocated workspace
cormacrelf 2923211
workspace: Add worktree support to MaybeColocatedGitRepo
cormacrelf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ use std::default::Default; | |
use std::fmt; | ||
use std::io::Read; | ||
use std::num::NonZeroU32; | ||
use std::path::Path; | ||
use std::path::PathBuf; | ||
use std::str; | ||
|
||
|
@@ -1812,3 +1813,96 @@ pub fn parse_gitmodules( | |
.collect(); | ||
Ok(ret) | ||
} | ||
|
||
/// Helper for the GitBackend to open a git repo and detect when Git | ||
/// regards the workspace root as a working directory for it. Common scenarios: | ||
/// | ||
/// - bare, in the .jj/repo/store/git directory; | ||
/// - colocated + non-bare, as a .git directory at the workspace root; | ||
/// - a git worktree (= .git file) pointing to the same git repo as the JJ | ||
/// store; | ||
/// - a .git symlink pointing to the same git repo as the JJ store | ||
/// - etc. | ||
/// | ||
/// Also represents a newly created git repo in one of those configurations, if | ||
/// constructed manually. | ||
pub(crate) struct MaybeColocatedGitRepo { | ||
pub git_repo: gix::ThreadSafeRepository, | ||
pub colocated: bool, | ||
} | ||
|
||
impl MaybeColocatedGitRepo { | ||
/// First, open the repo that the store points to, which is possibly bare. | ||
/// If possible, and only if it matches, open the workspace root as a git | ||
/// repo. | ||
pub(crate) fn open_automatic( | ||
store_repo_path: &Path, | ||
workspace_root: Option<&Path>, | ||
open_opts: gix::open::Options, | ||
) -> Result<Self, Box<gix::open::Error>> { | ||
let maybe = Self::open_store_repo(store_repo_path, open_opts.clone())?; | ||
if let Some(workspace_root) = workspace_root { | ||
return Ok(maybe.try_detect_colocated_workspace(workspace_root, open_opts)); | ||
} | ||
Ok(maybe) | ||
} | ||
|
||
fn open_store_repo( | ||
store_repo_path: &Path, | ||
open_opts: gix::open::Options, | ||
) -> Result<Self, Box<gix::open::Error>> { | ||
let git_repo = gix::ThreadSafeRepository::open_opts(store_repo_path, open_opts)?; | ||
|
||
Ok(Self { | ||
git_repo, | ||
colocated: false, | ||
}) | ||
} | ||
|
||
fn with_colocated(self, colocated: bool) -> Self { | ||
Self { colocated, ..self } | ||
} | ||
|
||
fn try_detect_colocated_workspace( | ||
self, | ||
workspace_root: &Path, | ||
_open_opts: gix::open::Options, | ||
) -> Self { | ||
let store_repo = &self.git_repo; | ||
|
||
// The git backend may be a bare repository if we created it without --colocate | ||
// but then created a workspace using `jj workspace add --colocate ../second` | ||
let git_backend_workdir = store_repo.work_dir(); | ||
|
||
// 1. Check if we are in a colocated workspace, specifically the one that has | ||
// both .git and .jj/repo. | ||
// -------------------------------------------------------------------------- | ||
|
||
// Fast path -- the paths are the same, without looking through symlinks. | ||
if git_backend_workdir == Some(workspace_root) { | ||
// We are in a colocated workspace that's home to the real .git directory. | ||
// e.g. /repo with /repo/.git | ||
return self.with_colocated(true); | ||
} | ||
|
||
// Otherwise, canonicalize both the git backend workdir and the workspace | ||
let git_backend_workdir_canonical = git_backend_workdir.and_then(|p| p.canonicalize().ok()); | ||
|
||
// Colocated workspace should have ".git" directory, file, or symlink. Compare | ||
// its parent as the git_workdir might be resolved from the real ".git" path. | ||
let workspace_dot_git = workspace_root.join(".git"); | ||
let Ok(workspace_dot_git_canonical) = workspace_dot_git.canonicalize() else { | ||
return self.with_colocated(false); | ||
}; | ||
|
||
// i.e. (/symlink_to_repo -> /repo).canonicalize() == (/repo/.git).parent() | ||
if let Some(gbw) = git_backend_workdir_canonical.as_deref() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: s/gbw/git_backend_workdir as we don't use the initial binding |
||
if workspace_dot_git_canonical.parent() == Some(gbw) { | ||
// This is the default workspace of a colocated repo | ||
return self.with_colocated(true); | ||
} | ||
} | ||
|
||
self.with_colocated(false) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I would put this into git_backend module, but I don't have a strong opinion about that.
Good abstraction, btw.