-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initially, we can only colocate the primary workspace. See <https://martinvonz.github.io/jj/latest/git-compatibility/#converting-a-repo-into-a-co-located-repo> for the steps.
- Loading branch information
1 parent
b15d8dc
commit c5ad1b8
Showing
8 changed files
with
197 additions
and
17 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2024 The Jujutsu Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::fs; | ||
|
||
use jj_lib::file_util::IoResultExt; | ||
use jj_lib::git::{self}; | ||
use jj_lib::repo::Repo; | ||
use tracing::instrument; | ||
|
||
use crate::cli_util::CommandHelper; | ||
use crate::command_error::internal_error; | ||
use crate::command_error::user_error; | ||
use crate::command_error::CommandError; | ||
use crate::commands::git::maybe_add_gitignore; | ||
use crate::ui::Ui; | ||
|
||
/// Make the current workspace colocated | ||
#[derive(clap::Args, Clone, Debug)] | ||
pub struct GitColocateArgs {} | ||
|
||
#[instrument(skip_all)] | ||
pub fn cmd_git_colocate( | ||
ui: &mut Ui, | ||
command: &CommandHelper, | ||
_args: &GitColocateArgs, | ||
) -> Result<(), CommandError> { | ||
let workspace_command = command.workspace_helper(ui)?; | ||
let workspace = workspace_command.workspace(); | ||
|
||
if workspace_command.working_copy_shared_with_git() { | ||
return Err(user_error(format!( | ||
"Workspace '{}' is already colocated.", | ||
workspace.workspace_id().as_str() | ||
))); | ||
} | ||
|
||
let Some(git_backend) = workspace_command.git_backend() else { | ||
return Err(user_error( | ||
"This repo is not using the Git backend, so you cannot colocate a workspace.", | ||
)); | ||
}; | ||
|
||
let dotgit_path = workspace.workspace_root().join(".git"); | ||
if dotgit_path.exists() { | ||
return Err(user_error(format!( | ||
"Path {} already exists, cannot create a corresponding git worktree here.", | ||
dotgit_path.display() | ||
))); | ||
} | ||
|
||
let git_repo = git_backend.git_repo(); | ||
if workspace.is_primary_workspace() { | ||
// Change the settings before we mess up the paths | ||
let common_dir = git_repo.common_dir(); | ||
let mut config = | ||
gix::config::File::from_git_dir(common_dir.to_path_buf()).map_err(internal_error)?; | ||
config | ||
.set_raw_value(&"core.bare", "false") | ||
.map_err(internal_error)?; | ||
let config_path = common_dir.join("config"); | ||
let mut file = fs::OpenOptions::new() | ||
.write(true) | ||
.open(&config_path) | ||
.context(config_path)?; | ||
config | ||
.write_to(&mut file) | ||
.map_err(|e| internal_error(format!("Could not write git config file: {e}")))?; | ||
drop(file); | ||
|
||
let old_path = workspace_command.repo_path().join("store").join("git"); | ||
let new_path = workspace.workspace_root().join(".git"); | ||
fs::rename(&old_path, &new_path).context(old_path)?; | ||
let git_target_path = workspace_command | ||
.repo_path() | ||
.join("store") | ||
.join("git_target"); | ||
fs::write(&git_target_path, "../../../.git").context(git_target_path)?; | ||
// we write .gitignore below | ||
} else { | ||
return Err(internal_error( | ||
"Unimplemented: colocating a secondary workspace", | ||
)); | ||
} | ||
|
||
let working_copy_factory = command.get_working_copy_factory()?; | ||
let workspace_loader = command.workspace_loader()?; | ||
let new_workspace = workspace | ||
.reload_store( | ||
workspace_loader, | ||
command.settings(), | ||
working_copy_factory, | ||
command.get_store_factories(), | ||
) | ||
.map_err(internal_error)?; | ||
|
||
let repo = new_workspace | ||
.repo_loader() | ||
.load_at_head(command.settings())?; | ||
let mut new_workspace_command = command.for_workable_repo(ui, new_workspace, repo)?; | ||
maybe_add_gitignore(&new_workspace_command)?; | ||
|
||
if let Some(wc_commit_id) = new_workspace_command.get_wc_commit_id() { | ||
let name = new_workspace_command.workspace_id().as_str().to_owned(); | ||
let wc_commit = new_workspace_command | ||
.repo() | ||
.store() | ||
.get_commit(wc_commit_id)?; | ||
|
||
let Some(git_backend) = new_workspace_command.git_backend() else { | ||
panic!(); | ||
}; | ||
let git2_repo = git_backend.open_git_repo()?; | ||
let mut tx = new_workspace_command.start_transaction(); | ||
|
||
git::reset_head(tx.repo_mut(), &git2_repo, &wc_commit)?; | ||
|
||
tx.finish(ui, format!("Colocated existing workspace {name}"))?; | ||
} | ||
|
||
Ok(()) | ||
} |
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
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
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
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
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