@@ -26,7 +26,7 @@ pub struct Args {
26
26
27
27
/// Read last commit from the specified file or fallbacks to ./.git/COMMIT_EDITMSG
28
28
#[ arg( short = 'e' , long) ]
29
- pub edit : bool ,
29
+ pub edit : Option < String > ,
30
30
31
31
/// Lower end of the commit range to lint
32
32
#[ arg( short = 'f' , long) ]
@@ -53,10 +53,12 @@ impl Args {
53
53
pub fn read ( & self ) -> Result < Vec < Message > , Error > {
54
54
// Check first whether or not the --edit option was supplied. When running from tooling such as
55
55
// `pre-commit`, stdin exists, so this needs to come first.
56
- if self . edit {
57
- let msg = std:: fs:: read_to_string ( "./.git/COMMIT_EDITMSG" )
58
- . expect ( "Failed to read './.git/COMMIT_EDITMSG'" ) ;
59
- return Ok ( vec ! [ Message :: new( msg) ] ) ;
56
+ if let Some ( edit) = self . edit . as_deref ( ) {
57
+ if edit != "false" {
58
+ let msg = std:: fs:: read_to_string ( edit)
59
+ . expect ( format ! ( "Failed to read commit message from {}" , edit) . as_str ( ) ) ;
60
+ return Ok ( vec ! [ Message :: new( msg) ] ) ;
61
+ }
60
62
}
61
63
62
64
// Otherwise, check for stdin and use the incoming text buffer from there if so.
@@ -68,18 +70,30 @@ impl Args {
68
70
return Ok ( vec ! [ Message :: new( buffer) ] ) ;
69
71
}
70
72
71
- // And if none of the above, we're expecting to be reading directly from Git...
72
- let config = ReadCommitMessageOptions {
73
- from : self . from . clone ( ) ,
74
- path : self . cwd . clone ( ) ,
75
- to : self . to . clone ( ) ,
76
- } ;
73
+ if self . from . is_some ( ) || self . to . is_some ( ) {
74
+ // Reading directly from Git if from or to is specified.
75
+ let config = ReadCommitMessageOptions {
76
+ from : self . from . clone ( ) ,
77
+ path : self . cwd . clone ( ) ,
78
+ to : self . to . clone ( ) ,
79
+ } ;
77
80
78
- let messages = git:: read ( config)
79
- . iter ( )
80
- . map ( |s| Message :: new ( s. to_string ( ) ) )
81
- . collect ( ) ;
81
+ let messages = git:: read ( config)
82
+ . iter ( )
83
+ . map ( |s| Message :: new ( s. to_string ( ) ) )
84
+ . collect ( ) ;
82
85
83
- Ok ( messages)
86
+ return Ok ( messages) ;
87
+ }
88
+
89
+ let default_path = std:: path:: PathBuf :: from ( ".git" ) . join ( "COMMIT_EDITMSG" ) ;
90
+ let msg = std:: fs:: read_to_string ( & default_path) . expect (
91
+ format ! (
92
+ "Failed to read commit message from {}" ,
93
+ default_path. display( )
94
+ )
95
+ . as_str ( ) ,
96
+ ) ;
97
+ Ok ( vec ! [ Message :: new( msg) ] )
84
98
}
85
99
}
0 commit comments