Skip to content

Commit e1cb465

Browse files
add some comments
1 parent 84c9746 commit e1cb465

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/textdiff/addHeaders.zig

+17-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub fn main() !void {
2121
}
2222
}
2323

24-
// load lines of file into memory
2524
args = try std.process.argsWithAllocator(arena);
2625
_ = args.skip();
2726
const headerDir = args.next() orelse return;
@@ -36,6 +35,8 @@ pub fn main() !void {
3635

3736
var walker = try dir.walk(arena);
3837
while (try walker.next()) |entry| {
38+
39+
// only process files and sym links - right now we copy the sym links into regular files
3940
if (entry.kind != .file and entry.kind != .sym_link) {
4041
continue;
4142
}
@@ -44,11 +45,13 @@ pub fn main() !void {
4445

4546
//std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
4647

48+
// file extension for our sidecar version information
4749
const extra = ".uhversion.txt";
4850

4951
var filepath = try std.fs.path.join(arena, &.{ headerDir, entry.path });
5052
var workpath = try std.fs.path.join(arena, &.{ "uh_workspace", entry.path });
5153

54+
// read all the lines of our work-in-progress file
5255
var worklines = std.ArrayList([]const u8).init(arena);
5356
{
5457
if (debug) std.debug.print("createFile {s}\n", .{workpath});
@@ -70,6 +73,7 @@ pub fn main() !void {
7073
}
7174
}
7275

76+
// read all the lines of our work-in-progress sidecar version file
7377
buf = try arena.alloc(u8, 1000);
7478
const versionpath = try std.fmt.bufPrint(buf, "{s}{s}", .{ workpath, extra });
7579
var versionlines = std.ArrayList([]const u8).init(arena);
@@ -89,6 +93,7 @@ pub fn main() !void {
8993
}
9094
}
9195

96+
// read all the lines of the new file we are going to merge
9297
var filelines = std.ArrayList([]const u8).init(arena);
9398
{
9499
var file = try std.fs.cwd().openFile(filepath, .{});
@@ -110,8 +115,11 @@ pub fn main() !void {
110115
}
111116

112117
// first add context to the new file
118+
// - this prepends each line with a hash value
119+
// - solves diff problems later by keeping #if-#endif blocks and comment blocks together
113120
try addContext(arena, &filelines);
114121

122+
// write out the new file with added context to a temp file
115123
{
116124
var file = try std.fs.cwd().createFile("uh_workfile", .{});
117125
defer file.close();
@@ -123,6 +131,7 @@ pub fn main() !void {
123131

124132
//std.debug.print("filepath: {s}, workpath: {s}\n", .{ filepath, workpath });
125133

134+
// run diff and get the output
126135
var diff_stdout = std.ArrayList(u8).init(arena);
127136
var diff_stderr = std.ArrayList(u8).init(arena);
128137
var diff_child = std.process.Child.init(&.{ "diff", "-wdN", workpath, "uh_workfile" }, arena);
@@ -136,7 +145,7 @@ pub fn main() !void {
136145

137146
// worklines has all lines accumulated from headers so far
138147
// versionlines has versions for all lines in worklines
139-
// ctx_stdout.items has new header lines
148+
// filelines has new header lines
140149

141150
// go through diff output and adjust worklines and versionlines
142151
var line_adj: isize = 0;
@@ -275,7 +284,7 @@ pub fn main() !void {
275284
versionlines.items[i] = try std.fmt.bufPrint(buf, "{s}{s}", .{ old, versionStr });
276285
}
277286

278-
// write out worklines and versionlines back to disk
287+
// write out work-in-progress file back to disk
279288
{
280289
var file = try std.fs.cwd().createFile(workpath, .{});
281290
defer file.close();
@@ -285,6 +294,7 @@ pub fn main() !void {
285294
}
286295
}
287296

297+
// write out work-in-progress sidecar version file back to disk
288298
{
289299
var file = try std.fs.cwd().createFile(versionpath, .{});
290300
defer file.close();
@@ -296,6 +306,7 @@ pub fn main() !void {
296306
}
297307
}
298308

309+
// Prepend each line of this file with a hash to prevent the diff from splitting #if/#endif into separate versions
299310
pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !void {
300311
var seen_contexts = std.ArrayList(u32).init(arena);
301312
var context = std.ArrayList(u32).init(arena);
@@ -373,6 +384,9 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
373384
}
374385
}
375386

387+
// We are given the line containing the first #if
388+
// - hash that line, any #elif, and the #endif line together
389+
// - must skip over any nested #if blocks (and comments)
376390
pub fn newContext(lines: [][]const u8, i: usize, ctx: u32) u32 {
377391
const fnv = std.hash.Fnv1a_32;
378392
var h = fnv.init();

src/textdiff/outputHeaders.zig

+9-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub fn main() !void {
4444

4545
//std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
4646

47+
// read work-in-progress file into memory
4748
var workpath = try std.fs.path.join(arena, &.{ "uh_workspace", entry.path });
48-
4949
var worklines = std.ArrayList([]const u8).init(arena);
5050
{
5151
var file = try std.fs.cwd().openFile(workpath, .{});
@@ -63,6 +63,7 @@ pub fn main() !void {
6363
}
6464
}
6565

66+
// read work-in-progress sidecar version file into memory
6667
var buf = try arena.alloc(u8, 1000);
6768
const versionpath = try std.fmt.bufPrint(buf, "{s}{s}", .{ workpath, extra });
6869
var versionlines = std.ArrayList([]const u8).init(arena);
@@ -82,6 +83,7 @@ pub fn main() !void {
8283
}
8384
}
8485

86+
// make writer to output file that will be our universal header
8587
var filepath = try std.fs.path.join(arena, &.{ outDir, entry.path });
8688
if (std.fs.path.dirname(filepath)) |dirname| {
8789
try std.fs.cwd().makePath(dirname);
@@ -92,12 +94,18 @@ pub fn main() !void {
9294
var outwriter = outfile.writer();
9395

9496
if (versionStr) |version| {
97+
// this is for debugging
9598
for (worklines.items, versionlines.items) |workline, versionline| {
9699
if (std.mem.indexOf(u8, versionline, version) != null) {
97100
try outwriter.print("{s}\n", .{workline[9..]});
98101
}
99102
}
100103
} else {
104+
// output the universal header
105+
// - we maintain a stack of the version #if blocks we are inside of
106+
// - if the version changes, then either:
107+
// - - it is a subset of the previous version, so we can add a new nested #if block
108+
// - - it is not a subset, so #endif the block and start a new one
101109
var versionstack = std.ArrayList([]const u8).init(arena);
102110
for (worklines.items, versionlines.items) |workline, versionline| {
103111
if (versionstack.items.len > 0 and std.mem.eql(u8, versionline, versionstack.items[versionstack.items.len - 1])) {

src/textdiff/testHeaders.zig

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const std = @import("std");
22

33
const debug = false;
44

5+
// This utility goes through universal headers and only evaluates the #if blocks that we added to make them universal
6+
// - what comes out can be diffed against the original headers for testing
57
pub fn main() !void {
68
var arena_allocator = std.heap.ArenaAllocator.init(std.heap.page_allocator);
79
const arena = arena_allocator.allocator();
@@ -41,8 +43,8 @@ pub fn main() !void {
4143

4244
//std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
4345

46+
// read universal header into memory
4447
var inpath = try std.fs.path.join(arena, &.{ inDir, entry.path });
45-
4648
var inlines = std.ArrayList([]const u8).init(arena);
4749
{
4850
var file = try std.fs.cwd().openFile(inpath, .{});
@@ -69,7 +71,11 @@ pub fn main() !void {
6971
defer outfile.close();
7072
var outwriter = outfile.writer();
7173

74+
// maintain a stack corresponding to #if blocks we are inside of
75+
// - bit 1 is if we should be outputting these lines (because the version matches)
76+
// - bit 2 is if this #if block is one we added to make the universal header (we don't want to output those)
7277
var outputing = std.ArrayList(u2).init(arena);
78+
7379
var in_comment: bool = false;
7480
for (inlines.items) |line| {
7581
if (debug) std.debug.print("{d} {s}\n", .{ outputing.items.len, line });

0 commit comments

Comments
 (0)