upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/funcs/find_commits_ahead.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:14:47 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2023-05-21 11:14:47 +0000
commit0067804cc00e94ce2b7043e67f9ff50968525479 (patch)
tree2accdc6d4e9b73df4f20499238ec24f24a52a1b8 /src/funcs/find_commits_ahead.rs
parent5c5feaa732363e32e2a980a887fa42b4394b1a5e (diff)
v0.0.1-alpha funcs
Diffstat (limited to 'src/funcs/find_commits_ahead.rs')
-rw-r--r--src/funcs/find_commits_ahead.rs42
1 files changed, 42 insertions, 0 deletions
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 @@
1use std::path::PathBuf;
2
3use git2::{Repository, Oid};
4
5pub fn find_commits_ahead (
6 git_repo: &Repository,
7 repo_dir_path: &PathBuf,
8 branch_name: &String,
9
10) -> Vec<Oid> {
11 // get revwalk of commits
12 let mut revwalk = git_repo.revwalk()
13 .expect("revwalk not to error");
14 // using the specified branch
15 match revwalk.push_glob(&branch_name) {
16 Ok(_) => (),
17 // errors when there are no commits
18 Err(_) => { return vec![]; }
19 }
20
21 // find commit that need pushing
22 let mut revwalk = git_repo.revwalk()
23 .expect("git_repo.revwalk() to not error");
24 revwalk.push_head()
25 .expect("revwalk.push_head not to error. already checked for some commits. headless?");
26
27 let mut new_commits = vec![];
28
29 for oid in revwalk {
30 // whatever branch we are on, we are only interested in returning how many unpublished commits we are ahead.
31 if repo_dir_path.join(".ngit").join(format!(
32 "patches/{}.json",
33 oid.as_ref().expect("oid to refernce commits").clone(),
34 )).exists() {
35 break;
36 }
37 new_commits.push(oid.expect("oid to refernce commits"));
38 }
39 // most often used ancestor first
40 new_commits.reverse();
41 new_commits
42}