Skip to content

Commit

Permalink
cli/lib: add git.colocate to colocate repositories by default
Browse files Browse the repository at this point in the history
Summary: Closes #2507.

Signed-off-by: Austin Seipp <[email protected]>
Change-Id: I333ff2d16584c51a5e6957b341e0315e
  • Loading branch information
thoughtpolice committed Nov 3, 2023
1 parent 37d6e71 commit 601a565
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
10 changes: 8 additions & 2 deletions cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -465,15 +471,15 @@ fn cmd_git_clone(
let clone_result = do_git_clone(
ui,
command,
args.colocate,
colocate,
remote_name,
&source,
&canonical_wc_path,
);
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 {
Expand Down
10 changes: 8 additions & 2 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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(),
Expand Down
7 changes: 6 additions & 1 deletion cli/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down Expand Up @@ -352,4 +357,4 @@
}
}
}
}
}
3 changes: 3 additions & 0 deletions lib/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand All @@ -54,6 +56,7 @@ impl Default for GitSettings {
fn default() -> Self {
GitSettings {
auto_local_branch: true,
colocate: false,
}
}
}
Expand Down

0 comments on commit 601a565

Please sign in to comment.