summaryrefslogtreecommitdiff
path: root/src/bin/git-autosave.rs
blob: 64ffd679fbcf1c2075fb57adcb46040add3acd24 (plain)
use auth_git2::GitAuthenticator;
use git_autosave::{Config, authenticate::Inquirer, commit_autosave, push_autosaves};
use git2::{RemoteCallbacks, Repository};

fn main() -> Result<(), anyhow::Error> {
	let repository = Repository::discover(".")?;
	let gitconfig = repository.config()?;
	let config: &'static mut Config = Box::leak(Box::new(git_autosave::load_config()?));

	if std::env::args().any(|arg| arg == "--init") {
		git_autosave::init(&repository, Some(config))?;
		git_autosave::save_config(config)?;
	}

	let auth = GitAuthenticator::new().set_prompter(Inquirer(config));
	let mut callbacks = RemoteCallbacks::new();
	callbacks.credentials(auth.credentials(&gitconfig));

	commit_autosave(&repository)?;
	push_autosaves(&repository, callbacks)?;

	Ok(())
}