pushing works with generated vec

This commit is contained in:
Bryson Steck 2025-03-03 23:15:35 -07:00
parent c49b076cef
commit 25a6871126
Signed by: brysonsteck
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4

View file

@ -16,9 +16,19 @@ fn set_up_work_dir(work_dir: PathBuf) -> String {
work_dir.to_string_lossy().to_string() work_dir.to_string_lossy().to_string()
} }
fn get_branches(repo: &Repository, branches: &Option<Vec<String>>) -> Vec<String> { fn get_branches(repo: &Repository, branches: &Option<Vec<String>>, refs: bool) -> Vec<String> {
match branches { match branches {
Some(repo_branches) => repo_branches.to_vec(), Some(repo_branches) => {
if refs {
let mut refs_branches = Vec::new();
for branch in repo_branches {
refs_branches.push(format!("refs/heads/{}", branch));
}
refs_branches
} else {
repo_branches.to_vec()
}
},
None => { None => {
let mut strings = Vec::new(); let mut strings = Vec::new();
let remote_branches = match repo.branches(Some(git2::BranchType::Remote)) { let remote_branches = match repo.branches(Some(git2::BranchType::Remote)) {
@ -28,10 +38,14 @@ fn get_branches(repo: &Repository, branches: &Option<Vec<String>>) -> Vec<String
for branch in remote_branches { for branch in remote_branches {
if let Ok((b, _)) = branch { if let Ok((b, _)) = branch {
if let Ok(Some(name)) = b.name() { if let Ok(Some(name)) = b.name() {
if refs {
strings.push(format!("refs/heads/{}", name.to_string()))
} else {
strings.push(name.to_string()); strings.push(name.to_string());
} }
} }
} }
}
strings strings
} }
} }
@ -39,7 +53,7 @@ fn get_branches(repo: &Repository, branches: &Option<Vec<String>>) -> Vec<String
fn fast_forward(repo_dir: &str, branches: &Option<Vec<String>>) -> Result<(), Error> { fn fast_forward(repo_dir: &str, branches: &Option<Vec<String>>) -> Result<(), Error> {
let repo = Repository::open(repo_dir)?; let repo = Repository::open(repo_dir)?;
let branch_list: Vec<String> = get_branches(&repo, branches); let branch_list: Vec<String> = get_branches(&repo, branches, false);
repo.find_remote("origin")?.fetch(&branch_list, None, None)?; repo.find_remote("origin")?.fetch(&branch_list, None, None)?;
Ok(()) Ok(())
@ -108,12 +122,19 @@ pub fn start(refractr: Refractr, cfgs: Vec<ConfigFile>) -> std::io::Result<()> {
} }
for mut remote in remote_list { for mut remote in remote_list {
common::verbose(refractr.verbose, 1, format!("Pushing to remote: {}", remote.url().unwrap()));
let mut callbacks = RemoteCallbacks::new(); let mut callbacks = RemoteCallbacks::new();
callbacks.credentials(|_,_,_| Cred::ssh_key("git", None, &Path::new(&cfg.config.git.ssh_identity_file), None)); callbacks.credentials(|_,_,_| Cred::ssh_key("git", None, &Path::new(&cfg.config.git.ssh_identity_file), None));
let mut push_options = PushOptions::new(); let mut push_options = PushOptions::new();
push_options.remote_callbacks(callbacks); push_options.remote_callbacks(callbacks);
match remote.push::<&str>(&["refs/heads/master"], Some(&mut push_options)) { let mut refs = Vec::new();
let strings = get_branches(&repo, &cfg.config.branches, true);
for branch in &strings {
refs.push(branch.as_str());
}
match remote.push::<&str>(&refs, Some(&mut push_options)) {
Ok(_) => (), Ok(_) => (),
Err(e) => { Err(e) => {
eprintln!("refractr: failed to push to remote: {}: {}", remote.url().unwrap(), e) eprintln!("refractr: failed to push to remote: {}: {}", remote.url().unwrap(), e)