srctree

Gregory Mullen parent 360de6db 21a62792
partial migration of diff template to typed page

inlinesplit
src/endpoints/repos/commits.zig added: 61, removed: 53, total 8
@@ -39,7 +39,7 @@ const AddComment = struct {
pub fn router(ctx: *Context) Error!Route.Callable {
const rd = RouteData.make(&ctx.uri) orelse return commits;
if (rd.verb != null and std.mem.eql(u8, "commit", rd.verb.?))
return commit;
return viewCommit;
return commits;
}
 
@@ -60,37 +60,8 @@ pub fn patchContext(a: Allocator, patch: *Patch.Patch) ![]Template.Context {
 
return error.PatchInvalid;
};
//const dstat = patch.patchStat();
//const stat = try std.fmt.allocPrint(a, "added: {}, removed: {}, total {}", .{
// dstat.additions,
// dstat.deletions,
// dstat.total,
//});
 
const patch_ctx = try patch.diffsContextSlice(a);
 
//for (diffs, files) |diff, *file| {
// const body = diff.changes orelse {
// file.* = Template.Context.init(a);
// continue;
// };
// var ctx: Template.Context = Template.Context.init(a);
// try ctx.putSlice("DiffStat", stat);
// {
// const changes = Patch.diffLine(a, body);
// const list = try a.alloc([]u8, changes.len);
// defer a.free(list);
// for (list, changes) |*l, e| {
// l.* = try std.fmt.allocPrint(a, "{pretty}", .{e});
// }
// defer for (list) |l| a.free(l);
// const value = try std.mem.join(a, "", list);
// try ctx.putSlice("Diff", value);
// }
// file.* = ctx;
//}
 
return patch_ctx;
return try patch.diffsContextSlice(a);
}
 
fn commitHtml(ctx: *Context, sha: []const u8, repo_name: []const u8, repo: Git.Repo) Error!void {
@@ -200,7 +171,7 @@ pub fn commitPatch(ctx: *Context, sha: []const u8, repo: Git.Repo) Error!void {
}
}
 
pub fn commit(ctx: *Context) Error!void {
pub fn viewCommit(ctx: *Context) Error!void {
const rd = RouteData.make(&ctx.uri) orelse return error.Unrouteable;
if (rd.verb == null) return commits(ctx);
 
 
src/endpoints/repos/diffs.zig added: 61, removed: 53, total 8
@@ -141,6 +141,45 @@ fn newComment(ctx: *Context) Error!void {
return error.Unknown;
}
 
pub fn patchStruct(a: Allocator, patch: *Patch.Patch) !Template.Structs.PatchHtml {
patch.parse(a) catch |err| {
if (std.mem.indexOf(u8, patch.blob, "\nMerge: ") == null) {
std.debug.print("err: {any}\n", .{err});
std.debug.print("'''\n{s}\n'''\n", .{patch.blob});
return err;
} else {
std.debug.print("Unable to parse diff {} (merge commit)\n", .{err});
return error.UnableToGeneratePatch;
}
};
 
const diffs = patch.diffs orelse unreachable;
const files = try a.alloc(Template.Structs.Files, diffs.len);
errdefer a.free(files);
for (diffs, files) |diff, *file| {
const body = diff.changes orelse continue;
 
const dstat = patch.patchStat();
const stat = try allocPrint(
a,
"added: {}, removed: {}, total {}",
.{ dstat.additions, dstat.deletions, dstat.total },
);
 
file.* = .{
.diff_stat = stat,
.filename = if (diff.filename) |name|
try allocPrint(a, "{s}", .{name})
else
try allocPrint(a, "{s} was Deleted", .{"filename"}),
.diff = try allocPrint(a, "{}", .{Patch.diffLineHtml(a, body)[0]}),
};
}
return .{
.files = files,
};
}
 
pub fn patchHtml(a: Allocator, patch: *Patch.Patch) ![]HTML.Element {
patch.parse(a) catch |err| {
if (std.mem.indexOf(u8, patch.blob, "\nMerge: ") == null) {
@@ -162,11 +201,11 @@ pub fn patchHtml(a: Allocator, patch: *Patch.Patch) ![]HTML.Element {
const body = diff.changes orelse continue;
 
const dstat = patch.patchStat();
const stat = try std.fmt.allocPrint(a, "added: {}, removed: {}, total {}", .{
dstat.additions,
dstat.deletions,
dstat.total,
});
const stat = try std.fmt.allocPrint(
a,
"added: {}, removed: {}, total {}",
.{ dstat.additions, dstat.deletions, dstat.total },
);
dom.push(HTML.element("diffstat", stat, null));
dom = dom.open(HTML.diff());
 
@@ -245,19 +284,21 @@ fn view(ctx: *Context) Error!void {
 
try ctx.putContext("Delta_id", .{ .slice = delta_id });
 
var patch_formatted: ?[]u8 = null;
var patch_formatted: ?Template.Structs.PatchHtml = null;
const filename = try std.fmt.allocPrint(ctx.alloc, "data/patch/{s}.{x}.patch", .{ rd.name, delta.index });
const file: ?std.fs.File = std.fs.cwd().openFile(filename, .{}) catch null;
if (file) |f| {
 
if (std.fs.cwd().openFile(filename, .{})) |f| {
const fdata = f.readToEndAlloc(ctx.alloc, 0xFFFFF) catch return error.Unknown;
var patch = Patch.Patch.init(fdata);
if (patchHtml(ctx.alloc, &patch)) |phtml| {
patch_formatted = try allocPrint(ctx.alloc, "{pretty}", .{phtml});
if (patchStruct(ctx.alloc, &patch)) |phtml| {
patch_formatted = phtml;
} else |err| {
std.debug.print("Unable to generate patch {any}\n", .{err});
}
f.close();
} else try ctx.putContext("Patch", .{ .slice = "Patch not found" });
} else |err| {
std.debug.print("Unable to load patch {} {s}\n", .{ err, filename });
}
 
var page = DiffViewPage.init(.{
.meta_head = .{
@@ -269,10 +310,10 @@ fn view(ctx: *Context) Error!void {
} },
.patch = if (patch_formatted) |pf| .{
.header = patch_header,
.data = pf,
.patch = pf,
} else .{
.header = patch_header,
.data = "unable to generate a usable patch",
.patch = .{ .files = &[0]Template.Structs.Files{} },
},
.comments = comments_,
.delta_id = delta_id,
 
templates/delta-diff.html added: 61, removed: 53, total 8
@@ -8,12 +8,8 @@
<_body_header.html>
<content class="diff">
<With Patch>
<patchheader>
<Header>
<patch>
<Data>
</patch>
</patchheader>
<_patch.html>
</With>
 
<comments>