cli: git: extract helper function that sets up trunk() alias

This commit is contained in:
Yuya Nishihara 2024-12-13 17:10:45 +09:00
parent d6ca0c9940
commit a6f705bc92
3 changed files with 37 additions and 28 deletions

View File

@ -19,7 +19,6 @@ use std::num::NonZeroU32;
use std::path::Path;
use std::path::PathBuf;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::git;
use jj_lib::git::GitFetchError;
use jj_lib::git::GitFetchStats;
@ -27,6 +26,7 @@ use jj_lib::repo::Repo;
use jj_lib::str_util::StringPattern;
use jj_lib::workspace::Workspace;
use super::write_repository_level_trunk_alias;
use crate::cli_util::CommandHelper;
use crate::cli_util::WorkspaceCommandHelper;
use crate::command_error::cli_error;
@ -34,7 +34,6 @@ use crate::command_error::user_error;
use crate::command_error::user_error_with_message;
use crate::command_error::CommandError;
use crate::commands::git::maybe_add_gitignore;
use crate::config::write_config_value_to_file;
use crate::git_util::get_git_repo;
use crate::git_util::map_git_error;
use crate::git_util::print_git_import_stats;
@ -165,16 +164,11 @@ pub fn cmd_git_clone(
let (mut workspace_command, stats) = clone_result?;
if let Some(default_branch) = &stats.default_branch {
// Set repository level `trunk()` alias to the default remote branch.
let config_path = workspace_command.repo_path().join("config.toml");
write_config_value_to_file(
&ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]),
format!("{default_branch}@{remote_name}").into(),
&config_path,
)?;
writeln!(
ui.status(),
"Setting the revset alias \"trunk()\" to \"{default_branch}@{remote_name}\""
write_repository_level_trunk_alias(
ui,
workspace_command.repo_path(),
remote_name,
default_branch,
)?;
let default_branch_remote_ref = workspace_command

View File

@ -17,7 +17,6 @@ use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use jj_lib::config::ConfigNamePathBuf;
use jj_lib::file_util;
use jj_lib::git;
use jj_lib::git::parse_git_ref;
@ -26,6 +25,7 @@ use jj_lib::repo::ReadonlyRepo;
use jj_lib::repo::Repo;
use jj_lib::workspace::Workspace;
use super::write_repository_level_trunk_alias;
use crate::cli_util::print_trackable_remote_bookmarks;
use crate::cli_util::start_repo_transaction;
use crate::cli_util::CommandHelper;
@ -35,7 +35,6 @@ use crate::command_error::user_error_with_hint;
use crate::command_error::user_error_with_message;
use crate::command_error::CommandError;
use crate::commands::git::maybe_add_gitignore;
use crate::config::write_config_value_to_file;
use crate::git_util::get_git_repo;
use crate::git_util::is_colocated_git_workspace;
use crate::git_util::print_failed_git_export;
@ -240,20 +239,12 @@ pub fn maybe_set_repository_level_trunk_alias(
let git_repo = get_git_repo(workspace_command.repo().store())?;
if let Ok(reference) = git_repo.find_reference("refs/remotes/origin/HEAD") {
if let Some(reference_name) = reference.symbolic_target() {
if let Some(RefName::RemoteBranch {
branch: default_branch,
..
}) = parse_git_ref(reference_name)
{
let config_path = workspace_command.repo_path().join("config.toml");
write_config_value_to_file(
&ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]),
format!("{default_branch}@origin").into(),
&config_path,
)?;
writeln!(
ui.status(),
"Setting the revset alias \"trunk()\" to \"{default_branch}@origin\"",
if let Some(RefName::RemoteBranch { branch, .. }) = parse_git_ref(reference_name) {
write_repository_level_trunk_alias(
ui,
workspace_command.repo_path(),
"origin",
&branch,
)?;
}
};

View File

@ -21,7 +21,10 @@ pub mod push;
pub mod remote;
pub mod submodule;
use std::path::Path;
use clap::Subcommand;
use jj_lib::config::ConfigNamePathBuf;
use self::clone::cmd_git_clone;
use self::clone::GitCloneArgs;
@ -43,6 +46,7 @@ use crate::cli_util::CommandHelper;
use crate::cli_util::WorkspaceCommandHelper;
use crate::command_error::user_error_with_message;
use crate::command_error::CommandError;
use crate::config::write_config_value_to_file;
use crate::ui::Ui;
/// Commands for working with Git remotes and the underlying Git repo
@ -102,3 +106,23 @@ fn get_single_remote(git_repo: &git2::Repository) -> Result<Option<String>, Comm
_ => None,
})
}
/// Sets repository level `trunk()` alias to the specified remote branch.
fn write_repository_level_trunk_alias(
ui: &Ui,
repo_path: &Path,
remote: &str,
branch: &str,
) -> Result<(), CommandError> {
let config_path = repo_path.join("config.toml");
write_config_value_to_file(
&ConfigNamePathBuf::from_iter(["revset-aliases", "trunk()"]),
format!("{branch}@{remote}").into(),
&config_path,
)?;
writeln!(
ui.status(),
r#"Setting the revset alias "trunk()" to "{branch}@{remote}""#,
)?;
Ok(())
}