feat(backend) update (unfinished)

This commit is contained in:
lumither committed 2025-02-22 00:00:13 -08:00
1 parent 84ec754c65
commit 8358a8ae33
20 files changed
+131 -317

No files matched your search

+9 -38
View File
@@ -16,6 +16,7 @@ pub struct FileDelta {
pub status: Delta,
}
#[allow(dead_code)]
fn do_fetch<'a>(
repo: &'a Repository,
refs: &[&str],
@@ -23,8 +24,6 @@ fn do_fetch<'a>(
) -> Result<AnnotatedCommit<'a>, git2::Error> {
let mut fetch_option = FetchOptions::new();
// Always fetch all tags.
// Perform a download and also update tips
fetch_option.download_tags(AutotagOption::All);
remote.fetch(refs, Some(&mut fetch_option), None)?;
@@ -32,6 +31,7 @@ fn do_fetch<'a>(
repo.reference_to_annotated_commit(&fetch_head)
}
#[allow(dead_code)]
fn fast_forward(
repo: &Repository,
lb: &mut Reference,
@@ -45,16 +45,11 @@ fn fast_forward(
// println!("{}", msg);
lb.set_target(rc.id(), &msg)?;
repo.set_head(&name)?;
repo.checkout_head(Some(
build::CheckoutBuilder::default()
// For some reason, the force is required to make the working directory actually get updated
// I suspect we should be adding some logic to handle dirty working directory states,
// but this is just an example so maybe not.
.force(),
))?;
repo.checkout_head(Some(build::CheckoutBuilder::default().force()))?;
Ok(())
}
#[allow(dead_code)]
fn normal_merge(
repo: &Repository,
local: &AnnotatedCommit,
@@ -73,13 +68,12 @@ fn normal_merge(
return Ok(());
}
let result_tree = repo.find_tree(idx.write_tree_to(repo)?)?;
// now create the merge commit
let msg = format!("merge({}) -> {}", remote.id(), local.id());
let sig = repo.signature()?;
let local_commit = repo.find_commit(local.id())?;
let remote_commit = repo.find_commit(remote.id())?;
// Do our merge commit and set the current branch head to that commit.
let _merge_commit = repo.commit(
Some("HEAD"),
&sig,
@@ -88,34 +82,26 @@ fn normal_merge(
&result_tree,
&[&local_commit, &remote_commit],
)?;
// Set working tree to match head.
repo.checkout_head(None)?;
Ok(())
}
#[allow(dead_code)]
fn do_merge<'a>(
repo: &'a Repository,
remote_branch: &str,
fetch_commit: AnnotatedCommit<'a>,
) -> Result<Vec<FileDelta>, git2::Error> {
// 1. do a merge analysis
let analysis = repo.merge_analysis(&[&fetch_commit])?;
let head_commit = repo.reference_to_annotated_commit(&repo.head()?)?;
// 2. Do the appropriate merge
if analysis.0.is_fast_forward() {
// println!("Doing a fast-forward");
// do a fast-forward
let ref_name = format!("refs/heads/{}", remote_branch);
match repo.find_reference(&ref_name) {
Ok(mut r) => {
fast_forward(repo, &mut r, &fetch_commit)?;
}
Err(_) => {
// The branch doesn't exist, so set the reference to the
// commit directly. Usually this is because you are pulling
// into an empty repository.
repo.reference(
&ref_name,
fetch_commit.id(),
@@ -132,17 +118,12 @@ fn do_merge<'a>(
}
};
} else if analysis.0.is_normal() {
// do a normal merge
normal_merge(repo, &head_commit, &fetch_commit)?;
}
// else {
// // already up-to-date
// println!("Nothing to do...");
// }
calculate_diff(repo, &head_commit, &fetch_commit)
}
#[allow(dead_code)]
fn calculate_diff(
repo: &Repository,
local: &AnnotatedCommit,
@@ -156,23 +137,12 @@ fn calculate_diff(
if let Err(e) = diff.foreach(
&mut |d, _| {
// let delta = d.status();
//
// let n_path = d.new_file().path();
// let o_path = d.old_file().path();
//
// if let Some(path) = n_path {
// updated_files.insert(path.to_owned());
// }
// if let Some(path) = o_path {
// updated_files.insert(path.to_owned());
// }
updated_files.push(FileDelta {
old_path: d.old_file().path().map(PathBuf::from),
new_path: d.new_file().path().map(PathBuf::from),
status: d.status(),
});
true // Continue
true
},
None,
None,
@@ -184,6 +154,7 @@ fn calculate_diff(
Ok(updated_files.into_iter().collect())
}
#[allow(dead_code)]
pub fn sync(
remote_name: &str,
remote_branch: &str,