From 601a5651ee623ad8e4aef20632b1462914a9e4e6 Mon Sep 17 00:00:00 2001 From: Austin Seipp Date: Thu, 2 Nov 2023 21:12:22 -0500 Subject: [PATCH] cli/lib: add `git.colocate` to colocate repositories by default Summary: Closes #2507. Signed-off-by: Austin Seipp Change-Id: I333ff2d16584c51a5e6957b341e0315e --- CHANGELOG.md | 4 ++++ cli/src/commands/git.rs | 10 ++++++++-- cli/src/commands/init.rs | 10 ++++++++-- cli/src/config-schema.json | 7 ++++++- lib/src/settings.rs | 3 +++ 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5603e96f08..e49fe62b4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### New features +* Git repositories created with `jj git clone` or `jj init --git` can now be + automatically colocated on creation with the setting `git.colocate = true`. + The default is `false`. + ### Fixed bugs diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index 3576ee6573..50df4c8e3b 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -457,6 +457,12 @@ fn cmd_git_clone( )); } + let colocate = if command.settings().git_settings().colocate { + true + } else { + args.colocate + }; + // Canonicalize because fs::remove_dir_all() doesn't seem to like e.g. // `/some/path/.` let canonical_wc_path: PathBuf = wc_path @@ -465,7 +471,7 @@ fn cmd_git_clone( let clone_result = do_git_clone( ui, command, - args.colocate, + colocate, remote_name, &source, &canonical_wc_path, @@ -473,7 +479,7 @@ fn cmd_git_clone( if clone_result.is_err() { let clean_up_dirs = || -> io::Result<()> { fs::remove_dir_all(canonical_wc_path.join(".jj"))?; - if args.colocate { + if colocate { fs::remove_dir_all(canonical_wc_path.join(".git"))?; } if !wc_path_existed { diff --git a/cli/src/commands/init.rs b/cli/src/commands/init.rs index 686d5fec77..1c06601a5f 100644 --- a/cli/src/commands/init.rs +++ b/cli/src/commands/init.rs @@ -65,6 +65,12 @@ pub(crate) fn cmd_init( .canonicalize() .map_err(|e| user_error(format!("Failed to create workspace: {e}")))?; // raced? + let colocate = if command.settings().git_settings().colocate { + true + } else { + args.git + }; + if let Some(git_store_str) = &args.git_repo { let mut git_store_path = command.cwd().join(git_store_str); git_store_path = git_store_path @@ -105,7 +111,7 @@ pub(crate) fn cmd_init( tx.finish(ui)?; } } - } else if args.git { + } else if colocate { Workspace::init_internal_git(command.settings(), &wc_path)?; } else { if !command.settings().allow_native_backend() { @@ -124,7 +130,7 @@ Set `ui.allow-init-native` to allow initializing a repo with the native backend. "Initialized repo in \"{}\"", relative_wc_path.display() )?; - if args.git && wc_path.join(".git").exists() { + if colocate && wc_path.join(".git").exists() { writeln!(ui.warning(), "Empty repo created.")?; writeln!( ui.hint(), diff --git a/cli/src/config-schema.json b/cli/src/config-schema.json index 8c30f64bc3..0712d6ebc7 100644 --- a/cli/src/config-schema.json +++ b/cli/src/config-schema.json @@ -249,6 +249,11 @@ "type": "string", "description": "The remote to which commits are pushed", "default": "origin" + }, + "colocate": { + "type": "boolean", + "description": "Automatically colocate all Git-backed repositories", + "default": false } } }, @@ -352,4 +357,4 @@ } } } -} \ No newline at end of file +} diff --git a/lib/src/settings.rs b/lib/src/settings.rs index b60f657548..e6b34abd55 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -40,12 +40,14 @@ pub struct RepoSettings { #[derive(Debug, Clone)] pub struct GitSettings { pub auto_local_branch: bool, + pub colocate: bool, } impl GitSettings { pub fn from_config(config: &config::Config) -> Self { GitSettings { auto_local_branch: config.get_bool("git.auto-local-branch").unwrap_or(true), + colocate: config.get_bool("git.colocate").unwrap_or(false), } } } @@ -54,6 +56,7 @@ impl Default for GitSettings { fn default() -> Self { GitSettings { auto_local_branch: true, + colocate: false, } } }