diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:14:47 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:14:47 +0000 |
| commit | 0067804cc00e94ce2b7043e67f9ff50968525479 (patch) | |
| tree | 2accdc6d4e9b73df4f20499238ec24f24a52a1b8 /src/funcs/checkout_branch.rs | |
| parent | 5c5feaa732363e32e2a980a887fa42b4394b1a5e (diff) | |
v0.0.1-alpha funcs
Diffstat (limited to 'src/funcs/checkout_branch.rs')
| -rw-r--r-- | src/funcs/checkout_branch.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/funcs/checkout_branch.rs b/src/funcs/checkout_branch.rs new file mode 100644 index 0000000..df08ce8 --- /dev/null +++ b/src/funcs/checkout_branch.rs | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | use git2::{Branch, Repository}; | ||
| 2 | |||
| 3 | pub fn checkout_branch( | ||
| 4 | git_repo: &Repository, | ||
| 5 | branch: Branch, | ||
| 6 | ) { | ||
| 7 | // checkout branch | ||
| 8 | let (object, reference) = git_repo.revparse_ext( | ||
| 9 | branch.name() | ||
| 10 | .expect("valid name not to error") | ||
| 11 | .expect("valid name name to exist") | ||
| 12 | ) | ||
| 13 | .expect("object to be located from branch name"); | ||
| 14 | match git_repo.checkout_tree(&object, None) { | ||
| 15 | Ok(_) => (), | ||
| 16 | Err(err) => { | ||
| 17 | panic!("You cannot checkout branch because {}", err.message()); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | // set head to branch | ||
| 21 | match reference { | ||
| 22 | Some(gref) => git_repo.set_head(gref.name().unwrap()), | ||
| 23 | None => git_repo.set_head_detached(object.id()), | ||
| 24 | } | ||
| 25 | .expect("succesfully set head"); | ||
| 26 | } | ||
| 27 | |||
| 28 | pub fn checkout_branch_from_name( | ||
| 29 | git_repo: &Repository, | ||
| 30 | branch_name: &String, | ||
| 31 | ) { | ||
| 32 | // checkout branch | ||
| 33 | let (object, reference) = git_repo.revparse_ext(branch_name) | ||
| 34 | .expect("object to be located from branch name"); | ||
| 35 | match git_repo.checkout_tree(&object, None) { | ||
| 36 | Ok(_) => (), | ||
| 37 | Err(err) => { | ||
| 38 | panic!("You cannot checkout branch because {}", err.message()); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | // set head to branch | ||
| 42 | match reference { | ||
| 43 | Some(gref) => git_repo.set_head(gref.name().unwrap()), | ||
| 44 | None => git_repo.set_head_detached(object.id()), | ||
| 45 | } | ||
| 46 | .expect("succesfully set head"); | ||
| 47 | } \ No newline at end of file | ||