gitpane 0.9.1

Multi-repo Git workspace dashboard TUI
Documentation
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use color_eyre::{Result, eyre::eyre};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::theme::{LoadThemeError, Theme, load_theme};

mod defaults;
mod load;
mod terminal;
#[cfg(test)]
mod tests;

use defaults::*;
use load::{LoadResolution, default_write_path, resolve_load};
use terminal::default_goto_command;

pub(crate) use load::{ConfigEnv, RealEnv, candidate_theme_dirs, worktree_path};

const APP_NAME: &str = "gitpane";
const CONFIG_FILE: &str = "config.toml";

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct Config {
    #[serde(default = "default_root_dirs")]
    pub root_dirs: Vec<PathBuf>,
    #[serde(default)]
    pub excluded_repos: Vec<String>,
    #[serde(default)]
    pub pinned_repos: Vec<PathBuf>,
    #[serde(default = "default_scan_depth")]
    pub scan_depth: usize,
    #[serde(default)]
    pub watch: WatchConfig,
    #[serde(default)]
    pub ui: UiConfig,
    #[serde(default)]
    pub graph: GraphConfig,
    #[serde(default)]
    pub submodules: SubmoduleConfig,
    #[serde(default)]
    pub github: GithubConfig,
    #[serde(default)]
    pub open: OpenConfig,
    #[serde(default)]
    pub review: ReviewConfig,
    #[serde(default)]
    pub worktree: WorktreeConfig,
    #[serde(default)]
    pub goto: GotoConfig,
    /// User-defined keybindings. Each binds a single key to a templated shell
    /// command run against the selected repo/worktree. See [`Keybinding`].
    #[serde(default)]
    pub keybindings: Vec<Keybinding>,
    /// Name of the active theme. Built-in: "default" or "muted". Any other
    /// value loads `<config_dir>/gitpane/themes/<name>.toml`.
    #[serde(default = "default_theme_name", rename = "theme")]
    pub theme_name: String,
    #[serde(skip, default)]
    pub theme: Theme,
    /// Session-only theme override (set by `--theme`). Not serialized; the
    /// `theme_name` field on disk is preserved across saves while this is
    /// active. The picker reads this via `effective_theme_name()`.
    #[serde(skip, default)]
    pub runtime_theme_override: Option<String>,
    #[serde(skip, default)]
    pub(crate) loaded_path: Option<PathBuf>,
    #[serde(skip, default)]
    pub(crate) write_target_override: Option<PathBuf>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct WatchConfig {
    #[serde(default = "default_debounce_ms")]
    pub debounce_ms: u64,
    /// Minimum milliseconds between watcher-triggered local status refreshes
    /// for the same repo.
    #[serde(default = "default_refresh_cooldown_ms")]
    pub refresh_cooldown_ms: u64,
    /// Install watches inside each repo worktree. Disabled by default because
    /// busy generated or training files can saturate Linux inotify even when
    /// status refreshes are throttled. Local polling still catches changes.
    #[serde(default = "default_watch_worktree_dirs")]
    pub watch_worktree_dirs: bool,
    /// Local status poll interval in seconds (fast, catches missed watcher events)
    #[serde(default = "default_poll_local_secs")]
    pub poll_local_secs: u64,
    /// Remote fetch poll interval in seconds (updates ahead/behind from origin)
    #[serde(default = "default_poll_fetch_secs")]
    pub poll_fetch_secs: u64,
    /// Max concurrent poll tasks (limits CPU usage with many repos)
    #[serde(default = "default_max_concurrent_polls")]
    pub max_concurrent_polls: usize,
    /// Directory names to ignore in watcher events (reduces noise)
    #[serde(default = "default_watch_exclude_dirs")]
    pub watch_exclude_dirs: Vec<String>,
    /// Minimum seconds between two auto-rescans triggered by root-dir
    /// filesystem changes. Higher values reduce wasted scans during long
    /// operations like `git clone`; lower values shorten the delay before
    /// a newly-cloned repo appears.
    #[serde(default = "default_discovery_cooldown_secs")]
    pub discovery_cooldown_secs: u64,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum UpdatePosition {
    #[default]
    TopRight,
    TopLeft,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct UiConfig {
    #[serde(default = "default_frame_rate")]
    pub frame_rate: u16,
    #[serde(default = "default_check_for_updates")]
    pub check_for_updates: bool,
    #[serde(default)]
    pub update_position: UpdatePosition,
    /// Mark repos/worktrees that have a live tmux pane cwd'd inside them.
    #[serde(default = "default_show_liveness")]
    pub show_liveness: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub(crate) enum BranchFilter {
    #[default]
    All,
    Local,
    Remote,
    None,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct GraphConfig {
    #[serde(default)]
    pub branches: BranchFilter,
    #[serde(default = "default_label_max_len")]
    pub label_max_len: usize,
    #[serde(default = "default_show_stats")]
    pub show_stats: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct SubmoduleConfig {
    #[serde(default)]
    pub ignore_dirty: bool,
    #[serde(default = "default_warn_unpushed")]
    pub warn_unpushed: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct GithubConfig {
    /// Show the GitHub panel (open issues/PRs for the selected repo, fetched via
    /// the `gh` CLI). Opt-out: enabled by default. The panel only appears for a
    /// selected repo whose `origin` is a github.com remote and only when it has
    /// open issues or PRs (press `p` to force it open otherwise). Requires `gh`
    /// on PATH; absent, gitpane keeps the usual three panels with no error.
    #[serde(default = "default_github_enabled")]
    pub enabled: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct OpenConfig {
    /// Command run by `o` to open the selected repo/worktree. `{path}` is the
    /// target directory. How it runs depends on `placement` (below). Unset opens
    /// a tmux pane (a shell) when `placement` is the default `command`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    /// Where/how to launch `command`. `command` (default): the command IS the
    /// launcher, run directly (e.g. `cursor {path}`); empty opens a tmux pane.
    /// `split-window`/`new-window` (+ tmux flags like `-h`/`-t <name>`): gitpane
    /// runs it in a tmux pane/window. `inline`: suspend gitpane and run it here.
    /// `ask`: pick placement interactively. Outside tmux, a tmux placement runs
    /// inline.
    #[serde(default = "default_open_placement")]
    pub placement: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct ReviewConfig {
    /// Command run by `v` to review the selected repo/worktree's changes.
    /// `{base}` is the resolved base ref, `{path}` the directory. When unset,
    /// defaults to `git diff {base}...HEAD`; pipe through a viewer for nicer
    /// output, e.g. `git diff {base}...HEAD | delta --side-by-side`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub command: Option<String>,
    /// Base ref to diff against, e.g. `origin/main`. When unset, gitpane
    /// resolves the repository's default branch (`origin/HEAD` → main → master).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub base: Option<String>,
    /// Where/how to launch the review command. Same vocabulary as `[open]
    /// placement`; defaults to `new-window` (a new tmux window).
    #[serde(default = "default_review_placement")]
    pub placement: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct GotoConfig {
    /// Command run by `G` to open a repo's live tmux session, `{session}` being
    /// the session name. By default it is auto-detected from your terminal (see
    /// the `TERMINAL_GOTOS` table) to open a new tab/window. Override here for an
    /// unsupported terminal or a different behavior, e.g.
    /// `wezterm cli spawn -- tmux attach -t {session}`. Run as argv (no shell),
    /// `{session}` substituted per token.
    #[serde(default = "default_goto_command")]
    pub command: String,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub(crate) struct WorktreeConfig {
    /// Directory new worktrees are created under, each as `<repo>-<branch>`.
    /// When unset, a worktree is created as a sibling of its repo.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dir: Option<PathBuf>,
}

/// One user-defined keybinding: press `key` to run `command` against the
/// selected repo/worktree. Configured as a `[[keybindings]]` array in
/// `config.toml`, e.g.
///
/// ```toml
/// [[keybindings]]
/// key = "b"
/// command = "gh repo view --web"
/// placement = "command"
/// desc = "Open repo on github.com"
/// ```
///
/// `key` is a single character. Keys already bound to built-in actions
/// (`o v G r R t g p q y a d s`, `Tab`, `Esc`, `?`) are reserved and cannot be
/// rebound; a panel-local key (`j`/`k`/`Enter`/…) can be shadowed but then no
/// longer navigates that panel. `command` uses the same `{path}` substitution
/// and `placement` vocabulary as `[open]` (`command`/`inline`/`ask`/
/// `split-window`/`new-window`).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct Keybinding {
    /// The single character that triggers the command.
    pub key: String,
    /// Command template. `{path}` expands to the target directory.
    pub command: String,
    /// Where/how to launch, like `[open] placement`. Defaults to `command`
    /// (run the command directly as its own launcher).
    #[serde(default = "default_open_placement")]
    pub placement: String,
    /// Optional label shown in the help overlay; falls back to the command.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desc: Option<String>,
}

/// True when a keybinding's `key` is exactly the single character `c`. A
/// multi-char `key` (e.g. `"ctrl+b"`) never matches, so it is inert rather
/// than firing on `c`.
pub(crate) fn key_matches(key: &str, c: char) -> bool {
    key.chars().eq(std::iter::once(c))
}

impl Config {
    pub fn load() -> Result<Self> {
        Self::load_with_env(&RealEnv)
    }

    #[allow(dead_code)]
    pub fn config_path() -> PathBuf {
        default_write_path(&RealEnv).unwrap_or_else(|| PathBuf::from("config.toml"))
    }

    pub fn save(&self) -> Result<()> {
        self.save_with_env(&RealEnv)
    }

    pub(crate) fn load_with_env(env: &dyn ConfigEnv) -> Result<Self> {
        let mut config = match resolve_load(env) {
            LoadResolution::EnvOverride(path) => {
                let exists = env.file_exists(&path);
                let mut config = if exists {
                    let contents = std::fs::read_to_string(&path)?;
                    let mut config: Config = toml::from_str(&contents)?;
                    config.expand_tildes();
                    tracing::info!(path = %path.display(), "loaded config (GITPANE_CONFIG)");
                    config
                } else {
                    tracing::info!(
                        path = %path.display(),
                        "GITPANE_CONFIG points to missing file, using defaults"
                    );
                    Config::default()
                };

                config.loaded_path = exists.then(|| path.clone());
                config.write_target_override = Some(path);
                config
            }
            LoadResolution::SearchOrder(paths) => {
                let mut loaded = None;
                for path in &paths {
                    if env.file_exists(path) {
                        let contents = std::fs::read_to_string(path)?;
                        let mut config: Config = toml::from_str(&contents)?;
                        config.expand_tildes();
                        config.loaded_path = Some(path.clone());
                        tracing::info!(path = %path.display(), "loaded config");
                        loaded = Some(config);
                        break;
                    }
                }
                loaded.unwrap_or_else(|| {
                    tracing::info!(candidates = ?paths, "no config file found, using defaults");
                    Config::default()
                })
            }
        };

        config.resolve_theme(env);
        Ok(config)
    }

    pub(crate) fn resolve_theme_with_env(&mut self, env: &dyn ConfigEnv) {
        self.resolve_theme(env);
    }

    /// The theme name actually in effect right now: a CLI `--theme` override
    /// takes precedence over the value persisted in `theme_name`.
    pub fn effective_theme_name(&self) -> &str {
        self.runtime_theme_override
            .as_deref()
            .unwrap_or(&self.theme_name)
    }

    /// Full theme-search list including any dir beside the active config
    /// (`loaded_path` / `write_target_override`). Use this for the in-app
    /// picker and any other code that needs to mirror `resolve_theme`'s
    /// lookup semantics.
    pub(crate) fn theme_dirs(&self, env: &dyn ConfigEnv) -> Vec<PathBuf> {
        let mut dirs = Vec::new();
        for source in [
            self.loaded_path.as_deref(),
            self.write_target_override.as_deref(),
        ]
        .into_iter()
        .flatten()
        {
            if let Some(parent) = source.parent()
                && !parent.as_os_str().is_empty()
            {
                let parent = parent.to_path_buf();
                if !dirs.contains(&parent) {
                    dirs.push(parent);
                }
            }
        }
        for dir in candidate_theme_dirs(env) {
            if !dirs.contains(&dir) {
                dirs.push(dir);
            }
        }
        dirs
    }

    fn resolve_theme(&mut self, env: &dyn ConfigEnv) {
        let name = self.effective_theme_name().to_string();
        let dirs = self.theme_dirs(env);

        match load_theme(&name, &dirs) {
            Ok(theme) => self.theme = theme,
            Err(e @ LoadThemeError::Unknown { .. }) => {
                tracing::warn!("{e}; falling back to default theme");
                self.theme = Theme::default();
            }
            Err(e @ LoadThemeError::InvalidFile { .. }) => {
                tracing::warn!("{e}; falling back to default theme");
                self.theme = Theme::default();
            }
        }
    }

    pub(crate) fn save_with_env(&self, env: &dyn ConfigEnv) -> Result<()> {
        let config_path = self
            .write_target_override
            .clone()
            .or_else(|| self.loaded_path.clone())
            .or_else(|| default_write_path(env))
            .ok_or_else(|| eyre!("no writable config path available"))?;

        if let Some(parent) = config_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let contents = toml::to_string_pretty(self)?;
        std::fs::write(&config_path, contents)?;
        Ok(())
    }

    pub fn add_pinned_repo(&mut self, path: PathBuf) {
        if !self.pinned_repos.contains(&path) {
            self.pinned_repos.push(path);
        }
    }

    pub fn override_root(&mut self, root: PathBuf) {
        self.root_dirs = vec![root];
    }

    fn expand_tildes(&mut self) {
        if let Some(home) = dirs::home_dir() {
            for dir in &mut self.root_dirs {
                if dir.starts_with("~") {
                    *dir = home.join(dir.strip_prefix("~").unwrap());
                }
            }
            for dir in &mut self.pinned_repos {
                if dir.starts_with("~") {
                    *dir = home.join(dir.strip_prefix("~").unwrap());
                }
            }
            if let Some(dir) = &mut self.worktree.dir
                && dir.starts_with("~")
            {
                *dir = home.join(dir.strip_prefix("~").unwrap());
            }
        }
    }
}