Skip to main content

git_stk/
settings.rs

1//! Every stk-owned git config key and its resolution logic, in one place.
2
3use anyhow::Result;
4
5use crate::cli::PushMode;
6use crate::git;
7
8pub const PROVIDER_KEY: &str = "stk.provider";
9pub const REMOTE_KEY: &str = "stk.remote";
10pub const UPDATE_REFS_KEY: &str = "stk.updateRefs";
11pub const PUSH_ON_RESTACK_KEY: &str = "stk.pushOnRestack";
12pub const PUSH_ON_SUBMIT_KEY: &str = "stk.pushOnSubmit";
13pub const SUBMIT_STACK_KEY: &str = "stk.submitStack";
14pub const MERGE_STRATEGY_KEY: &str = "stk.mergeStrategy";
15pub const MERGE_WAIT_KEY: &str = "stk.mergeWait";
16pub const SUBMIT_DRAFT_KEY: &str = "stk.submitDraft";
17pub const NO_UPDATE_CHECK_KEY: &str = "stk.noUpdateCheck";
18pub const ABSORB_INCLUDE_UNSTAGED_KEY: &str = "stk.absorbIncludeUnstaged";
19pub const GITLAB_HOST_KEY: &str = "stk.gitlabHost";
20pub const DEFAULT_REMOTE: &str = "origin";
21
22/// Every `[stk]` setting the tool reads, with its default behavior. Shown by
23/// `git stk config`.
24pub const SETTINGS: &[(&str, &str)] = &[
25    (PROVIDER_KEY, "auto-detect from the remote URL"),
26    (REMOTE_KEY, DEFAULT_REMOTE),
27    (UPDATE_REFS_KEY, "false"),
28    (PUSH_ON_RESTACK_KEY, "false"),
29    (PUSH_ON_SUBMIT_KEY, "false"),
30    (SUBMIT_STACK_KEY, "false"),
31    (MERGE_STRATEGY_KEY, "squash"),
32    (MERGE_WAIT_KEY, "false"),
33    (SUBMIT_DRAFT_KEY, "false"),
34    (NO_UPDATE_CHECK_KEY, "false"),
35    (ABSORB_INCLUDE_UNSTAGED_KEY, "false"),
36    (GITLAB_HOST_KEY, "gitlab.com"),
37];
38
39/// The remote used for provider detection, trunk discovery, and pushes.
40pub fn remote() -> Result<String> {
41    Ok(git::config_get(REMOTE_KEY)?.unwrap_or_else(|| DEFAULT_REMOTE.to_owned()))
42}
43
44/// A self-hosted GitLab host (e.g. `gitlab.example.com`) to recognize as
45/// GitLab alongside gitlab.com (`stk.gitlabHost`). `glab` reads the host from
46/// the git remote on its own, so this only widens stk's provider detection.
47pub fn gitlab_host() -> Result<Option<String>> {
48    git::config_get(GITLAB_HOST_KEY)
49}
50
51/// The merge strategy for `git stk merge`: squash, rebase, or merge.
52pub fn merge_strategy() -> Result<String> {
53    let strategy = git::config_get(MERGE_STRATEGY_KEY)?.unwrap_or_else(|| "squash".to_owned());
54    match strategy.as_str() {
55        "squash" | "rebase" | "merge" => Ok(strategy),
56        other => anyhow::bail!(
57            "unsupported stk.mergeStrategy value {other:?}; expected squash, rebase, or merge"
58        ),
59    }
60}
61
62/// A boolean setting's value, defaulting to false when unset.
63pub fn bool_setting(key: &str) -> Result<bool> {
64    Ok(git::config_get_bool(key)?.unwrap_or(false))
65}
66
67/// Resolve a `--push`/`--no-push` flag pair against its config-key default.
68pub fn push_enabled(mode: PushMode, key: &str) -> Result<bool> {
69    match mode {
70        PushMode::Config => Ok(git::config_get_bool(key)?.unwrap_or(false)),
71        PushMode::Enabled => Ok(true),
72        PushMode::Disabled => Ok(false),
73    }
74}