ok/jj
1
0
Fork 0
forked from mirrors/jj

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.
This commit is contained in:
Yuya Nishihara 2023-08-12 17:06:59 +09:00
parent 8b5ff20874
commit 5c6ef75b2b
2 changed files with 41 additions and 41 deletions

View file

@ -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,

View file

@ -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())?;