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
//! This is primarily a command line utility. The documentation for the command
//! line interface is in [README.md][].
//!
//! The primary entrance to this code is [`summarize_repository()`]. It opens a
//! [`Repository`], then calls [`summarize_opened_repository()`] on it.
//!
//! Currently the minimum supported Rust version (MSRV) is **1.64**.
//!
//! # Versioning
//!
//! This follows semantic versioning for the command line utility, not the crate
//! API. Breaking changes to the API are not guaranteed to involve a major
//! version change, since I don’t anticipate this being used as a crate by
//! anyone else.
//!
//! [README.md]: https://github.com/danielparks/git-status-vars/blob/main/README.md

// Most lint configuration is in lints.toml, but that isn’t supported by
// cargo-geiger, and it only supports deny, not forbid.
#![forbid(unsafe_code)]

use git2::Branch;
use git2::ReferenceType;
use git2::Repository;
use git2::{ErrorClass, ErrorCode};
use git2::{Status, StatusOptions, StatusShow};
use std::fmt;
use std::io;
use std::path::Path;

/// Manage outputting shell variables.
mod shell_writer;
pub use shell_writer::*;

/// A reference in a git repository.
#[derive(Debug, Default)]
pub struct Reference {
    /// The name of the reference, e.g. `"refs/heads/my_branch"`.
    pub name: String,

    /// The kind of reference, e.g. `"symbolic"` or `"direct"`.
    pub kind: String,

    /// An error encountered when trying to resolve the reference, or `""`.
    pub error: String,
}

impl Reference {
    /// Create a new reference without an error.
    #[must_use]
    pub fn new<N: fmt::Display, K: fmt::Display>(name: N, kind: K) -> Self {
        Self {
            name: name.to_string(),
            kind: kind.to_string(),
            error: "".to_owned(),
        }
    }

    /// Create a new reference with an error.
    #[must_use]
    pub fn new_with_error<N, K, E>(name: N, kind: K, error: E) -> Self
    where
        N: fmt::Display,
        K: fmt::Display,
        E: fmt::Debug,
    {
        Self {
            name: name.to_string(),
            kind: kind.to_string(),
            error: format!("{error:?}"),
        }
    }

    /// Create a new symbolic reference.
    #[must_use]
    pub fn symbolic<N: fmt::Display>(name: N) -> Self {
        Self::new(name, "symbolic")
    }

    /// Create a new direct reference.
    #[must_use]
    pub fn direct<N: fmt::Display>(name: N) -> Self {
        Self::new(name, "direct")
    }

    /// Get the short name of a reference if it’s a tag or branch. Otherwise,
    /// get the full name.
    #[must_use]
    pub fn short(&self) -> &str {
        self.name
            .strip_prefix("refs/heads/")
            .or_else(|| self.name.strip_prefix("refs/tags/"))
            .unwrap_or(&self.name)
    }
}

impl ShellVars for Reference {
    // Output the reference information with a prefix (e.g. "ref_").
    fn write_to_shell<W: io::Write>(&self, out: &ShellWriter<W>) {
        out.write_var("name", &self.name);
        out.write_var("short", self.short());
        out.write_var("kind", &self.kind);
        out.write_var("error", &self.error);
    }
}

/// The trail of a `HEAD` reference.
#[derive(Debug, Default)]
pub struct Head {
    /// The trail of references leading to the actual underlying commit.
    pub trail: Vec<Reference>,

    /// The hash of the commit.
    pub hash: String,

    /// How many commits are we ahead of upstream?
    ///
    /// `None` means that there is no upstream, or there is no equivalent branch
    /// in upstream.
    pub ahead_of_upstream: Option<usize>,

    /// How many commits are we behind upstream?
    ///
    /// `None` means that there is no upstream, or there is no equivalent branch
    /// in upstream.
    pub behind_upstream: Option<usize>,

    /// An error encountered trying to calculate differences with upstream.
    pub upstream_error: String,
}

impl ShellVars for Head {
    fn write_to_shell<W: io::Write>(&self, out: &ShellWriter<W>) {
        let trail = self.trail.get(1..).unwrap_or(&[]);
        out.write_var("ref_length", trail.len());
        for (i, reference) in trail.iter().enumerate() {
            // self.trail is actually 1 longer, so i + 1 always fits.
            #[allow(clippy::arithmetic_side_effects)]
            out.group_n("ref", i + 1).write_vars(reference);
        }
        out.write_var("hash", &self.hash);
        out.write_var("ahead", display_option(self.ahead_of_upstream));
        out.write_var("behind", display_option(self.behind_upstream));
        out.write_var("upstream_error", &self.upstream_error);
    }
}

/// Summarize information about a repository.
///
/// This takes the `Result` from one of the `Repository::open()` functions.
///
/// # Example
///
/// ```no_run
/// use git_status_vars::{summarize_repository, ShellWriter};
/// use git2::Repository;
///
/// summarize_repository(&ShellWriter::default(), Repository::open_from_env());
/// ```
///
/// # Panics
///
/// This may panic if it can’t resolve a symbolic reference to a symbolic
/// target.
pub fn summarize_repository<W: std::io::Write>(
    out: &ShellWriter<W>,
    opened: Result<Repository, git2::Error>,
) {
    let result = match opened {
        Ok(repository) => summarize_opened_repository(out, &repository),
        Err(error)
            if error.code() == ErrorCode::NotFound
                && error.class() == ErrorClass::Repository =>
        {
            out.write_var("repo_state", "NotFound");
            Ok(())
        }
        Err(error) => Err(error),
    };

    if let Err(error) = result {
        out.write_var("repo_state", "Error");
        out.write_var_debug("repo_error", error);
    }
}

/// Summarize information about a successfully opened repository.
///
/// # Example
///
/// ```no_run
/// use git_status_vars::{summarize_opened_repository, ShellWriter};
/// use git2::Repository;
///
/// summarize_opened_repository(
///     &ShellWriter::default(),
///     &Repository::open_from_env().unwrap(),
/// ).unwrap();
/// ```
///
/// # Errors
///
/// This will return a [`git2::Error`] if there were problems getting repository
/// information. This is careful to load all repository information (and thus
/// encountering any errors) before generating any output.
///
/// # Panics
///
/// This may panic if it can’t resolve a symbolic reference to a symbolic
/// target.
pub fn summarize_opened_repository<W: std::io::Write>(
    out: &ShellWriter<W>,
    repository: &Repository,
) -> Result<(), git2::Error> {
    let state = repository.state();
    let workdir = display_option(repository.workdir().map(Path::display));
    let empty = repository.is_empty()?;
    let bare = repository.is_bare();
    let head = &head_info(repository);
    let changes = &count_changes(repository)?;

    out.write_var_debug("repo_state", state);
    out.write_var("repo_workdir", workdir);
    out.write_var("repo_empty", empty);
    out.write_var("repo_bare", bare);
    out.group("head").write_vars(head);
    out.write_vars(changes);

    Ok(())
}

/// Trace the `HEAD` reference for a repository.
///
/// # Panics
///
/// This may panic if it can’t resolve a symbolic reference to a symbolic
/// target.
#[allow(clippy::similar_names)]
#[must_use]
pub fn head_info(repository: &Repository) -> Head {
    let mut current = "HEAD".to_owned();
    let mut head = Head::default();
    loop {
        match repository.find_reference(&current) {
            Ok(reference) => match reference.kind() {
                Some(ReferenceType::Direct) => {
                    head.trail.push(Reference::direct(display_option(
                        reference.name(),
                    )));
                    head.hash = display_option(reference.target());
                    break;
                }
                Some(ReferenceType::Symbolic) => {
                    head.trail.push(Reference::symbolic(display_option(
                        reference.name(),
                    )));
                    let target = reference
                        .symbolic_target()
                        .expect("Symbolic ref should have symbolic target");
                    current = target.to_owned();
                }
                None => {
                    head.trail.push(Reference::new(
                        display_option(reference.name()),
                        "unknown",
                    ));
                    break;
                }
            },
            Err(error) => {
                head.trail
                    .push(Reference::new_with_error(current, "", error));
                break;
            }
        };
    }

    match get_upstream_difference(repository) {
        Ok(Some((ahead, behind))) => {
            head.ahead_of_upstream = Some(ahead);
            head.behind_upstream = Some(behind);
        }
        Ok(None) => {}
        Err(error) => {
            head.upstream_error = format!("{error:?}");
        }
    }

    head
}

/// Get the (ahead, behind) count of HEAD versus its upstream branch.
///
/// # Errors
///
/// This will return [`git2::Error`] if there were problems resolving the
/// the repository head, or if there was an error finding the upstream branch
/// (but it will return `Ok(None)` if there simply is no upstream or upstream
/// branch).
pub fn get_upstream_difference(
    repository: &Repository,
) -> Result<Option<(usize, usize)>, git2::Error> {
    let local_ref = repository.head()?.resolve()?;
    if let Some(local_oid) = local_ref.target() {
        Branch::wrap(local_ref)
            .upstream()?
            .get()
            .target()
            .map(|upstream_oid| {
                repository.graph_ahead_behind(local_oid, upstream_oid)
            })
            .transpose()
    } else {
        Ok(None)
    }
}

/// Format `Option<impl fmt::Display>` for display. `None` becomes `""`.
fn display_option<V: fmt::Display>(s: Option<V>) -> String {
    s.map(|s| s.to_string()).unwrap_or_else(|| "".to_owned())
}

/// Track changes in the working tree and index (staged area).
#[derive(Debug, Default)]
pub struct ChangeCounters {
    /// The number of untracked files (not in the index).
    pub untracked: usize,

    /// The number of files that have been modified, but haven’t been staged.
    pub unstaged: usize,

    /// The number of files that have been staged.
    pub staged: usize,

    /// The number of files with conflicts.
    pub conflicted: usize,
}

impl From<[usize; 4]> for ChangeCounters {
    fn from(array: [usize; 4]) -> Self {
        Self {
            untracked: array[0],
            unstaged: array[1],
            staged: array[2],
            conflicted: array[3],
        }
    }
}

impl ShellVars for ChangeCounters {
    // Output the tree change information with a prefix (e.g. "tree_").
    fn write_to_shell<W: io::Write>(&self, out: &ShellWriter<W>) {
        out.write_var("untracked_count", self.untracked);
        out.write_var("unstaged_count", self.unstaged);
        out.write_var("staged_count", self.staged);
        out.write_var("conflicted_count", self.conflicted);
    }
}

/// Count changes in the working tree and index (staged area) of a repository.
///
/// # Errors
///
/// This will return [`git2::Error`] if there was an error getting status
/// information from the repository.
pub fn count_changes(
    repository: &Repository,
) -> Result<ChangeCounters, git2::Error> {
    if repository.is_bare() {
        // Can't run status on bare repo.
        return Ok(ChangeCounters::default());
    }

    let mut options = StatusOptions::new();
    // exclude_submodules optional?
    options
        .show(StatusShow::IndexAndWorkdir)
        .include_untracked(true)
        .exclude_submodules(true);
    let statuses = repository.statuses(Some(&mut options))?;

    let mut counters: [usize; 4] = [0; 4];
    let buckets = [
        // Untracked
        Status::WT_NEW,
        // Working tree changed
        Status::WT_MODIFIED
            | Status::WT_DELETED
            | Status::WT_TYPECHANGE
            | Status::WT_RENAMED,
        // Staged
        Status::INDEX_NEW
            | Status::INDEX_MODIFIED
            | Status::INDEX_DELETED
            | Status::INDEX_RENAMED
            | Status::INDEX_TYPECHANGE,
        // Conflicted
        Status::CONFLICTED,
    ];

    for status in statuses.iter() {
        for (i, bits) in buckets.iter().enumerate() {
            if status.status().intersects(*bits) {
                counters[i] = counters[i].saturating_add(1);
            }
        }
    }

    Ok(ChangeCounters::from(counters))
}