girt 0.11.0

An incremental, idiomatic Rust implementation of Git
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! Revision and range syntax, parsed without reading a repository.

use std::fmt;
use std::str::FromStr;

use crate::ObjectKind;

/// A parsed revision expression naming one object, such as `main~2` or `v1.0^{tree}:README`.
///
/// A revision is a [`Base`] that names a starting object, a sequence of [`Step`]s applied in
/// order, and an optional path looked up in the resulting tree. Parse it with [`str::parse`] and
/// resolve it with [`super::Resolver::resolve`].
///
/// ```
/// use girt::revision::{Base, Revision, Step};
/// let revision: Revision = "main~2^2".parse()?;
/// assert_eq!(revision.base(), &Base::Name("main".into()));
/// assert_eq!(revision.steps(), [Step::Ancestor(2), Step::Parent(2)]);
/// assert_eq!(revision.path(), None);
///
/// let file: Revision = "HEAD:src/lib.rs".parse()?;
/// assert_eq!(file.path(), Some("src/lib.rs"));
/// # Ok::<(), girt::revision::ParseRevisionError>(())
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Revision {
    base: Base,
    steps: Vec<Step>,
    path: Option<String>,
}

impl Revision {
    /// `HEAD`, the revision an omitted range side stands for.
    pub fn head() -> Self {
        Self {
            base: Base::Head,
            steps: Vec::new(),
            path: None,
        }
    }

    /// The starting object.
    pub fn base(&self) -> &Base {
        &self.base
    }

    /// Navigation applied to the base, in order.
    pub fn steps(&self) -> &[Step] {
        &self.steps
    }

    /// The path after `:`, looked up in the tree of the stepped revision. `Some("")` names that
    /// tree itself.
    pub fn path(&self) -> Option<&str> {
        self.path.as_deref()
    }
}

/// The object a [`Revision`] starts from.
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Base {
    /// `@`: the current `HEAD`.
    Head,
    /// A reference name or full object ID, expanded at resolution in Git's order.
    Name(String),
    /// `name@{N}` or `@{N}`: the value `index` updates before the reference's latest one.
    ///
    /// Without a name, the reflog of the branch `HEAD` points to is used, or `HEAD`'s own
    /// reflog when it is detached.
    Reflog {
        /// The reference, or `None` for the current branch.
        name: Option<String>,
        /// Entries back from the newest; `0` is the newest entry's value.
        index: usize,
    },
    /// `name@{upstream}` or `@{u}`: the remote-tracking branch the branch merges from.
    Upstream {
        /// The branch, or `None` for the current branch.
        branch: Option<String>,
    },
    /// `name@{push}`: the remote-tracking branch the branch would push to.
    Push {
        /// The branch, or `None` for the current branch.
        branch: Option<String>,
    },
}

/// One navigation step after a [`Base`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Step {
    /// `~N`: follow first parents `N` times. `~0` peels to a commit.
    Ancestor(usize),
    /// `^N`: the `N`th parent, counting from 1. `^0` peels to a commit.
    Parent(usize),
    /// `^{...}`: peel or check the object kind.
    Peel(Peel),
}

/// The target of a `^{...}` step.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Peel {
    /// `^{}`: follow annotated tags to the first object that is not a tag.
    Tags,
    /// `^{object}`: require that the object exists, without peeling.
    Object,
    /// `^{commit}`, `^{tree}`, `^{blob}` or `^{tag}`: peel until reaching that kind. Commits
    /// peel to their tree; `^{tag}` requires the object itself to be a tag.
    Kind(ObjectKind),
}

/// A commit-set expression for history commands, such as `main..topic`.
///
/// Several ranges combine into one [`super::Selection`]: the commits reachable from any included
/// revision and from no excluded one.
///
/// ```
/// use girt::revision::{Range, Revision};
/// let range: Range = "main...topic".parse()?;
/// assert_eq!(
///     range,
///     Range::Symmetric {
///         left: "main".parse()?,
///         right: "topic".parse()?
///     }
/// );
/// let since: Range = "v1.0..".parse()?;
/// assert_eq!(
///     since,
///     Range::Between {
///         from: "v1.0".parse()?,
///         to: Revision::head()
///     }
/// );
/// # Ok::<(), girt::revision::ParseRevisionError>(())
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Range {
    /// `rev`: commits reachable from `rev`.
    Include(Revision),
    /// `^rev`: exclude commits reachable from `rev`.
    Exclude(Revision),
    /// `from..to`: reachable from `to` but not from `from`.
    Between {
        /// Excluded side.
        from: Revision,
        /// Included side.
        to: Revision,
    },
    /// `left...right`: reachable from either side but not from both.
    Symmetric {
        /// Left side.
        left: Revision,
        /// Right side.
        right: Revision,
    },
}

/// Text that is not a revision or range expression.
#[derive(Clone, Debug, Eq, PartialEq, thiserror::Error)]
#[error("invalid revision `{text}`: {reason}")]
pub struct ParseRevisionError {
    text: String,
    reason: Reason,
}

impl ParseRevisionError {
    fn new(text: &str, reason: Reason) -> Self {
        Self {
            text: text.to_owned(),
            reason,
        }
    }

    /// The rejected text.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Whether the text uses recognised syntax girt does not support yet, rather than being
    /// malformed.
    pub fn is_unsupported(&self) -> bool {
        matches!(self.reason, Reason::Unsupported(_))
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Reason {
    Empty,
    Malformed(&'static str),
    Unsupported(&'static str),
}

impl fmt::Display for Reason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => f.write_str("empty revision"),
            Self::Malformed(what) => f.write_str(what),
            Self::Unsupported(what) => write!(f, "{what} are not supported yet"),
        }
    }
}

impl FromStr for Revision {
    type Err = ParseRevisionError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let error = |reason| ParseRevisionError::new(text, reason);
        let (expression, path) = match path_separator(text) {
            Some(0) => {
                return Err(error(Reason::Unsupported(
                    "index paths and message searches (`:path`, `:/text`)",
                )));
            }
            Some(colon) => (&text[..colon], Some(&text[colon + 1..])),
            None => (text, None),
        };
        if let Some(path) = path
            && (path.starts_with("./") || path.starts_with("../") || path == "." || path == "..")
        {
            return Err(error(Reason::Unsupported(
                "paths relative to the working directory",
            )));
        }
        let end = expression
            .find(['~', '^'])
            .into_iter()
            .chain(expression.find("@{"))
            .min()
            .unwrap_or(expression.len());
        let (name, mut rest) = expression.split_at(end);
        let base = if let Some(selector) = rest.strip_prefix("@{") {
            let close = selector
                .find('}')
                .ok_or_else(|| error(Reason::Malformed("unclosed `@{`")))?;
            rest = &selector[close + 1..];
            let name = match name {
                "" | "@" => None,
                name => Some(name.to_owned()),
            };
            at_selector(&selector[..close], name).map_err(error)?
        } else {
            match name {
                "" => return Err(error(Reason::Empty)),
                "@" => Base::Head,
                name => Base::Name(name.to_owned()),
            }
        };
        let steps = steps(rest).map_err(error)?;
        Ok(Self {
            base,
            steps,
            path: path.map(str::to_owned),
        })
    }
}

impl FromStr for Range {
    type Err = ParseRevisionError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        if let Some(excluded) = text.strip_prefix('^') {
            return Ok(Self::Exclude(excluded.parse()?));
        }
        // Reference names cannot contain `..`, so the first one splits a range unless it is part
        // of a path after `:`.
        let range_end = path_separator(text).unwrap_or(text.len());
        let side = |side: &str| {
            if side.is_empty() {
                Ok(Revision::head())
            } else {
                side.parse()
            }
        };
        if let Some(dots) = text[..range_end].find("..") {
            let (left, right) = (&text[..dots], &text[dots + 2..]);
            if let Some(right) = right.strip_prefix('.') {
                return Ok(Self::Symmetric {
                    left: side(left)?,
                    right: side(right)?,
                });
            }
            return Ok(Self::Between {
                from: side(left)?,
                to: side(right)?,
            });
        }
        if text.ends_with("^@") || text.ends_with("^!") || text.contains("^-") {
            return Err(ParseRevisionError::new(
                text,
                Reason::Unsupported("`^@`, `^!` and `^-` parent shorthands"),
            ));
        }
        Ok(Self::Include(text.parse()?))
    }
}

/// The byte offset of the `:` that starts a path, skipping `:` inside `{...}`.
fn path_separator(text: &str) -> Option<usize> {
    let mut depth = 0usize;
    for (index, byte) in text.bytes().enumerate() {
        match byte {
            b'{' => depth += 1,
            b'}' => depth = depth.saturating_sub(1),
            b':' if depth == 0 => return Some(index),
            _ => {}
        }
    }
    None
}

fn at_selector(selector: &str, name: Option<String>) -> Result<Base, Reason> {
    if selector.eq_ignore_ascii_case("u") || selector.eq_ignore_ascii_case("upstream") {
        return Ok(Base::Upstream { branch: name });
    }
    if selector.eq_ignore_ascii_case("push") {
        return Ok(Base::Push { branch: name });
    }
    if selector.starts_with('-') {
        return Err(Reason::Unsupported("previous checkouts (`@{-N}`)"));
    }
    if !selector.is_empty() && selector.bytes().all(|b| b.is_ascii_digit()) {
        let index = selector
            .parse()
            .map_err(|_| Reason::Malformed("reflog index out of range"))?;
        return Ok(Base::Reflog { name, index });
    }
    Err(Reason::Unsupported("dated reflog selectors (`@{date}`)"))
}

fn steps(mut rest: &str) -> Result<Vec<Step>, Reason> {
    let mut steps = Vec::new();
    while let Some(operator) = rest.chars().next() {
        rest = &rest[operator.len_utf8()..];
        if operator == '^'
            && let Some(inner) = rest.strip_prefix('{')
        {
            let close = inner.find('}').ok_or(Reason::Malformed("unclosed `^{`"))?;
            let peel = match &inner[..close] {
                "" => Peel::Tags,
                "object" => Peel::Object,
                "commit" => Peel::Kind(ObjectKind::Commit),
                "tree" => Peel::Kind(ObjectKind::Tree),
                "blob" => Peel::Kind(ObjectKind::Blob),
                "tag" => Peel::Kind(ObjectKind::Tag),
                text if text.starts_with('/') => {
                    return Err(Reason::Unsupported("message searches (`^{/text}`)"));
                }
                _ => return Err(Reason::Malformed("unknown `^{...}` object kind")),
            };
            steps.push(Step::Peel(peel));
            rest = &inner[close + 1..];
            continue;
        }
        let digits = rest.bytes().take_while(u8::is_ascii_digit).count();
        let count = if digits == 0 {
            1
        } else {
            rest[..digits]
                .parse()
                .map_err(|_| Reason::Malformed("step count out of range"))?
        };
        rest = &rest[digits..];
        steps.push(match operator {
            '~' => Step::Ancestor(count),
            '^' => Step::Parent(count),
            '@' | '!' | '-' => {
                return Err(Reason::Unsupported("`^@`, `^!` and `^-` parent shorthands"));
            }
            _ => return Err(Reason::Malformed("expected `~`, `^` or `:` after a name")),
        });
    }
    Ok(steps)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(text: &str) -> Revision {
        text.parse().unwrap()
    }

    fn name(text: &str) -> Base {
        Base::Name(text.to_owned())
    }

    #[test]
    fn parses_steps_in_order() {
        let revision = parse("v1.0^{}~3^2^^{tree}");
        assert_eq!(revision.base(), &name("v1.0"));
        assert_eq!(
            revision.steps(),
            [
                Step::Peel(Peel::Tags),
                Step::Ancestor(3),
                Step::Parent(2),
                Step::Parent(1),
                Step::Peel(Peel::Kind(ObjectKind::Tree)),
            ]
        );
        assert_eq!(parse("HEAD~").steps(), [Step::Ancestor(1)]);
        assert_eq!(parse("HEAD^0").steps(), [Step::Parent(0)]);
    }

    #[test]
    fn parses_at_forms() {
        assert_eq!(parse("@").base(), &Base::Head);
        assert_eq!(parse("@~2").steps(), [Step::Ancestor(2)]);
        assert_eq!(
            parse("@{2}").base(),
            &Base::Reflog {
                name: None,
                index: 2
            }
        );
        assert_eq!(
            parse("main@{0}~1").base(),
            &Base::Reflog {
                name: Some("main".into()),
                index: 0
            }
        );
        assert_eq!(parse("@{U}").base(), &Base::Upstream { branch: None });
        assert_eq!(
            parse("topic@{upstream}").base(),
            &Base::Upstream {
                branch: Some("topic".into())
            }
        );
        assert_eq!(parse("@{push}").base(), &Base::Push { branch: None });
    }

    #[test]
    fn parses_paths() {
        assert_eq!(parse("HEAD:").path(), Some(""));
        assert_eq!(parse("HEAD^{tree}:a/b").path(), Some("a/b"));
        assert_eq!(parse("main:a..b").path(), Some("a..b"));
    }

    #[test]
    fn parses_ranges() {
        let range = |text: &str| text.parse::<Range>().unwrap();
        assert_eq!(range("^main"), Range::Exclude(parse("main")));
        assert_eq!(
            range("a..b"),
            Range::Between {
                from: parse("a"),
                to: parse("b")
            }
        );
        assert_eq!(
            range("..b"),
            Range::Between {
                from: Revision::head(),
                to: parse("b")
            }
        );
        assert_eq!(
            range("a..."),
            Range::Symmetric {
                left: parse("a"),
                right: Revision::head()
            }
        );
        assert_eq!(range("main:x..y"), Range::Include(parse("main:x..y")));
    }

    #[test]
    fn rejects_malformed_and_unsupported() {
        for text in [
            "",
            "~1",
            "main^{",
            "main@{1",
            "main^{nope}",
            "main~x",
            "HEAD~é",
        ] {
            let error = text.parse::<Revision>().unwrap_err();
            assert!(!error.is_unsupported(), "{text}: {error}");
        }
        for text in [
            ":path",
            ":/fix",
            "main^{/fix}",
            "@{-1}",
            "main@{yesterday}",
            "HEAD:./x",
        ] {
            let error = text.parse::<Revision>().unwrap_err();
            assert!(error.is_unsupported(), "{text}: {error}");
        }
        for text in ["main^@", "main^!", "main^-"] {
            assert!(
                text.parse::<Range>().unwrap_err().is_unsupported(),
                "{text}"
            );
        }
    }
}