upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/funcs/create_local_branch_from_user_input.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/create_local_branch_from_user_input.rs
parent5c5feaa732363e32e2a980a887fa42b4394b1a5e (diff)
v0.0.1-alpha funcs
Diffstat (limited to 'src/funcs/create_local_branch_from_user_input.rs')
-rw-r--r--src/funcs/create_local_branch_from_user_input.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/funcs/create_local_branch_from_user_input.rs b/src/funcs/create_local_branch_from_user_input.rs
new file mode 100644
index 0000000..94abb62
--- /dev/null
+++ b/src/funcs/create_local_branch_from_user_input.rs
@@ -0,0 +1,57 @@
1use std::path::PathBuf;
2
3use dialoguer::{Input, theme::ColorfulTheme};
4use git2::{Repository, Oid};
5
6use crate::repo_config::RepoConfig;
7
8use super::checkout_branch::checkout_branch;
9
10
11/// prompts user for name of local branch, creates the branch (checking if it is valid, if not looping) and returns it.
12pub fn create_local_branch_from_user_input(
13 repo_dir_path: &PathBuf,
14 git_repo: &Repository,
15 suggestion:&Option<String>,
16 commit_id:&String,
17 branch_id: &String,
18) -> String {
19
20 let target_commit = git_repo.find_commit(
21 Oid::from_str(
22 commit_id
23 )
24 .expect("commit_id supplied to be a valid Oid")
25 )
26 .expect("commit_id supplied exists in git repository");
27
28 loop {
29 let response: String = Input::with_theme(&ColorfulTheme::default())
30 .with_prompt("local branch name")
31 .with_initial_text(match &suggestion {
32 None => "".to_string(),
33 Some(s) => s.to_string(),
34 })
35 .report(true)
36 .interact_text()
37 .unwrap();
38 match git_repo.branch(
39 &response,
40 &target_commit,
41 false,
42 ) {
43 Ok(branch) => {
44 // check out branch
45 checkout_branch(git_repo, branch);
46 // set mapping
47 let mut repo_config = RepoConfig::open(repo_dir_path);
48 repo_config.set_mapping(&response, branch_id);
49 break response;
50 },
51 Err(_) => {
52 println!("not a valid nevent, note or hex string. try again. (or a local branch with that name exists)");
53 continue
54 },
55 }
56 }
57}