@@ -16,8 +16,9 @@ use std::env;
16
16
17
17
use clap:: Parser ;
18
18
19
+ const MAX_COMMITS_VAR : & str = "GIT_INSTAFIX_MAX_COMMITS" ;
19
20
const UPSTREAM_VAR : & str = "GIT_INSTAFIX_UPSTREAM" ;
20
- const REQUIRE_NEWLINE : & str = "GIT_INSTAFIX_REQUIRE_NEWLINE" ;
21
+ const REQUIRE_NEWLINE_VAR : & str = "GIT_INSTAFIX_REQUIRE_NEWLINE" ;
21
22
22
23
#[ derive( Parser , Debug ) ]
23
24
#[ clap(
@@ -39,36 +40,38 @@ When run with no arguments this will:
39
40
) ]
40
41
struct Args {
41
42
/// Change the commit message that you amend, instead of using the original commit message
42
- #[ clap( short = 's' , long = "squash" ) ]
43
- squash : bool ,
43
+ #[ clap( short = 's' , long, hide = true ) ]
44
+ squash : Option < bool > ,
44
45
/// The maximum number of commits to show when looking for your merge point
45
- #[ clap( short = 'm' , long = "max-commits" , default_value = "15" ) ]
46
- max_commits : usize ,
46
+ ///
47
+ /// [gitconfig: instafix.max-commits]
48
+ #[ clap( short = 'm' , long = "max-commits" , env = MAX_COMMITS_VAR ) ]
49
+ max_commits : Option < usize > ,
47
50
48
51
/// Specify a commit to ammend by the subject line of the commit
49
52
#[ clap( short = 'P' , long) ]
50
53
commit_message_pattern : Option < String > ,
51
54
55
+ /// The branch to not go past when looking for your merge point
56
+ ///
57
+ /// [gitconfig: instafix.default-upstream-branch]
52
58
#[ clap( short = 'u' , long, env = UPSTREAM_VAR ) ]
53
59
default_upstream_branch : Option < String > ,
54
60
55
61
/// Require a newline when confirming y/n questions
56
- #[ clap( long, env = REQUIRE_NEWLINE ) ]
57
- require_newline : bool ,
62
+ ///
63
+ /// [gitconfig: instafix.require-newline]
64
+ #[ clap( long, env = REQUIRE_NEWLINE_VAR ) ]
65
+ require_newline : Option < bool > ,
58
66
}
59
67
60
68
fn main ( ) {
61
69
let mut args = Args :: parse ( ) ;
62
70
if env:: args ( ) . next ( ) . unwrap ( ) . ends_with ( "squash" ) {
63
- args. squash = true
71
+ args. squash = Some ( true )
64
72
}
65
- if let Err ( e) = git_instafix:: instafix (
66
- args. squash ,
67
- args. max_commits ,
68
- args. commit_message_pattern ,
69
- args. default_upstream_branch . as_deref ( ) ,
70
- args. require_newline ,
71
- ) {
73
+ let config = args_to_config_using_git_config ( args) . unwrap ( ) ;
74
+ if let Err ( e) = git_instafix:: instafix ( config) {
72
75
// An empty message means don't display any error message
73
76
let msg = e. to_string ( ) ;
74
77
if !msg. is_empty ( ) {
@@ -81,3 +84,24 @@ fn main() {
81
84
std:: process:: exit ( 1 ) ;
82
85
}
83
86
}
87
+
88
+ fn args_to_config_using_git_config ( args : Args ) -> Result < git_instafix:: Config , anyhow:: Error > {
89
+ let mut cfg = git2:: Config :: open_default ( ) ?;
90
+ let repo = git2:: Repository :: discover ( "." ) ?;
91
+ cfg. add_file ( & repo. path ( ) . join ( "config" ) , git2:: ConfigLevel :: Local , false ) ?;
92
+ Ok ( git_instafix:: Config {
93
+ squash : args
94
+ . squash
95
+ . unwrap_or_else ( || cfg. get_bool ( "instafix.squash" ) . unwrap_or ( false ) ) ,
96
+ max_commits : args
97
+ . max_commits
98
+ . unwrap_or_else ( || cfg. get_i32 ( "instafix.max-commits" ) . unwrap_or ( 15 ) as usize ) ,
99
+ commit_message_pattern : args. commit_message_pattern ,
100
+ default_upstream_branch : args
101
+ . default_upstream_branch
102
+ . or_else ( || cfg. get_string ( "instafix.default-upstream-branch" ) . ok ( ) ) ,
103
+ require_newline : args
104
+ . require_newline
105
+ . unwrap_or_else ( || cfg. get_bool ( "instafix.require-newline" ) . unwrap_or ( false ) ) ,
106
+ } )
107
+ }
0 commit comments