From 0067804cc00e94ce2b7043e67f9ff50968525479 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 21 May 2023 11:14:47 +0000 Subject: v0.0.1-alpha funcs --- src/funcs/find_commits_ahead.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/funcs/find_commits_ahead.rs (limited to 'src/funcs/find_commits_ahead.rs') diff --git a/src/funcs/find_commits_ahead.rs b/src/funcs/find_commits_ahead.rs new file mode 100644 index 0000000..3358b11 --- /dev/null +++ b/src/funcs/find_commits_ahead.rs @@ -0,0 +1,42 @@ +use std::path::PathBuf; + +use git2::{Repository, Oid}; + +pub fn find_commits_ahead ( + git_repo: &Repository, + repo_dir_path: &PathBuf, + branch_name: &String, + +) -> Vec { + // get revwalk of commits + let mut revwalk = git_repo.revwalk() + .expect("revwalk not to error"); + // using the specified branch + match revwalk.push_glob(&branch_name) { + Ok(_) => (), + // errors when there are no commits + Err(_) => { return vec![]; } + } + + // find commit that need pushing + let mut revwalk = git_repo.revwalk() + .expect("git_repo.revwalk() to not error"); + revwalk.push_head() + .expect("revwalk.push_head not to error. already checked for some commits. headless?"); + + let mut new_commits = vec![]; + + for oid in revwalk { + // whatever branch we are on, we are only interested in returning how many unpublished commits we are ahead. + if repo_dir_path.join(".ngit").join(format!( + "patches/{}.json", + oid.as_ref().expect("oid to refernce commits").clone(), + )).exists() { + break; + } + new_commits.push(oid.expect("oid to refernce commits")); + } + // most often used ancestor first + new_commits.reverse(); + new_commits +} -- cgit v1.2.3