git-meta-cli 0.1.6

Command-line tool for structured Git metadata (get/set, serialize, materialize, push/pull). Installs the `git-meta` binary.
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
use std::io::IsTerminal;

use anyhow::{bail, Context, Result};
use dialoguer::Confirm;
use gix::refs::transaction::PreviousValue;

use crate::commands::{materialize, serialize};
use crate::context::CommandContext;
use crate::style::Style;

/// Expand shorthand "owner/repo" to a full GitHub SSH URL.
fn expand_url(url: &str) -> String {
    // Already a full URL or path -- leave it alone
    if url.contains(':') || url.starts_with('/') || url.starts_with('.') {
        return url.to_string();
    }
    // "owner/repo" shorthand (exactly one slash, no other path separators)
    if url.matches('/').count() == 1 {
        let url = url.strip_suffix(".git").unwrap_or(url);
        return format!("git@github.com:{url}.git");
    }
    url.to_string()
}

/// Scan ls-remote output for meta refs under a given namespace.
/// Returns (has_match, other_namespaces) where other_namespaces are
/// namespace prefixes that contain a "main" ref (e.g. "altmeta" from "refs/altmeta/main").
fn check_remote_refs(
    session: &git_meta_lib::Session,
    url: &str,
    ns: &str,
) -> Result<(bool, Vec<String>)> {
    let output = git_meta_lib::git_utils::run_git(session.repo(), &["ls-remote", url])?;

    let expected_ref = format!("refs/{ns}/main");
    let mut has_match = false;
    let mut other_namespaces = Vec::new();

    for line in output.lines() {
        // ls-remote format: "<sha>\t<refname>"
        let refname = match line.split('\t').nth(1) {
            Some(r) => r.trim(),
            None => continue,
        };

        if refname == expected_ref {
            has_match = true;
        } else if let Some(rest) = refname.strip_prefix("refs/") {
            // Look for refs/*/main patterns that could be meta namespaces
            if let Some(candidate_ns) = rest.strip_suffix("/main") {
                // Skip standard git namespaces
                if !matches!(
                    candidate_ns,
                    "heads" | "tags" | "remotes" | "notes" | "stash"
                ) && !candidate_ns.contains('/')
                {
                    other_namespaces.push(candidate_ns.to_string());
                }
            }
        }
    }

    Ok((has_match, other_namespaces))
}

/// Prompt the user to confirm initializing a fresh metadata remote.
///
/// Returns `Ok(true)` if the user accepts. Returns `Ok(false)` when stdin is
/// not a terminal (so the caller can bail with an actionable hint instead of
/// hanging in CI), or when the user declines the prompt.
fn prompt_for_init(url: &str, ns: &str) -> Result<bool> {
    if !std::io::stdin().is_terminal() {
        return Ok(false);
    }
    eprintln!();
    eprintln!("No metadata refs (refs/{ns}/main) found on {url}.");
    eprintln!("This looks like a fresh metadata remote.");
    let answer = Confirm::new()
        .with_prompt(format!(
            "Initialize refs/{ns}/main with a starter README commit?"
        ))
        .default(true)
        .interact()
        .unwrap_or(false);
    Ok(answer)
}

/// Ensure `refs/{ns}/local/main` exists, creating it with a README commit if
/// it does not. Returns the OID at the tip of that ref.
///
/// If the local ref already exists (e.g. from a previous project on the same
/// machine), it is reused as-is and no new commit is created -- the caller
/// will simply push whatever is there.
///
/// # Parameters
/// - `ctx`: command context with the open session
/// - `ns`: metadata namespace (e.g. `"meta"`)
/// - `origin_url`: URL of the project's `origin` remote, embedded in the
///   README so the metadata remote is self-describing
/// - `meta_url`: URL of the metadata remote being added, also embedded in
///   the README
fn ensure_local_meta_ref(
    ctx: &CommandContext,
    ns: &str,
    origin_url: &str,
    meta_url: &str,
) -> Result<gix::ObjectId> {
    let repo = ctx.session.repo();
    let local_ref = format!("refs/{ns}/local/main");

    let s = Style::detect_stderr();

    if let Ok(reference) = repo.find_reference(&local_ref) {
        let tip = reference
            .into_fully_peeled_id()
            .map_err(|e| anyhow::anyhow!("{e}"))?
            .detach();
        eprintln!(
            "{} existing {local_ref} {}",
            s.ok("Reusing"),
            s.dim(&format!("(tip {})", &tip.to_string()[..12])),
        );
        return Ok(tip);
    }

    let readme = meta_readme_content(origin_url, meta_url, ns);
    let blob_oid: gix::ObjectId = repo
        .write_blob(readme.as_bytes())
        .context("write README blob")?
        .into();
    let tree_oid = {
        let mut editor = repo
            .empty_tree()
            .edit()
            .context("create tree editor for README")?;
        editor
            .upsert("README.md", gix::objs::tree::EntryKind::Blob, blob_oid)
            .context("insert README into tree")?;
        editor.write().context("write README tree")?
    };

    let sig = gix::actor::Signature {
        name: ctx.session.name().into(),
        email: ctx.session.email().into(),
        time: gix::date::Time::now_local_or_utc(),
    };
    let commit = gix::objs::Commit {
        message: format!(
            "git-meta: initialize {ns} metadata\n\n\
             First commit on refs/{ns}/local/main, created by `git meta remote add --init`.\n\
             Stores a README that documents the metadata layout for new contributors."
        )
        .into(),
        tree: tree_oid.into(),
        author: sig.clone(),
        committer: sig,
        encoding: None,
        parents: vec![].into(),
        extra_headers: Default::default(),
    };

    let commit_oid = repo
        .write_object(&commit)
        .context("write initial metadata commit")?
        .detach();

    repo.reference(
        local_ref.as_str(),
        commit_oid,
        PreviousValue::MustNotExist,
        format!("git-meta: initialize {local_ref}"),
    )
    .map_err(|e| anyhow::anyhow!("create {local_ref}: {e}"))?;

    eprintln!(
        "{} {local_ref} with initial README commit {}",
        s.ok("Created"),
        s.dim(&format!("({})", &commit_oid.to_string()[..12])),
    );
    Ok(commit_oid)
}

/// Generate the README body for the initial metadata commit.
fn meta_readme_content(origin_url: &str, meta_url: &str, namespace: &str) -> String {
    format!(
        r#"# Git Metadata Repository

This ref stores structured metadata for the project at:

    {origin_url}

It is managed by [git meta](https://git-meta.com/), which associates
key-value metadata with Git objects (commits, branches, paths, change-ids,
and project-wide settings) and synchronises them across repositories using
ordinary Git transports.

## How it works

Metadata lives locally in a SQLite database (`.git/git-meta.sqlite`) and is
serialized into Git trees and commits under `refs/{namespace}/` for transport.
This remote stores the canonical history under `refs/{namespace}/main`; the
`main` branch you may see at the repository root is unrelated and only exists
for browsing.

Other contributors do **not** clone this repository directly. Instead they
configure it as a metadata remote on top of their existing checkout:

```
git meta remote add {meta_url} --name meta --namespace {namespace}
git meta pull
```

After that, reading and writing metadata works against the project's normal
checkout:

```
git meta get commit:HEAD
git meta set commit:HEAD review:status approved
git meta push
```

## Important notes

- Metadata is exchanged on `refs/{namespace}/main`, never on `refs/heads/main`.
- Never push directly to `refs/{namespace}/main` -- always go through
  `git meta push`, which serializes local changes and resolves conflicts.
- This README only lives in the very first commit on `refs/{namespace}/main`;
  later metadata commits replace the tip tree with the metadata layout.
"#
    )
}

pub fn run_add(url: &str, name: &str, namespace_override: Option<&str>, init: bool) -> Result<()> {
    let ctx = CommandContext::open(None)?;
    let repo = ctx.session.repo();
    let ns = namespace_override
        .unwrap_or(ctx.session.namespace())
        .to_string();
    let url = expand_url(url);

    let s_err = Style::detect_stderr();
    let s_out = Style::detect_stdout();

    // Check if this remote name already exists
    let config = repo.config_snapshot();
    let remote_url_key = format!("remote.{name}.url");
    if config.string(&remote_url_key).is_some() {
        bail!("remote '{name}' already exists");
    }

    // Check the remote for meta refs before configuring. If none are found
    // under the requested namespace and the user has opted in (either via
    // `--init` or by confirming an interactive prompt), we will initialize
    // the remote with a README commit on `refs/{ns}/main` after configuring.
    eprintln!("{} {url}...", s_err.step("Checking"));
    let mut should_init = false;
    match check_remote_refs(&ctx.session, &url, &ns) {
        Ok((has_match, other_namespaces)) => {
            if !has_match {
                if !other_namespaces.is_empty() {
                    let found_refs = other_namespaces
                        .iter()
                        .map(|alt| format!("  refs/{alt}/main"))
                        .collect::<Vec<_>>()
                        .join("\n");
                    let suggestions = other_namespaces
                        .iter()
                        .map(|alt| format!("  git meta remote add {url} --namespace={alt}"))
                        .collect::<Vec<_>>()
                        .join("\n");
                    bail!(
                        "no metadata refs found under refs/{ns}/main on {url}\n\n\
                         However, metadata refs were found under other namespaces:\n{found_refs}\n\n\
                         To use one of these, re-run with --namespace:\n{suggestions}",
                    );
                }

                // No metadata refs anywhere on the remote. Decide whether to
                // initialize it with a starter README commit.
                should_init = init || prompt_for_init(&url, &ns)?;
                if !should_init {
                    bail!(
                        "no metadata refs found on {url}\n\n\
                         The remote does not have refs/{ns}/main or any other recognizable metadata refs.\n\
                         If this is a new metadata remote, re-run with --init to create refs/{ns}/main with a README:\n  \
                         git meta remote add {url} --name {name} --namespace {ns} --init",
                    );
                }
            }
        }
        Err(e) => {
            eprintln!(
                "{}: could not inspect remote refs: {e}",
                s_err.warn("Warning")
            );
            eprintln!("Proceeding with setup anyway...");
        }
    }

    // Write git config entries for the meta remote via subprocess
    // (gix's config mutation API is limited; using git config is the reliable path)
    let git_dir = repo.path();
    let git_dir_str = git_dir.to_string_lossy();
    let run = |args: &[&str]| -> Result<()> {
        let mut full_args = vec!["--git-dir", &git_dir_str, "config"];
        full_args.extend_from_slice(args);
        let output = std::process::Command::new("git")
            .args(&full_args)
            .output()?;
        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            bail!("git config failed: {}", stderr.trim());
        }
        Ok(())
    };

    let prefix = format!("remote.{name}");
    run(&[&format!("{prefix}.url"), &url])?;
    run(&[
        &format!("{prefix}.fetch"),
        &format!("+refs/{ns}/main:refs/{ns}/remotes/main"),
    ])?;
    run(&[&format!("{prefix}.meta"), "true"])?;
    run(&[&format!("{prefix}.promisor"), "true"])?;
    run(&[&format!("{prefix}.partialclonefilter"), "blob:none"])?;

    // If a non-default namespace was specified, store it so other commands can find it
    if namespace_override.is_some() {
        run(&[&format!("{prefix}.metanamespace"), &ns])?;
    }

    println!("{} meta remote '{name}' -> {url}", s_out.ok("Added"));

    // If we are initializing a fresh remote, create a starter commit on
    // `refs/{ns}/local/main` (or reuse one if it already exists) and push it
    // so the subsequent fetch has something to track.
    if should_init {
        let origin_url = config
            .string("remote.origin.url")
            .map_or_else(|| url.clone(), |s| s.to_string());
        ensure_local_meta_ref(&ctx, &ns, &origin_url, &url)?;

        let push_refspec = format!("refs/{ns}/local/main:refs/{ns}/main");
        eprint!("{} refs/{ns}/main on {name}...", s_err.step("Initializing"));
        match git_meta_lib::git_utils::run_git(repo, &["push", name, &push_refspec]) {
            Ok(_) => eprintln!(" {}", s_err.ok("done.")),
            Err(e) => {
                eprintln!(" {}", s_err.err("failed."));
                bail!(
                    "could not push the initial metadata commit to {name} ({url}): {e}\n\n\
                     The remote was configured locally. To retry the push:\n  \
                     git meta push {name}",
                );
            }
        }
    }

    // Initial blobless fetch
    let fetch_refspec = format!("refs/{ns}/main:refs/{ns}/remotes/main");
    eprint!("{} metadata (blobless)...", s_err.step("Fetching"));
    match git_meta_lib::git_utils::run_git(
        repo,
        &["fetch", "--filter=blob:none", name, &fetch_refspec],
    ) {
        Ok(_) => {
            eprintln!(" {}", s_err.ok("done."));

            // Verify the tracking ref was created
            let remote_ref = format!("{ns}/remotes/main");
            let tracking_ref_name = format!("refs/{remote_ref}");
            match repo.find_reference(&tracking_ref_name) {
                Ok(r) => {
                    let tip_oid = r.into_fully_peeled_id()?.detach();
                    eprintln!(
                        "  {} {} -> {}",
                        s_err.dim("tracking ref:"),
                        tracking_ref_name,
                        s_err.dim(&tip_oid.to_string()[..12]),
                    );
                }
                Err(e) => {
                    eprintln!(
                        "  {}: tracking ref {tracking_ref_name} not found after fetch: {e}",
                        s_err.warn("warning"),
                    );
                    eprintln!("You can try again with: git meta pull");
                    return Ok(());
                }
            }

            // Hydrate tip tree blobs so gix can read the metadata
            eprint!("{} tip blobs...", s_err.step("Hydrating"));
            let blob_count =
                git_meta_lib::git_utils::hydrate_tip_blobs_counted(repo, name, &remote_ref)?;
            eprintln!(" {}", s_err.ok(&format!("{blob_count} blobs fetched.")));

            // Materialize remote metadata into local SQLite
            eprint!("{} local metadata...", s_err.step("Serializing"));
            serialize::run(false)?;
            eprintln!(" {}", s_err.ok("done."));

            eprint!("{} remote metadata...", s_err.step("Materializing"));
            materialize::run(None, false, false)?;
            eprintln!(" {}", s_err.ok("done."));

            // Index historical keys as promisor entries
            let tracking_ref_name = format!("refs/{ns}/remotes/main");
            if let Ok(r) = repo.find_reference(&tracking_ref_name) {
                if let Ok(tip_id) = r.into_fully_peeled_id() {
                    let count = git_meta_lib::sync::insert_promisor_entries(
                        repo,
                        ctx.session.store(),
                        tip_id.detach(),
                        None,
                    )?;
                    if count > 0 {
                        eprintln!(
                            "{} {count} keys from history {}",
                            s_err.ok("Indexed"),
                            s_err.dim("(available on demand)."),
                        );
                    }
                }
            }
        }
        Err(e) => {
            eprintln!("\n{}: initial fetch failed: {e}", s_err.warn("Warning"));
            eprintln!("You can fetch later with: git meta pull");
        }
    }

    Ok(())
}

pub fn run_remove(name: &str) -> Result<()> {
    let ctx = CommandContext::open(None)?;
    let repo = ctx.session.repo();
    let ns = ctx.session.namespace();
    let s_out = Style::detect_stdout();

    // Verify this is a meta remote
    let config = repo.config_snapshot();
    let meta_key = format!("remote.{name}.meta");
    let is_meta = config.boolean(&meta_key).unwrap_or(false);
    if !is_meta {
        bail!("'{name}' is not a metadata remote (no meta = true)");
    }

    // Remove the git config section for this remote via subprocess
    let git_dir = repo.path();
    let git_dir_str = git_dir.to_string_lossy();
    let unset = |key: &str| {
        let _ = std::process::Command::new("git")
            .args(["--git-dir", &git_dir_str, "config", "--unset-all", key])
            .output();
    };

    unset(&format!("remote.{name}.url"));
    unset(&format!("remote.{name}.fetch"));
    unset(&format!("remote.{name}.meta"));
    unset(&format!("remote.{name}.promisor"));
    unset(&format!("remote.{name}.partialclonefilter"));
    unset(&format!("remote.{name}.metanamespace"));

    // Delete refs under refs/{ns}/remotes/
    let ref_prefix = format!("refs/{ns}/remotes/");
    let mut refs_to_delete = Vec::new();

    let platform = repo.references()?;
    for reference in platform.all()? {
        let reference = reference.map_err(|e| anyhow::anyhow!("{e}"))?;
        let name_str = reference.name().as_bstr().to_string();
        if name_str.starts_with(&ref_prefix) {
            refs_to_delete.push(name_str);
        }
    }

    for refname in &refs_to_delete {
        let reference = repo.find_reference(refname)?;
        reference.delete().map_err(|e| anyhow::anyhow!("{e}"))?;
        println!("{} ref {refname}", s_out.ok("Deleted"));
    }

    // Also delete refs under refs/{ns}/local/
    let local_prefix = format!("refs/{ns}/local/");
    let mut local_refs_to_delete = Vec::new();

    let platform = repo.references()?;
    for reference in platform.all()? {
        let reference = reference.map_err(|e| anyhow::anyhow!("{e}"))?;
        let name_str = reference.name().as_bstr().to_string();
        if name_str.starts_with(&local_prefix) {
            local_refs_to_delete.push(name_str);
        }
    }

    for refname in &local_refs_to_delete {
        let reference = repo.find_reference(refname)?;
        reference.delete().map_err(|e| anyhow::anyhow!("{e}"))?;
        println!("{} ref {refname}", s_out.ok("Deleted"));
    }

    println!("{} meta remote '{name}'", s_out.ok("Removed"));
    Ok(())
}

pub fn run_list() -> Result<()> {
    let ctx = CommandContext::open(None)?;
    let remotes = git_meta_lib::git_utils::list_meta_remotes(ctx.session.repo())?;

    if remotes.is_empty() {
        println!("No metadata remotes configured.");
        println!("Add one with: git meta remote add <url>");
    } else {
        for (name, url) in &remotes {
            println!("{name}\t{url}");
        }
    }

    Ok(())
}