Skip to main content

git_su/
git.rs

1// Git: config get/set/unset, scopes local/global/system
2
3use std::io::IsTerminal;
4
5use crate::shell;
6use crate::user::User;
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum Scope {
10    Local,
11    Global,
12    System,
13    Derived,
14    Default,
15}
16
17impl Scope {
18    pub fn as_flag(self) -> &'static str {
19        match self {
20            Scope::Local => "local",
21            Scope::Global => "global",
22            Scope::System => "system",
23            Scope::Derived | Scope::Default => "",
24        }
25    }
26
27    pub fn display_name(self) -> &'static str {
28        match self {
29            Scope::Local => "local",
30            Scope::Global => "global",
31            Scope::System => "system",
32            Scope::Derived => "derived",
33            Scope::Default => "local",
34        }
35    }
36}
37
38pub struct Git;
39
40impl Git {
41    pub fn get_config(scope: Scope, key: &str) -> String {
42        let suffix = format!("--get {}", key);
43        let cmd = if scope == Scope::Derived {
44            format!("git config {}", suffix)
45        } else {
46            format!("git config --{} {}", scope.as_flag(), suffix)
47        };
48        shell::capture(&cmd)
49    }
50
51    pub fn set_config(scope: Scope, key: &str, value: &str) -> bool {
52        let scope_flag = if scope == Scope::Derived {
53            None
54        } else {
55            Some(scope.as_flag())
56        };
57        shell::git_config_set(scope_flag, key, value)
58    }
59
60    pub fn unset_config(scope: Scope, key: &str) -> bool {
61        let suffix = format!("--unset {}", key);
62        let cmd = if scope == Scope::Derived {
63            format!("git config {}", suffix)
64        } else {
65            format!("git config --{} {}", scope.as_flag(), suffix)
66        };
67        shell::execute(&cmd)
68    }
69
70    pub fn list_config(scope: Scope) -> Vec<String> {
71        let suffix = "--list";
72        let cmd = if scope == Scope::Derived {
73            format!("git config {}", suffix)
74        } else {
75            format!("git config --{} {}", scope.as_flag(), suffix)
76        };
77        let s = shell::capture(&cmd);
78        if s.is_empty() {
79            return vec![];
80        }
81        s.lines().map(str::to_string).collect()
82    }
83
84    pub fn remove_section(scope: Scope, section: &str) -> bool {
85        let suffix = format!("--remove-section {} 2>/dev/null", section);
86        let cmd = if scope == Scope::Derived {
87            format!("git config {}", suffix)
88        } else {
89            format!("git config --{} {}", scope.as_flag(), suffix)
90        };
91        shell::execute(&cmd)
92    }
93
94    pub fn select_user(user: &User, scope: Scope) -> bool {
95        Self::set_config(scope, "user.name", &user.name())
96            && Self::set_config(scope, "user.email", &user.email())
97    }
98
99    pub fn selected_user(scope: Scope) -> User {
100        let name = Self::get_config(scope, "user.name");
101        if name.is_empty() {
102            return User::none();
103        }
104        let email = Self::get_config(scope, "user.email");
105        User::new(name, email)
106    }
107
108    pub fn clear_user(scope: Scope) {
109        let current = Self::selected_user(scope);
110        if current.is_none() {
111            return;
112        }
113        Self::unset_config(scope, "user.name");
114        Self::unset_config(scope, "user.email");
115        let list = Self::list_config(scope);
116        if list.iter().filter(|e| e.starts_with("user.")).count() == 0 {
117            Self::remove_section(scope, "user");
118        }
119    }
120
121    pub fn edit_gitsu_config(path: &std::path::Path) -> bool {
122        let path_str = path.to_string_lossy();
123        let cmd = format!("git config --edit --file {}", path_str);
124        shell::execute(&cmd)
125    }
126
127    pub fn get_color(name: &str) -> String {
128        let suffix = format!("--get-color '' '{}'", name);
129        let cmd = format!("git config {}", suffix);
130        shell::capture(&cmd)
131    }
132
133    pub fn color_output() -> bool {
134        if !std::io::stdout().is_terminal() {
135            return false;
136        }
137        let cmd = "git config --get-colorbool color.ui true";
138        let v = shell::capture(cmd).trim().to_string();
139        v == "true"
140    }
141
142    pub fn render(user: &User) -> String {
143        if Self::color_output() {
144            let user_color = Self::get_color("blue");
145            let email_color = Self::get_color("green");
146            let reset = Self::get_color("reset");
147            format!(
148                "{}{} {}<{}>{}",
149                user_color,
150                user.name(),
151                email_color,
152                user.email(),
153                reset
154            )
155        } else {
156            user.to_string()
157        }
158    }
159}