gitpoulpi-git2-backend 1.1.0

GitBackend implementation over libgit2 (git2 crate), decorating gitpoulpi-cli-backend for operations git2 doesn't support natively.
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
mod files;
mod messages;
mod refs;

#[cfg(feature = "submodule")]
pub use files::submodules;
pub use files::{
    blame, conflict_entries, grep_tree, list_tree, merge_document, provably_no_conflicts,
    provably_no_rebase, read_blob, read_blob_bytes, worktrees,
};
pub use messages::commit_messages;
pub use refs::{ahead_behind, merge_base, refs, remotes, resolve, stashes};

use std::cell::RefCell;
use std::path::PathBuf;

use gitpoulpi_core::domain::{
    commit_summary, decode_commit_text, CommitGraph, CommitNode, CommitStat, DiffFileList,
    DiffLine, DiffTarget, FileDiff, FileStatus, Hunk, HunkId, LineOrigin, LogOptions, Oid, RepoRef,
    StatusEntry, WorkingTree,
};
use gitpoulpi_core::Result;

use crate::repo::{map_err, open, to_domain_oid, to_git2_oid};

pub fn status(repo: &RepoRef) -> Result<WorkingTree> {
    let git_repo = open(repo)?;
    let mut opts = git2::StatusOptions::new();
    opts.include_untracked(true)
        .recurse_untracked_dirs(true)
        // A staged rename is one entry at its new path, as git's own status.
        .renames_head_to_index(true);
    let statuses = git_repo.statuses(Some(&mut opts)).map_err(map_err)?;

    let mut files: Vec<StatusEntry> = statuses
        .iter()
        .filter_map(|entry| {
            let s = entry.status();
            // For a staged rename libgit2's `path()` is the OLD path: the
            // new one and the origin come from the head-to-index delta.
            let (path, renamed_from) = match entry.head_to_index() {
                Some(delta) if s.contains(git2::Status::INDEX_RENAMED) => (
                    delta.new_file().path().map(PathBuf::from)?,
                    delta.old_file().path().map(PathBuf::from),
                ),
                _ => (entry.path().ok().map(PathBuf::from)?, None),
            };

            let index = if s.contains(git2::Status::CONFLICTED) {
                Some(FileStatus::Conflicted)
            } else if s.contains(git2::Status::INDEX_NEW) {
                Some(FileStatus::Added)
            } else if s.contains(git2::Status::INDEX_MODIFIED) {
                Some(FileStatus::Modified)
            } else if s.contains(git2::Status::INDEX_DELETED) {
                Some(FileStatus::Deleted)
            } else if s.contains(git2::Status::INDEX_RENAMED) {
                Some(FileStatus::Renamed)
            } else {
                None
            };

            let worktree = if s.contains(git2::Status::CONFLICTED) {
                Some(FileStatus::Conflicted)
            } else if s.contains(git2::Status::WT_NEW) {
                Some(FileStatus::Untracked)
            } else if s.contains(git2::Status::WT_MODIFIED) {
                Some(FileStatus::Modified)
            } else if s.contains(git2::Status::WT_DELETED) {
                Some(FileStatus::Deleted)
            } else if s.contains(git2::Status::WT_RENAMED) {
                Some(FileStatus::Renamed)
            } else {
                None
            };

            if index.is_none() && worktree.is_none() {
                return None;
            }
            Some(StatusEntry {
                renamed_from,
                path,
                index,
                worktree,
            })
        })
        .collect();

    pair_worktree_renames(&git_repo, &mut files);

    Ok(WorkingTree {
        files,
        ..WorkingTree::default()
    })
}

/// Pairs the working tree's deleted tracked files with its new ones (what
/// libgit2's status leaves as a deletion plus an untracked file): the shared
/// `pair_unstaged_renames`, the old side read from the index.
fn pair_worktree_renames(git_repo: &git2::Repository, files: &mut [StatusEntry]) {
    use gitpoulpi_core::domain::{
        is_unstaged_rename_source, pair_unstaged_renames, read_workdir_candidate, RenameSide,
        RENAME_LIMIT,
    };
    let sources = files
        .iter()
        .filter(|e| is_unstaged_rename_source(e))
        .count();
    let any_new = files.iter().any(|e| {
        e.index.is_none()
            && matches!(
                e.worktree,
                Some(FileStatus::Untracked) | Some(FileStatus::Added)
            )
    });
    if sources == 0 || sources > RENAME_LIMIT || !any_new {
        return;
    }
    let (Some(root), Ok(index)) = (git_repo.workdir().map(PathBuf::from), git_repo.index()) else {
        return;
    };
    pair_unstaged_renames(files, |path, side| match side {
        RenameSide::New => read_workdir_candidate(&root, path),
        RenameSide::Old => {
            let entry = index.get_path(path, 0)?;
            // A submodule (gitlink) is not a blob.
            if entry.mode & 0o170000 != 0o100000 {
                return None;
            }
            let blob = git_repo.find_blob(entry.id).ok()?;
            (blob.size() as u64 <= gitpoulpi_core::domain::RENAME_MAX_FILE_BYTES)
                .then(|| blob.content().to_vec())
        }
    });
}

/// The commit total counted in-process (`log.commit_count = "libgit2"`):
/// the same refs as `log` (branches, tags, remotes; `HEAD` or `start_ref`
/// otherwise), no sorting, nothing hidden and no commit object loaded by
/// this code, so no process is spawned and the walk is as cheap as libgit2
/// allows. A path filter needs history simplification libgit2 has no
/// equivalent of, so that one case still asks system git.
pub fn count_commits(repo: &RepoRef, opts: &LogOptions) -> Result<u64> {
    if !opts.paths.is_empty() {
        return gitpoulpi_core::GitBackend::count_commits(
            &gitpoulpi_cli_backend::SystemGitBackend::new(),
            repo,
            opts,
        );
    }
    let git_repo = open(repo)?;
    let Ok(mut revwalk) = git_repo.revwalk() else {
        return Ok(0);
    };
    let pushed = if opts.all_refs {
        // Same refs as `log` (never the remote-tag mirror).
        push_all_refs(&git_repo, &mut revwalk, &opts.hidden)
    } else {
        match &opts.start_ref {
            Some(start) => revwalk.push_ref(start).or_else(|_| {
                git_repo
                    .revparse_single(start)
                    .and_then(|object| revwalk.push(object.id()))
            }),
            None => revwalk.push_head(),
        }
    };
    if pushed.is_err() {
        // Unborn branch: a valid state, zero commits.
        return Ok(0);
    }
    // Order is irrelevant to a count; skipping the sort lets libgit2 stop
    // buffering and sorting the whole history.
    revwalk.set_sorting(git2::Sort::NONE).map_err(map_err)?;
    Ok(revwalk.count() as u64)
}

/// The repository's `.mailmap` (the worktree's file, else `HEAD:.mailmap`
/// in a bare repository) and then `mailmap.file`, as git layers them.
///
/// Parsed by `gitpoulpi_core`'s `Mailmap`, not libgit2's: libgit2 compares
/// the email case-sensitively where git does not (`ALEX@old.example.com` is
/// not mapped by a line for `alex@old.example.com`), and the contract suite
/// pins that both backends show what `git log --use-mailmap` shows.
fn load_mailmap(git_repo: &git2::Repository) -> gitpoulpi_core::domain::rewrite::Mailmap {
    use gitpoulpi_core::domain::rewrite::Mailmap;
    let mut map = match git_repo.workdir() {
        Some(dir) => std::fs::read_to_string(dir.join(".mailmap")).unwrap_or_default(),
        None => git_repo
            .revparse_single("HEAD:.mailmap")
            .ok()
            .and_then(|o| o.into_blob().ok())
            .map(|b| String::from_utf8_lossy(b.content()).into_owned())
            .unwrap_or_default(),
    };
    map = map.replace('\r', "");
    let mut out = Mailmap::parse(&map);
    if let Some(file) = git_repo
        .config()
        .ok()
        .and_then(|c| c.get_path("mailmap.file").ok())
    {
        if let Ok(text) = std::fs::read_to_string(file) {
            out.extend(Mailmap::parse(&text));
        }
    }
    out
}

/// Each identity as the `.mailmap` shows it (see `GitBackend::mailmap_resolve`).
pub fn mailmap_resolve(
    repo: &RepoRef,
    idents: &[gitpoulpi_core::domain::rewrite::Ident],
) -> Result<Vec<gitpoulpi_core::domain::rewrite::Ident>> {
    let map = load_mailmap(&open(repo)?);
    Ok(idents.iter().map(|i| map.resolve(i)).collect())
}

/// Seed `revwalk` for an `all_refs` walk. Without a filter, the three ref
/// families by glob; with one, a push per visible tip
/// (`gitpoulpi_core::domain::visible_tips`, the decision both backends
/// share). `Err` means the walk has no starting point (unborn, or nothing
/// visible).
fn push_all_refs(
    git_repo: &git2::Repository,
    revwalk: &mut git2::Revwalk<'_>,
    filter: &gitpoulpi_core::domain::RefFilter,
) -> std::result::Result<(), git2::Error> {
    if filter.is_empty() {
        // A repository can lack any of the three (no remotes, no tags) and
        // `push_glob` errors on a glob that matches nothing, so only a
        // failure of *all three* means the walk has no starting point.
        let heads = revwalk.push_glob("refs/heads/*");
        let tags = revwalk.push_glob("refs/tags/*");
        let remotes = revwalk.push_glob("refs/remotes/*");
        return if heads.is_ok() || tags.is_ok() || remotes.is_ok() {
            Ok(())
        } else {
            heads
        };
    }
    let mut refs: Vec<(String, Oid)> = Vec::new();
    for pattern in ["refs/heads/*", "refs/tags/*", "refs/remotes/*"] {
        for reference in git_repo.references_glob(pattern)?.flatten() {
            // `refs/remotes/<r>/HEAD` and friends mirror another ref: not a
            // tip of their own (hiding the branch they name must hide it).
            if reference.kind() == Some(git2::ReferenceType::Symbolic) {
                continue;
            }
            let (Ok(name), Ok(commit)) = (reference.name(), reference.peel_to_commit()) else {
                // A tag of a tree or a blob: no commit, no tip.
                continue;
            };
            if let Ok(id) = to_domain_oid(commit.id()) {
                refs.push((name.to_string(), id));
            }
        }
    }
    let head = git_repo
        .head()
        .ok()
        .and_then(|h| h.peel_to_commit().ok())
        .and_then(|c| to_domain_oid(c.id()).ok());
    let tips = gitpoulpi_core::domain::visible_tips(filter, &refs, head);
    if tips.is_empty() {
        return Err(git2::Error::from_str("no visible tip"));
    }
    for tip in tips {
        revwalk.push(git2::Oid::from_bytes(&tip.0)?)?;
    }
    Ok(())
}

pub fn log(repo: &RepoRef, opts: &LogOptions) -> Result<CommitGraph> {
    let git_repo = open(repo)?;
    let mut revwalk = match git_repo.revwalk() {
        Ok(w) => w,
        Err(_) => return Ok(CommitGraph::default()),
    };

    let pushed = if opts.all_refs {
        // The three ref families the UI draws, named explicitly: not
        // `push_glob("*")`, which would include GitPoulpi's remote-tag mirror
        // (`refs/gitpoulpi/remote-tags/...`), bookkeeping that must never put
        // a line in the commit graph. The system-git backend says
        // `--branches --tags --remotes` for the same reason.
        push_all_refs(&git_repo, &mut revwalk, &opts.hidden)
    } else {
        match &opts.start_ref {
            // A ref name, else any revision git parses (a commit id): the
            // system-git backend takes both, and so must this one.
            Some(start) => revwalk.push_ref(start).or_else(|_| {
                git_repo
                    .revparse_single(start)
                    .and_then(|object| revwalk.push(object.id()))
            }),
            None => revwalk.push_head(),
        }
    };
    if pushed.is_err() {
        // unborn branch (no commits yet) -- a valid, empty state.
        return Ok(CommitGraph::default());
    }
    // TIME alone is reverse-chronological and does NOT guarantee a
    // commit's parents are walked after it; with diverged branches a parent
    // could surface before a child and corrupt the TUI's one-row-per-commit
    // lane bookkeeping in `build_graph_lines` (it assumes parents appear
    // later in `nodes`). TOPOLOGICAL enforces "children before parents";
    // with TIME it is the git2 equivalent of `git log --graph --date-order`.
    revwalk
        .set_sorting(git2::Sort::TIME | git2::Sort::TOPOLOGICAL)
        .map_err(map_err)?;

    // `log.use_mailmap`: the same files `git log --format=%aN` reads.
    let mailmap = opts.use_mailmap.then(|| load_mailmap(&git_repo));
    let mut nodes = Vec::new();
    // `skip` is applied to the *walk*, before `limit`, so page `n` is the
    // same slice of the same ordering the unpaged walk would produce (see
    // `check_log_skip_pages_history`). libgit2 has no `--skip`, so oids are
    // skipped without being turned into a `Commit`: one revwalk step per
    // skipped commit and nothing more.
    let mut to_skip = opts.skip;
    for oid in revwalk {
        if to_skip > 0 {
            to_skip -= 1;
            continue;
        }
        if let Some(limit) = opts.limit {
            if nodes.len() as u32 >= limit {
                break;
            }
        }
        let oid = oid.map_err(map_err)?;
        let commit = git_repo.find_commit(oid).map_err(map_err)?;
        let author = commit.author();
        // Display text follows git: the commit's `encoding` header, else
        // valid UTF-8, else the fallback encoding (`git.default_encoding`).
        let header = commit.message_encoding().ok().flatten();
        let message_bytes = commit.message_bytes();
        let decoded = decode_commit_text(message_bytes, header);
        let summary = match (&decoded, header) {
            (std::borrow::Cow::Borrowed(_), None) => commit
                .summary()
                .ok()
                .flatten()
                .unwrap_or_default()
                .to_string(),
            _ => commit_summary(&decoded),
        };
        let message = decoded.trim_end().to_string();
        let mut author_name = decode_commit_text(author.name_bytes(), header).into_owned();
        let mut author_email = author.email().unwrap_or_default().to_string();
        if let Some(map) = mailmap.as_ref().filter(|m| !m.is_empty()) {
            let shown = map.resolve(&gitpoulpi_core::domain::rewrite::Ident::new(
                &author_name,
                &author_email,
            ));
            author_name = shown.name;
            author_email = shown.email;
        }
        nodes.push(CommitNode {
            id: to_domain_oid(oid)?,
            parents: commit
                .parent_ids()
                .map(to_domain_oid)
                .collect::<Result<Vec<_>>>()?,
            summary,
            // git2's message() keeps the trailing newline git normalizes
            // onto stored messages; trimmed to match SystemGitBackend's `%B`
            // parsing (see cli-backend's log.rs).
            message,
            author_name,
            author_email,
            // The author date, as SystemGitBackend's `%at` (a rebased or
            // amended commit keeps it; the committer date is "now").
            timestamp: commit.author().when().seconds(),
            refs: Vec::new(),
        });
    }

    Ok(CommitGraph { nodes })
}

struct DiffBuilder<'r> {
    files: Vec<FileDiff>,
    /// For a renamed file's similarity (libgit2 does not expose its own).
    repo: &'r git2::Repository,
}

impl<'r> DiffBuilder<'r> {
    fn new(repo: &'r git2::Repository) -> Self {
        DiffBuilder {
            files: Vec::new(),
            repo,
        }
    }

    /// A renamed or copied file's similarity in percent: 100 for the same
    /// object, else the shared measure over the two sides' bytes.
    fn rename_similarity(&self, delta: &git2::DiffDelta) -> Option<u8> {
        let (old, new) = (delta.old_file(), delta.new_file());
        if !new.id().is_zero() && old.id() == new.id() {
            return Some(100);
        }
        let old_bytes = self.repo.find_blob(old.id()).ok()?.content().to_vec();
        let new_bytes = if new.id().is_zero() {
            std::fs::read(self.repo.workdir()?.join(new.path()?)).ok()?
        } else {
            self.repo.find_blob(new.id()).ok()?.content().to_vec()
        };
        Some(gitpoulpi_core::domain::rename_similarity(
            &old_bytes, &new_bytes,
        ))
    }

    fn on_file(&mut self, delta: git2::DiffDelta) {
        let path = delta
            .new_file()
            .path()
            .or_else(|| delta.old_file().path())
            .map(PathBuf::from)
            .unwrap_or_default();
        let status = match delta.status() {
            git2::Delta::Added => FileStatus::Added,
            git2::Delta::Deleted => FileStatus::Deleted,
            git2::Delta::Renamed | git2::Delta::Copied => FileStatus::Renamed,
            _ => FileStatus::Modified,
        };
        let renamed = matches!(delta.status(), git2::Delta::Renamed | git2::Delta::Copied);
        let renamed_from = renamed
            .then(|| delta.old_file().path().map(PathBuf::from))
            .flatten();
        let similarity = renamed.then(|| self.rename_similarity(&delta)).flatten();
        self.files.push(FileDiff {
            renamed_from,
            similarity,
            path,
            status,
            hunks: Vec::new(),
        });
    }

    fn on_hunk(&mut self, hunk: git2::DiffHunk) {
        if let Some(file) = self.files.last_mut() {
            file.hunks.push(Hunk {
                id: HunkId(file.hunks.len() as u32),
                old_start: hunk.old_start(),
                old_lines: hunk.old_lines(),
                new_start: hunk.new_start(),
                new_lines: hunk.new_lines(),
                lines: Vec::new(),
            });
        }
    }

    fn on_line(&mut self, line: git2::DiffLine) {
        let origin = match line.origin() {
            '+' => LineOrigin::Add,
            '-' => LineOrigin::Delete,
            ' ' => LineOrigin::Context,
            _ => return,
        };
        let content = gitpoulpi_core::domain::decode_text_lossy(line.content())
            .trim_end_matches(['\r', '\n'])
            .to_string();
        if let Some(file) = self.files.last_mut() {
            if let Some(hunk) = file.hunks.last_mut() {
                hunk.lines.push(DiffLine {
                    origin,
                    content,
                    staged: false,
                });
            }
        }
    }
}

fn build_file_diffs(git_repo: &git2::Repository, diff: &git2::Diff) -> Result<Vec<FileDiff>> {
    let builder = RefCell::new(DiffBuilder::new(git_repo));
    diff.foreach(
        &mut |delta, _progress| {
            builder.borrow_mut().on_file(delta);
            true
        },
        None,
        Some(&mut |_delta, hunk| {
            builder.borrow_mut().on_hunk(hunk);
            true
        }),
        Some(&mut |_delta, _hunk, line| {
            builder.borrow_mut().on_line(line);
            true
        }),
    )
    .map_err(map_err)?;
    Ok(builder.into_inner().files)
}

pub fn diff(repo: &RepoRef, target: DiffTarget) -> Result<Vec<FileDiff>> {
    let git_repo = open(repo)?;
    let diff = git_diff(&git_repo, target, None)?;
    build_file_diffs(&git_repo, &diff)
}

/// `diff` restricted to literal `paths`: libgit2 pathspecs
/// with matching disabled, so `*` and `[` in a name mean themselves.
pub fn diff_paths(repo: &RepoRef, target: DiffTarget, paths: &[PathBuf]) -> Result<Vec<FileDiff>> {
    if paths.is_empty() {
        return Ok(Vec::new());
    }
    let git_repo = open(repo)?;
    let diff = git_diff(&git_repo, target, Some(paths))?;
    build_file_diffs(&git_repo, &diff)
}

/// `diff`'s file list without the hunks.
///
/// The same `git2::Diff`, built by the same `git_diff`, walked with the
/// same `DiffBuilder::on_file`, with only the patch generation skipped:
/// listing deltas compares two trees, while `foreach` with hunk and line
/// callbacks reads and diffs every changed blob (99% of a commit's diff
/// time).
pub fn diff_files(repo: &RepoRef, target: DiffTarget) -> Result<DiffFileList> {
    let git_repo = open(repo)?;
    let diff = git_diff(&git_repo, target, None)?;
    let mut builder = DiffBuilder::new(&git_repo);
    for delta in diff.deltas() {
        builder.on_file(delta);
    }
    Ok(DiffFileList {
        files: builder.files,
        hunks_included: false,
    })
}

/// `GitBackend::commit_stats`: each commit's tree against its first
/// parent's, through libgit2's own diff statistics (no rename detection,
/// binary files count no lines).
pub fn commit_stats(repo: &RepoRef, ids: &[Oid]) -> Result<Vec<CommitStat>> {
    let git_repo = open(repo)?;
    let mut opts = git2::DiffOptions::new();
    ids.iter()
        .map(|&id| {
            let commit = git_repo.find_commit(to_git2_oid(id)?).map_err(map_err)?;
            let new_tree = commit.tree().map_err(map_err)?;
            let old_tree = commit.parent(0).ok().and_then(|p| p.tree().ok());
            let stats = git_repo
                .diff_tree_to_tree(old_tree.as_ref(), Some(&new_tree), Some(&mut opts))
                .map_err(map_err)?
                .stats()
                .map_err(map_err)?;
            Ok(CommitStat {
                id,
                files: stats.files_changed() as u32,
                insertions: stats.insertions() as u32,
                deletions: stats.deletions() as u32,
            })
        })
        .collect()
}

fn git_diff<'r>(
    git_repo: &'r git2::Repository,
    target: DiffTarget,
    paths: Option<&[PathBuf]>,
) -> Result<git2::Diff<'r>> {
    let mut opts = git2::DiffOptions::new();
    opts.context_lines(3);
    if let Some(paths) = paths {
        opts.disable_pathspec_match(true);
        for p in paths {
            opts.pathspec(p.to_string_lossy().replace('\\', "/"));
        }
    }

    let diff = match target {
        DiffTarget::WorkingTreeVsIndex => git_repo
            .diff_index_to_workdir(None, Some(&mut opts))
            .map_err(map_err)?,
        DiffTarget::IndexVsHead => {
            let head_tree = git_repo.head().ok().and_then(|h| h.peel_to_tree().ok());
            git_repo
                .diff_tree_to_index(head_tree.as_ref(), None, Some(&mut opts))
                .map_err(map_err)?
        }
        DiffTarget::CommitVsParent(oid) => {
            let git2_oid = crate::repo::to_git2_oid(oid)?;
            let commit = git_repo.find_commit(git2_oid).map_err(map_err)?;
            let new_tree = commit.tree().map_err(map_err)?;
            let old_tree = commit.parent(0).ok().and_then(|p| p.tree().ok());
            git_repo
                .diff_tree_to_tree(old_tree.as_ref(), Some(&new_tree), Some(&mut opts))
                .map_err(map_err)?
        }
        DiffTarget::CommitRange(from, to) => {
            let from_tree = git_repo
                .find_commit(crate::repo::to_git2_oid(from)?)
                .map_err(map_err)?
                .tree()
                .map_err(map_err)?;
            let to_tree = git_repo
                .find_commit(crate::repo::to_git2_oid(to)?)
                .map_err(map_err)?
                .tree()
                .map_err(map_err)?;
            git_repo
                .diff_tree_to_tree(Some(&from_tree), Some(&to_tree), Some(&mut opts))
                .map_err(map_err)?
        }
        DiffTarget::CommitVsWorkingTree(oid) => {
            let tree = git_repo
                .find_commit(crate::repo::to_git2_oid(oid)?)
                .map_err(map_err)?
                .tree()
                .map_err(map_err)?;
            // Staged and unstaged edits of tracked files, like
            // `git diff <commit>`.
            git_repo
                .diff_tree_to_workdir_with_index(Some(&tree), Some(&mut opts))
                .map_err(map_err)?
        }
        DiffTarget::CommitSpan(oldest, newest) => {
            let oldest = git_repo
                .find_commit(crate::repo::to_git2_oid(oldest)?)
                .map_err(map_err)?;
            let from_tree = oldest.parent(0).ok().and_then(|p| p.tree().ok());
            let to_tree = git_repo
                .find_commit(crate::repo::to_git2_oid(newest)?)
                .map_err(map_err)?
                .tree()
                .map_err(map_err)?;
            git_repo
                .diff_tree_to_tree(from_tree.as_ref(), Some(&to_tree), Some(&mut opts))
                .map_err(map_err)?
        }
    };
    let mut diff = diff;
    // A rename is one file (from 50% similarity), as `git diff -M`.
    let mut find = git2::DiffFindOptions::new();
    find.renames(true).rename_limit(1000);
    diff.find_similar(Some(&mut find)).map_err(map_err)?;
    Ok(diff)
}