slick 0.26.0

async ZSH prompt
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// src/git.rs
use crate::get_env; // Assuming get_env is in lib.rs or another common module
use git2::{DiffOptions, Error, ErrorCode, Repository, Status, StatusOptions, StatusShow};
use serde::{Deserialize, Serialize};
use std::{
    env,
    fmt::Write as _,
    fs,
    path::PathBuf,
    time::{SystemTime, UNIX_EPOCH},
};

/// Returns the current Unix timestamp (seconds since `UNIX_EPOCH`).
///
/// Returns 0 if the system time is somehow before `UNIX_EPOCH` (an extremely rare occurrence).
#[must_use]
pub fn unix_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// Constants representing various Git actions or states.
/// These are used to provide short, descriptive labels for the current Git operation.
pub const ACTION_REBASE: &str = "rebase";
pub const ACTION_AM: &str = "am";
pub const ACTION_AM_REBASE: &str = "am/rebase";
pub const ACTION_REBASE_I: &str = "rebase-i";
pub const ACTION_REBASE_M: &str = "rebase-m";
pub const ACTION_MERGE: &str = "merge";
pub const ACTION_BISECT: &str = "bisect";
pub const ACTION_CHERRY_SEQ: &str = "cherry-seq";
pub const ACTION_CHERRY: &str = "cherry";
pub const ACTION_CHERRY_OR_REVERT: &str = "cherry-or-revert";
pub const NO_BRANCH: &str = "(no branch)";

#[derive(Default)]
struct StatusCounts {
    conflicted: u32,
    added_modified: u32,
    modified_modified: u32,
    modified: u32,
    deleted: u32,
    renamed: u32,
    typechanged: u32,
    added: u32,
    untracked: u32,
    ignored: u32,
    fallback: u32,
}

impl StatusCounts {
    const fn increment(&mut self, status: Status) {
        if status.contains(Status::CONFLICTED) {
            self.conflicted += 1;
        } else if status.contains(Status::INDEX_RENAMED) || status.contains(Status::WT_RENAMED) {
            self.renamed += 1;
        } else if status.contains(Status::INDEX_NEW) && status.contains(Status::WT_MODIFIED) {
            self.added_modified += 1;
        } else if status.contains(Status::INDEX_MODIFIED) && status.contains(Status::WT_MODIFIED) {
            self.modified_modified += 1;
        } else if status.contains(Status::INDEX_MODIFIED) || status.contains(Status::WT_MODIFIED) {
            self.modified += 1;
        } else if status.contains(Status::INDEX_DELETED) || status.contains(Status::WT_DELETED) {
            self.deleted += 1;
        } else if status.contains(Status::INDEX_TYPECHANGE)
            || status.contains(Status::WT_TYPECHANGE)
        {
            self.typechanged += 1;
        } else if status.contains(Status::INDEX_NEW) {
            self.added += 1;
        } else if status.contains(Status::WT_NEW) {
            self.untracked += 1;
        } else if status.contains(Status::IGNORED) {
            self.ignored += 1;
        } else {
            self.fallback += 1;
        }
    }
}

/// Represents the collected Git information for rendering the prompt.
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Prompt {
    /// The current Git action (e.g., "rebase", "merge", "am/rebase").
    pub action: String,
    /// The current branch name (or "(no branch)" for detached HEAD).
    pub branch: String,
    /// Information about remote tracking branches (e.g., "⇡1", "⇣2").
    pub remote: Vec<String>,
    /// True if there are staged changes.
    pub staged: bool,
    /// A summary of the repository's status (e.g., "M 1", "?? 2").
    pub status: String,
    /// The Git user name from the repository configuration.
    pub u_name: String,
    /// True if the last `git fetch` resulted in an authentication failure.
    pub auth_failed: bool,
    /// True if the last `git fetch` failed to reach the remote (network/DNS/host error).
    #[serde(default)]
    pub fetch_failed: bool,
}

/// Outcome of the most recent `git fetch`, as persisted in the cache file.
#[derive(Debug, Default, Eq, PartialEq, Clone, Copy)]
pub enum FetchStatus {
    /// The remote was reachable and authentication succeeded.
    #[default]
    Ok,
    /// The remote rejected our credentials.
    AuthFailed,
    /// The remote could not be reached at all (DNS, network, host or generic failure).
    Unreachable,
}

impl FetchStatus {
    /// The value persisted in the cache file for this status.
    #[must_use]
    pub const fn as_cache_value(self) -> &'static str {
        match self {
            Self::Ok => "0",
            Self::AuthFailed => "1",
            Self::Unreachable => "2",
        }
    }

    /// Parses a status previously written by [`Self::as_cache_value`].
    ///
    /// Unknown values are treated as [`Self::Ok`] so a corrupt cache never
    /// surfaces a phantom error in the prompt.
    #[must_use]
    pub fn from_cache_value(value: &str) -> Self {
        match value.trim() {
            "1" => Self::AuthFailed,
            "2" => Self::Unreachable,
            _ => Self::Ok,
        }
    }
}

/// Builds a `Prompt` struct containing fast/synchronous local Git information.
///
/// This function gathers information that does not require slow operations like network access
/// or extensive filesystem traversal (e.g., `git status`).
///
/// The collected information includes:
/// - Current branch name (`branch`)
/// - Git user name (`u_name`)
/// - Cached authentication status (`auth_failed`)
/// - Remote ahead/behind status (`remote`) based on local graph traversal
/// - Current Git action (`action`)
/// - Presence of staged changes (`staged`)
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// A `Prompt` struct populated with available fast Git information.
#[must_use]
pub fn build_prompt_fast(repo: &Repository) -> Prompt {
    // get branch (instant - just reading HEAD)
    let branch = match repo.head() {
        Ok(head) => head
            .shorthand()
            .map_or_else(|_| NO_BRANCH.to_owned(), str::to_owned),
        Err(error) if error.code() == ErrorCode::UnbornBranch => repo
            .find_reference("HEAD")
            .ok()
            .and_then(|head| {
                head.symbolic_target()
                    .ok()
                    .flatten()
                    .and_then(|target| target.strip_prefix("refs/heads/"))
                    .map(str::to_owned)
            })
            .unwrap_or_else(|| NO_BRANCH.to_owned()),
        Err(_) => NO_BRANCH.to_owned(),
    };
    let mut prompt = Prompt {
        branch,
        ..Prompt::default()
    };

    // get user.name (fast - just reading git config)
    if let Ok(config) = repo.config() {
        prompt.u_name = config
            .get_string("user.name")
            .unwrap_or_else(|_| String::new());
    }

    // Check for cached fetch status (synchronous, fast - just reads cache file)
    let fetch_status = read_fetch_status(repo);
    prompt.auth_failed = fetch_status == FetchStatus::AuthFailed;
    prompt.fetch_failed = fetch_status == FetchStatus::Unreachable;

    // git remote ahead/behind (fast - local graph traversal)
    prompt.remote = remote_markers(repo);

    // git action (instant - file existence checks)
    if let Some(action) = get_action(repo) {
        prompt.action = action;
    }

    // git staged (fast - index diff)
    if let Ok(staged) = is_staged(repo) {
        prompt.staged = staged;
    }

    prompt
}

/// Builds the ahead/behind markers (e.g. `["⇣2", "⇡1"]`) for the current branch.
///
/// The counts come from a local graph traversal, so they only reflect the remote
/// refs currently on disk. Call this again after a `git fetch` to pick up newly
/// fetched commits.
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// A vector with a behind marker and/or an ahead marker, empty when in sync.
#[must_use]
pub fn remote_markers(repo: &Repository) -> Vec<String> {
    let (ahead, behind) = is_ahead_behind_remote(repo);
    let mut markers = Vec::with_capacity(2);

    if behind > 0 {
        let mut s = String::with_capacity(8);
        // Writing to String never fails - ignore result
        let _ = write!(s, "{}{}", get_env("SLICK_PROMPT_GIT_REMOTE_BEHIND"), behind);
        markers.push(s);
    }
    if ahead > 0 {
        let mut s = String::with_capacity(8);
        // Writing to String never fails - ignore result
        let _ = write!(s, "{}{}", get_env("SLICK_PROMPT_GIT_REMOTE_AHEAD"), ahead);
        markers.push(s);
    }

    markers
}

/// Returns a string summarizing the git status of the repository.
///
/// # Errors
///
/// This function will return a `git2::Error` if it fails to get the repository statuses.
pub fn get_status(repo: &Repository) -> Result<String, Error> {
    // Pre-allocate with estimated capacity for common status types
    let mut status: Vec<String> = Vec::with_capacity(8);
    let mut status_opt = StatusOptions::new();
    status_opt
        .show(StatusShow::IndexAndWorkdir)
        .include_untracked(true)
        .recurse_untracked_dirs(true)
        .include_unmodified(false)
        .renames_head_to_index(true)
        .renames_index_to_workdir(true)
        .no_refresh(false); // Keep false to get real-time status

    let statuses = repo.statuses(Some(&mut status_opt))?;
    if !statuses.is_empty() {
        let mut counts = StatusCounts::default();
        for entry in statuses.iter() {
            counts.increment(entry.status());
        }

        for (label, count) in [
            ("UU", counts.conflicted),
            ("AM", counts.added_modified),
            ("MM", counts.modified_modified),
            ("M", counts.modified),
            ("D", counts.deleted),
            ("R", counts.renamed),
            ("T", counts.typechanged),
            ("A", counts.added),
            ("??", counts.untracked),
            ("!", counts.ignored),
            ("X", counts.fallback),
        ] {
            if count > 0 {
                status.push(format!("{label} {count}"));
            }
        }
    }
    Ok(status.join(" "))
}

/// Generates the path for the Git authentication cache file for a given repository.
///
/// The cache path is determined based on the repository's working directory,
/// hashed to create a unique filename within the system's cache directory
/// ( `XDG_CACHE_HOME` or ~/.cache/slick).
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// An `Option<PathBuf>` containing the full path to the cache file if it can be determined,
/// otherwise `None`.
#[must_use]
pub fn get_auth_cache_path(repo: &Repository) -> Option<PathBuf> {
    // Use workdir (repo root) instead of .git path for stable cache key
    // Canonicalize to get absolute path and resolve symlinks
    let repo_path = repo
        .workdir()
        .and_then(|p| p.canonicalize().ok())?
        .to_str()?
        .to_string();

    let cache_dir = env::var("SLICK_TEST_AUTH_CACHE_DIR")
        .or_else(|_| env::var("XDG_CACHE_HOME"))
        .or_else(|_| env::var("HOME").map(|h| format!("{h}/.cache")))
        .ok()?;

    // Create a hash of the canonicalized repo path for the cache filename
    let hash = repo_path.bytes().fold(0u64, |acc, b| {
        acc.wrapping_mul(31).wrapping_add(u64::from(b))
    });

    let cache_path = PathBuf::from(cache_dir).join("slick");
    Some(cache_path.join(format!("auth_{hash:x}")))
}

/// Reads the cached Git fetch status for a given repository.
///
/// The cache file stores a timestamp and a status (see [`FetchStatus::as_cache_value`]).
/// The cache is considered valid for 5 minutes.
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// The cached [`FetchStatus`], or [`FetchStatus::Ok`] when there is no fresh cache entry.
#[must_use]
pub fn read_fetch_status(repo: &Repository) -> FetchStatus {
    if let Some(cache_path) = get_auth_cache_path(repo)
        && let Ok(content) = fs::read_to_string(&cache_path)
        && let Some((ts_str, status)) = content.split_once(':')
        && let Ok(cached_time) = ts_str.parse::<u64>()
    {
        let now = unix_timestamp();

        // Cache valid for 5 minutes
        if now.saturating_sub(cached_time) < 300 {
            return FetchStatus::from_cache_value(status);
        }
    }
    FetchStatus::Ok
}

/// Reads the cached Git authentication status for a given repository.
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// `true` if authentication previously failed and the cache is still fresh, `false` otherwise.
#[must_use]
pub fn read_auth_status(repo: &Repository) -> bool {
    read_fetch_status(repo) == FetchStatus::AuthFailed
}

/// Determines the current Git action (e.g., rebase, merge, cherry-pick) by checking
/// for the presence of specific files in the `.git` directory.
///
/// This is a fast operation as it only involves filesystem checks.
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// An `Option<String>` containing the name of the current Git action if one is detected,
/// otherwise `None`.
#[must_use]
pub fn get_action(repo: &Repository) -> Option<String> {
    let gitdir = repo.path();

    for tmp in &[
        gitdir.join("rebase-apply"),
        gitdir.join("rebase"),
        gitdir.join("..").join(".dotest"),
    ] {
        if tmp.join("rebasing").exists() {
            return Some(ACTION_REBASE.to_string());
        }
        if tmp.join("applying").exists() {
            return Some(ACTION_AM.to_string());
        }
        if tmp.exists() {
            return Some(ACTION_AM_REBASE.to_string());
        }
    }

    for tmp in &[
        gitdir.join("rebase-merge").join("interactive"),
        gitdir.join(".dotest-merge").join("interactive"),
    ] {
        if tmp.exists() {
            return Some(ACTION_REBASE_I.to_string());
        }
    }

    for tmp in &[gitdir.join("rebase-merge"), gitdir.join(".dotest-merge")] {
        if tmp.exists() {
            return Some(ACTION_REBASE_M.to_string());
        }
    }

    if gitdir.join("MERGE_HEAD").exists() {
        return Some(ACTION_MERGE.to_string());
    }

    if gitdir.join("BISECT_LOG").exists() {
        return Some(ACTION_BISECT.to_string());
    }

    if gitdir.join("CHERRY_PICK_HEAD").exists() {
        if gitdir.join("sequencer").exists() {
            return Some(ACTION_CHERRY_SEQ.to_string());
        }
        return Some(ACTION_CHERRY.to_string());
    }

    if gitdir.join("sequencer").exists() {
        return Some(ACTION_CHERRY_OR_REVERT.to_string());
    }

    None
}

/// Determines how many commits the current HEAD is ahead or behind its upstream remote.
///
/// This is a fast operation as it involves local graph traversal.
///
/// # Arguments
///
/// * `repo` - A reference to the `git2::Repository` object.
///
/// # Returns
///
/// A tuple `(ahead, behind)` where:
/// - `ahead` is the number of commits HEAD is ahead of the upstream.
/// - `behind` is the number of commits HEAD is behind the upstream.
///
/// Returns `(0, 0)` if no upstream is configured or in case of an error.
#[must_use]
pub fn is_ahead_behind_remote(repo: &Repository) -> (usize, usize) {
    if let Ok(head) = repo.revparse_single("HEAD") {
        let head = head.id();
        if let Ok((upstream, _)) = repo.revparse_ext("@{u}") {
            return match repo.graph_ahead_behind(head, upstream.id()) {
                Ok((commits_ahead, commits_behind)) => (commits_ahead, commits_behind),
                Err(_) => (0, 0),
            };
        }
    }
    (0, 0)
}

/// Checks if there are any staged changes in the repository.
///
/// # Errors
///
/// This function will return a `git2::Error` if it fails to get the repository head or diff.
pub fn is_staged(repo: &Repository) -> Result<bool, Error> {
    let mut opts = DiffOptions::new();
    let tree = match repo.head() {
        Ok(head) => Some(head.peel_to_tree()?),
        Err(error) if error.code() == ErrorCode::UnbornBranch => None,
        Err(error) => return Err(error),
    };
    let diff = repo.diff_tree_to_index(tree.as_ref(), None, Some(&mut opts))?;
    // Only the presence of a staged change matters, so count deltas instead of
    // calling `stats()`, which loads and diffs blob contents to tally lines.
    Ok(diff.deltas().len() > 0)
}