| 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 gix::Url; |
| 16 |
use gix::remote::Direction; |
| 17 |
use jj_lib::git; |
| 18 |
use jj_lib::ref_name::RemoteName; |
| 19 |
use jj_lib::ref_name::RemoteNameBuf; |
| 20 |
use jj_lib::repo::Repo; |
| 21 |
|
| 22 |
use crate::cli_util::CommandHelper; |
| 23 |
use crate::command_error::CommandError; |
| 24 |
use crate::git_util::absolute_git_url; |
| 25 |
use crate::ui::Ui; |
| 26 |
|
| 27 |
/// Add a Git remote |
| 28 |
#[derive(clap::Args, Clone, Debug)] |
| 29 |
pub struct GitRemoteAddArgs { |
| 30 |
/// The remote's name |
| 31 |
remote: RemoteNameBuf, |
| 32 |
|
| 33 |
/// The remote's URL or path |
| 34 |
/// |
| 35 |
/// Local path will be resolved to absolute form. |
| 36 |
#[arg(value_hint = clap::ValueHint::Url)] |
| 37 |
url: String, |
| 38 |
|
| 39 |
/// The URL used for push |
| 40 |
/// |
| 41 |
/// Local path will be resolved to absolute form |
| 42 |
#[arg(long, value_hint = clap::ValueHint::Url)] |
| 43 |
push_url: Option<String>, |
| 44 |
} |
| 45 |
|
| 46 |
pub async fn cmd_git_remote_add( |
| 47 |
ui: &mut Ui, |
| 48 |
command: &CommandHelper, |
| 49 |
args: &GitRemoteAddArgs, |
| 50 |
) -> Result<(), CommandError> { |
| 51 |
let mut workspace_command = command.workspace_helper(ui).await?; |
| 52 |
let url = absolute_git_url(command.cwd(), &args.url)?; |
| 53 |
let push_url = args |
| 54 |
.push_url |
| 55 |
.as_deref() |
| 56 |
.map(|url| absolute_git_url(command.cwd(), url)) |
| 57 |
.transpose()?; |
| 58 |
|
| 59 |
let mut tx = workspace_command.start_transaction(); |
| 60 |
|
| 61 |
git::add_remote(tx.repo_mut(), &args.remote, &url, push_url.as_deref())?; |
| 62 |
warn_if_remote_url_matches(ui, tx.repo(), &args.remote, &url, push_url.as_deref())?; |
| 63 |
tx.finish(ui, format!("add git remote {}", args.remote.as_symbol())) |
| 64 |
.await?; |
| 65 |
Ok(()) |
| 66 |
} |
| 67 |
|
| 68 |
/// Warns if the new configured URL exactly matches an existing configured URL. |
| 69 |
/// |
| 70 |
/// URL equivalence is not reliable. For example, HTTPS, SSH, .git suffixes, and |
| 71 |
/// redirects can identify the same repository. Git URL rewrite rules can also |
| 72 |
/// make different configured strings resolve to the same destination, so this |
| 73 |
/// is only an exact-match warning. A remote without a configured push URL is |
| 74 |
/// treated as pushing to its fetch URL, matching Git's behavior. |
| 75 |
fn warn_if_remote_url_matches( |
| 76 |
ui: &Ui, |
| 77 |
repo: &dyn Repo, |
| 78 |
new_remote: &RemoteName, |
| 79 |
new_fetch_url: &str, |
| 80 |
new_push_url: Option<&str>, |
| 81 |
) -> Result<(), CommandError> { |
| 82 |
let git_repo = git::get_git_repo(repo.store())?; |
| 83 |
// Git uses the fetch URL for push when no push URL is configured. |
| 84 |
let new_push_url = new_push_url.unwrap_or(new_fetch_url); |
| 85 |
let mut warned = false; |
| 86 |
for remote_name in git_repo.remote_names() { |
| 87 |
// Ignore the remote that was just added. |
| 88 |
if remote_name == new_remote.as_str() { |
| 89 |
continue; |
| 90 |
} |
| 91 |
// Ignore empty or unloadable remote sections for this advisory warning. |
| 92 |
let Some(Ok(remote)) = git_repo.try_find_remote_without_url_rewrite(&**remote_name) else { |
| 93 |
continue; |
| 94 |
}; |
| 95 |
let remote_fetch_url = remote.url(Direction::Fetch).map(Url::to_bstring); |
| 96 |
let remote_push_url = remote.url(Direction::Push).map(Url::to_bstring); |
| 97 |
let remote_url_matches = [remote_fetch_url, remote_push_url] |
| 98 |
.iter() |
| 99 |
.flatten() |
| 100 |
.any(|remote_url| remote_url == new_fetch_url || remote_url == new_push_url); |
| 101 |
// Don't print the URL itself because remote URLs can contain credentials, |
| 102 |
// such as user:password or token path segments. |
| 103 |
if remote_url_matches { |
| 104 |
writeln!( |
| 105 |
ui.warning_default(), |
| 106 |
"Remote {remote_name} already uses the same URL." |
| 107 |
)?; |
| 108 |
warned = true; |
| 109 |
} |
| 110 |
} |
| 111 |
if warned { |
| 112 |
writeln!( |
| 113 |
ui.hint_default(), |
| 114 |
"If this was a mistake, run `jj git remote remove {new_remote}`.", |
| 115 |
new_remote = new_remote.as_symbol() |
| 116 |
)?; |
| 117 |
} |
| 118 |
Ok(()) |
| 119 |
} |