Skip to content

Commit 9253224

Browse files
codexByron
andcommitted
repo: migrate add_remote to gix config
Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent ad74d5c commit 9253224

1 file changed

Lines changed: 25 additions & 9 deletions

File tree

crates/gitbutler-repo/src/commands.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::{path::Path, sync::Mutex};
33
use anyhow::{Result, bail};
44
use base64::engine::Engine as _;
55
use but_core::commit::sign_buffer;
6+
use but_core::git_config::edit_repo_config;
67
use but_ctx::Context;
78
use fuzzy_matcher::{FuzzyMatcher, skim::SkimMatcherV2};
89
use ignore::WalkBuilder;
@@ -11,7 +12,7 @@ use itertools::Itertools;
1112
use serde::Serialize;
1213
use tracing::warn;
1314

14-
use crate::{RepositoryExt, remote::GitRemote};
15+
use crate::remote::GitRemote;
1516

1617
#[derive(Default, Debug, Serialize)]
1718
#[serde(rename_all = "camelCase")]
@@ -202,25 +203,40 @@ impl RepoCommands for Context {
202203
}
203204

204205
fn add_remote(&self, name: &str, url: &str) -> Result<()> {
205-
#[expect(deprecated, reason = "legacy remote configuration adapter")]
206-
let repo = self.git2_repo.get()?;
206+
let repo = self.open_isolated_repo()?;
207207

208208
// Bail if remote with given name already exists.
209209
if repo.find_remote(name).is_ok() {
210210
bail!("Remote name '{name}' already exists");
211211
}
212212

213213
// Bail if remote with given url already exists.
214-
if repo
215-
.remotes_as_string()?
214+
if let Some(remote_name) = repo
215+
.remote_names()
216216
.iter()
217-
.map(|name| repo.find_remote(name))
218-
.any(|result| result.is_ok_and(|remote| remote.url() == Some(url)))
217+
.filter_map(|name| repo.find_remote(name.as_ref()).ok())
218+
.find_map(|remote| {
219+
remote
220+
.url(gix::remote::Direction::Fetch)
221+
.and_then(|remote_url| {
222+
if remote_url.to_bstring() == url {
223+
remote
224+
.name()
225+
.and_then(|n| n.as_symbol().map(ToOwned::to_owned))
226+
} else {
227+
None
228+
}
229+
})
230+
})
219231
{
220-
bail!("Remote with url '{url}' already exists");
232+
bail!("Remote with url '{url}' already exists at {remote_name}");
221233
}
222234

223-
repo.remote(name, url)?;
235+
edit_repo_config(&repo, gix::config::Source::Local, |config| {
236+
let mut section = config.section_mut_or_create_new("remote", Some(name.into()))?;
237+
section.push("url".try_into()?, Some(url.into()));
238+
Ok(())
239+
})?;
224240
Ok(())
225241
}
226242

0 commit comments

Comments
 (0)