Skip to content

Commit

Permalink
git/config: supports local configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilou97 committed Apr 2, 2024
1 parent 98c4528 commit 8f4d28b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
13 changes: 2 additions & 11 deletions src/git/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,14 @@ pub struct Yggit {
}

impl GitConfig {
/// Try to load gitconfig from:
/// - global: $HOME/.gitconfig
/// - XDG: $HOME/.config/git/config
/// - system: /etc/gitconfig
pub fn open_default() -> Result<GitConfig> {
let config = git2::Config::open_default().context("Cannot open git config")?;
Self::open_with_git_config(config)
}

/// Parse the git config and return a Config
///
/// It parses the following field:
/// - user.email : required
/// - user.name : required
/// - notes.rewriteRef = "refs/notes/commits" : required
/// - yggit.defaultUpstream : optional, default(origin)
fn open_with_git_config(config: git2::Config) -> Result<GitConfig> {
pub fn parse(config: git2::Config) -> Result<GitConfig> {
let email = config
.get_string("user.email")
.context("email not found in configuration")?;
Expand Down Expand Up @@ -88,7 +79,7 @@ mod tests {
impl GitConfig {
fn open(path: &Path) -> Result<GitConfig> {
let config = git2::Config::open(path).context("config not found")?;
Self::open_with_git_config(config)
Self::parse(config)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/git/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ impl Git {
current_dir.join(path)
};
let repository = Repository::discover(path).context("repository not found")?;
let gitconfig = GitConfig::open_default()?;
let config = repository.config().context("config not found")?;
let gitconfig = GitConfig::parse(config)?;
let signature = Signature::now(&gitconfig.user.name, &gitconfig.user.email)
.context("cannot compute signature")?;
Ok(Git {
Expand Down

0 comments on commit 8f4d28b

Please sign in to comment.