1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// git-z - A Git extension to go beyond.
// Copyright (C) 2023 Jean-Philippe Cugnet <jean-philippe@cugnet.eu>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{io, process::Command};

use eyre::Result;
use thiserror::Error;

use crate::{
    config::{Config, CONFIG_FILE_NAME, VERSION},
    hint, warning,
};

/// An error occuring when not inside a Git worktree.
#[derive(Debug, Error)]
pub enum NotInGitWorktree {
    #[error("Failed to run the git command")]
    CannotRunGit(#[from] io::Error),
    #[error("Not in a Git repository")]
    NotInRepo,
    #[error("Not inside a Git worktree")]
    NotInWorktree,
}

/// Ensures the command is run from a Git worktree.
pub fn ensure_in_git_worktree() -> Result<(), NotInGitWorktree> {
    let is_inside_work_tree = Command::new("git")
        .args(["rev-parse", "--is-inside-work-tree"])
        .output()?;

    if !is_inside_work_tree.status.success() {
        return Err(NotInGitWorktree::NotInRepo);
    }

    if is_inside_work_tree.stdout == b"true\n" {
        Ok(())
    } else {
        Err(NotInGitWorktree::NotInWorktree)
    }
}

/// Loads the configuration.
pub fn load_config() -> Result<Config> {
    let config = Config::load()?;

    if config.version != VERSION {
        warning!("The configuration in {CONFIG_FILE_NAME} is out of date.");
        hint!("You can update it by running `git z update`.");
    }

    Ok(config)
}

/// Uncapitalises the first character in s.
pub fn uncapitalise(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(c) => c.to_lowercase().collect::<String>() + chars.as_str(),
    }
}

/// Prints a success.
#[macro_export]
macro_rules! success {
    ($($arg:tt)*) => {{
        use colored::Colorize;
        let message = format!($($arg)*).green().bold();
        println!("{message}");
    }};
}

/// Prints a warning.
#[macro_export]
macro_rules! warning {
    ($($arg:tt)*) => {{
        use colored::Colorize;
        let message = format!($($arg)*).yellow().bold();
        eprintln!("{message}");
    }};
}

/// Prints an error.
#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => {{
        use colored::Colorize;
        let message = format!($($arg)*);
        let message = $crate::command::helpers::uncapitalise(&message);
        let message = format!("Error: {message}").red().bold();
        eprintln!("{message}");
    }};
}

/// Prints a hint.
#[macro_export]
macro_rules! hint {
    ($($arg:tt)*) => {{
        use colored::Colorize;
        let message = format!($($arg)*).blue();
        eprintln!("{message}");
    }};
}