From 0067804cc00e94ce2b7043e67f9ff50968525479 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 21 May 2023 11:14:47 +0000 Subject: v0.0.1-alpha funcs --- src/funcs/create_local_branch_from_user_input.rs | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/funcs/create_local_branch_from_user_input.rs (limited to 'src/funcs/create_local_branch_from_user_input.rs') 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 @@ +use std::path::PathBuf; + +use dialoguer::{Input, theme::ColorfulTheme}; +use git2::{Repository, Oid}; + +use crate::repo_config::RepoConfig; + +use super::checkout_branch::checkout_branch; + + +/// prompts user for name of local branch, creates the branch (checking if it is valid, if not looping) and returns it. +pub fn create_local_branch_from_user_input( + repo_dir_path: &PathBuf, + git_repo: &Repository, + suggestion:&Option, + commit_id:&String, + branch_id: &String, +) -> String { + + let target_commit = git_repo.find_commit( + Oid::from_str( + commit_id + ) + .expect("commit_id supplied to be a valid Oid") + ) + .expect("commit_id supplied exists in git repository"); + + loop { + let response: String = Input::with_theme(&ColorfulTheme::default()) + .with_prompt("local branch name") + .with_initial_text(match &suggestion { + None => "".to_string(), + Some(s) => s.to_string(), + }) + .report(true) + .interact_text() + .unwrap(); + match git_repo.branch( + &response, + &target_commit, + false, + ) { + Ok(branch) => { + // check out branch + checkout_branch(git_repo, branch); + // set mapping + let mut repo_config = RepoConfig::open(repo_dir_path); + repo_config.set_mapping(&response, branch_id); + break response; + }, + Err(_) => { + println!("not a valid nevent, note or hex string. try again. (or a local branch with that name exists)"); + continue + }, + } + } +} -- cgit v1.2.3