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
blob: 3358b11fe094db3194f769e6c501df96b5036661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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<Oid> {
    // 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
}