fast forwarding works now

This commit is contained in:
Bryson Steck 2025-03-05 00:27:21 -07:00
parent dc86ad15fe
commit f86fb79e73
Signed by: brysonsteck
SSH key fingerprint: SHA256:XpKABw/nP4z8UVaH+weLaBnEOD86+cVwif+QjuYLGT4

View file

@ -1,4 +1,4 @@
use git2::{Cred, PushOptions, RemoteCallbacks, Repository}; use git2::{BranchType, Cred, PushOptions, RemoteCallbacks, Repository};
use sha2::{Sha256, Digest}; use sha2::{Sha256, Digest};
use crate::common::{self, Refractr}; use crate::common::{self, Refractr};
@ -15,6 +15,7 @@ use std::sync::Arc;
struct OpenedRepository { struct OpenedRepository {
repo: Repository, repo: Repository,
path: String,
remotes: Vec<String>, remotes: Vec<String>,
cfg: Config, cfg: Config,
} }
@ -61,11 +62,25 @@ fn get_branches(repo: &Repository, branches: &Option<Vec<String>>, refs: bool) -
} }
} }
fn fast_forward(repo_dir: &str, branches: &Option<Vec<String>>) -> Result<(), Error> { fn fast_forward(refractr: &Refractr, 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, false); let branch_list: Vec<String> = get_branches(&repo, branches, false);
common::verbose(refractr.verbose, 2, format!("Pulling origin"));
repo.find_remote("origin")?.fetch(&branch_list, None, None)?; repo.find_remote("origin")?.fetch(&branch_list, None, None)?;
for branch in branch_list {
let local_branch = repo.find_branch(&branch, BranchType::Local).unwrap();
let local_commit = local_branch.get().peel_to_commit().unwrap();
let origin_branch = repo.find_branch(&format!("origin/{}", branch), BranchType::Remote).unwrap();
let origin_commit = origin_branch.get().peel_to_commit().unwrap();
if local_commit.id() < origin_commit.id() {
local_branch.into_reference().set_target(origin_commit.id(), "Fast-forwarding").unwrap();
}
}
Ok(()) Ok(())
} }
@ -149,7 +164,7 @@ fn looper(refractr: Refractr, repos: Vec<OpenedRepository>) {
if i <= 0 { if i <= 0 {
current_ints[i] = original_ints[i].clone(); current_ints[i] = original_ints[i].clone();
common::verbose(refractr.verbose, 2, format!("Interval for {} has arrived, pulling", repos[i].cfg.from)); common::verbose(refractr.verbose, 2, format!("Interval for {} has arrived, pulling", repos[i].cfg.from));
let _ = fast_forward(&repos[i].repo.path().to_string_lossy(), &repos[i].cfg.branches); let _ = fast_forward(&refractr, &repos[i].path, &repos[i].cfg.branches);
push_remotes(&refractr, &repos[i].cfg, &repos[i].repo, &repos[i].remotes); push_remotes(&refractr, &repos[i].cfg, &repos[i].repo, &repos[i].remotes);
} }
} }
@ -196,7 +211,7 @@ pub fn run(refractr: Refractr, cfgs: Vec<ConfigFile>) -> std::io::Result<()> {
Ok(repo) => repo, Ok(repo) => repo,
Err(_) => { Err(_) => {
eprintln!("refractr: warning: found existing repo at {}, attempting to use", repo_dir); eprintln!("refractr: warning: found existing repo at {}, attempting to use", repo_dir);
match fast_forward(repo_dir, &cfg.config.branches) { match fast_forward(&refractr, repo_dir, &cfg.config.branches) {
Ok(_) => if let Ok(repo) = Repository::open(Path::new(&repo_dir)) { Ok(_) => if let Ok(repo) = Repository::open(Path::new(&repo_dir)) {
repo repo
} else { } else {
@ -214,6 +229,7 @@ pub fn run(refractr: Refractr, cfgs: Vec<ConfigFile>) -> std::io::Result<()> {
if cfg.config.schedule.enabled { if cfg.config.schedule.enabled {
loop_repos.push(OpenedRepository { loop_repos.push(OpenedRepository {
repo, repo,
path: path_str,
remotes, remotes,
cfg: cfg.config cfg: cfg.config
}); });