@@ -21,7 +21,6 @@ pub fn main() !void {
21
21
}
22
22
}
23
23
24
- // load lines of file into memory
25
24
args = try std .process .argsWithAllocator (arena );
26
25
_ = args .skip ();
27
26
const headerDir = args .next () orelse return ;
@@ -36,6 +35,8 @@ pub fn main() !void {
36
35
37
36
var walker = try dir .walk (arena );
38
37
while (try walker .next ()) | entry | {
38
+
39
+ // only process files and sym links - right now we copy the sym links into regular files
39
40
if (entry .kind != .file and entry .kind != .sym_link ) {
40
41
continue ;
41
42
}
@@ -44,11 +45,13 @@ pub fn main() !void {
44
45
45
46
//std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
46
47
48
+ // file extension for our sidecar version information
47
49
const extra = ".uhversion.txt" ;
48
50
49
51
var filepath = try std .fs .path .join (arena , &.{ headerDir , entry .path });
50
52
var workpath = try std .fs .path .join (arena , &.{ "uh_workspace" , entry .path });
51
53
54
+ // read all the lines of our work-in-progress file
52
55
var worklines = std .ArrayList ([]const u8 ).init (arena );
53
56
{
54
57
if (debug ) std .debug .print ("createFile {s}\n " , .{workpath });
@@ -70,6 +73,7 @@ pub fn main() !void {
70
73
}
71
74
}
72
75
76
+ // read all the lines of our work-in-progress sidecar version file
73
77
buf = try arena .alloc (u8 , 1000 );
74
78
const versionpath = try std .fmt .bufPrint (buf , "{s}{s}" , .{ workpath , extra });
75
79
var versionlines = std .ArrayList ([]const u8 ).init (arena );
@@ -89,6 +93,7 @@ pub fn main() !void {
89
93
}
90
94
}
91
95
96
+ // read all the lines of the new file we are going to merge
92
97
var filelines = std .ArrayList ([]const u8 ).init (arena );
93
98
{
94
99
var file = try std .fs .cwd ().openFile (filepath , .{});
@@ -110,8 +115,11 @@ pub fn main() !void {
110
115
}
111
116
112
117
// 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
113
120
try addContext (arena , & filelines );
114
121
122
+ // write out the new file with added context to a temp file
115
123
{
116
124
var file = try std .fs .cwd ().createFile ("uh_workfile" , .{});
117
125
defer file .close ();
@@ -123,6 +131,7 @@ pub fn main() !void {
123
131
124
132
//std.debug.print("filepath: {s}, workpath: {s}\n", .{ filepath, workpath });
125
133
134
+ // run diff and get the output
126
135
var diff_stdout = std .ArrayList (u8 ).init (arena );
127
136
var diff_stderr = std .ArrayList (u8 ).init (arena );
128
137
var diff_child = std .process .Child .init (&.{ "diff" , "-wdN" , workpath , "uh_workfile" }, arena );
@@ -136,7 +145,7 @@ pub fn main() !void {
136
145
137
146
// worklines has all lines accumulated from headers so far
138
147
// versionlines has versions for all lines in worklines
139
- // ctx_stdout.items has new header lines
148
+ // filelines has new header lines
140
149
141
150
// go through diff output and adjust worklines and versionlines
142
151
var line_adj : isize = 0 ;
@@ -275,7 +284,7 @@ pub fn main() !void {
275
284
versionlines .items [i ] = try std .fmt .bufPrint (buf , "{s}{s}" , .{ old , versionStr });
276
285
}
277
286
278
- // write out worklines and versionlines back to disk
287
+ // write out work-in-progress file back to disk
279
288
{
280
289
var file = try std .fs .cwd ().createFile (workpath , .{});
281
290
defer file .close ();
@@ -285,6 +294,7 @@ pub fn main() !void {
285
294
}
286
295
}
287
296
297
+ // write out work-in-progress sidecar version file back to disk
288
298
{
289
299
var file = try std .fs .cwd ().createFile (versionpath , .{});
290
300
defer file .close ();
@@ -296,6 +306,7 @@ pub fn main() !void {
296
306
}
297
307
}
298
308
309
+ // Prepend each line of this file with a hash to prevent the diff from splitting #if/#endif into separate versions
299
310
pub fn addContext (arena : std.mem.Allocator , lines : * std .ArrayList ([]const u8 )) ! void {
300
311
var seen_contexts = std .ArrayList (u32 ).init (arena );
301
312
var context = std .ArrayList (u32 ).init (arena );
@@ -373,6 +384,9 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
373
384
}
374
385
}
375
386
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)
376
390
pub fn newContext (lines : [][]const u8 , i : usize , ctx : u32 ) u32 {
377
391
const fnv = std .hash .Fnv1a_32 ;
378
392
var h = fnv .init ();
0 commit comments