|
| 1 | +use std::env; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | + |
| 5 | +// Env vars that provide defaults for args |
| 6 | +const MAX_COMMITS_VAR: &str = "GIT_INSTAFIX_MAX_COMMITS"; |
| 7 | +const UPSTREAM_VAR: &str = "GIT_INSTAFIX_UPSTREAM"; |
| 8 | +const REQUIRE_NEWLINE_VAR: &str = "GIT_INSTAFIX_REQUIRE_NEWLINE"; |
| 9 | +const THEME_VAR: &str = "GIT_INSTAFIX_THEME"; |
| 10 | + |
| 11 | +// Other defaults |
| 12 | +pub(crate) const DEFAULT_UPSTREAM_BRANCHES: &[&str] = &["main", "master", "develop", "trunk"]; |
| 13 | +pub const DEFAULT_THEME: &str = "base16-ocean.dark"; |
| 14 | + |
| 15 | +#[derive(Parser, Debug)] |
| 16 | +#[clap( |
| 17 | + version, |
| 18 | + about = "Fix a commit in your history with your currently-staged changes", |
| 19 | + long_about = "Fix a commit in your history with your currently-staged changes |
| 20 | +
|
| 21 | +When run with no arguments this will: |
| 22 | +
|
| 23 | + * If you have no staged changes, ask if you'd like to stage all changes |
| 24 | + * Print a `diff --stat` of your currently staged changes |
| 25 | + * Provide a list of commits to fixup or amend going back to: |
| 26 | + * The merge-base of HEAD and the environment var GIT_INSTAFIX_UPSTREAM |
| 27 | + (if it is set) |
| 28 | + * HEAD's upstream |
| 29 | + * Fixup your selected commit with the staged changes |
| 30 | +", |
| 31 | + max_term_width = 100 |
| 32 | +)] |
| 33 | +struct Args { |
| 34 | + /// Change the commit message that you amend, instead of using the original commit message |
| 35 | + #[clap(short = 's', long, hide = true)] |
| 36 | + squash: Option<bool>, |
| 37 | + /// The maximum number of commits to show when looking for your merge point |
| 38 | + /// |
| 39 | + /// [gitconfig: instafix.max-commits] |
| 40 | + #[clap(short = 'm', long = "max-commits", env = MAX_COMMITS_VAR)] |
| 41 | + max_commits: Option<usize>, |
| 42 | + |
| 43 | + /// Specify a commit to ammend by the subject line of the commit |
| 44 | + #[clap(short = 'P', long)] |
| 45 | + commit_message_pattern: Option<String>, |
| 46 | + |
| 47 | + /// The branch to not go past when looking for your merge point |
| 48 | + /// |
| 49 | + /// [gitconfig: instafix.default-upstream-branch] |
| 50 | + #[clap(short = 'u', long, env = UPSTREAM_VAR)] |
| 51 | + default_upstream_branch: Option<String>, |
| 52 | + |
| 53 | + /// Require a newline when confirming y/n questions |
| 54 | + /// |
| 55 | + /// [gitconfig: instafix.require-newline] |
| 56 | + #[clap(long, env = REQUIRE_NEWLINE_VAR)] |
| 57 | + require_newline: Option<bool>, |
| 58 | + |
| 59 | + /// Show the possible color themes for output |
| 60 | + #[clap(long)] |
| 61 | + help_themes: bool, |
| 62 | + |
| 63 | + /// Use this theme |
| 64 | + #[clap(long, env = THEME_VAR)] |
| 65 | + theme: Option<String>, |
| 66 | +} |
| 67 | + |
| 68 | +/// Fully configured arguments after loading from env and gitconfig |
| 69 | +pub struct Config { |
| 70 | + /// Change the commit message that you amend, instead of using the original commit message |
| 71 | + pub squash: bool, |
| 72 | + /// The maximum number of commits to show when looking for your merge point |
| 73 | + pub max_commits: usize, |
| 74 | + /// Specify a commit to ammend by the subject line of the commit |
| 75 | + pub commit_message_pattern: Option<String>, |
| 76 | + pub default_upstream_branch: Option<String>, |
| 77 | + /// Require a newline when confirming y/n questions |
| 78 | + pub require_newline: bool, |
| 79 | + /// User requested info about themes |
| 80 | + pub help_themes: bool, |
| 81 | + /// Which theme to use |
| 82 | + pub theme: String, |
| 83 | +} |
| 84 | + |
| 85 | +/// Create a Config based on arguments and env vars |
| 86 | +pub fn load_config_from_args_env_git() -> Config { |
| 87 | + let mut args = Args::parse(); |
| 88 | + if env::args().next().unwrap().ends_with("squash") { |
| 89 | + args.squash = Some(true) |
| 90 | + } |
| 91 | + args_to_config_using_git_config(args).unwrap() |
| 92 | +} |
| 93 | + |
| 94 | +fn args_to_config_using_git_config(args: Args) -> Result<Config, anyhow::Error> { |
| 95 | + let mut cfg = git2::Config::open_default()?; |
| 96 | + let repo = git2::Repository::discover(".")?; |
| 97 | + cfg.add_file(&repo.path().join("config"), git2::ConfigLevel::Local, false)?; |
| 98 | + Ok(Config { |
| 99 | + squash: args |
| 100 | + .squash |
| 101 | + .unwrap_or_else(|| cfg.get_bool("instafix.squash").unwrap_or(false)), |
| 102 | + max_commits: args |
| 103 | + .max_commits |
| 104 | + .unwrap_or_else(|| cfg.get_i32("instafix.max-commits").unwrap_or(15) as usize), |
| 105 | + commit_message_pattern: args.commit_message_pattern, |
| 106 | + default_upstream_branch: args |
| 107 | + .default_upstream_branch |
| 108 | + .or_else(|| cfg.get_string("instafix.default-upstream-branch").ok()), |
| 109 | + require_newline: args |
| 110 | + .require_newline |
| 111 | + .unwrap_or_else(|| cfg.get_bool("instafix.require-newline").unwrap_or(false)), |
| 112 | + help_themes: args.help_themes, |
| 113 | + theme: args.theme.unwrap_or_else(|| { |
| 114 | + cfg.get_string("instafix.theme") |
| 115 | + .unwrap_or_else(|_| DEFAULT_THEME.to_string()) |
| 116 | + }), |
| 117 | + }) |
| 118 | +} |
0 commit comments