| 1 |
// Copyright 2024 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 |
use jj_lib::repo::Repo as _; |
| 19 |
|
| 20 |
use crate::cli_util::CommandHelper; |
| 21 |
use crate::command_error::CommandError; |
| 22 |
use crate::complete; |
| 23 |
use crate::git_util::absolute_git_url; |
| 24 |
use crate::ui::Ui; |
| 25 |
|
| 26 |
/// Set the URL of a Git remote |
| 27 |
#[derive(clap::Args, Clone, Debug)] |
| 28 |
pub struct GitRemoteSetUrlArgs { |
| 29 |
/// The remote's name |
| 30 |
#[arg(add = ArgValueCandidates::new(complete::git_remotes))] |
| 31 |
remote: RemoteNameBuf, |
| 32 |
|
| 33 |
/// The URL or path to fetch from |
| 34 |
/// |
| 35 |
/// This is a short form, equivalent to using the explicit --fetch. |
| 36 |
/// |
| 37 |
/// Local path will be resolved to absolute form. |
| 38 |
#[arg(value_hint = clap::ValueHint::Url)] |
| 39 |
url: Option<String>, |
| 40 |
|
| 41 |
/// The URL or path to push to |
| 42 |
/// |
| 43 |
/// Local path will be resolved to absolute form. |
| 44 |
#[arg(long, value_hint = clap::ValueHint::Url)] |
| 45 |
push: Option<String>, |
| 46 |
|
| 47 |
/// The URL or path to fetch from |
| 48 |
/// |
| 49 |
/// Local path will be resolved to absolute form. |
| 50 |
#[arg(long, value_hint = clap::ValueHint::Url, conflicts_with = "url")] |
| 51 |
fetch: Option<String>, |
| 52 |
} |
| 53 |
|
| 54 |
pub async fn cmd_git_remote_set_url( |
| 55 |
ui: &mut Ui, |
| 56 |
command: &CommandHelper, |
| 57 |
args: &GitRemoteSetUrlArgs, |
| 58 |
) -> Result<(), CommandError> { |
| 59 |
let workspace_command = command.workspace_helper(ui).await?; |
| 60 |
|
| 61 |
let process_url = |url: Option<&String>| { |
| 62 |
url.map(|url| absolute_git_url(command.cwd(), url)) |
| 63 |
.transpose() |
| 64 |
}; |
| 65 |
|
| 66 |
let fetch_url = process_url(args.url.as_ref().or(args.fetch.as_ref()))?; |
| 67 |
let push_url = process_url(args.push.as_ref())?; |
| 68 |
|
| 69 |
git::set_remote_urls( |
| 70 |
workspace_command.repo().store(), |
| 71 |
&args.remote, |
| 72 |
fetch_url.as_deref(), |
| 73 |
push_url.as_deref(), |
| 74 |
)?; |
| 75 |
Ok(()) |
| 76 |
} |