Skip to content

Commit 69d4472

Browse files
committed
Use gitoxide for all Git configuration
It's a fully fledged implementation that should be able to pick up additional special cases. Useful for users who configure things per repository.
1 parent 64f558b commit 69d4472

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/gitbutler-config/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ publish = false
77

88
[dependencies]
99
git2.workspace = true
10+
gix.workspace = true
1011
anyhow = "1.0.95"
12+
tracing.workspace = true
1113
serde = { workspace = true, features = ["std"]}
1214
gitbutler-project.workspace = true

crates/gitbutler-config/src/git.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use anyhow::Result;
22
use git2::ConfigLevel;
3+
use gix::bstr::{BStr, ByteVec};
34
use serde::{Deserialize, Serialize};
5+
use std::borrow::Cow;
6+
use std::ffi::OsStr;
47

58
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Default)]
69
#[serde(rename_all = "camelCase")]
@@ -24,11 +27,17 @@ pub trait GitConfig {
2427

2528
impl GitConfig for git2::Repository {
2629
fn gb_config(&self) -> Result<GbConfig> {
27-
let sign_commits = get_bool(self, SIGN_COMMITS)?;
28-
let signing_key = get_string(self, SIGNING_KEY)?;
29-
let signing_format = get_string(self, SIGNING_FORMAT)?;
30-
let gpg_program = get_string(self, GPG_PROGRAM)?;
31-
let gpg_ssh_program = get_string(self, GPG_SSH_PROGRAM)?;
30+
let repo = gix::open(self.path())?;
31+
let config = repo.config_snapshot();
32+
let sign_commits = config.boolean(SIGN_COMMITS);
33+
let signing_key = config.string(SIGNING_KEY).and_then(bstring_into_string);
34+
let signing_format = config.string(SIGNING_FORMAT).and_then(bstring_into_string);
35+
let gpg_program = config
36+
.trusted_program(GPG_PROGRAM)
37+
.and_then(osstr_into_string);
38+
let gpg_ssh_program = config
39+
.trusted_program(GPG_SSH_PROGRAM)
40+
.and_then(osstr_into_string);
3241
Ok(GbConfig {
3342
sign_commits,
3443
signing_key,
@@ -57,25 +66,23 @@ impl GitConfig for git2::Repository {
5766
}
5867
}
5968

60-
fn get_bool(repo: &git2::Repository, key: &str) -> Result<Option<bool>> {
61-
let config = repo.config()?;
62-
match config.get_bool(key) {
63-
Ok(value) => Ok(Some(value)),
64-
Err(err) => match err.code() {
65-
git2::ErrorCode::NotFound => Ok(None),
66-
_ => Err(err.into()),
67-
},
69+
fn bstring_into_string(s: Cow<'_, BStr>) -> Option<String> {
70+
match Vec::from(s.into_owned()).into_string() {
71+
Ok(s) => Some(s),
72+
Err(err) => {
73+
tracing::warn!("Could not convert to string due to illegal UTF8: {err}");
74+
None
75+
}
6876
}
6977
}
7078

71-
fn get_string(repo: &git2::Repository, key: &str) -> Result<Option<String>> {
72-
let config = repo.config()?;
73-
match config.get_string(key) {
74-
Ok(value) => Ok(Some(value)),
75-
Err(err) => match err.code() {
76-
git2::ErrorCode::NotFound => Ok(None),
77-
_ => Err(err.into()),
78-
},
79+
fn osstr_into_string(s: Cow<'_, OsStr>) -> Option<String> {
80+
match Vec::from(gix::path::try_os_str_into_bstr(s).ok()?.into_owned()).into_string() {
81+
Ok(s) => Some(s),
82+
Err(err) => {
83+
tracing::warn!("Could not convert to string due to illegal UTF8: {err}");
84+
None
85+
}
7986
}
8087
}
8188

0 commit comments

Comments
 (0)