use git2::{Config as GitConfig, Repository};
use std::env;
use std::path::Path;
mod config;
mod errors;
use crate::errors::SetGitConfigError;
use tracing;
use tracing_subscriber::EnvFilter;
fn set_gitconfig() -> Result<(), SetGitConfigError> {
let path = env::current_dir()?;
let absolute_path = path
.to_str()
.ok_or_else(|| errors::SetGitConfigError::PathError)?
.to_lowercase();
tracing::debug!("Opening the repo at {:?}", &absolute_path);
let repo = Repository::open(&Path::new(&absolute_path))?;
let remote = repo.find_remote("origin")?;
let url = remote.url().ok_or(git2::Error::from_str("failed"))?;
tracing::debug!("Found the remote url of the repo at {:?}", &url);
let mut gitconfig = GitConfig::open_default()?;
let configuration = config::read().expect("Unable to read the config file for setgitconfig");
for conf in configuration.repositories {
if url.contains(&conf.giturl) {
gitconfig.set_str("user.name", &conf.username)?;
gitconfig.set_str("user.email", &conf.email)?;
tracing::debug!(
"Setting the email {} and username {}",
&conf.username,
&conf.email
);
return Ok(());
}
}
Ok(())
}
fn main() -> Result<(), SetGitConfigError> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
tracing::debug!("starting up");
match set_gitconfig() {
Ok(_) => Ok(()),
Err(error) => {
tracing::debug!("Failed to execute setgitconfig with error {}", error);
Ok(())
}
}
}