~gpanders/wk

8b5ba70b42d7f0713899e9bf792b949d91fbf477 — Greg Anders 4 months ago c0b916b
Only create git commits on 'sync' and 'new' commands
6 files changed, 82 insertions(+), 76 deletions(-)

M src/cmd/backlinks.zig
M src/cmd/inbox.zig
M src/cmd/new.zig
M src/cmd/open.zig
M src/cmd/sync.zig
M src/zettel.zig
M src/cmd/backlinks.zig => src/cmd/backlinks.zig +9 -2
@@ 5,7 5,6 @@ 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;

pub const cmd = Command{


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

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    try commitZettels(allocator, try getZettels(allocator));
    var nmodified: u32 = 0;
    for (try getZettels(allocator)) |zet| {
        try zet.writeBacklinks();
        if (try zet.modified()) nmodified += 1;
    }

    if (nmodified > 0) {
        stdout.print("Updated backlinks in {} notes.\n", .{nmodified}) catch return;
    }
}

M src/cmd/inbox.zig => src/cmd/inbox.zig +0 -3
@@ 7,8 7,6 @@ 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{


@@ 21,5 19,4 @@ pub const cmd = Command{

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    try openZettels(allocator, &[_][]const u8{"inbox.md"});
    try commitZettels(allocator, try getZettels(allocator));
}

M src/cmd/new.zig => src/cmd/new.zig +21 -8
@@ 1,12 1,13 @@
const std = @import("std");
const io = std.io;
const fmt = std.fmt;
const fs = std.fs;
const io = std.io;
const mem = std.mem;
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 execAndCheck = @import("../util.zig").execAndCheck;
const getZettels = @import("../zettel.zig").getZettels;
const openZettels = @import("../zettel.zig").openZettels;



@@ 23,30 24,42 @@ pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!v
        const title = args.?[0];
        var new_zettel = try Zettel.new(allocator, title);

        if (fs.cwd().access(".git", .{})) |_| {
            // Fail early if git is not found
            _ = execAndCheck(allocator, &[_][]const u8{
                "git",
                "add",
                new_zettel.fname,
            }) catch |err| switch (err) {
                error.FileNotFound => {},
                else => return err,
            };
        } else |_| {}

        if (!io.getStdOut().isTty()) {
            stdout.print("{}\n", .{new_zettel.fname}) catch {};
            return;
        }

        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();
        var nmodified: u32 = 0;
        for (links) |link| {
            for (zettels) |*zet| {
                if (mem.eql(u8, link, zet.id)) {
                    try zet.backlinks.append(&new_zettel);
                    if (try zet.modified()) nmodified += 1;
                }
            }
        }

        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);
        if (nmodified > 0) {
            stdout.print("Updated backlinks in {} notes.\n", .{nmodified}) catch return;
        }
    } else {
        return error.MissingRequiredArgument;
    }

M src/cmd/open.zig => src/cmd/open.zig +0 -2
@@ 7,7 7,6 @@ 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;



@@ 31,5 30,4 @@ pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!v
    }

    try openZettels(allocator, zettels.items);
    try commitZettels(allocator, all_zettels);
}

M src/cmd/sync.zig => src/cmd/sync.zig +44 -6
@@ 1,10 1,14 @@
const std = @import("std");
const fmt = std.fmt;
const fs = std.fs;
const mem = std.mem;
const stdout = std.io.getStdOut().outStream();
const warn = std.debug.warn;

const Command = @import("../cmd.zig").Command;
const execAndCheck = @import("../util.zig").execAndCheck;
const getZettels = @import("../zettel.zig").getZettels;
const strftime = @import("../zettel.zig").strftime;

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


@@ 15,17 19,51 @@ pub const cmd = Command{
};

pub fn run(allocator: *mem.Allocator, args: ?[]const []const u8) Command.Error!void {
    var result = execAndCheck(allocator, &[_][]const u8{ "git", "pull", "--rebase" }) catch |err| switch (err) {
        error.FileNotFound => {
            warn("git not found or is not executable\n", .{});
            return error.CommandFailed;
        },
    fs.cwd().access(".git", .{}) catch return;

    const zettels = try getZettels(allocator);

    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| add_args.appendAssumeCapacity(zet.fname);

    // 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", "--" };
    var 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;
        },
    }

    result = try execAndCheck(allocator, &[_][]const u8{ "git", "pull", "--rebase" });
    stdout.print("{}", .{result.stderr}) catch {};

    result = try execAndCheck(allocator, &[_][]const u8{ "git", "push" });

    stdout.print("{}", .{result.stderr}) catch {};
}

M src/zettel.zig => src/zettel.zig +8 -55
@@ 169,6 169,8 @@ 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");

    // TODO zig 0.7.0
    // argv.appendSliceAssumeCapacity(zettels)
    for (zettels) |zet| argv.appendAssumeCapacity(zet);

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


@@ 226,53 228,13 @@ 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,
    };
pub fn strftime(allocator: *mem.Allocator, format: [:0]const u8) ![]const u8 {
    var t = c.time(0);
    var tmp = c.localtime(&t);

    // 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;
        },
    }
    var buf = try allocator.allocSentinel(u8, 128, 0);
    _ = c.strftime(buf.ptr, buf.len, format.ptr, tmp);
    return allocator.shrink(buf, mem.len(buf.ptr));
}

fn fromEntry(allocator: *mem.Allocator, entry: fs.Dir.Entry) !Zettel {


@@ 367,12 329,3 @@ test "hasId" {
    std.testing.expect(!hasId("test"));
    std.testing.expect(!hasId(" 20200512074730-test" ++ extension));
}

fn strftime(allocator: *mem.Allocator, format: [:0]const u8) ![]const u8 {
    var t = c.time(0);
    var tmp = c.localtime(&t);

    var buf = try allocator.allocSentinel(u8, 128, 0);
    _ = c.strftime(buf.ptr, buf.len, format.ptr, tmp);
    return allocator.shrink(buf, mem.len(buf.ptr));
}