upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/funcs/checkout_branch.rs
blob: df08ce8ebf8335ccabe37ab912358cf27f27c0df (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
43
44
45
46
47
use git2::{Branch, Repository};

pub fn checkout_branch(
    git_repo: &Repository,
    branch: Branch,
) {
    // checkout branch
    let (object, reference) = git_repo.revparse_ext(
        branch.name()
            .expect("valid name not to error")
            .expect("valid name name to exist")
    )
        .expect("object to be located from branch name");
    match git_repo.checkout_tree(&object, None) {
            Ok(_) => (),
            Err(err) => {
                panic!("You cannot checkout branch because {}", err.message());
            }
    }
    // set head to branch
    match reference {
        Some(gref) => git_repo.set_head(gref.name().unwrap()),
        None => git_repo.set_head_detached(object.id()),
    }
        .expect("succesfully set head");    
}

pub fn checkout_branch_from_name(
    git_repo: &Repository,
    branch_name: &String,
) {
        // checkout branch
        let (object, reference) = git_repo.revparse_ext(branch_name)
            .expect("object to be located from branch name");
        match git_repo.checkout_tree(&object, None) {
            Ok(_) => (),
            Err(err) => {
                panic!("You cannot checkout branch because {}", err.message());
            }
        }
        // set head to branch
        match reference {
            Some(gref) => git_repo.set_head(gref.name().unwrap()),
            None => git_repo.set_head_detached(object.id()),
        }
            .expect("succesfully set head");    
}