lgtm-cli 0.7.1

Run LGTM code reviews with a bundled web UI and gRPC server.
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Stateless `gitoxide` (`gix`) adapter for local shadow repositories (`~/.lgtm/snapshots/$repoSlug`) and repository inspection.

use std::{
    collections::{BTreeMap, BTreeSet},
    path::{Path, PathBuf},
};

use gix::{
    bstr::ByteSlice,
    objs::{
        Commit, Tree,
        tree::{Entry, EntryKind},
    },
    refs::transaction::{Change, LogChange, PreviousValue, RefEdit, RefLog},
};

use crate::backend::{
    error::{Error, Result},
    extensions::git::resolve_git_paths,
    utils::normalize_repository_url,
};

/// Resolved metadata for a local Git working checkout or linked worktree.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RepoContext {
    pub git_dir: String,
    pub worktree_dir: PathBuf,
    pub branch: String,
    pub head_sha: String,
    pub repository_url: Option<String>,
    pub user_name: String,
    pub user_email: String,
}

/// Metadata and file changes for the current HEAD commit on a local branch.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HeadCommitPayload {
    pub head_sha: String,
    pub parent_sha: String,
    pub message: String,
    pub author_name: String,
    pub author_email: String,
    pub timestamp: i64,
    pub changed_files: BTreeMap<String, Option<String>>,
}

/// Inspect a local Git repository or linked worktree and return its canonical shared `git_dir`, active `worktree_dir`, branch, `HEAD` SHA, remote URL, and configured human author.
pub fn get_repo_context(git_dir_or_worktree: &str) -> Result<RepoContext> {
    let (worktree_git_dir, worktree_dir, normalized_git_dir) =
        resolve_git_paths(git_dir_or_worktree)?;
    let repo = match gix::open(&worktree_dir) {
        Ok(repo) => repo,
        Err(_) => gix::open(&worktree_git_dir).map_err(|err| {
            Error::Git(format!(
                "failed to open repository '{normalized_git_dir}': {err}"
            ))
        })?,
    };

    let branch = repo
        .head_name()
        .map_err(|err| Error::Git(format!("failed to read HEAD name: {err}")))?
        .map(|name| name.shorten().to_str().unwrap_or("HEAD").to_owned())
        .unwrap_or_else(|| "HEAD".to_owned());

    let head_sha = repo
        .head_id()
        .map_err(|err| Error::Git(format!("repository has no unborn/committed HEAD: {err}")))?
        .to_hex()
        .to_string();

    let config = repo.config_snapshot();
    let user_name = config
        .string("user.name")
        .and_then(|v| v.to_str().ok().map(str::trim).map(str::to_owned))
        .filter(|v| !v.is_empty())
        .unwrap_or_else(|| "LGTM Reviewer".to_owned());
    let user_email = config
        .string("user.email")
        .and_then(|v| v.to_str().ok().map(str::trim).map(str::to_owned))
        .filter(|v| !v.is_empty())
        .unwrap_or_else(|| "reviewer@lgtm.local".to_owned());
    let repository_url = config.string("remote.origin.url").and_then(|v| {
        v.to_str()
            .ok()
            .and_then(|raw| normalize_repository_url(raw).ok())
    });

    Ok(RepoContext {
        git_dir: normalized_git_dir,
        worktree_dir,
        branch,
        head_sha,
        repository_url,
        user_name,
        user_email,
    })
}

/// Ensure a bare Git repository exists at `repo_dir`.
pub fn ensure_bare_repo(repo_dir: &Path) -> Result<()> {
    if repo_dir.join("HEAD").exists() {
        return Ok(());
    }
    std::fs::create_dir_all(repo_dir).map_err(|err| {
        Error::Git(format!(
            "failed to create snapshot directory '{}': {err}",
            repo_dir.display()
        ))
    })?;
    gix::init_bare(repo_dir).map_err(|err| {
        Error::Git(format!(
            "failed to initialize bare repository '{}': {err}",
            repo_dir.display()
        ))
    })?;
    Ok(())
}

/// Synchronize objects from `source_git_dir` into `target_repo_dir` so `commit_sha` and its trees/blobs are readable in `target_repo_dir`.
pub fn fetch_objects(source_git_dir: &str, target_repo_dir: &Path, commit_sha: &str) -> Result<()> {
    ensure_bare_repo(target_repo_dir)?;
    let oid = parse_oid(commit_sha)?;
    if let Ok(existing_repo) = gix::open(target_repo_dir)
        && existing_repo.find_object(oid).is_ok()
    {
        return Ok(());
    }

    let (_, _, common_git_dir) = resolve_git_paths(source_git_dir)?;
    let source_objects = Path::new(&common_git_dir).join("objects");
    let target_objects = target_repo_dir.join("objects");
    copy_objects_dir(&source_objects, &target_objects)?;

    let target_repo = gix::open(target_repo_dir)
        .map_err(|err| Error::Git(format!("failed to open target repo: {err}")))?;
    target_repo.find_object(oid).map_err(|err| {
        Error::NotFound(format!(
            "commit '{commit_sha}' was not found after object sync: {err}"
        ))
    })?;
    Ok(())
}

/// Stage worktree paths into `refs/lgtm/staged/<branch>` inside `target_repo_dir` via `gitoxide` without touching the working checkout's `.git/index`.
pub fn stage_worktree_to_shadow_ref(
    git_dir_or_worktree: &str,
    target_repo_dir: &Path,
    branch: &str,
    paths: &[String],
) -> Result<Vec<String>> {
    let ctx = get_repo_context(git_dir_or_worktree)?;
    fetch_objects(&ctx.git_dir, target_repo_dir, &ctx.head_sha)?;
    let expanded = collect_worktree_paths(&ctx.worktree_dir, paths)?;

    let snapshot_repo = gix::open(target_repo_dir)
        .map_err(|err| Error::Git(format!("failed to open snapshot repo: {err}")))?;
    let base_oid = parse_oid(&ctx.head_sha)?;
    let head_ref_name = format!("refs/lgtm/heads/{branch}");
    update_ref(&snapshot_repo, &head_ref_name, base_oid)?;

    let staged_ref_name = format!("refs/lgtm/staged/{branch}");
    let snap_ref_name = format!("refs/lgtm/snapshots/{branch}");

    let mut tree_entries =
        if let Some(staged_tree_oid) = resolve_ref_oid(&snapshot_repo, &staged_ref_name) {
            let mut flat = BTreeMap::new();
            collect_tree_recursive(&snapshot_repo, staged_tree_oid, "", &mut flat)?;
            flat
        } else if let Some(snap_commit_oid) = resolve_ref_oid(&snapshot_repo, &snap_ref_name) {
            read_commit_flat_tree(&snapshot_repo, snap_commit_oid)?
        } else {
            read_commit_flat_tree(&snapshot_repo, base_oid)?
        };

    for rel_path in &expanded {
        let full_path = ctx.worktree_dir.join(rel_path);
        if full_path.is_file() {
            let bytes = std::fs::read(&full_path).map_err(|err| {
                Error::Git(format!("failed to read '{}': {err}", full_path.display()))
            })?;
            let blob_id = snapshot_repo
                .write_blob(&bytes)
                .map_err(|err| Error::Git(format!("failed to write blob: {err}")))?
                .detach();
            tree_entries.insert(rel_path.clone(), blob_id);
        } else {
            tree_entries.remove(rel_path);
        }
    }

    let staged_tree_oid = write_flat_tree(&snapshot_repo, &tree_entries)?;
    update_ref(&snapshot_repo, &staged_ref_name, staged_tree_oid)?;
    Ok(expanded)
}

/// Create a shadow commit on `ref_name` (`refs/lgtm/snapshots/<branch>`) inside `target_repo_dir` using the human author from `git config`.
pub fn create_commit_on_ref(
    git_dir_or_worktree: &str,
    target_repo_dir: &Path,
    ref_name: &str,
    branch: &str,
    message: Option<&str>,
) -> Result<String> {
    let ctx = get_repo_context(git_dir_or_worktree)?;
    fetch_objects(&ctx.git_dir, target_repo_dir, &ctx.head_sha)?;
    let head_ref_name = format!("refs/lgtm/heads/{branch}");
    let staged_ref_name = format!("refs/lgtm/staged/{branch}");
    let snapshot_repo = gix::open(target_repo_dir)
        .map_err(|err| Error::Git(format!("failed to open snapshot repo: {err}")))?;
    let base_oid = parse_oid(&ctx.head_sha)?;
    update_ref(&snapshot_repo, &head_ref_name, base_oid)?;

    let parent_oid = resolve_ref_oid(&snapshot_repo, ref_name).unwrap_or(base_oid);
    let new_tree_oid =
        if let Some(staged_tree_oid) = resolve_ref_oid(&snapshot_repo, &staged_ref_name) {
            let _ = delete_ref(&snapshot_repo, &staged_ref_name);
            staged_tree_oid
        } else {
            let worktree_entries = hash_worktree_entries(&snapshot_repo, &ctx.worktree_dir)?;
            write_flat_tree(&snapshot_repo, &worktree_entries)?
        };

    let commit_message = normalize_commit_message(message, "LGTM local snapshot");
    let commit_oid = write_commit_object(
        &snapshot_repo,
        new_tree_oid,
        vec![parent_oid],
        &commit_message,
        &ctx.user_name,
        &ctx.user_email,
    )?;
    update_ref(&snapshot_repo, ref_name, commit_oid)?;

    Ok(commit_oid.to_hex().to_string())
}

/// Snapshot the working tree into `target_repo_dir` idempotently via `gitoxide`, returning `(base_sha, head_sha)`.
pub fn snapshot_worktree(
    git_dir_or_worktree: &str,
    target_repo_dir: &Path,
) -> Result<(String, String)> {
    let ctx = get_repo_context(git_dir_or_worktree)?;
    fetch_objects(&ctx.git_dir, target_repo_dir, &ctx.head_sha)?;
    let snapshot_repo = gix::open(target_repo_dir)
        .map_err(|err| Error::Git(format!("failed to open snapshot repo: {err}")))?;

    let base_oid = parse_oid(&ctx.head_sha)?;
    let head_ref_name = format!("refs/lgtm/heads/{}", ctx.branch);
    update_ref(&snapshot_repo, &head_ref_name, base_oid)?;

    let snap_ref_name = format!("refs/lgtm/snapshots/{}", ctx.branch);
    let current_snap_oid = resolve_ref_oid(&snapshot_repo, &snap_ref_name);
    let worktree_entries = hash_worktree_entries(&snapshot_repo, &ctx.worktree_dir)?;

    if let Some(existing_oid) = current_snap_oid {
        let existing_tree = read_commit_flat_tree(&snapshot_repo, existing_oid)?;
        if existing_tree == worktree_entries {
            return Ok((ctx.head_sha, existing_oid.to_hex().to_string()));
        }
        let new_tree_oid = write_flat_tree(&snapshot_repo, &worktree_entries)?;
        let commit_oid = write_commit_object(
            &snapshot_repo,
            new_tree_oid,
            vec![existing_oid],
            "LGTM worktree snapshot",
            &ctx.user_name,
            &ctx.user_email,
        )?;
        update_ref(&snapshot_repo, &snap_ref_name, commit_oid)?;
        return Ok((ctx.head_sha, commit_oid.to_hex().to_string()));
    }

    let base_tree = read_commit_flat_tree(&snapshot_repo, base_oid)?;
    let new_tree_oid = write_flat_tree(&snapshot_repo, &worktree_entries)?;
    let parent_oids = if base_tree == worktree_entries {
        if let Some(first_parent) = read_commit_first_parent(&snapshot_repo, base_oid)? {
            let first_parent_hex = first_parent.to_hex().to_string();
            update_ref(&snapshot_repo, &snap_ref_name, base_oid)?;
            return Ok((first_parent_hex, ctx.head_sha));
        }
        vec![base_oid]
    } else {
        vec![base_oid]
    };

    let commit_oid = write_commit_object(
        &snapshot_repo,
        new_tree_oid,
        parent_oids,
        "LGTM worktree snapshot",
        &ctx.user_name,
        &ctx.user_email,
    )?;
    update_ref(&snapshot_repo, &snap_ref_name, commit_oid)?;
    Ok((ctx.head_sha, commit_oid.to_hex().to_string()))
}

/// Return all relative file paths modified, added, or removed between `old_sha` and `new_sha` inside `repo_dir`.
pub fn get_changed_files(repo_dir: &Path, old_sha: &str, new_sha: &str) -> Result<Vec<String>> {
    if old_sha.trim() == new_sha.trim() {
        return Ok(Vec::new());
    }
    let repo = gix::open(repo_dir).map_err(|err| {
        Error::Git(format!(
            "failed to open repo '{}': {err}",
            repo_dir.display()
        ))
    })?;
    let old_oid = parse_oid(old_sha)?;
    let new_oid = parse_oid(new_sha)?;
    let old_tree = read_commit_flat_tree(&repo, old_oid)?;
    let new_tree = read_commit_flat_tree(&repo, new_oid)?;

    let all_paths: BTreeSet<String> = old_tree.keys().chain(new_tree.keys()).cloned().collect();
    let mut changed = Vec::new();
    for path in all_paths {
        if old_tree.get(&path) != new_tree.get(&path) {
            changed.push(path);
        }
    }
    Ok(changed)
}

/// Read all UTF-8 file blobs in `commit_sha`'s tree inside `repo_dir`.
pub fn get_commit_blobs(repo_dir: &Path, commit_sha: &str) -> Result<BTreeMap<String, String>> {
    let repo = gix::open(repo_dir).map_err(|err| {
        Error::Git(format!(
            "failed to open repo '{}': {err}",
            repo_dir.display()
        ))
    })?;
    let commit_oid = parse_oid(commit_sha)?;
    let flat_tree = read_commit_flat_tree(&repo, commit_oid)?;
    let mut blobs = BTreeMap::new();
    for (path, oid) in flat_tree {
        blobs.insert(path, read_blob_string(&repo, oid)?);
    }
    Ok(blobs)
}

/// Read a single UTF-8 file blob at `commit_sha` inside `repo_dir`.
pub fn get_file_at_commit(
    repo_dir: &Path,
    commit_sha: &str,
    file_path: &str,
) -> Result<Option<String>> {
    let file_path = file_path.trim();
    if file_path.is_empty() {
        return Err(Error::Invalid("file_path must not be empty".to_owned()));
    }
    let repo = gix::open(repo_dir).map_err(|err| {
        Error::Git(format!(
            "failed to open repo '{}': {err}",
            repo_dir.display()
        ))
    })?;
    let commit_oid = parse_oid(commit_sha)?;
    let flat_tree = read_commit_flat_tree(&repo, commit_oid)?;
    let Some(blob_oid) = flat_tree.get(file_path) else {
        return Ok(None);
    };
    Ok(Some(read_blob_string(&repo, *blob_oid)?))
}

/// Read the current HEAD commit metadata and file changes relative to its first parent.
pub fn get_head_commit_payload(git_dir_or_worktree: &str) -> Result<HeadCommitPayload> {
    let ctx = get_repo_context(git_dir_or_worktree)?;
    let (_, worktree_dir, _) = resolve_git_paths(git_dir_or_worktree)?;
    let repo = gix::open(&worktree_dir)
        .map_err(|err| Error::Git(format!("failed to open checkout repository: {err}")))?;
    let head_oid = parse_oid(&ctx.head_sha)?;
    let object = repo
        .find_object(head_oid)
        .map_err(|err| Error::NotFound(format!("commit '{head_oid}' not found: {err}")))?;
    let commit = object
        .try_into_commit()
        .map_err(|err| Error::Git(format!("object '{head_oid}' is not a commit: {err}")))?;
    let decoded = commit
        .decode()
        .map_err(|err| Error::Git(format!("failed to decode HEAD commit: {err}")))?;
    let parent_oid = decoded.parents().next().ok_or_else(|| {
        Error::Invalid("HEAD commit has no parent commit to push against".to_owned())
    })?;
    let parent_sha = parent_oid.to_hex().to_string();
    let message = decoded.message.to_str_lossy().into_owned();
    let author_name = decoded.author.name.to_str_lossy().into_owned();
    let author_email = decoded.author.email.to_str_lossy().into_owned();
    let timestamp = decoded.author.time.seconds;

    let parent_tree = read_commit_flat_tree(&repo, parent_oid)?;
    let head_tree = read_commit_flat_tree(&repo, head_oid)?;
    let all_paths: BTreeSet<String> = parent_tree
        .keys()
        .chain(head_tree.keys())
        .cloned()
        .collect();
    let mut changed_files = BTreeMap::new();
    for path in all_paths {
        if parent_tree.get(&path) != head_tree.get(&path) {
            match head_tree.get(&path) {
                Some(blob_oid) => {
                    let content = read_blob_string(&repo, *blob_oid)?;
                    changed_files.insert(path, Some(content));
                }
                None => {
                    changed_files.insert(path, None);
                }
            }
        }
    }

    Ok(HeadCommitPayload {
        head_sha: ctx.head_sha,
        parent_sha,
        message,
        author_name,
        author_email,
        timestamp,
        changed_files,
    })
}

/// Expand and validate relative worktree paths under `worktree_dir`.
pub fn collect_worktree_paths(worktree_dir: &Path, raw_paths: &[String]) -> Result<Vec<String>> {
    if raw_paths.is_empty() {
        return Err(Error::Invalid("at least one path is required".to_owned()));
    }
    let mut collected = BTreeSet::new();
    for raw in raw_paths {
        let trimmed = raw.trim();
        if trimmed.is_empty() {
            continue;
        }
        let candidate = Path::new(trimmed);
        if candidate.is_absolute()
            || candidate
                .components()
                .any(|c| matches!(c, std::path::Component::ParentDir))
        {
            return Err(Error::Invalid(format!(
                "path '{trimmed}' must be a relative path inside the worktree"
            )));
        }
        let target = if trimmed == "." {
            worktree_dir.to_path_buf()
        } else {
            worktree_dir.join(candidate)
        };
        if target.is_dir() {
            collect_files_under_dir(worktree_dir, &target, &mut collected)?;
        } else if target.is_file() {
            let rel = target
                .strip_prefix(worktree_dir)
                .map_err(|err| Error::Invalid(format!("invalid worktree path: {err}")))?
                .to_string_lossy()
                .replace('\\', "/");
            collected.insert(rel);
        } else {
            let rel = trimmed.trim_start_matches("./").replace('\\', "/");
            collected.insert(rel);
        }
    }
    if collected.is_empty() {
        return Err(Error::Invalid(
            "no matching worktree paths found".to_owned(),
        ));
    }
    Ok(collected.into_iter().collect())
}

fn hash_worktree_entries(
    snapshot_repo: &gix::Repository,
    worktree_dir: &Path,
) -> Result<BTreeMap<String, gix::ObjectId>> {
    let mut worktree_entries = BTreeMap::new();
    let worktree_files = collect_worktree_paths(worktree_dir, &[".".to_owned()])?;
    for rel_path in worktree_files {
        let full_path = worktree_dir.join(&rel_path);
        if full_path.is_file() {
            let bytes = std::fs::read(&full_path).map_err(|err| {
                Error::Git(format!("failed to read '{}': {err}", full_path.display()))
            })?;
            let blob_id = snapshot_repo
                .write_blob(&bytes)
                .map_err(|err| Error::Git(format!("failed to write snapshot blob: {err}")))?
                .detach();
            worktree_entries.insert(rel_path, blob_id);
        }
    }
    Ok(worktree_entries)
}

fn parse_oid(sha: &str) -> Result<gix::ObjectId> {
    gix::ObjectId::from_hex(sha.trim().as_bytes())
        .map_err(|err| Error::Invalid(format!("invalid commit SHA '{sha}': {err}")))
}

fn resolve_ref_oid(repo: &gix::Repository, ref_name: &str) -> Option<gix::ObjectId> {
    repo.find_reference(ref_name)
        .ok()
        .and_then(|mut reference| reference.peel_to_id_in_place().ok().map(|id| id.detach()))
}

fn update_ref(repo: &gix::Repository, ref_name: &str, target: gix::ObjectId) -> Result<()> {
    repo.edit_reference(RefEdit {
        change: Change::Update {
            log: LogChange {
                mode: RefLog::AndReference,
                force_create_reflog: false,
                message: "lgtm-forge update".into(),
            },
            expected: PreviousValue::Any,
            new: gix::refs::Target::Object(target),
        },
        name: ref_name
            .try_into()
            .map_err(|err| Error::Git(format!("invalid ref name '{ref_name}': {err}")))?,
        deref: false,
    })
    .map_err(|err| Error::Git(format!("failed to update ref '{ref_name}': {err}")))?;
    Ok(())
}

fn delete_ref(repo: &gix::Repository, ref_name: &str) -> Result<()> {
    repo.edit_reference(RefEdit {
        change: Change::Delete {
            expected: PreviousValue::Any,
            log: RefLog::AndReference,
        },
        name: ref_name
            .try_into()
            .map_err(|err| Error::Git(format!("invalid ref name '{ref_name}': {err}")))?,
        deref: false,
    })
    .map_err(|err| Error::Git(format!("failed to delete ref '{ref_name}': {err}")))?;
    Ok(())
}

fn read_commit_first_parent(
    repo: &gix::Repository,
    commit_oid: gix::ObjectId,
) -> Result<Option<gix::ObjectId>> {
    let object = repo
        .find_object(commit_oid)
        .map_err(|err| Error::NotFound(format!("commit '{commit_oid}' not found: {err}")))?;
    let commit = object
        .try_into_commit()
        .map_err(|err| Error::Git(format!("object '{commit_oid}' is not a commit: {err}")))?;
    Ok(commit.parent_ids().next().map(|id| id.detach()))
}

fn read_commit_flat_tree(
    repo: &gix::Repository,
    commit_oid: gix::ObjectId,
) -> Result<BTreeMap<String, gix::ObjectId>> {
    let object = repo
        .find_object(commit_oid)
        .map_err(|err| Error::NotFound(format!("commit '{commit_oid}' not found: {err}")))?;
    let commit = object
        .try_into_commit()
        .map_err(|err| Error::Git(format!("object '{commit_oid}' is not a commit: {err}")))?;
    let tree_id = commit
        .tree_id()
        .map_err(|err| Error::Git(format!("failed to read commit tree id: {err}")))?
        .detach();
    let mut flat = BTreeMap::new();
    collect_tree_recursive(repo, tree_id, "", &mut flat)?;
    Ok(flat)
}

fn collect_tree_recursive(
    repo: &gix::Repository,
    tree_oid: gix::ObjectId,
    prefix: &str,
    out: &mut BTreeMap<String, gix::ObjectId>,
) -> Result<()> {
    let object = repo
        .find_object(tree_oid)
        .map_err(|err| Error::Git(format!("failed to find tree '{tree_oid}': {err}")))?;
    let tree = object
        .try_into_tree()
        .map_err(|err| Error::Git(format!("object '{tree_oid}' is not a tree: {err}")))?;
    let tree_ref = tree
        .decode()
        .map_err(|err| Error::Git(format!("failed to decode tree '{tree_oid}': {err}")))?;
    for entry in &tree_ref.entries {
        let name = entry
            .filename
            .to_str()
            .map_err(|err| Error::Git(format!("non-utf8 filename in tree: {err}")))?;
        let rel_path = if prefix.is_empty() {
            name.to_owned()
        } else {
            format!("{prefix}/{name}")
        };
        if entry.mode.is_tree() {
            collect_tree_recursive(repo, entry.oid.into(), &rel_path, out)?;
        } else {
            out.insert(rel_path, entry.oid.into());
        }
    }
    Ok(())
}

fn write_flat_tree(
    repo: &gix::Repository,
    entries: &BTreeMap<String, gix::ObjectId>,
) -> Result<gix::ObjectId> {
    write_subtree(repo, entries, "")
}

fn write_subtree(
    repo: &gix::Repository,
    entries: &BTreeMap<String, gix::ObjectId>,
    prefix: &str,
) -> Result<gix::ObjectId> {
    let mut direct_files: BTreeMap<String, gix::ObjectId> = BTreeMap::new();
    let mut subdirs: BTreeSet<String> = BTreeSet::new();

    for (path, oid) in entries {
        let rel = if prefix.is_empty() {
            path.as_str()
        } else if let Some(stripped) = path.strip_prefix(prefix).and_then(|s| s.strip_prefix('/')) {
            stripped
        } else {
            continue;
        };

        if let Some((dir, _)) = rel.split_once('/') {
            subdirs.insert(dir.to_owned());
        } else {
            direct_files.insert(rel.to_owned(), *oid);
        }
    }

    let mut tree_entries: Vec<Entry> = Vec::new();
    for (filename, oid) in direct_files {
        tree_entries.push(Entry {
            mode: EntryKind::Blob.into(),
            filename: filename.into(),
            oid,
        });
    }
    for dir in subdirs {
        let sub_prefix = if prefix.is_empty() {
            dir.clone()
        } else {
            format!("{prefix}/{dir}")
        };
        let sub_oid = write_subtree(repo, entries, &sub_prefix)?;
        tree_entries.push(Entry {
            mode: EntryKind::Tree.into(),
            filename: dir.into(),
            oid: sub_oid,
        });
    }

    tree_entries.sort_by(|a, b| {
        let a_key = if a.mode.is_tree() {
            format!("{}/", a.filename)
        } else {
            a.filename.to_string()
        };
        let b_key = if b.mode.is_tree() {
            format!("{}/", b.filename)
        } else {
            b.filename.to_string()
        };
        a_key.cmp(&b_key)
    });

    let tree = Tree {
        entries: tree_entries,
    };
    repo.write_object(&tree)
        .map(|id| id.detach())
        .map_err(|err| Error::Git(format!("failed to write tree object: {err}")))
}

fn write_commit_object(
    repo: &gix::Repository,
    tree_oid: gix::ObjectId,
    parents: Vec<gix::ObjectId>,
    message: &str,
    author_name: &str,
    author_email: &str,
) -> Result<gix::ObjectId> {
    let timestamp = if std::env::var("LGTM_GITHUB_TEST_MODE").as_deref() == Ok("1") {
        parents
            .first()
            .and_then(|parent_oid| {
                repo.find_object(*parent_oid)
                    .ok()?
                    .try_into_commit()
                    .ok()?
                    .decode()
                    .ok()
                    .map(|c| c.author.time.seconds + 1)
            })
            .unwrap_or(1_700_000_000)
    } else {
        chrono::Utc::now().timestamp()
    };
    let sig = gix::actor::Signature {
        name: author_name.to_owned().into(),
        email: author_email.to_owned().into(),
        time: gix::date::Time::new(timestamp, 0),
    };
    let commit = Commit {
        tree: tree_oid,
        parents: parents.into(),
        author: sig.clone(),
        committer: sig,
        encoding: None,
        message: message.trim_end_matches('\n').to_owned().into(),
        extra_headers: Vec::new(),
    };
    repo.write_object(&commit)
        .map(|id| id.detach())
        .map_err(|err| Error::Git(format!("failed to write commit object: {err}")))
}

fn read_blob_string(repo: &gix::Repository, blob_oid: gix::ObjectId) -> Result<String> {
    let object = repo
        .find_object(blob_oid)
        .map_err(|err| Error::Git(format!("failed to find blob '{blob_oid}': {err}")))?;
    Ok(String::from_utf8_lossy(&object.data).into_owned())
}

fn collect_files_under_dir(
    worktree_dir: &Path,
    dir: &Path,
    out: &mut BTreeSet<String>,
) -> Result<()> {
    let entries = std::fs::read_dir(dir).map_err(|err| {
        Error::Git(format!(
            "failed to read directory '{}': {err}",
            dir.display()
        ))
    })?;
    for entry in entries {
        let entry = entry.map_err(|err| Error::Git(format!("directory entry error: {err}")))?;
        let file_name = entry.file_name();
        if file_name == ".git" || file_name == "target" || file_name == "node_modules" {
            continue;
        }
        let path = entry.path();
        if path.is_dir() {
            collect_files_under_dir(worktree_dir, &path, out)?;
        } else if path.is_file() {
            let rel = path
                .strip_prefix(worktree_dir)
                .map_err(|err| Error::Git(format!("strip_prefix error: {err}")))?
                .to_string_lossy()
                .replace('\\', "/");
            out.insert(rel);
        }
    }
    Ok(())
}

fn copy_objects_dir(src_dir: &Path, dst_dir: &Path) -> Result<()> {
    if !src_dir.exists() {
        return Ok(());
    }
    std::fs::create_dir_all(dst_dir).map_err(|err| {
        Error::Git(format!(
            "failed to create objects dir '{}': {err}",
            dst_dir.display()
        ))
    })?;
    for entry in std::fs::read_dir(src_dir)
        .map_err(|err| Error::Git(format!("failed to read objects dir: {err}")))?
    {
        let entry = entry.map_err(|err| Error::Git(format!("dir entry error: {err}")))?;
        let src_path = entry.path();
        let dst_path = dst_dir.join(entry.file_name());
        if src_path.is_dir() {
            copy_objects_dir(&src_path, &dst_path)?;
        } else if src_path.is_file() && !dst_path.exists() {
            std::fs::copy(&src_path, &dst_path).map_err(|err| {
                Error::Git(format!(
                    "failed to copy git object '{}': {err}",
                    src_path.display()
                ))
            })?;
        }
    }
    Ok(())
}

fn normalize_commit_message(message: Option<&str>, fallback: &str) -> String {
    message
        .map(str::trim)
        .filter(|msg| !msg.is_empty())
        .unwrap_or(fallback)
        .to_owned()
}