summaryrefslogtreecommitdiff
path: root/src/bin/git-autosave.rs
blob: fa0d9e6d9a031620f614d17ffcf1dcdeecaa42e7 (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(Config::load()?));

	if std::env::args().any(|arg| arg == "--init") {
		let id = git_autosave::init(&repository, Some(config))?;
		config.save()?;
		println!("Initialized autosave for repository: {id}");
	}

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

	let commit = commit_autosave(&repository)?;
	println!("Commited autosave: {commit}");
	push_autosaves(&repository, callbacks)?;
	println!("Successfully pushed autosave to remote");

	Ok(())
}