| ... | @@ -517,80 +517,6 @@ pub fn getTreeDiffPath(alloc: std.mem.Allocator, dir: nfs.Dir, commitid: CommitI | ... | @@ -517,80 +517,6 @@ pub fn getTreeDiffPath(alloc: std.mem.Allocator, dir: nfs.Dir, commitid: CommitI |
| 517 | return std.mem.trim(u8, result.stdout, "\n"); | 517 | return std.mem.trim(u8, result.stdout, "\n"); |
| 518 | } | 518 | } |
| 519 | | 519 | |
| 520 | pub fn parseTreeDiffMeta(input: string) !TreeDiffMeta { | | |
| 521 | const t = tracer.trace(@src(), "", .{}); | | |
| 522 | defer t.end(); | | |
| 523 | | | |
| 524 | var result = std.mem.zeroes(TreeDiffMeta); | | |
| 525 | var lineiter = std.mem.splitScalar(u8, input, '\n'); | | |
| 526 | | | |
| 527 | while (lineiter.next()) |lin| { | | |
| 528 | if (lin.len == 0) break; | | |
| 529 | std.debug.assert(lin[0] == ':'); | | |
| 530 | result.files_changed += 1; | | |
| 531 | } | | |
| 532 | | | |
| 533 | std.debug.assert(std.mem.startsWith(u8, lineiter.next() orelse return result, "diff --git")); | | |
| 534 | blk: while (true) { | | |
| 535 | while (lineiter.next()) |lin| { | | |
| 536 | if (std.mem.startsWith(u8, lin, "index")) break; | | |
| 537 | if (std.mem.startsWith(u8, lin, "new file mode")) continue; | | |
| 538 | if (std.mem.startsWith(u8, lin, "deleted file mode")) continue; | | |
| 539 | if (std.mem.startsWith(u8, lin, "old mode")) continue; | | |
| 540 | if (std.mem.startsWith(u8, lin, "new mode")) continue; | | |
| 541 | if (std.mem.startsWith(u8, lin, "diff --git")) continue :blk; | | |
| 542 | | | |
| 543 | std.log.err("{s}", .{lin}); | | |
| 544 | unreachable; | | |
| 545 | } | | |
| 546 | if (lineiter.peek() == null) break; // handle empty file being last or being at the end | | |
| 547 | | | |
| 548 | if (std.mem.startsWith(u8, lineiter.peek().?, "Binary files")) { | | |
| 549 | _ = lineiter.next().?; | | |
| 550 | result.lines_added += 1; | | |
| 551 | result.lines_removed += 1; | | |
| 552 | if (lineiter.peek() == null) break; // handle binary file being last | | |
| 553 | std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git")); | | |
| 554 | continue; | | |
| 555 | } | | |
| 556 | | | |
| 557 | if (std.mem.startsWith(u8, lineiter.peek().?, "diff --git")) { // handle empty file being in the middle | | |
| 558 | std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "diff --git")); | | |
| 559 | continue; | | |
| 560 | } | | |
| 561 | | | |
| 562 | // Every affected text file in the diff has a preamble like below | | |
| 563 | // | | |
| 564 | // diff --git a/notes/all_packages.txt b/notes/all_packages.txt | | |
| 565 | // index c06b41d..e8f91cf 100644 | | |
| 566 | // --- a/notes/all_packages.txt | | |
| 567 | // +++ b/notes/all_packages.txt | | |
| 568 | std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "---")); | | |
| 569 | std.debug.assert(std.mem.startsWith(u8, lineiter.next().?, "+++")); | | |
| 570 | | | |
| 571 | while (lineiter.next()) |lin| { | | |
| 572 | if (std.mem.startsWith(u8, lin, "diff --git")) break; | | |
| 573 | switch (lin[0]) { | | |
| 574 | '@' => continue, // @@ -89,3 +89,4 @@ freedesktop/xorg/libsm | | |
| 575 | ' ' => continue, | | |
| 576 | '+' => result.lines_added += 1, | | |
| 577 | '-' => result.lines_removed += 1, | | |
| 578 | '\\' => continue, // \ No newline at end of file | | |
| 579 | else => @panic(lin), | | |
| 580 | } | | |
| 581 | } | | |
| 582 | if (lineiter.peek() == null) break; // handle being at the end | | |
| 583 | } | | |
| 584 | | | |
| 585 | return result; | | |
| 586 | } | | |
| 587 | | | |
| 588 | pub const TreeDiffMeta = struct { | | |
| 589 | files_changed: u16, | | |
| 590 | lines_added: u32, | | |
| 591 | lines_removed: u32, | | |
| 592 | }; | | |
| 593 | | | |
| 594 | pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { | 520 | pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { |
| 595 | const t = tracer.trace(@src(), "", .{}); | 521 | const t = tracer.trace(@src(), "", .{}); |
| 596 | defer t.end(); | 522 | defer t.end(); |
| ... | @@ -598,6 +524,7 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { | ... | @@ -598,6 +524,7 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { |
| 598 | var lineiter = std.mem.splitScalar(u8, input, '\n'); | 524 | var lineiter = std.mem.splitScalar(u8, input, '\n'); |
| 599 | var overview = std.ArrayList(TreeDiff.StateLine).init(alloc); | 525 | var overview = std.ArrayList(TreeDiff.StateLine).init(alloc); |
| 600 | var diffs = std.ArrayList(TreeDiff.Diff).init(alloc); | 526 | var diffs = std.ArrayList(TreeDiff.Diff).init(alloc); |
| | 527 | var meta = std.mem.zeroes(TreeDiff.Meta); |
| 601 | | 528 | |
| 602 | while (lineiter.next()) |lin| { | 529 | while (lineiter.next()) |lin| { |
| 603 | if (lin.len == 0) break; | 530 | if (lin.len == 0) break; |
| ... | @@ -626,11 +553,13 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { | ... | @@ -626,11 +553,13 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { |
| 626 | .action = std.meta.stringToEnum(TreeDiff.Action, action_s).?, | 553 | .action = std.meta.stringToEnum(TreeDiff.Action, action_s).?, |
| 627 | .sub_path = kter.rest(), | 554 | .sub_path = kter.rest(), |
| 628 | }); | 555 | }); |
| | 556 | meta.files_changed += 1; |
| 629 | } | 557 | } |
| 630 | if (lineiter.peek() == null) { | 558 | if (lineiter.peek() == null) { |
| 631 | return TreeDiff{ | 559 | return TreeDiff{ |
| 632 | .overview = overview.items, | 560 | .overview = overview.items, |
| 633 | .diffs = diffs.items, | 561 | .diffs = diffs.items, |
| | 562 | .meta = meta, |
| 634 | }; | 563 | }; |
| 635 | } | 564 | } |
| 636 | | 565 | |
| ... | @@ -747,9 +676,11 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { | ... | @@ -747,9 +676,11 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { |
| 747 | } | 676 | } |
| 748 | if (lin[0] == '-') { | 677 | if (lin[0] == '-') { |
| 749 | diff.subs += 1; | 678 | diff.subs += 1; |
| | 679 | meta.lines_removed += 1; |
| 750 | } | 680 | } |
| 751 | if (lin[0] == '+') { | 681 | if (lin[0] == '+') { |
| 752 | diff.adds += 1; | 682 | diff.adds += 1; |
| | 683 | meta.lines_added += 1; |
| 753 | } | 684 | } |
| 754 | lineiter.index.? += lin.len + 1; | 685 | lineiter.index.? += lin.len + 1; |
| 755 | | 686 | |
| ... | @@ -764,12 +695,14 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { | ... | @@ -764,12 +695,14 @@ pub fn parseTreeDiff(alloc: std.mem.Allocator, input: string) !TreeDiff { |
| 764 | return TreeDiff{ | 695 | return TreeDiff{ |
| 765 | .overview = overview.items, | 696 | .overview = overview.items, |
| 766 | .diffs = diffs.items, | 697 | .diffs = diffs.items, |
| | 698 | .meta = meta, |
| 767 | }; | 699 | }; |
| 768 | } | 700 | } |
| 769 | | 701 | |
| 770 | pub const TreeDiff = struct { | 702 | pub const TreeDiff = struct { |
| 771 | overview: []const StateLine, | 703 | overview: []const StateLine, |
| 772 | diffs: []const Diff, | 704 | diffs: []const Diff, |
| | 705 | meta: Meta, |
| 773 | | 706 | |
| 774 | pub const StateLine = struct { | 707 | pub const StateLine = struct { |
| 775 | before: State, | 708 | before: State, |
| ... | @@ -808,6 +741,12 @@ pub const TreeDiff = struct { | ... | @@ -808,6 +741,12 @@ pub const TreeDiff = struct { |
| 808 | subs: u32, | 741 | subs: u32, |
| 809 | content: string, | 742 | content: string, |
| 810 | }; | 743 | }; |
| | 744 | |
| | 745 | pub const Meta = struct { |
| | 746 | files_changed: u32, |
| | 747 | lines_added: u64, |
| | 748 | lines_removed: u64, |
| | 749 | }; |
| 811 | }; | 750 | }; |
| 812 | | 751 | |
| 813 | pub fn getBranches(alloc: std.mem.Allocator, dir: nfs.Dir) ![]const Ref { | 752 | pub fn getBranches(alloc: std.mem.Allocator, dir: nfs.Dir) ![]const Ref { |