| 1 |
// Copyright 2020-2023 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use clap_complete::ArgValueCandidates; |
| 16 |
use jj_lib::git; |
| 17 |
use jj_lib::ref_name::RemoteNameBuf; |
| 18 |
|
| 19 |
use super::super::rename_remote_in_repo_config; |
| 20 |
use crate::cli_util::CommandHelper; |
| 21 |
use crate::command_error::CommandError; |
| 22 |
use crate::complete; |
| 23 |
use crate::ui::Ui; |
| 24 |
|
| 25 |
/// Rename a Git remote |
| 26 |
#[derive(clap::Args, Clone, Debug)] |
| 27 |
pub struct GitRemoteRenameArgs { |
| 28 |
/// The name of an existing remote |
| 29 |
#[arg(add = ArgValueCandidates::new(complete::git_remotes))] |
| 30 |
old: RemoteNameBuf, |
| 31 |
|
| 32 |
/// The desired name for `old` |
| 33 |
new: RemoteNameBuf, |
| 34 |
} |
| 35 |
|
| 36 |
pub async fn cmd_git_remote_rename( |
| 37 |
ui: &mut Ui, |
| 38 |
command: &CommandHelper, |
| 39 |
args: &GitRemoteRenameArgs, |
| 40 |
) -> Result<(), CommandError> { |
| 41 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 42 |
let mut tx = workspace_command.start_transaction(); |
| 43 |
git::rename_remote(tx.repo_mut(), &args.old, &args.new)?; |
| 44 |
rename_remote_in_repo_config(ui, command.raw_config(), &args.old, &args.new)?; |
| 45 |
if tx.repo().has_changes() { |
| 46 |
tx.finish( |
| 47 |
ui, |
| 48 |
format!( |
| 49 |
"rename git remote {old} to {new}", |
| 50 |
old = args.old.as_symbol(), |
| 51 |
new = args.new.as_symbol() |
| 52 |
), |
| 53 |
) |
| 54 |
.await?; |
| 55 |
} else { |
| 56 |
// Do not print "Nothing changed." |
| 57 |
} |
| 58 |
Ok(()) |
| 59 |
} |