Skip to content

Commit 8ba765f

Browse files
committed
refactor: libgit2 extension initialization
- Move the implementation below main() in the file. - Update test function name. - Reformat and improve docstrings and comments Refs: #234
1 parent 440e80f commit 8ba765f

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/main.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,6 @@ const COMMAND_ERROR: i32 = 2;
4242
/// Process exit code for when a command halts due to merge conflicts.
4343
const CONFLICT_ERROR: i32 = 3;
4444

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-
7045
/// Create base [`clap::Command`] instance.
7146
///
7247
/// The base [`clap::Command`] returned by this function is intended to be supplemented
@@ -193,10 +168,10 @@ fn main() -> ! {
193168
} else if matches.get_flag("help-option") {
194169
full_app_help(argv, None, color_choice)
195170
} 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");
200175

201176
// If the name matches any known subcommands, then only the Command for that
202177
// particular command is constructed and the costs of searching for aliases
@@ -257,6 +232,34 @@ fn main() -> ! {
257232
}
258233
}
259234

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+
260263
/// Exit the program based on the provided [`Result`].
261264
///
262265
/// Error results from conflicts trigger merge conflicts to be printed and an exit code

0 commit comments

Comments
 (0)