Brijesh's Git Server — gitcc @ 6e9c8b27caead7a7c0ed64f134ef90979e06be3b

failed attempt at automatic git workflow

feat: push to remote with options for remotes
Brijesh Wawdhane brijesh@wawdhane.com
Mon, 25 Sep 2023 14:52:36 +0530
commit

6e9c8b27caead7a7c0ed64f134ef90979e06be3b

parent

989a633d27f73f5b5eb5af65a3ef109db8a1cf07

4 files changed, 60 insertions(+), 9 deletions(-)

jump to
M src/git_actions.rssrc/git_actions.rs

@@ -1,7 +1,9 @@

+use git2::Repository; +use std::env::current_dir; +use std::process::Command; + use crate::inputs::*; use crate::utils::*; - -use std::process::Command; pub fn make_commit(commit_type: String, commit_message: String) { Command::new("git")

@@ -20,10 +22,40 @@ let staging_choice: String = staging_options[staging_choice_number.unwrap()].to_string();

if staging_choice == "Yes" { Command::new("git") - .arg("add") - .arg("-A") - .output() - .expect("Failed to add files."); + .arg("add") + .arg("-A") + .output() + .expect("Failed to add files."); + } +} + +// function to push to remote +pub fn confirm_and_push_to_remote() { + let push_options: [&str; 2] = ["Yes", "No"]; + let push_choice_number: Option<usize> = select_option(&push_options); + let push_choice: String = push_options[push_choice_number.unwrap()].to_string(); + + if push_choice == "Yes" { + let repo: Repository = Repository::open(current_dir().unwrap()).unwrap(); + let remotes_string_array = repo.remotes().unwrap(); + + let mut remotes: Vec<String> = Vec::new(); + for remote in &remotes_string_array { + remotes.push(remote.unwrap().to_string()); + } + + let remote_choice_number: Option<usize> = select_option_string_vec(&remotes); + let remote_choice: String = remotes[remote_choice_number.unwrap()].to_string(); + + // TODO: ask user to choose branch + + println!("Pushing to {}...", remote_choice); + + Command::new("git") + .arg("push") + .arg(remote_choice) + .output() + .expect("Failed to push to remote."); } }

@@ -51,4 +83,4 @@ }

confirm_and_stage_all(); make_commit(commit_type, commit_message); -}+}
M src/inputs.rssrc/inputs.rs

@@ -7,7 +7,8 @@ ];

let commit_type_choice_number: Option<usize> = select_option(&commit_type_options); - let commit_type_choice: String = commit_type_options[commit_type_choice_number.unwrap()].to_string(); + let commit_type_choice: String = + commit_type_options[commit_type_choice_number.unwrap()].to_string(); return commit_type_choice; }
M src/main.rssrc/main.rs

@@ -24,9 +24,11 @@ make_initial_commit();

} else { let commit_type: String = get_commit_type(); let commit_message: String = get_commit_message(); - + confirm_and_stage_all(); make_commit(commit_type, commit_message); + + confirm_and_push_to_remote(); } } else { println!("Not a git repository");
M src/utils.rssrc/utils.rs

@@ -15,3 +15,19 @@ }

return selection; } + +pub fn select_option_string_vec(options: &Vec<String>) -> Option<usize> { + let selection = Select::with_theme(&ColorfulTheme::default()) + .with_prompt("Select an option:") + .default(0) + .items(&options) + .interact_opt() + .unwrap(); + + if selection.is_none() { + println!("Please choose an option"); + std::process::exit(1); + } + + return selection; +}