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

View file

@ -35,53 +35,25 @@ impl Refractr {
work_dir.to_string_lossy().to_string()
}
fn get_branches(&self, repo: &Repository, branches: &Option<Vec<String>>, refs: bool) -> Vec<String> {
match branches {
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 => {
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 get_refs(&self, branches: &Vec<String>) -> Vec<String> {
let mut refs_branches = Vec::new();
for branch in branches {
refs_branches.push(format!("refs/heads/{}", branch));
}
refs_branches
}
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 branch_list: Vec<String> = self.get_branches(&repo, branches, false);
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_commit = repo.reference_to_annotated_commit(&fetch_head)?;
let analysis = repo.merge_analysis(&[&fetch_commit])?;
if analysis.0.is_fast_forward() {
for branch in branch_list {
for branch in branches {
let refname = format!("refs/heads/{}", branch);
let mut reference = repo.find_reference(&refname)?;
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() {
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)
});
let mut push_options = PushOptions::new();
push_options.remote_callbacks(callbacks);
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 {
refs.push(branch.as_str());
}