~gpanders/wk

5d019098d3155024ee3fa57ac8fbfbea5ccc27db — Greg Anders a month ago e9bbe23
Move all committing logic into commitZettels function
6 files changed, 81 insertions(+), 121 deletions(-)

M src/cmd/backlinks.zig
M src/cmd/inbox.zig
M src/cmd/new.zig
M src/cmd/open.zig
M src/util.zig
M src/zettel.zig
M src/cmd/backlinks.zig => src/cmd/backlinks.zig +2 -13
@@ 5,8 5,8 @@ const stdout = std.io.getStdOut().outStream();

const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const commitZettels = @import("../zettel.zig").commitZettels;
const getZettels = @import("../zettel.zig").getZettels;
const commit = @import("../util.zig").commit;

pub const cmd = Command{
    .name = "backlinks",


@@ 17,16 17,5 @@ pub const cmd = Command{
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const zettels = try getZettels(allocator);
    var modified = std.ArrayList([]const u8).init(allocator);
    for (zettels) |*zet| {
        try zet.writeBacklinks();
        if (try zet.modified()) try modified.append(zet.fname);
    }

    if (modified.items.len > 0) {
        const msg = try fmt.allocPrint(allocator, "Updating backlinks in {} file(s)", .{modified.items.len});
        stdout.print("{}.\n", .{msg}) catch {};
        try commit(allocator, modified.items, msg);
    }
    try commitZettels(allocator, try getZettels(allocator));
}

M src/cmd/inbox.zig => src/cmd/inbox.zig +5 -17
@@ 7,6 7,9 @@ const warn = std.debug.warn;

const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const commitZettels = @import("../zettel.zig").commitZettels;
const getZettels = @import("../zettel.zig").getZettels;
const openZettels = @import("../zettel.zig").openZettels;

pub const cmd = Command{
    .name = "inbox",


@@ 17,21 20,6 @@ pub const cmd = Command{
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const argv = &[_][]const u8{
        os.getenv("EDITOR") orelse "vi",
        "inbox.md",
    };

    var proc = try std.ChildProcess.init(argv, allocator);
    defer proc.deinit();

    const term = try proc.spawnAndWait();
    switch (term) {
        .Exited => {},
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (argv) |arg| warn("{} ", .{arg});
            return error.CommandFailed;
        },
    }
    try openZettels(allocator, &[_][]const u8{"inbox.md"});
    try commitZettels(allocator, try getZettels(allocator));
}

M src/cmd/new.zig => src/cmd/new.zig +11 -14
@@ 6,9 6,9 @@ const stdout = std.io.getStdOut().outStream();

const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const commitZettels = @import("../zettel.zig").commitZettels;
const getZettels = @import("../zettel.zig").getZettels;
const openZettels = @import("../zettel.zig").openZettels;
const commit = @import("../util.zig").commit;

pub const cmd = Command{
    .name = "new",


@@ 28,28 28,25 @@ pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!v
            return;
        }

        try openZettels(allocator, &[_]Zettel{new_zettel});

        try openZettels(allocator, &[_][]const u8{new_zettel.fname});
        new_zettel.contents = try new_zettel.read();

        const zettels = try getZettels(allocator);
        const links = try new_zettel.links();
        for (links) |link| {
            for (zettels) |*zet| {
                if (mem.eql(u8, link, zet.id)) try zet.backlinks.append(&new_zettel);
                if (mem.eql(u8, link, zet.id)) {
                    try zet.backlinks.append(&new_zettel);
                }
            }
        }

        var modified = std.ArrayList([]const u8).init(allocator);
        try modified.append(new_zettel.fname);

        for (zettels) |*zet| {
            try zet.writeBacklinks();
            if (try zet.modified()) try modified.append(zet.fname);
        }

        const msg = try fmt.allocPrint(allocator, "New note: {}", .{title});
        try commit(allocator, modified.items, msg);
        var all_zettels = try std.ArrayList(Zettel).initCapacity(allocator, zettels.len + 1);
        // TODO zig 0.7.0
        // all_zettels.appendSliceAssumeCapacity(zettels);
        for (zettels) |zet| all_zettels.appendAssumeCapacity(zet);
        all_zettels.appendAssumeCapacity(new_zettel);
        try commitZettels(allocator, all_zettels.items);
    } else {
        return error.MissingRequiredArgument;
    }

M src/cmd/open.zig => src/cmd/open.zig +9 -38
@@ 7,8 7,9 @@ const warn = std.debug.warn;

const Command = @import("../cmd.zig").Command;
const Zettel = @import("../zettel.zig").Zettel;
const commitZettels = @import("../zettel.zig").commitZettels;
const getZettels = @import("../zettel.zig").getZettels;
const commit = @import("../util.zig").commit;
const openZettels = @import("../zettel.zig").openZettels;

pub const cmd = Command{
    .name = "open",


@@ 19,46 20,16 @@ pub const cmd = Command{
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    const zettels = try getZettels(allocator);

    var argv = std.ArrayList([]const u8).init(allocator);
    try argv.append(os.getenv("EDITOR") orelse "vi");
    var zettels = std.ArrayList([]const u8).init(allocator);
    defer zettels.deinit();

    const all_zettels = try getZettels(allocator);
    if (args) |patterns| {
        for (zettels) |zet| {
            if (zet.match(patterns)) try argv.append(zet.fname);
        for (all_zettels) |zet| {
            if (zet.match(patterns)) try zettels.append(zet.fname);
        }
    }

    var proc = try std.ChildProcess.init(argv.items, allocator);
    defer proc.deinit();

    const term = try proc.spawnAndWait();
    switch (term) {
        .Exited => {},
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (argv.items) |arg| warn("{} ", .{arg});
            return error.CommandFailed;
        },
    }

    var modified = std.ArrayList(*const Zettel).init(allocator);

    for (zettels) |*zet| {
        try zet.writeBacklinks();
        if (try zet.modified()) try modified.append(zet);
    }

    var lines = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len + 2);
    var files = try std.ArrayList([]const u8).initCapacity(allocator, modified.items.len);

    lines.appendAssumeCapacity(try fmt.allocPrint(allocator, "Updated {} files\n", .{modified.items.len}));
    for (modified.items) |mod| {
        lines.appendAssumeCapacity(try fmt.allocPrint(allocator, "- [[{}]] {}", .{ mod.id, mod.title }));
        files.appendAssumeCapacity(mod.fname);
    }

    const msg = try mem.join(allocator, "\n", lines.items);
    try commit(allocator, files.items, msg);
    try openZettels(allocator, zettels.items);
    try commitZettels(allocator, all_zettels);
}

M src/util.zig => src/util.zig +0 -36
@@ 30,39 30,3 @@ pub fn execAndCheck(allocator: *mem.Allocator, args: []const []const u8) !ChildP

    return result;
}

pub fn commit(allocator: *mem.Allocator, files: []const []const u8, msg: []const u8) !void {
    fs.cwd().access(".git", .{}) catch return;

    var add_args = std.ArrayList([]const u8).init(allocator);

    try add_args.appendSlice(&[_][]const u8{ "git", "add" });
    try add_args.appendSlice(files);

    // Fail early if git is not found
    _ = execAndCheck(allocator, add_args.items) catch |err| switch (err) {
        error.FileNotFound => return,
        else => return err,
    };

    // Only commit if index is modified
    const diff_index_args = [_][]const u8{ "git", "diff-index", "--quiet", "--cached", "HEAD", "--" };
    const result = try std.ChildProcess.exec(.{
        .allocator = allocator,
        .argv = &diff_index_args,
    });

    switch (result.term) {
        .Exited => |code| if (code == 1) {
            // Index is modified
            _ = try execAndCheck(allocator, &[_][]const u8{ "git", "commit", "-m", msg });
        },
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (diff_index_args) |arg| warn("{} ", .{arg});
            warn("\n", .{});
            warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }
}

M src/zettel.zig => src/zettel.zig +54 -3
@@ 8,6 8,8 @@ const warn = std.debug.warn;

const c = @cImport(@cInclude("time.h"));

const execAndCheck = @import("util.zig").execAndCheck;

const id_len = "20200106121000".len;
const extension = ".md";
const bl_header = "## Backlinks";


@@ 131,7 133,7 @@ pub const Zettel = struct {
    }

    /// Write backlinks to file
    pub fn writeBacklinks(self: *Zettel) !void {
    pub fn writeBacklinks(self: Zettel) !void {
        if (self.backlinks.items.len == 0) return;

        const start_index = if (mem.indexOf(u8, self.contents, bl_header)) |s|


@@ 163,11 165,11 @@ pub const Zettel = struct {
    }
};

pub fn openZettels(allocator: *mem.Allocator, zettels: []const Zettel) !void {
pub fn openZettels(allocator: *mem.Allocator, zettels: []const []const u8) !void {
    var argv = try std.ArrayList([]const u8).initCapacity(allocator, zettels.len + 1);
    argv.appendAssumeCapacity(os.getenv("EDITOR") orelse "vi");

    for (zettels) |zet| argv.appendAssumeCapacity(zet.fname);
    for (zettels) |zet| argv.appendAssumeCapacity(zet);

    var proc = try std.ChildProcess.init(argv.items, allocator);
    defer proc.deinit();


@@ 224,6 226,55 @@ pub fn getZettels(allocator: *mem.Allocator) ![]Zettel {
    return zettels.toOwnedSlice();
}

pub fn commitZettels(allocator: *mem.Allocator, zettels: []Zettel) !void {
    fs.cwd().access(".git", .{}) catch return;

    var add_args = try std.ArrayList([]const u8).initCapacity(allocator, zettels.len + 2);

    // TODO: zig 0.7.0
    // add_args.appendSliceAssumeCapacity(&[_][]const u8{ "git", "add" });
    inline for (&[_][]const u8{ "git", "add" }) |s| {
        add_args.appendAssumeCapacity(s);
    }

    for (zettels) |zet| {
        try zet.writeBacklinks();
        add_args.appendAssumeCapacity(zet.fname);
    }

    // Any files that live "outside" the Zettelkasten but that should still be
    // tracked should be listed here
    try add_args.append("inbox.md");

    // Fail early if git is not found
    _ = execAndCheck(allocator, add_args.items) catch |err| switch (err) {
        error.FileNotFound => return,
        else => return err,
    };

    // Only commit if index is modified
    const diff_index_args = [_][]const u8{ "git", "diff-index", "--quiet", "--cached", "HEAD", "--" };
    const result = try std.ChildProcess.exec(.{
        .allocator = allocator,
        .argv = &diff_index_args,
    });

    switch (result.term) {
        .Exited => |code| if (code == 1) {
            // Index is modified
            const msg = try fmt.allocPrint(allocator, "Update on {}\n", .{strftime(allocator, "%+")});
            _ = try execAndCheck(allocator, &[_][]const u8{ "git", "commit", "-m", msg });
        },
        else => {
            warn("The following command terminated unexpectedly:\n", .{});
            for (diff_index_args) |arg| warn("{} ", .{arg});
            warn("\n", .{});
            warn("{}\n", .{result.stderr});
            return error.CommandFailed;
        },
    }
}

fn fromEntry(allocator: *mem.Allocator, entry: fs.Dir.Entry) !Zettel {
    var file = try fs.cwd().openFile(entry.name, .{ .read = true });
    defer file.close();