@@ -42,31 +42,6 @@ const COMMAND_ERROR: i32 = 2;
42
42
/// Process exit code for when a command halts due to merge conflicts.
43
43
const CONFLICT_ERROR : i32 = 3 ;
44
44
45
- /// Initialize [`git2`] library.
46
- ///
47
- /// [`git2`] library by default fails any operation if it encounters any extensions configured in a repo.
48
- /// StackedGit is using [`git2`] library as an interface to object database and for looking alias lookup from git configuration.
49
- ///
50
- /// As a workaround until `worktreeconfig` extension is supported upstream, add it to whitelisted extensions.
51
- ///
52
- /// # Safety
53
- /// * Modifying whitelisted extensions in [`git2`] is like modifying a static mut variable (unsafe).
54
- /// * This function must be called at initialization time (before git2 library usage) and should be called only once.
55
- unsafe fn init_libgit2 ( ) -> Result < ( ) > {
56
- git2:: opts:: set_extensions ( & [ "worktreeconfig" ] )
57
- . context ( "Failed to set worktreeconfig extension" )
58
- }
59
-
60
- #[ cfg( test) ]
61
- mod test {
62
- use super :: * ;
63
-
64
- #[ test]
65
- fn test_me ( ) {
66
- unsafe { init_libgit2 ( ) } . expect ( "success" ) ;
67
- }
68
- }
69
-
70
45
/// Create base [`clap::Command`] instance.
71
46
///
72
47
/// The base [`clap::Command`] returned by this function is intended to be supplemented
@@ -193,10 +168,10 @@ fn main() -> ! {
193
168
} else if matches. get_flag ( "help-option" ) {
194
169
full_app_help ( argv, None , color_choice)
195
170
} else if let Some ( ( sub_name, sub_matches) ) = matches. subcommand ( ) {
196
- // Initialize git2 library only in case a subcommand is matched.
197
- // The operation to initialize LibGit2 with extension is quite fast but still
198
- // takes a non-negligible amount of time .
199
- unsafe { init_libgit2 ( ) } . expect ( "Failed libgit2 init " ) ;
171
+ // Initialize git2 library only in case a subcommand is matched. This limits
172
+ // the cost of libgit2 initialization to code paths that will actually use
173
+ // libgit2 .
174
+ unsafe { init_libgit2_extensions ( ) } . expect ( "can set libgit2 extensions " ) ;
200
175
201
176
// If the name matches any known subcommands, then only the Command for that
202
177
// particular command is constructed and the costs of searching for aliases
@@ -257,6 +232,34 @@ fn main() -> ! {
257
232
}
258
233
}
259
234
235
+ /// Initialize [`git2`] library extensions.
236
+ ///
237
+ /// By default, [`git2`] fails any operation if it encounters any unknown extensions
238
+ /// configured in a repo. StGit is uses [`git2`] library as an interface to the git
239
+ /// object database and for interrogating the git configuration.
240
+ ///
241
+ /// As a workaround until `worktreeconfig` extension is supported upstream, add it to
242
+ /// whitelisted extensions.
243
+ ///
244
+ /// # Safety
245
+ /// * Modifying whitelisted extensions in [`git2`] is like modifying a static mut
246
+ /// variable (unsafe).
247
+ /// * This function must be called at initialization time (before git2 library usage)
248
+ /// and should be called only once.
249
+ unsafe fn init_libgit2_extensions ( ) -> Result < ( ) > {
250
+ git2:: opts:: set_extensions ( & [ "worktreeconfig" ] ) . context ( "set `worktreeconfig` extension" )
251
+ }
252
+
253
+ #[ cfg( test) ]
254
+ mod test {
255
+ use super :: * ;
256
+
257
+ #[ test]
258
+ fn test_libgit2_extensions ( ) {
259
+ unsafe { init_libgit2_extensions ( ) } . unwrap ( ) ;
260
+ }
261
+ }
262
+
260
263
/// Exit the program based on the provided [`Result`].
261
264
///
262
265
/// Error results from conflicts trigger merge conflicts to be printed and an exit code
0 commit comments