From 5c6ef75b2b35123d5623fd0d123693614c9a04d6 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 12 Aug 2023 17:06:59 +0900 Subject: [PATCH] cli: move add_to_git_exclude() helper to commands.git module I'm going to add a call site to the git module, and I think it's better to host the helper function there instead of importing from the super module. --- cli/src/commands/git.rs | 41 ++++++++++++++++++++++++++++++++++++++++- cli/src/commands/mod.rs | 41 +---------------------------------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index fdf4ec382..63d912403 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -1,5 +1,5 @@ use std::collections::HashSet; -use std::io::{Read, Write}; +use std::io::{Read, Seek as _, SeekFrom, Write}; use std::ops::Deref; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -204,6 +204,45 @@ fn map_git_error(err: git2::Error) -> CommandError { } } +pub fn add_to_git_exclude(ui: &Ui, git_repo: &git2::Repository) -> Result<(), CommandError> { + let exclude_file_path = git_repo.path().join("info").join("exclude"); + if exclude_file_path.exists() { + match fs::OpenOptions::new() + .read(true) + .write(true) + .open(&exclude_file_path) + { + Ok(mut exclude_file) => { + let mut buf = vec![]; + exclude_file.read_to_end(&mut buf)?; + let pattern = b"\n/.jj/\n"; + if !buf.windows(pattern.len()).any(|window| window == pattern) { + exclude_file.seek(SeekFrom::End(0))?; + if !buf.ends_with(b"\n") { + exclude_file.write_all(b"\n")?; + } + exclude_file.write_all(b"/.jj/\n")?; + } + } + Err(err) => { + writeln!( + ui.error(), + "Failed to add `.jj/` to {}: {}", + exclude_file_path.to_string_lossy(), + err + )?; + } + } + } else { + writeln!( + ui.error(), + "Failed to add `.jj/` to {} because it doesn't exist", + exclude_file_path.to_string_lossy() + )?; + } + Ok(()) +} + fn cmd_git_remote_add( ui: &mut Ui, command: &CommandHelper, diff --git a/cli/src/commands/mod.rs b/cli/src/commands/mod.rs index aae2b1ce1..9ffc694e2 100644 --- a/cli/src/commands/mod.rs +++ b/cli/src/commands/mod.rs @@ -1063,45 +1063,6 @@ struct UtilMangenArgs {} #[derive(clap::Args, Clone, Debug)] struct UtilConfigSchemaArgs {} -fn add_to_git_exclude(ui: &Ui, git_repo: &git2::Repository) -> Result<(), CommandError> { - let exclude_file_path = git_repo.path().join("info").join("exclude"); - if exclude_file_path.exists() { - match fs::OpenOptions::new() - .read(true) - .write(true) - .open(&exclude_file_path) - { - Ok(mut exclude_file) => { - let mut buf = vec![]; - exclude_file.read_to_end(&mut buf)?; - let pattern = b"\n/.jj/\n"; - if !buf.windows(pattern.len()).any(|window| window == pattern) { - exclude_file.seek(SeekFrom::End(0))?; - if !buf.ends_with(b"\n") { - exclude_file.write_all(b"\n")?; - } - exclude_file.write_all(b"/.jj/\n")?; - } - } - Err(err) => { - writeln!( - ui.error(), - "Failed to add `.jj/` to {}: {}", - exclude_file_path.to_string_lossy(), - err - )?; - } - } - } else { - writeln!( - ui.error(), - "Failed to add `.jj/` to {} because it doesn't exist", - exclude_file_path.to_string_lossy() - )?; - } - Ok(()) -} - #[instrument(skip_all)] fn cmd_version( ui: &mut Ui, @@ -1158,7 +1119,7 @@ fn cmd_init(ui: &mut Ui, command: &CommandHelper, args: &InitArgs) -> Result<(), let mut workspace_command = command.for_loaded_repo(ui, workspace, repo)?; workspace_command.snapshot(ui)?; if workspace_command.working_copy_shared_with_git() { - add_to_git_exclude(ui, &git_repo)?; + git::add_to_git_exclude(ui, &git_repo)?; } else { let mut tx = workspace_command.start_transaction("import git refs"); jj_lib::git::import_refs(tx.mut_repo(), &git_repo, &command.settings().git_settings())?;