authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-08 07:38:28 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-05-08 07:38:28 -07:00
log1dcc91b655c1cef7d1e515a1c2aef83f1f3b31db
treedc27554409284f5180540ebebdfdde6d166011c7
parent803c0cd0c72c3db01885a3706d67499f9679c87f

rewrite parseTreeDiffMeta and parseTreeDiff to handle diffs with empty files


1 files changed, 65 insertions(+), 35 deletions(-)

git.zig+65-35
...@@ -335,42 +335,54 @@ pub fn parseTreeDiffMeta(input: string) !TreeDiffMeta {...@@ -335,42 +335,54 @@ pub fn parseTreeDiffMeta(input: string) !TreeDiffMeta {
335 result.files_changed += 1;335 result.files_changed += 1;
336 }336 }
337337
338 while (lineiter.next()) |lin| {338 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git"));
339 std.debug.assert(lin.len > 0);339 while (true) {
340 switch (lin[0]) {340 while (lineiter.next()) |lin| {
341 // zig fmt: off341 if (std.mem.startsWith(u8, lin, "index")) break;
342 'd' => continue, // diff --git a/src/Sema.zig b/src/Sema.zig342 if (std.mem.startsWith(u8, lin, "new file mode")) continue;
343 // deleted file mode 100644343 if (std.mem.startsWith(u8, lin, "deleted file mode")) continue;
344 'n' => continue, // new file mode 100644344
345 'i' => continue, // index 327ff3800..df199be97 100644345 std.log.err("{s}", .{lin});
346 '@' => continue, // @@ -24629,10 +24634,11 @@346 unreachable;
347 ' ' => continue,347 }
348 '+' => result.lines_added += 1,348 if (lineiter.peek() == null) break; // handle empty file being last or being at the end
349 '-' => result.lines_removed += 1,349
350 '\\' => continue, // \ No newline at end of file350 if (std.mem.startsWith(u8, lineiter.peek().?, "Binary files")) {
351 'B' => {351 _ = lineiter.next().?;
352 // Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ352 result.lines_added += 1;
353 result.lines_added += 1;353 result.lines_removed += 1;
354 result.lines_removed += 1;354 if (lineiter.peek() == null) break; // handle binary file being last
355 },355 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git"));
356 else => {356 continue;
357 std.log.err("{s}", .{lin});
358 std.log.err("{s}", .{input});
359 @panic("unreachable");
360 },
361 // zig fmt: on
362 }357 }
363 }
364358
365 // Every affected file in the diff has a preamble like below that we don't want to double count.359 if (std.mem.startsWith(u8, lineiter.peek().?, "diff --git")) { // handle empty file being in the middle
366 //360 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git"));
367 // diff --git a/notes/all_packages.txt b/notes/all_packages.txt361 continue;
368 // index c06b41d..e8f91cf 100644362 }
369 // --- a/notes/all_packages.txt363
370 // +++ b/notes/all_packages.txt364 // Every affected text file in the diff has a preamble like below
371 // @@ -89,3 +89,4 @@ freedesktop/xorg/libsm365 //
372 result.lines_added -= result.files_changed;366 // diff --git a/notes/all_packages.txt b/notes/all_packages.txt
373 result.lines_removed -= result.files_changed;367 // index c06b41d..e8f91cf 100644
368 // --- a/notes/all_packages.txt
369 // +++ b/notes/all_packages.txt
370 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "---"));
371 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "+++"));
372
373 while (lineiter.next()) |lin| {
374 if (std.mem.startsWith(u8, lin, "diff --git")) break;
375 switch (lin[0]) {
376 '@' => continue, // @@ -89,3 +89,4 @@ freedesktop/xorg/libsm
377 ' ' => continue,
378 '+' => result.lines_added += 1,
379 '-' => result.lines_removed += 1,
380 '\\' => continue, // \ No newline at end of file
381 else => @panic(lin),
382 }
383 }
384 if (lineiter.peek() == null) break; // handle being at the end
385 }
374386
375 return result;387 return result;
376}388}
...@@ -435,7 +447,15 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff {...@@ -435,7 +447,15 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff {
435 unreachable;447 unreachable;
436 }448 }
437449
438 const before_path_raw = lineiter.next().?;450 const before_path_raw = lineiter.next() orelse {
451 // empty file diff has nothing after 'index' line and this branch handles when its the last item
452 try diffs.append(.{
453 .before_path = overview.items[diffs.items.len].sub_path,
454 .after_path = overview.items[diffs.items.len].sub_path,
455 .content = "",
456 });
457 break;
458 };
439 if (std.mem.startsWith(u8, before_path_raw, "Binary files")) {459 if (std.mem.startsWith(u8, before_path_raw, "Binary files")) {
440 try diffs.append(.{460 try diffs.append(.{
441 .before_path = overview.items[diffs.items.len].sub_path,461 .before_path = overview.items[diffs.items.len].sub_path,
...@@ -443,6 +463,16 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff {...@@ -443,6 +463,16 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff {
443 .content = before_path_raw,463 .content = before_path_raw,
444 });464 });
445 _ = lineiter.index orelse break :blk;465 _ = lineiter.index orelse break :blk;
466 std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git"));
467 continue :blk;
468 }
469 if (std.mem.startsWith(u8, before_path_raw, "diff --git")) {
470 // empty file in the middle, no diff
471 try diffs.append(.{
472 .before_path = overview.items[diffs.items.len].sub_path,
473 .after_path = overview.items[diffs.items.len].sub_path,
474 .content = "",
475 });
446 continue :blk;476 continue :blk;
447 }477 }
448 const before_path = extras.trimPrefixEnsure(before_path_raw, "--- a/") orelse extras.trimPrefixEnsure(before_path_raw, "--- ") orelse @panic(before_path_raw);478 const before_path = extras.trimPrefixEnsure(before_path_raw, "--- a/") orelse extras.trimPrefixEnsure(before_path_raw, "--- ") orelse @panic(before_path_raw);