grit-lib 0.5.21

Core library for the grit Git implementation
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
//! Typed inputs for building tree and commit objects without an index or work tree.
//!
//! [`Objects::write_tree`](super::Objects::write_tree) and
//! [`Objects::write_commit`](super::Objects::write_commit) take these types and
//! produce objects that pass `git fsck --strict`.

use std::collections::HashSet;

use crate::change_id::ChangeId;
use crate::fsck_standalone::{tree_entry_mode_allowed, MAX_TREE_ENTRY_LEN};
use crate::ident::Signature;
use crate::objects::{tree_entry_cmp, CommitData, HashAlgo, ObjectId, TreeEntry};
use crate::path_validate::{audit_tree_entry_name, PathComponentError, ProtectOptions};

const MODE_TREE: u32 = 0o040000;

/// Why [`Objects::write_tree`](super::Objects::write_tree) refused an entry.
///
/// Each variant carries the offending entry name as raw bytes (tree entry names
/// are not required to be UTF-8).
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum TreeEntryError {
    /// The name is empty, `.`, `..`, a `.git` alias, or contains `/` or NUL.
    #[error("invalid tree entry name '{}': {reason}", String::from_utf8_lossy(.name))]
    InvalidName {
        /// Entry name as given.
        name: Vec<u8>,
        /// Which path-component rule the name breaks.
        reason: PathComponentError,
    },
    /// The name is longer than the 4096 bytes `git fsck --strict` accepts.
    #[error(
        "tree entry name of {} bytes exceeds the {max}-byte limit",
        .name.len()
    )]
    NameTooLong {
        /// Entry name.
        name: Vec<u8>,
        /// Longest accepted name, in bytes.
        max: usize,
    },
    /// The mode is not one of `100644`, `100755`, `120000`, `40000` or `160000`.
    #[error("tree entry '{}' has unsupported mode {mode:o}", String::from_utf8_lossy(.name))]
    InvalidMode {
        /// Entry name.
        name: Vec<u8>,
        /// Mode as given.
        mode: u32,
    },
    /// The entry points at the all-zero object id.
    #[error("tree entry '{}' points at the null object id", String::from_utf8_lossy(.name))]
    NullObjectId {
        /// Entry name.
        name: Vec<u8>,
    },
    /// The entry's object id uses a different hash algorithm than the repository.
    #[error(
        "tree entry '{}' has a {} object id in a {} repository",
        String::from_utf8_lossy(.name),
        .actual.name(),
        .expected.name()
    )]
    HashAlgoMismatch {
        /// Entry name.
        name: Vec<u8>,
        /// The repository's object format.
        expected: HashAlgo,
        /// The algorithm of the entry's object id.
        actual: HashAlgo,
    },
    /// Two entries share a name (including a file and a directory of the same name).
    #[error("tree lists '{}' more than once", String::from_utf8_lossy(.name))]
    DuplicateName {
        /// The repeated name.
        name: Vec<u8>,
    },
}

/// Validate `entries` for `algo` and return them in canonical Git tree order.
///
/// The checks cover every rule `git fsck --strict` applies to a tree, so the
/// serialized result needs no second fsck pass.
///
/// # Errors
///
/// The first [`TreeEntryError`] found, checking each entry in input order and
/// then duplicate names in sorted order.
pub(crate) fn canonical_tree_entries(
    entries: impl IntoIterator<Item = TreeEntry>,
    algo: HashAlgo,
) -> Result<Vec<TreeEntry>, TreeEntryError> {
    let mut entries: Vec<TreeEntry> = entries.into_iter().collect();
    for entry in &entries {
        validate_tree_entry(entry, algo)?;
    }
    entries
        .sort_by(|a, b| tree_entry_cmp(&a.name, a.mode == MODE_TREE, &b.name, b.mode == MODE_TREE));
    let mut seen = HashSet::with_capacity(entries.len());
    if let Some(dup) = entries.iter().find(|e| !seen.insert(e.name.as_slice())) {
        return Err(TreeEntryError::DuplicateName {
            name: dup.name.clone(),
        });
    }
    Ok(entries)
}

fn validate_tree_entry(entry: &TreeEntry, algo: HashAlgo) -> Result<(), TreeEntryError> {
    audit_tree_entry_name(&entry.name, ProtectOptions::fsck())
        .first_error()
        .map_err(|reason| TreeEntryError::InvalidName {
            name: entry.name.clone(),
            reason,
        })?;
    if entry.name.len() > MAX_TREE_ENTRY_LEN {
        return Err(TreeEntryError::NameTooLong {
            name: entry.name.clone(),
            max: MAX_TREE_ENTRY_LEN,
        });
    }
    if !tree_entry_mode_allowed(entry.mode) {
        return Err(TreeEntryError::InvalidMode {
            name: entry.name.clone(),
            mode: entry.mode,
        });
    }
    if entry.oid.algo() != algo {
        return Err(TreeEntryError::HashAlgoMismatch {
            name: entry.name.clone(),
            expected: algo,
            actual: entry.oid.algo(),
        });
    }
    if entry.oid.is_zero() {
        return Err(TreeEntryError::NullObjectId {
            name: entry.name.clone(),
        });
    }
    Ok(())
}

/// A commit to create with [`Objects::write_commit`](super::Objects::write_commit).
///
/// Identities are typed [`Signature`]s, so the `author` and `committer` header
/// lines always have Git's `Name <email> <unix> <±HHMM>` shape.
///
/// # Examples
///
/// ```
/// # fn main() -> grit_lib::error::Result<()> {
/// use grit_lib::ident::Signature;
/// use grit_lib::objects::{NewCommit, ObjectKind, TreeEntry};
/// use grit_lib::repo::{InitOptions, Repository};
/// use time::OffsetDateTime;
///
/// let dir = tempfile::tempdir()?;
/// let repo = Repository::init(dir.path(), &InitOptions::new().with_bare(true))?;
/// let blob = repo.objects().write(ObjectKind::Blob, b"hello\n")?;
/// let tree = repo.objects().write_tree([TreeEntry {
///     mode: 0o100644,
///     name: b"README".to_vec(),
///     oid: blob,
/// }])?;
/// let when = OffsetDateTime::from_unix_timestamp(1_700_000_000).map_err(|e| {
///     grit_lib::error::Error::Message(e.to_string())
/// })?;
/// let ada = Signature::new("Ada Lovelace", "ada@example.com", when)?;
/// let commit = repo
///     .objects()
///     .write_commit(&NewCommit::new(tree, ada.clone(), ada, "Initial commit"))?;
/// assert_eq!(repo.objects().read_commit(&commit)?.message, "Initial commit\n");
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NewCommit {
    tree: ObjectId,
    parents: Vec<ObjectId>,
    author: Signature,
    committer: Signature,
    message: String,
    change_id: Option<ChangeId>,
}

impl NewCommit {
    /// A root commit of `tree` with no parents.
    ///
    /// # Parameters
    ///
    /// - `tree`: the commit's root tree (must exist in the repository when written).
    /// - `author`, `committer`: identities and timestamps for the two header lines.
    /// - `message`: commit message; a trailing newline is added when missing.
    #[must_use]
    pub fn new(
        tree: ObjectId,
        author: Signature,
        committer: Signature,
        message: impl Into<String>,
    ) -> Self {
        Self {
            tree,
            parents: Vec::new(),
            author,
            committer,
            message: message.into(),
            change_id: None,
        }
    }

    /// Replace the parent list (first parent first; each must name a commit when written).
    #[must_use]
    pub fn with_parents(mut self, parents: impl IntoIterator<Item = ObjectId>) -> Self {
        self.parents = parents.into_iter().collect();
        self
    }

    /// Record `id` as the commit's `change-id` header instead of assigning a fresh one.
    ///
    /// Use this when the new commit rewrites an existing change (amend, rebase) so the
    /// change keeps its identity. Without it, [`Objects::write_commit`](super::Objects::write_commit)
    /// assigns a new id when `grit.changeIds` is enabled (the default).
    #[must_use]
    pub fn with_change_id(mut self, id: ChangeId) -> Self {
        self.change_id = Some(id);
        self
    }

    /// Explicit change id set with [`Self::with_change_id`].
    pub(crate) fn change_id(&self) -> Option<&ChangeId> {
        self.change_id.as_ref()
    }

    /// The tree and parents, for existence checks before writing.
    pub(crate) fn tree_and_parents(&self) -> (&ObjectId, &[ObjectId]) {
        (&self.tree, &self.parents)
    }

    /// The two identities, for validation before writing.
    pub(crate) fn signatures(&self) -> [&Signature; 2] {
        [&self.author, &self.committer]
    }

    /// Commit fields with header lines rendered from the signatures and no extra headers.
    pub(crate) fn to_commit_data(&self) -> CommitData {
        let mut message = self.message.clone();
        if !message.is_empty() && !message.ends_with('\n') {
            message.push('\n');
        }
        CommitData {
            tree: self.tree,
            parents: self.parents.clone(),
            author: self.author.header_line(),
            committer: self.committer.header_line(),
            author_raw: Vec::new(),
            committer_raw: Vec::new(),
            encoding: None,
            message,
            raw_message: None,
            extra_headers: Vec::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::objects::HashAlgo;
    use time::OffsetDateTime;

    fn oid(byte: u8) -> ObjectId {
        ObjectId::from_bytes(&[byte; 20]).unwrap()
    }

    fn entry(mode: u32, name: &[u8], byte: u8) -> TreeEntry {
        TreeEntry {
            mode,
            name: name.to_vec(),
            oid: oid(byte),
        }
    }

    #[test]
    fn sorts_into_git_tree_order() {
        let sorted = canonical_tree_entries(
            [
                entry(0o100644, b"foo.c", 1),
                entry(MODE_TREE, b"foo", 2),
                entry(0o100644, b"foo-bar", 3),
                entry(0o100644, b"a", 4),
            ],
            HashAlgo::Sha1,
        )
        .unwrap();
        let names: Vec<&[u8]> = sorted.iter().map(|e| e.name.as_slice()).collect();
        assert_eq!(
            names,
            [&b"a"[..], &b"foo-bar"[..], &b"foo.c"[..], &b"foo"[..]]
        );
    }

    #[test]
    fn rejects_file_and_directory_with_the_same_name() {
        let err = canonical_tree_entries(
            [
                entry(0o100644, b"foo", 1),
                entry(0o100644, b"foo.c", 2),
                entry(MODE_TREE, b"foo", 3),
            ],
            HashAlgo::Sha1,
        )
        .unwrap_err();
        assert_eq!(
            err,
            TreeEntryError::DuplicateName {
                name: b"foo".to_vec()
            }
        );
    }

    #[test]
    fn rejects_bad_names_modes_and_ids() {
        let cases: [(TreeEntry, TreeEntryError); 7] = [
            (
                entry(0o100644, &[b'x'; MAX_TREE_ENTRY_LEN + 1], 1),
                TreeEntryError::NameTooLong {
                    name: vec![b'x'; MAX_TREE_ENTRY_LEN + 1],
                    max: MAX_TREE_ENTRY_LEN,
                },
            ),
            (
                entry(0o100644, b"", 1),
                TreeEntryError::InvalidName {
                    name: Vec::new(),
                    reason: PathComponentError::Empty,
                },
            ),
            (
                entry(0o100644, b"a/b", 1),
                TreeEntryError::InvalidName {
                    name: b"a/b".to_vec(),
                    reason: PathComponentError::ContainsSlash,
                },
            ),
            (
                entry(0o100644, b"..", 1),
                TreeEntryError::InvalidName {
                    name: b"..".to_vec(),
                    reason: PathComponentError::DotDot,
                },
            ),
            (
                entry(MODE_TREE, b".GIT", 1),
                TreeEntryError::InvalidName {
                    name: b".GIT".to_vec(),
                    reason: PathComponentError::DotGit,
                },
            ),
            (
                entry(0o100664, b"a", 1),
                TreeEntryError::InvalidMode {
                    name: b"a".to_vec(),
                    mode: 0o100664,
                },
            ),
            (
                entry(0o100644, b"a", 0),
                TreeEntryError::NullObjectId {
                    name: b"a".to_vec(),
                },
            ),
        ];
        for (input, expected) in cases {
            assert_eq!(
                canonical_tree_entries([input], HashAlgo::Sha1).unwrap_err(),
                expected
            );
        }
    }

    #[test]
    fn rejects_object_ids_of_the_other_hash_algorithm() {
        let err = canonical_tree_entries([entry(0o100644, b"a", 1)], HashAlgo::Sha256).unwrap_err();
        assert!(matches!(
            err,
            TreeEntryError::HashAlgoMismatch {
                expected: HashAlgo::Sha256,
                actual: HashAlgo::Sha1,
                ..
            }
        ));
    }

    #[test]
    fn commit_data_terminates_message_and_renders_signatures() {
        let when = OffsetDateTime::from_unix_timestamp(1_700_000_000).unwrap();
        let sig = Signature::new("A U Thor", "a@example.com", when).unwrap();
        let data = NewCommit::new(oid(1), sig.clone(), sig, "subject")
            .with_parents([oid(2)])
            .to_commit_data();
        assert_eq!(data.message, "subject\n");
        assert_eq!(data.parents, vec![oid(2)]);
        assert_eq!(data.author, "A U Thor <a@example.com> 1700000000 +0000");
        assert!(data.extra_headers.is_empty());
    }
}