require branches for simplicity

This commit is contained in:
Bryson Steck 2025-03-08 20:48:28 -07:00
parent 23561ad34a
commit dd77c19124
Signed by: brysonsteck
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4
2 changed files with 19 additions and 52 deletions

View file

@ -17,20 +17,14 @@ pub struct ConfigFile {
impl fmt::Display for ConfigFile { impl fmt::Display for ConfigFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let branches_list = match &self.config.branches { let mut branches_list = String::from("[");
None => String::from("All branches"), for i in 0..self.config.branches.len() {
Some(vec) => { branches_list = format!("{}{}", branches_list, &self.config.branches[i]);
let mut out = String::from("["); if i < self.config.branches.len() - 1 {
for i in 0..vec.len() { branches_list.push_str(", ");
out = format!("{}{}", out, vec[i]);
if i < vec.len() - 1 {
out.push_str(", ");
} }
} }
out.push(']'); branches_list.push(']');
out
}
};
let mut to_list = String::from("["); let mut to_list = String::from("[");
for i in 0..self.config.to.len() { for i in 0..self.config.to.len() {
@ -88,7 +82,7 @@ impl fmt::Display for ConfigFile {
pub struct Config { pub struct Config {
pub from: String, pub from: String,
pub to: Vec<String>, pub to: Vec<String>,
pub branches: Option<Vec<String>>, pub branches: Vec<String>,
pub work_dir: Option<String>, pub work_dir: Option<String>,
pub git: Git, pub git: Git,
pub schedule: Schedule pub schedule: Schedule

View file

@ -35,53 +35,25 @@ impl Refractr {
work_dir.to_string_lossy().to_string() work_dir.to_string_lossy().to_string()
} }
fn get_branches(&self, repo: &Repository, branches: &Option<Vec<String>>, refs: bool) -> Vec<String> { fn get_refs(&self, branches: &Vec<String>) -> Vec<String> {
match branches {
Some(repo_branches) => {
if refs {
let mut refs_branches = Vec::new(); let mut refs_branches = Vec::new();
for branch in repo_branches { for branch in branches {
refs_branches.push(format!("refs/heads/{}", branch)); refs_branches.push(format!("refs/heads/{}", branch));
} }
refs_branches refs_branches
} else {
repo_branches.to_vec()
}
},
None => {
let mut strings = Vec::new();
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() {
if refs {
strings.push(format!("refs/heads/{}", name.to_string()))
} else {
strings.push(name.to_string());
}
}
}
}
strings
}
}
} }
fn fast_forward(&self, repo_dir: &str, branches: &Option<Vec<String>>) -> Result<(), Error> { fn fast_forward(&self, repo_dir: &str, branches: &Vec<String>) -> Result<(), Error> {
let repo = Repository::open(repo_dir)?; let repo = Repository::open(repo_dir)?;
let branch_list: Vec<String> = self.get_branches(&repo, branches, false);
common::verbose(self.verbose, 2, format!("Pulling origin")); common::verbose(self.verbose, 2, format!("Pulling origin"));
repo.find_remote("origin")?.fetch(&branch_list, None, None)?; repo.find_remote("origin")?.fetch(&branches, None, None)?;
let fetch_head = repo.find_reference("FETCH_HEAD")?; let fetch_head = repo.find_reference("FETCH_HEAD")?;
let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?; let fetch_commit = repo.reference_to_annotated_commit(&fetch_head)?;
let analysis = repo.merge_analysis(&[&fetch_commit])?; let analysis = repo.merge_analysis(&[&fetch_commit])?;
if analysis.0.is_fast_forward() { if analysis.0.is_fast_forward() {
for branch in branch_list { for branch in branches {
let refname = format!("refs/heads/{}", branch); let refname = format!("refs/heads/{}", branch);
let mut reference = repo.find_reference(&refname)?; let mut reference = repo.find_reference(&refname)?;
reference.set_target(fetch_commit.id(), "Fast-forward")?; reference.set_target(fetch_commit.id(), "Fast-forward")?;
@ -129,14 +101,15 @@ impl Refractr {
for i in cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec() { for i in cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec() {
sha256.push_str(&hex::encode(i.to_string())); sha256.push_str(&hex::encode(i.to_string()));
} }
eprintln!("warning: trusting unknown host {} with sha256 host key {}", url, hex::encode(cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec())); eprintln!("refractr: warning: implicitly trusting unknown host {} with sha256 host key {}", url, hex::encode(cert.as_hostkey().unwrap().hash_sha256().unwrap().to_vec()));
eprintln!("refractr: warning: to ignore this error in the future, add this host to your known_hosts file");
Ok(CertificateCheckStatus::CertificateOk) Ok(CertificateCheckStatus::CertificateOk)
}); });
let mut push_options = PushOptions::new(); let mut push_options = PushOptions::new();
push_options.remote_callbacks(callbacks); push_options.remote_callbacks(callbacks);
let mut refs = Vec::new(); let mut refs = Vec::new();
let strings = self.get_branches(&repo, &cfg.branches, true); let strings = self.get_refs(&cfg.branches);
for branch in &strings { for branch in &strings {
refs.push(branch.as_str()); refs.push(branch.as_str());
} }