From e4f7795fd359ed5aa5120b01a3ec283f88a5827b Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 13 Oct 2021 09:41:20 -0700 Subject: [PATCH] cli: add `jj git remote add` command The user currently has to edit `.jj/git/config` (or run `git --git-dir=.jj/git config`) to manage remotes in the underlying Git repo. That's not very discoverable (and we may change the path some day), so let's provide a command for it. --- src/commands.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/commands.rs b/src/commands.rs index e05b021f56..7ac0de2b1a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1066,6 +1066,26 @@ https://github.com/martinvonz/jj/blob/main/docs/git-comparison.md.\ ", ) .setting(clap::AppSettings::SubcommandRequiredElseHelp) + .subcommand( + SubCommand::with_name("remote") + .about("Manage Git remotes") + .subcommand( + SubCommand::with_name("add") + .about("Add a Git remote") + .arg( + Arg::with_name("remote") + .index(1) + .required(true) + .help("The remote's name"), + ) + .arg( + Arg::with_name("url") + .index(2) + .required(true) + .help("The remote's URL"), + ), + ), + ) .subcommand( SubCommand::with_name("fetch") .about("Fetch from a Git remote") @@ -3316,6 +3336,41 @@ fn get_git_repo(store: &Store) -> Result { } } +fn cmd_git_remote( + ui: &mut Ui, + command: &CommandHelper, + _git_matches: &ArgMatches, + remote_matches: &ArgMatches, +) -> Result<(), CommandError> { + if let Some(command_matches) = remote_matches.subcommand_matches("add") { + cmd_git_remote_add(ui, command, remote_matches, remote_matches, command_matches)?; + } else { + panic!("unhandled command: {:#?}", command.root_matches()); + } + Ok(()) +} + +fn cmd_git_remote_add( + ui: &mut Ui, + command: &CommandHelper, + _git_matches: &ArgMatches, + _remote_matches: &ArgMatches, + cmd_matches: &ArgMatches, +) -> Result<(), CommandError> { + let repo_command = command.repo_helper(ui)?; + let repo = repo_command.repo(); + let git_repo = get_git_repo(repo.store())?; + let remote_name = cmd_matches.value_of("remote").unwrap(); + let url = cmd_matches.value_of("url").unwrap(); + if git_repo.find_remote(remote_name).is_ok() { + return Err(CommandError::UserError("Remote already exists".to_string())); + } + git_repo + .remote(remote_name, url) + .map_err(|err| CommandError::UserError(err.to_string()))?; + Ok(()) +} + fn cmd_git_fetch( ui: &mut Ui, command: &CommandHelper, @@ -3526,7 +3581,9 @@ fn cmd_git( command: &CommandHelper, sub_matches: &ArgMatches, ) -> Result<(), CommandError> { - if let Some(command_matches) = sub_matches.subcommand_matches("fetch") { + if let Some(command_matches) = sub_matches.subcommand_matches("remote") { + cmd_git_remote(ui, command, sub_matches, command_matches)?; + } else if let Some(command_matches) = sub_matches.subcommand_matches("fetch") { cmd_git_fetch(ui, command, sub_matches, command_matches)?; } else if let Some(command_matches) = sub_matches.subcommand_matches("clone") { cmd_git_clone(ui, command, sub_matches, command_matches)?;