-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
125 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
mod commands; | ||
mod process; | ||
mod prompts; | ||
mod utils; | ||
|
||
pub use commands::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,47 @@ | ||
pub fn include() {} | ||
use std::{fs, path::PathBuf}; | ||
|
||
use anyhow::{Ok, Result}; | ||
use dialoguer::Input; | ||
use ini::Ini; | ||
|
||
use crate::{run_piped_command, select_git_user_config, GitUserConfig, GlobalDir}; | ||
|
||
const GIT_CONFIG_DIR: &str = "git-configs"; | ||
|
||
pub fn include() -> Result<()> { | ||
// input the folder path | ||
let folder_path: String = Input::new().with_prompt("Enter the folder path").interact()?; | ||
// Select the git user config | ||
let git_user_config = select_git_user_config()?; | ||
|
||
let git_config_file = create_git_config_file(&git_user_config)?; | ||
|
||
add_include_git_config_to_global(&folder_path, git_config_file)?; | ||
|
||
println!("Git user config included successfully!"); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn create_git_config_file(git_user_config: &GitUserConfig) -> Result<PathBuf> { | ||
let global_dir = GlobalDir::new(); | ||
|
||
let git_config_path = global_dir | ||
.0 | ||
.join(GIT_CONFIG_DIR) | ||
.join(format!("{}-config", git_user_config.config_id)); | ||
fs::create_dir_all(git_config_path.parent().expect("failed to get the parent path"))?; | ||
let mut config = Ini::new(); | ||
config | ||
.with_section(Some("user")) | ||
.set("name", &git_user_config.name) | ||
.set("email", &git_user_config.email); | ||
config.write_to_file(&git_config_path)?; | ||
Ok(git_config_path) | ||
} | ||
|
||
fn add_include_git_config_to_global(folder_path: &str, git_config_path: PathBuf) -> Result<()> { | ||
let config_key = format!("includeIf.\"gitdir:{}\".path", git_config_path.display()); | ||
run_piped_command(vec!["git", "config", "--global", &config_key, folder_path])?; | ||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod get_git_user_config_id; | ||
mod git_url; | ||
mod global_config; | ||
mod select_git_user_config; | ||
mod shell; | ||
|
||
pub use get_git_user_config_id::get_git_user_config_id; | ||
pub use git_url::GitUrl; | ||
pub use global_config::GlobalConfig; | ||
pub use global_config::{GlobalConfig, GlobalDir}; | ||
pub use select_git_user_config::select_git_user_config; | ||
pub use shell::run_piped_command; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::GitUserConfig; | ||
use anyhow::{Ok, Result}; | ||
use console::style; | ||
use dialoguer::Select; | ||
|
||
pub fn select_git_user_config() -> Result<GitUserConfig> { | ||
let git_user_configs = GitUserConfig::get_all()?; | ||
let items = git_user_configs | ||
.iter() | ||
.map(|config| { | ||
format!( | ||
"{}: {}<{}>", | ||
style(&config.config_id).bold(), | ||
&config.name, | ||
&config.email | ||
) | ||
}) | ||
.collect::<Vec<String>>(); | ||
|
||
if items.is_empty() { | ||
return Err(anyhow::anyhow!( | ||
"No git user config found. Please add one by running `gitez user-config add`" | ||
)); | ||
} | ||
|
||
let selection = Select::new() | ||
.with_prompt("choose one of your git user config") | ||
.items(&items) | ||
.default(0) | ||
.interact()?; | ||
Ok(git_user_configs[selection].to_owned()) | ||
} |