grit-lib 0.1.4

Core library for the grit Git implementation
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
//! Multi-parent combined tree diff (Git `diff_tree_paths` / `find_paths_multitree`).
//!
//! Implements the `D(T,P1...Pn)` walk from `tree-diff.c` for merge commits, producing
//! per-path parent status letters used by `git diff-tree -c --name-status` and
//! `--find-object` filtering (`combined_objfind` in `combine-diff.c`).

use crate::diff::zero_oid;
use crate::error::Result;
use crate::objects::{parse_commit, parse_tree, tree_entry_cmp, ObjectId, ObjectKind, TreeEntry};
use crate::odb::Odb;

/// One character per parent for raw / name-status combined output (`A`/`M`/`D`).
#[must_use]
pub fn combined_parent_status_char(s: CombinedParentStatus) -> char {
    match s {
        CombinedParentStatus::Added => 'A',
        CombinedParentStatus::Modified => 'M',
        CombinedParentStatus::Deleted => 'D',
    }
}

/// Git raw combined line (`::::modes... oids... MM\tpath`).
#[must_use]
pub fn format_combined_raw_line(p: &CombinedDiffPath, abbrev_len: Option<usize>) -> String {
    let n = p.parents.len();
    let mut colons = String::with_capacity(n);
    for _ in 0..n {
        colons.push(':');
    }
    let mut modes = String::new();
    for side in &p.parents {
        modes.push_str(&format!("{:06o} ", side.mode));
    }
    modes.push_str(&format!("{:06o}", p.merge_mode));
    let mut oids = String::new();
    for side in &p.parents {
        let h = format!("{}", side.oid);
        let disp = if let Some(len) = abbrev_len {
            &h[..len.min(h.len())]
        } else {
            h.as_str()
        };
        oids.push(' ');
        oids.push_str(disp);
    }
    let rh = format!("{}", p.merge_oid);
    let rdisp = if let Some(len) = abbrev_len {
        &rh[..len.min(rh.len())]
    } else {
        rh.as_str()
    };
    oids.push(' ');
    oids.push_str(rdisp);
    oids.push(' ');
    let status: String = p
        .parents
        .iter()
        .map(|s| combined_parent_status_char(s.status))
        .collect();
    format!("{colons}{modes}{oids}{status}\t{}", p.path)
}

/// Per-parent coarse status in a combined diff (`A` / `M` / `D`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CombinedParentStatus {
    /// Parent lacks the path; merge tree added it.
    Added,
    /// Parent has the path at the same name as the merge result.
    Modified,
    /// Path removed from merge (only in parents that had it at the min name).
    Deleted,
}

/// One path in a combined diff: merge result tree vs each parent tree.
#[derive(Debug, Clone)]
pub struct CombinedDiffPath {
    /// Path relative to repository root.
    pub path: String,
    /// Mode and OID on the merge result side (`0` / zero OID when deleted from merge).
    pub merge_mode: u32,
    pub merge_oid: ObjectId,
    /// One slot per parent commit (same order as `parents` passed to [`combined_diff_paths_filtered`]).
    pub parents: Vec<CombinedParentSide>,
}

/// One parent's contribution at a combined-diff path.
#[derive(Debug, Clone)]
pub struct CombinedParentSide {
    pub mode: u32,
    pub oid: ObjectId,
    pub status: CombinedParentStatus,
}

/// Options for the multitree walk.
#[derive(Debug, Clone, Default)]
pub struct CombinedTreeDiffOptions {
    /// Recurse into sub-trees (`diff_options.flags.recursive`).
    pub recursive: bool,
    /// Emit tree directory lines when recursing (`tree_in_recursive`, e.g. `--find-object`).
    pub tree_in_recursive: bool,
}

fn is_tree_mode(mode: u32) -> bool {
    (mode & 0o170000) == 0o040000
}

fn read_tree_entries(odb: &Odb, oid: Option<&ObjectId>) -> Result<Vec<TreeEntry>> {
    let Some(oid) = oid else {
        return Ok(Vec::new());
    };
    let obj = odb.read(oid)?;
    if obj.kind != ObjectKind::Tree {
        return Ok(Vec::new());
    }
    parse_tree(&obj.data)
}

fn tree_entry_pathcmp(a: Option<&TreeEntry>, b: Option<&TreeEntry>) -> std::cmp::Ordering {
    match (a, b) {
        (None, None) => std::cmp::Ordering::Equal,
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (Some(_), None) => std::cmp::Ordering::Less,
        (Some(e1), Some(e2)) => tree_entry_cmp(
            &e1.name,
            is_tree_mode(e1.mode),
            &e2.name,
            is_tree_mode(e2.mode),
        ),
    }
}

fn combined_matches_find_object(path: &CombinedDiffPath, find: Option<&ObjectId>) -> bool {
    let Some(target) = find else {
        return true;
    };
    if path.merge_oid == *target {
        return true;
    }
    path.parents.iter().any(|p| p.oid == *target)
}

fn emit_combined_path(
    path: String,
    merge_entry: Option<&TreeEntry>,
    tp: &[Vec<TreeEntry>],
    tp_idx: &[usize],
    parent_neq: &[bool],
    find_object: Option<&ObjectId>,
    out: &mut Vec<CombinedDiffPath>,
) {
    let nparent = tp.len();
    let mut parents = Vec::with_capacity(nparent);

    if let Some(te) = merge_entry {
        for i in 0..nparent {
            let tpi_valid = !parent_neq[i];
            let (mode_i, oid_i, status) = if tpi_valid {
                let e = &tp[i][tp_idx[i]];
                (e.mode, e.oid, CombinedParentStatus::Modified)
            } else {
                (0u32, zero_oid(), CombinedParentStatus::Added)
            };
            parents.push(CombinedParentSide {
                mode: mode_i,
                oid: oid_i,
                status,
            });
        }
        let p = CombinedDiffPath {
            path,
            merge_mode: te.mode,
            merge_oid: te.oid,
            parents,
        };
        if combined_matches_find_object(&p, find_object) {
            out.push(p);
        }
    } else {
        for i in 0..nparent {
            let tpi_valid = !parent_neq[i];
            let (mode_i, oid_i, status) = if tpi_valid {
                let e = &tp[i][tp_idx[i]];
                (e.mode, e.oid, CombinedParentStatus::Deleted)
            } else {
                (0u32, zero_oid(), CombinedParentStatus::Added)
            };
            parents.push(CombinedParentSide {
                mode: mode_i,
                oid: oid_i,
                status,
            });
        }
        let p = CombinedDiffPath {
            path,
            merge_mode: 0,
            merge_oid: zero_oid(),
            parents,
        };
        if combined_matches_find_object(&p, find_object) {
            out.push(p);
        }
    }
}

fn should_recurse_dir(_path: &str, _opt: &CombinedTreeDiffOptions) -> bool {
    // Git's `diff_tree_paths` / combined merge diff always recurses into subtrees
    // (see `diff_tree_combined`: `diffopts.flags.recursive = 1`).
    true
}

fn ll_diff_tree_paths(
    odb: &Odb,
    out: &mut Vec<CombinedDiffPath>,
    base_path: &str,
    opt: &CombinedTreeDiffOptions,
    merge_oid: Option<&ObjectId>,
    parents_oid: &[Option<ObjectId>],
    find_object: Option<&ObjectId>,
) -> Result<()> {
    let nparent = parents_oid.len();
    let t_entries = read_tree_entries(odb, merge_oid)?;
    let mut tp_entries: Vec<Vec<TreeEntry>> = Vec::with_capacity(nparent);
    for po in parents_oid {
        tp_entries.push(read_tree_entries(odb, po.as_ref())?);
    }

    let mut ti = 0usize;
    let mut tp_idx = vec![0usize; nparent];

    loop {
        let t_cur = t_entries.get(ti);

        if t_cur.is_none() && (0..nparent).all(|i| tp_idx[i] >= tp_entries[i].len()) {
            break;
        }

        let mut imin = 0usize;
        if nparent > 0 {
            for i in 1..nparent {
                let e_imin = tp_entries[imin].get(tp_idx[imin]);
                let e_i = tp_entries[i].get(tp_idx[i]);
                if tree_entry_pathcmp(e_i, e_imin) == std::cmp::Ordering::Less {
                    imin = i;
                }
            }
        }

        let mut parent_neq = vec![false; nparent];
        for i in 0..nparent {
            let e_imin = tp_entries[imin].get(tp_idx[imin]);
            let e_i = tp_entries[i].get(tp_idx[i]);
            parent_neq[i] = tree_entry_pathcmp(e_i, e_imin) != std::cmp::Ordering::Equal;
        }
        for ne in parent_neq.iter_mut().take(imin) {
            *ne = true;
        }

        let p_min = tp_entries[imin].get(tp_idx[imin]);

        let cmp = tree_entry_pathcmp(t_cur, p_min);

        if cmp == std::cmp::Ordering::Equal {
            if let Some(te) = t_cur {
                let mut skip_emit = true;
                for i in 0..nparent {
                    if parent_neq[i] {
                        continue;
                    }
                    let Some(pe) = tp_entries[i].get(tp_idx[i]) else {
                        skip_emit = false;
                        break;
                    };
                    if pe.oid != te.oid || pe.mode != te.mode {
                        skip_emit = false;
                        break;
                    }
                }

                if !skip_emit {
                    let name = std::str::from_utf8(&te.name).unwrap_or("");
                    let full_path = if base_path.is_empty() {
                        name.to_string()
                    } else {
                        format!("{base_path}/{name}")
                    };

                    let isdir = is_tree_mode(te.mode);
                    let mut do_emit = true;
                    if isdir && should_recurse_dir(&full_path, opt) {
                        do_emit = opt.tree_in_recursive;
                    }

                    if do_emit {
                        emit_combined_path(
                            full_path.clone(),
                            Some(te),
                            &tp_entries,
                            &tp_idx,
                            &parent_neq,
                            find_object,
                            out,
                        );
                    }

                    if isdir && should_recurse_dir(&full_path, opt) {
                        let merge_child = te.oid;
                        let mut child_parent_opts = vec![None; nparent];
                        for i in 0..nparent {
                            if parent_neq[i] {
                                continue;
                            }
                            if let Some(pe) = tp_entries[i].get(tp_idx[i]) {
                                if tree_entry_cmp(
                                    &pe.name,
                                    is_tree_mode(pe.mode),
                                    &te.name,
                                    is_tree_mode(te.mode),
                                ) == std::cmp::Ordering::Equal
                                {
                                    child_parent_opts[i] = Some(pe.oid);
                                }
                            }
                        }
                        ll_diff_tree_paths(
                            odb,
                            out,
                            &full_path,
                            opt,
                            Some(&merge_child),
                            &child_parent_opts,
                            find_object,
                        )?;
                    }
                }
            }

            ti += 1;
            for i in 0..nparent {
                if !parent_neq[i] {
                    tp_idx[i] += 1;
                }
            }
        } else if cmp == std::cmp::Ordering::Less {
            let Some(te) = t_cur else {
                ti += 1;
                continue;
            };
            let name = std::str::from_utf8(&te.name).unwrap_or("");
            let full_path = if base_path.is_empty() {
                name.to_string()
            } else {
                format!("{base_path}/{name}")
            };
            let isdir = is_tree_mode(te.mode);
            let mut do_emit = true;
            if isdir && should_recurse_dir(&full_path, opt) {
                do_emit = opt.tree_in_recursive;
            }
            if do_emit {
                let all_parents_absent: Vec<bool> = vec![true; nparent];
                emit_combined_path(
                    full_path.clone(),
                    Some(te),
                    &tp_entries,
                    &tp_idx,
                    &all_parents_absent,
                    find_object,
                    out,
                );
            }
            if isdir && should_recurse_dir(&full_path, opt) {
                let merge_child = te.oid;
                let child_parent_opts = vec![None; nparent];
                ll_diff_tree_paths(
                    odb,
                    out,
                    &full_path,
                    opt,
                    Some(&merge_child),
                    &child_parent_opts,
                    find_object,
                )?;
            }
            ti += 1;
        } else {
            let skip_emit_tp = (0..nparent).all(|i| parent_neq[i]);
            if !skip_emit_tp {
                if let Some(pe) = p_min {
                    let name = std::str::from_utf8(&pe.name).unwrap_or("");
                    let full_path = if base_path.is_empty() {
                        name.to_string()
                    } else {
                        format!("{base_path}/{name}")
                    };
                    let isdir = is_tree_mode(pe.mode);
                    let mut do_emit = true;
                    if isdir && should_recurse_dir(&full_path, opt) {
                        do_emit = opt.tree_in_recursive;
                    }
                    if do_emit {
                        emit_combined_path(
                            full_path.clone(),
                            None,
                            &tp_entries,
                            &tp_idx,
                            &parent_neq,
                            find_object,
                            out,
                        );
                    }
                    if isdir && should_recurse_dir(&full_path, opt) {
                        let mut child_parent_opts = vec![None; nparent];
                        for i in 0..nparent {
                            if !parent_neq[i] {
                                if let Some(e) = tp_entries[i].get(tp_idx[i]) {
                                    child_parent_opts[i] = Some(e.oid);
                                }
                            }
                        }
                        ll_diff_tree_paths(
                            odb,
                            out,
                            &full_path,
                            opt,
                            None,
                            &child_parent_opts,
                            find_object,
                        )?;
                    }
                }
            }
            for i in 0..nparent {
                if !parent_neq[i] {
                    tp_idx[i] += 1;
                }
            }
        }
    }

    Ok(())
}

/// Build combined-diff paths for a merge commit's tree against each parent's tree.
pub fn combined_diff_paths_filtered(
    odb: &Odb,
    commit_tree: &ObjectId,
    parents: &[ObjectId],
    walk: &CombinedTreeDiffOptions,
    find_object: Option<&ObjectId>,
) -> Result<Vec<CombinedDiffPath>> {
    if parents.is_empty() {
        return Ok(Vec::new());
    }
    let mut parent_trees = Vec::with_capacity(parents.len());
    for p in parents {
        let obj = odb.read(p)?;
        if obj.kind != ObjectKind::Commit {
            return Ok(Vec::new());
        }
        let c = parse_commit(&obj.data)?;
        parent_trees.push(Some(c.tree));
    }
    let parent_opts: Vec<Option<ObjectId>> = parent_trees;
    combined_diff_paths_trees(odb, commit_tree, &parent_opts, walk, find_object)
}

/// Combined diff paths when `parents` are already tree OIDs (plumbing `git diff --cc` with N+1 trees).
pub fn combined_diff_paths_trees(
    odb: &Odb,
    merge_tree: &ObjectId,
    parent_trees: &[Option<ObjectId>],
    walk: &CombinedTreeDiffOptions,
    find_object: Option<&ObjectId>,
) -> Result<Vec<CombinedDiffPath>> {
    if parent_trees.is_empty() {
        return Ok(Vec::new());
    }
    let mut out = Vec::new();
    ll_diff_tree_paths(
        odb,
        &mut out,
        "",
        walk,
        Some(merge_tree),
        parent_trees,
        find_object,
    )?;
    Ok(out)
}