heddle-cli 0.10.4

An AI-native version control system
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
// SPDX-License-Identifier: Apache-2.0
//! Fsck command - verify repository integrity.

use anyhow::{Result, anyhow};
use heddle_core::{FsckOptions, FsckRepair, fsck};
use repo::RepositorySourceAuthority;

use super::advice::RecoveryAdvice;
use crate::cli::{Cli, execution_context_from_cli, render, should_output_json};

pub fn cmd_fsck(cli: &Cli, full: bool, thorough: bool, git_projection: bool) -> Result<()> {
    let ctx = execution_context_from_cli(cli)?;
    run_fsck(cli, &ctx, full, thorough, git_projection, None)
}

pub fn cmd_fsck_repair_git(
    cli: &Cli,
    ref_name: Option<String>,
    prefer: Option<String>,
    preview: bool,
) -> Result<()> {
    let ctx = execution_context_from_cli(cli)?;
    let repairs = repair_git(ctx.require_repo()?, ref_name, prefer, preview)?;
    run_fsck(cli, &ctx, false, false, true, Some(repairs))
}

fn run_fsck(
    cli: &Cli,
    ctx: &heddle_core::ExecutionContext,
    full: bool,
    thorough: bool,
    git_projection: bool,
    repairs: Option<Vec<FsckRepair>>,
) -> Result<()> {
    let mut report = fsck(
        ctx,
        FsckOptions {
            full,
            thorough,
            git_projection,
        },
    )?;

    if let Some(repairs) = repairs {
        report.repair_target = Some("git".to_string());
        report.repaired = repairs.iter().any(|repair| repair.repaired);
        report.repairs = repairs;
    }

    if should_output_json(cli, Some(ctx.require_repo()?.config())) {
        render::fsck::fsck_json(&report)?;
    } else {
        render::fsck::fsck_text(&report)?;
    }

    if !report.valid {
        return Err(anyhow!(fsck_integrity_error_advice(report.errors.len())));
    }

    Ok(())
}

#[cfg(feature = "git-overlay")]
fn repair_git(
    repo: &repo::Repository,
    ref_name: Option<String>,
    prefer: Option<String>,
    preview: bool,
) -> Result<Vec<FsckRepair>> {
    let required_direction = match repo.source_authority() {
        RepositorySourceAuthority::GitOverlay => "git",
        RepositorySourceAuthority::Native => "heddle",
    };
    let requested_direction = prefer.as_deref().unwrap_or(required_direction);
    if requested_direction != required_direction {
        return Err(anyhow!(git_repair_authority_mismatch_advice(
            repo.source_authority(),
            requested_direction,
            ref_name.as_deref(),
        )));
    }
    if let Some(ref_name) = ref_name {
        return repair_git_ref(repo, &ref_name, required_direction, preview);
    }
    if repo.source_authority() == RepositorySourceAuthority::Native {
        return Err(anyhow!(git_repair_native_ref_required_advice()));
    }
    if preview {
        return Err(anyhow!(git_repair_ref_required_advice()));
    }
    repair_git_metadata(repo)
}

#[cfg(feature = "git-overlay")]
fn repair_git_metadata(repo: &repo::Repository) -> Result<Vec<FsckRepair>> {
    use heddle_git_projection::GitProjection;

    let mut bridge = GitProjection::new(repo);
    if !bridge.mirror_path().exists() && sley::Repository::discover(repo.root()).is_err() {
        return Ok(vec![FsckRepair {
            name: "git_projection_metadata".to_string(),
            repaired: false,
            detail: "no Git repository or legacy Bridge Mirror was found".to_string(),
            count: 0,
        }]);
    }

    let mapping_path = bridge.mapping_path();
    let mapping_tmp_path = bridge.mapping_tmp_path();
    let tmp_existed = mapping_tmp_path.exists();
    let mapping_existed = mapping_path.exists();

    bridge.build_existing_mapping(None)?;
    let mut repairs = Vec::new();

    if tmp_existed {
        let repaired = !mapping_tmp_path.exists();
        let detail = if mapping_existed {
            "removed stale Git projection mapping temp file"
        } else {
            "promoted Git projection mapping temp file into place"
        };
        repairs.push(FsckRepair {
            name: "git_projection_mapping_tmp".to_string(),
            repaired,
            detail: detail.to_string(),
            count: usize::from(repaired),
        });
    }

    let before_seed = bridge.mapping.iter().count();
    let git_repo = bridge.open_git_repo()?;
    bridge.seed_ingest_identity_mappings_from_repo(&git_repo)?;
    let seeded = bridge.mapping.iter().count().saturating_sub(before_seed);
    if seeded > 0 {
        bridge.save_mapping_to_disk()?;
    }
    repairs.push(FsckRepair {
        name: "git_projection_mapping_rebuild".to_string(),
        repaired: seeded > 0,
        detail: "rebuilt missing projection mappings from portable ingest identity".to_string(),
        count: seeded,
    });

    let pruned = bridge.prune_unreachable_mapping_entries()?;
    repairs.push(FsckRepair {
        name: "git_projection_mapping_prune".to_string(),
        repaired: pruned > 0,
        detail: "removed projection mappings whose Git objects are no longer reachable".to_string(),
        count: pruned,
    });

    Ok(repairs)
}

#[cfg(feature = "git-overlay")]
fn repair_git_ref(
    repo: &repo::Repository,
    ref_name: &str,
    prefer: &str,
    preview: bool,
) -> Result<Vec<FsckRepair>> {
    use heddle_git_projection::git_ingest::import_git_history;
    use ingest::ImportOptions;
    use objects::object::ThreadName;
    use refs::Head;

    if preview {
        return Ok(vec![FsckRepair {
            name: "git_projection_ref_reconcile_preview".to_string(),
            repaired: false,
            detail: git_repair_ref_command(prefer, ref_name, false),
            count: 0,
        }]);
    }

    match prefer {
        "git" => {
            let mut bridge = heddle_git_projection::GitProjection::new(repo);
            let stats = import_git_history(
                &mut bridge,
                Some(repo.root()),
                std::slice::from_ref(&ref_name.to_string()),
                ImportOptions::default(),
                None,
            )?;
            if repo.git_overlay_current_branch()?.as_deref() == Some(ref_name) {
                repo.refs().write_head(&Head::Attached {
                    thread: ThreadName::new(ref_name),
                })?;
            }
            Ok(vec![FsckRepair {
                name: "git_projection_ref_prefer_git".to_string(),
                repaired: stats.commits_imported > 0 || stats.states_created > 0,
                detail: format!(
                    "imported {} Git commit(s) from '{ref_name}' into Heddle",
                    stats.commits_imported
                ),
                count: stats.commits_imported,
            }])
        }
        "heddle" => {
            let tn = ThreadName::new(ref_name);
            let state = repo
                .refs()
                .get_thread(&tn)?
                .ok_or_else(|| git_repair_missing_heddle_thread_advice(ref_name))?;
            repo.goto_without_record(&state)?;
            repo.refs().write_head(&Head::Attached { thread: tn })?;
            let mut bridge = heddle_git_projection::GitProjection::new(repo);
            match bridge.write_through_current_checkout()? {
                heddle_git_projection::WriteThroughOutcome::Wrote(git_oid) => {
                    Ok(vec![FsckRepair {
                        name: "git_projection_ref_prefer_heddle".to_string(),
                        repaired: true,
                        detail: format!(
                            "wrote Heddle state {} for '{ref_name}' through to Git commit {git_oid}",
                            state.short()
                        ),
                        count: 1,
                    }])
                }
                heddle_git_projection::WriteThroughOutcome::Skipped(reason) => Err(anyhow!(
                    git_repair_write_through_skipped_advice(ref_name, reason.to_string(),)
                )),
            }
        }
        _ => unreachable!("clap restricts --prefer values"),
    }
}

fn git_repair_ref_required_advice() -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "git_repair_ref_required",
        "Git ref repair needs a ref name",
        "Run `heddle fsck repair git --ref <branch> --preview` to inspect the authority-valid repair.",
        "--preview was supplied without --ref",
        "repairing a Git projection ref without a ref name could mutate the wrong branch",
        "Git refs, Heddle refs, index, remotes, and worktree files were left unchanged",
        "heddle fsck repair git --ref <branch> --preview",
        vec!["heddle fsck repair git --ref <branch> --preview".to_string()],
    )
}

fn git_repair_native_ref_required_advice() -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "git_repair_native_ref_required",
        "Native Git projection repair needs a ref name",
        "Run `heddle fsck repair git --ref <branch> --preview` to inspect the Heddle-to-Git projection repair.",
        "the native repository has no projection ref target",
        "repairing every retained Git ref could overwrite unrelated adapter state",
        "Git refs, Heddle refs, index, remotes, and worktree files were left unchanged",
        "heddle fsck repair git --ref <branch> --preview",
        vec!["heddle fsck repair git --ref <branch> --preview".to_string()],
    )
}

fn git_repair_authority_mismatch_advice(
    authority: RepositorySourceAuthority,
    requested: &str,
    ref_name: Option<&str>,
) -> RecoveryAdvice {
    match authority {
        RepositorySourceAuthority::GitOverlay => RecoveryAdvice::safety_refusal(
            "git_repair_requires_adoption",
            "Git owns source history in this repository",
            "Run `heddle adopt` before repairing Git from Heddle-native state.",
            format!("--prefer {requested} conflicts with git-overlay source authority"),
            "writing Heddle state through to Git would override the authoritative Git checkout",
            "Git refs, Heddle refs, index, remotes, and worktree files were left unchanged",
            "heddle adopt",
            vec!["heddle adopt".to_string()],
        ),
        RepositorySourceAuthority::Native => {
            let import = ref_name.map_or_else(
                || "heddle import git".to_string(),
                heddle_core::status::next_action::canonical_git_import_ref_command,
            );
            RecoveryAdvice::safety_refusal(
                "git_repair_requires_import",
                "Heddle owns source history in this repository",
                format!(
                    "Use `{import}` to import Git data explicitly instead of treating the retained adapter as authoritative."
                ),
                format!("--prefer {requested} conflicts with native source authority"),
                "importing retained Git adapter state during repair would override Heddle-native history",
                "Git refs, Heddle refs, index, remotes, and worktree files were left unchanged",
                import.clone(),
                vec![import],
            )
        }
    }
}

fn git_repair_missing_heddle_thread_advice(ref_name: &str) -> RecoveryAdvice {
    use heddle_core::status::next_action::canonical_git_import_ref_command;

    let import_command = canonical_git_import_ref_command(ref_name);
    RecoveryAdvice::safety_refusal(
        "git_repair_missing_heddle_thread",
        format!("Cannot prefer Heddle for '{ref_name}': no matching Heddle thread exists"),
        format!("Import the Git ref explicitly with `{import_command}`."),
        format!("Heddle has no thread named '{ref_name}'"),
        "preferring Heddle would need to move Git to a Heddle state that does not exist",
        "Git refs, Heddle refs, and the worktree were left unchanged",
        import_command.clone(),
        vec![import_command, "heddle thread list".to_string()],
    )
}

fn git_repair_write_through_skipped_advice(ref_name: &str, reason: String) -> RecoveryAdvice {
    let preview_command = git_repair_ref_command("heddle", ref_name, true);
    RecoveryAdvice::safety_refusal(
        "git_repair_write_through_skipped",
        format!("Could not repair '{ref_name}' by preferring Heddle: {reason}"),
        format!("Inspect the repair plan with `{preview_command}` before retrying."),
        reason,
        "writing the Heddle state into Git could not be completed for the active checkout",
        "Heddle state was preserved; Git write-through did not report a new commit",
        preview_command.clone(),
        vec![preview_command],
    )
}

#[cfg(not(feature = "git-overlay"))]
fn repair_git(
    _repo: &repo::Repository,
    _ref_name: Option<String>,
    _prefer: Option<String>,
    _preview: bool,
) -> Result<Vec<FsckRepair>> {
    Err(anyhow!("fsck repair git requires the git-overlay feature"))
}

fn git_repair_ref_command(prefer: &str, ref_name: &str, preview: bool) -> String {
    let mut command = format!(
        "heddle fsck repair git --prefer {} --ref {}",
        repo::shell_quote(prefer),
        repo::shell_quote(ref_name)
    );
    if preview {
        command.push_str(" --preview");
    }
    command
}

fn fsck_integrity_error_advice(error_count: usize) -> RecoveryAdvice {
    RecoveryAdvice::safety_refusal(
        "repository_integrity_error",
        "Repository has integrity errors",
        "Inspect repository integrity with `heddle fsck --full`, then restore or repair the reported object/ref.",
        format!("{error_count} integrity error(s) remain after fsck"),
        "treating this repository as verified could hide missing or corrupt objects/refs",
        "no repository objects, refs, or worktree files were changed",
        "heddle fsck --full",
        vec!["heddle fsck --full".to_string()],
    )
}

#[cfg(all(test, feature = "git-overlay"))]
mod tests {
    use repo::Repository;
    use tempfile::TempDir;

    use super::*;

    #[test]
    fn native_rejects_git_as_repair_authority_before_mutation() {
        let temp = TempDir::new().unwrap();
        let repo = Repository::init_default(temp.path()).unwrap();
        let head_before = repo.head().unwrap();

        let error = repair_git(
            &repo,
            Some("main".to_string()),
            Some("git".to_string()),
            false,
        )
        .unwrap_err();

        assert_eq!(
            error
                .downcast_ref::<RecoveryAdvice>()
                .map(|advice| advice.kind),
            Some("git_repair_requires_import")
        );
        assert_eq!(repo.head().unwrap(), head_before);
    }

    #[test]
    fn git_overlay_rejects_heddle_as_repair_authority_before_mutation() {
        let temp = TempDir::new().unwrap();
        sley::Repository::init(temp.path()).unwrap();
        let repo = Repository::init_git_overlay_sidecar(temp.path()).unwrap();
        let head_before = repo.head().unwrap();

        let error = repair_git(
            &repo,
            Some("main".to_string()),
            Some("heddle".to_string()),
            false,
        )
        .unwrap_err();

        assert_eq!(
            error
                .downcast_ref::<RecoveryAdvice>()
                .map(|advice| advice.kind),
            Some("git_repair_requires_adoption")
        );
        assert_eq!(repo.head().unwrap(), head_before);
    }

    #[test]
    fn preview_defaults_to_the_repository_authority_direction() {
        let native_dir = TempDir::new().unwrap();
        let native = Repository::init_default(native_dir.path()).unwrap();
        let native_preview = repair_git(&native, Some("main".to_string()), None, true).unwrap();
        assert_eq!(native_preview.len(), 1);
        assert!(native_preview[0].detail.contains("--prefer heddle"));

        let overlay_dir = TempDir::new().unwrap();
        sley::Repository::init(overlay_dir.path()).unwrap();
        let overlay = Repository::init_git_overlay_sidecar(overlay_dir.path()).unwrap();
        let overlay_preview = repair_git(&overlay, Some("main".to_string()), None, true).unwrap();
        assert_eq!(overlay_preview.len(), 1);
        assert!(overlay_preview[0].detail.contains("--prefer git"));
    }
}