From c49b076cef67026ceff3eec09a5bba38453da4ae Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Mon, 3 Mar 2025 22:54:58 -0700 Subject: [PATCH] pushing works with hard-coded ref --- src/refractr.rs | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/src/refractr.rs b/src/refractr.rs index 039b717..2988c5f 100644 --- a/src/refractr.rs +++ b/src/refractr.rs @@ -1,4 +1,4 @@ -use git2::Repository; +use git2::{Cred, PushOptions, Remote, RemoteCallbacks, Repository}; use sha2::{Sha256, Digest}; use crate::common::{self, Refractr}; @@ -16,13 +16,15 @@ fn set_up_work_dir(work_dir: PathBuf) -> String { work_dir.to_string_lossy().to_string() } -fn fast_forward(repo_dir: &str, branches: &Option>) -> Result<(), Error> { - let repo = Repository::open(repo_dir)?; - let branch_list: Vec = match branches { +fn get_branches(repo: &Repository, branches: &Option>) -> Vec { + match branches { Some(repo_branches) => repo_branches.to_vec(), None => { let mut strings = Vec::new(); - let remote_branches = repo.branches(Some(git2::BranchType::Remote))?; + let remote_branches = match repo.branches(Some(git2::BranchType::Remote)) { + Ok(b) => b, + Err(e) => panic!("refractr: failed to get branches: {}", e) + }; for branch in remote_branches { if let Ok((b, _)) = branch { if let Ok(Some(name)) = b.name() { @@ -32,7 +34,12 @@ fn fast_forward(repo_dir: &str, branches: &Option>) -> Result<(), Er } strings } - }; + } +} + +fn fast_forward(repo_dir: &str, branches: &Option>) -> Result<(), Error> { + let repo = Repository::open(repo_dir)?; + let branch_list: Vec = get_branches(&repo, branches); repo.find_remote("origin")?.fetch(&branch_list, None, None)?; Ok(()) @@ -82,7 +89,7 @@ pub fn start(refractr: Refractr, cfgs: Vec) -> std::io::Result<()> { }; // create remotes for each "to" repo - let mut remote_list = Vec::new(); + let mut remote_list: Vec> = Vec::new(); for to in &cfg.config.to { let mut hasher = Sha256::new(); hasher.update(to); @@ -99,6 +106,20 @@ pub fn start(refractr: Refractr, cfgs: Vec) -> std::io::Result<()> { } } } + + for mut remote in remote_list { + let mut callbacks = RemoteCallbacks::new(); + callbacks.credentials(|_,_,_| Cred::ssh_key("git", None, &Path::new(&cfg.config.git.ssh_identity_file), None)); + let mut push_options = PushOptions::new(); + push_options.remote_callbacks(callbacks); + + match remote.push::<&str>(&["refs/heads/master"], Some(&mut push_options)) { + Ok(_) => (), + Err(e) => { + eprintln!("refractr: failed to push to remote: {}: {}", remote.url().unwrap(), e) + } + } + } } Ok(())