Skip to content
zit Artifact [4ed0c931e5]
Login

Artifact [4ed0c931e5]

Artifact 4ed0c931e5496623754e48318b3572c13aa5860654e0cdd50b565b8188e48516:


//! `zit merge-base`: the merge base of two check-ins.

const std = @import("std");
const zit = @import("zit");
const cli = @import("zit").cli;
const Repository = @import("zit").Repository;
const diagnostics = @import("../cli/diagnostics.zig");
const help_pages = @import("../cli/help.zig");
const stateOom = diagnostics.stateOom;
const openRepo = diagnostics.openRepo;

/// `zit merge-base A B` — the merge base of two check-ins, one full hash on
/// stdout, or exit 1 with the reason on stderr when there is none or a name
/// does not resolve to a held check-in. Plumbing for an origin (quarry)
/// answering a horizon clone that cannot compute the base itself because one
/// side's ancestry lies below its horizon.
pub fn runMergeBase(
    gpa: std.mem.Allocator,
    io: std.Io,
    arena: std.mem.Allocator,
    verb_args: []const []const u8,
    streams: cli.Streams,
    amb: cli.Ambient,
) cli.Exit {
    var names: [2][]const u8 = undefined;
    var n: usize = 0;
    var no_cache = false;
    for (verb_args) |tok| {
        if (std.mem.eql(u8, tok, "-h") or std.mem.eql(u8, tok, "--help")) {
            streams.out.writeAll(help_pages.merge_base_help) catch {};
            return .ok;
        }
        if (std.mem.eql(u8, tok, "--no-cache")) {
            no_cache = true;
            continue;
        }
        if (tok.len > 1 and tok[0] == '-') return cli.usageError(streams, "merge-base", .unknown_option, tok);
        if (n == 2) return cli.usageError(streams, "merge-base", .extra_argument, tok);
        names[n] = tok;
        n += 1;
    }
    if (n != 2) return cli.usageError(streams, "merge-base", .no_checkin, "");

    var repo: Repository = undefined;
    var root_dir: std.Io.Dir = undefined;
    const rc = openRepo(gpa, io, arena, streams, "merge-base", amb.repo_override, no_cache, true, &repo, &root_dir);
    if (rc != .ok) return rc;
    defer root_dir.close(io);
    defer repo.deinit(gpa);

    var rids: [2]Repository.Rid = undefined;
    for (names, 0..) |name, i| {
        rids[i] = repo.resolve(name, .checkin) catch |e| switch (e) {
            error.NotFound => {
                streams.err.print("zit merge-base: check-in '{s}' not found\n", .{name}) catch {};
                cli.horizonNameHint(gpa, io, root_dir, streams, "merge-base");
                return .failure;
            },
            error.Ambiguous => {
                streams.err.print("zit merge-base: name '{s}' is ambiguous\n", .{name}) catch {};
                return .failure;
            },
            error.OutOfMemory => return stateOom(streams, "merge-base"),
        };
    }
    // Horizon draft section 5.5 -- a structural horizon holds K's ancestry
    // only down to the boundary; past it a better base may exist. Fail
    // closed rather than name one the repository would not.
    if (structuralHorizon(gpa, io, root_dir) and (repo.mergeBaseMayLieBelow(gpa, rids[0], rids[1..2]) catch return stateOom(streams, "merge-base"))) {
        streams.err.print("zit merge-base: the merge base of '{s}' and '{s}' may lie below the store's horizon; the origin answers it\n", .{ names[0], names[1] }) catch {};
        return .failure;
    }
    const base = (repo.mergeBaseOf(gpa, rids[0], rids[1]) catch return stateOom(streams, "merge-base")) orelse {
        streams.err.print("zit merge-base: '{s}' and '{s}' share no history\n", .{ names[0], names[1] }) catch {};
        return .failure;
    };
    streams.out.print("{s}\n", .{repo.hashOf(base)}) catch {};
    return .ok;
}

/// Whether this store's cone carries a structural horizon.
fn structuralHorizon(gpa: std.mem.Allocator, io: std.Io, root_dir: std.Io.Dir) bool {
    var cone = zit.repocache.readStoreCone(gpa, io, root_dir) orelse return false;
    defer cone.deinit(gpa);
    const h = cone.horizon orelse return false;
    return h.depth == .structural;
}