File tree 2 files changed +26
-10
lines changed
2 files changed +26
-10
lines changed Original file line number Diff line number Diff line change @@ -83,3 +83,15 @@ impl CommitPattern {
83
83
]
84
84
}
85
85
}
86
+
87
+ impl Default for CommitPattern {
88
+ fn default ( ) -> Self {
89
+ Self {
90
+ config : Config :: default ( ) ,
91
+ commit_types : Self :: commit_types ( ) ,
92
+ commit_scopes : Self :: commit_scopes ( ) ,
93
+ skip_commit : vec ! [ ] ,
94
+ msg : Messages :: default ( ) ,
95
+ }
96
+ }
97
+ }
Original file line number Diff line number Diff line change 2
2
mod tests;
3
3
4
4
use anyhow:: { anyhow, Context , Result } ;
5
- use std:: fs;
6
- use std:: path:: Path ;
5
+ use std:: fs:: File ;
7
6
use std:: path:: PathBuf ;
8
7
9
8
use crate :: commit_pattern:: CommitPattern ;
10
9
11
- fn get_config_path_content ( config_path : impl AsRef < Path > ) -> Result < String > {
12
- let content = fs:: read_to_string ( config_path) ?;
13
- Ok ( content)
14
- }
15
-
16
10
fn select_custom_config_path ( config : Option < PathBuf > ) -> Result < PathBuf > {
17
11
match config {
18
12
Some ( config_path) => {
@@ -62,7 +56,17 @@ fn get_config_path() -> Result<PathBuf> {
62
56
63
57
pub fn get_pattern ( config_path : Option < PathBuf > ) -> Result < CommitPattern > {
64
58
let selected_config_path = select_custom_config_path ( config_path) ?;
65
- let pattern_str =
66
- get_config_path_content ( selected_config_path) . unwrap_or_else ( |_| "{}" . to_owned ( ) ) ;
67
- serde_json:: from_str ( & pattern_str) . context ( "Failed to parse commit pattern from file" )
59
+ if selected_config_path. exists ( ) {
60
+ let file = File :: open ( & selected_config_path) . context ( format ! (
61
+ "Could not open config file: {}" ,
62
+ selected_config_path. display( )
63
+ ) ) ?;
64
+ let reader = std:: io:: BufReader :: new ( file) ;
65
+ Ok ( serde_json:: from_reader ( reader) . context ( format ! (
66
+ "Could not parse config file: {}" ,
67
+ selected_config_path. display( )
68
+ ) ) ?)
69
+ } else {
70
+ Ok ( CommitPattern :: default ( ) )
71
+ }
68
72
}
You can’t perform that action at this time.
0 commit comments