git_uncommit/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(
3    missing_docs,
4    clippy::missing_errors_doc,
5    clippy::missing_panics_doc,
6    clippy::missing_safety_doc
7)]
8
9use {
10    eyre::Result,
11    std::{
12        env,
13        process::{self, Command},
14    },
15    tracing::{debug, instrument, warn},
16};
17
18/// CLI entry point.
19///
20/// # Panics
21///
22/// For some fatal errors.
23///
24/// # Errors
25///
26/// For other fatal errors.
27#[instrument(level = "debug")]
28pub fn main() -> Result<()> {
29    let mut command = Command::new("git");
30    let command = command.args(["reset", "--soft", "HEAD~"]);
31
32    #[allow(unreachable_code)]
33    if cfg!(unix) {
34        debug!("execing {:?}", command);
35        #[cfg(unix)]
36        return Err(std::os::unix::process::CommandExt::exec(command).into());
37        unreachable!()
38    } else {
39        debug!("running {:?}", command);
40        let status = command.status()?;
41        if status.success() {
42            Ok(())
43        } else {
44            process::exit(status.code().unwrap_or(1))
45        }
46    }
47}
48
49/// Initialize the typical global environment for git-uncommit's [main] CLI
50/// entry point.
51///
52/// # Panics
53///
54/// This will panic if called multiple times, or if other code attempts
55/// conflicting global initialization of systems such as logging.
56pub fn init() {
57    color_eyre::install().unwrap();
58
59    let log_env = env::var("RUST_LOG").unwrap_or_default();
60
61    let log_level = if !log_env.is_empty() {
62        log_env
63    } else {
64        "warn".to_string()
65    };
66
67    tracing_subscriber::util::SubscriberInitExt::init(tracing_subscriber::Layer::with_subscriber(
68        tracing_error::ErrorLayer::default(),
69        tracing_subscriber::fmt()
70            .with_env_filter(::tracing_subscriber::EnvFilter::new(log_level))
71            .with_target(false)
72            .with_span_events(
73                tracing_subscriber::fmt::format::FmtSpan::ENTER
74                    | tracing_subscriber::fmt::format::FmtSpan::CLOSE,
75            )
76            .compact()
77            .finish(),
78    ));
79}