|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn main() !void { |
| 4 | + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
| 5 | + var aa = std.heap.ArenaAllocator.init(gpa.allocator()); |
| 6 | + defer aa.deinit(); |
| 7 | + const allocator = aa.allocator(); |
| 8 | + const proc = try std.process.Child.run(.{ |
| 9 | + .allocator = allocator, |
| 10 | + .argv = &.{ "git", "remote", "-v" }, |
| 11 | + }); |
| 12 | + if (proc.stdout.len <= 0) { |
| 13 | + return error.GitRemoteMissing; |
| 14 | + } |
| 15 | + const remote = try origin(proc.stdout); |
| 16 | + const project = try repo(remote); |
| 17 | + const url = try coverage(allocator, project); |
| 18 | + _ = std.process.Child.run(.{ |
| 19 | + .allocator = allocator, |
| 20 | + .argv = &[_][]const u8{ "open", url }, |
| 21 | + }) catch { |
| 22 | + try std.io.getStdOut().writer().print("{s}\n", .{ url }); |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +fn origin(remotes: []const u8) ![]const u8 { |
| 27 | + var remote = std.mem.splitScalar(u8, remotes, '\n'); |
| 28 | + while (true) { |
| 29 | + const line = remote.next(); |
| 30 | + if (line) |val| { |
| 31 | + if (std.mem.startsWith(u8, val, "origin") and std.mem.endsWith(u8, val, "(push)")) { |
| 32 | + return std.mem.trim(u8, val[7..val.len-6], " "); |
| 33 | + } |
| 34 | + } else { |
| 35 | + return error.GitOriginMissing; |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +test "origin http" { |
| 41 | + const remotes = "origin https://github.com/zimeg/git-coverage.git (fetch)\norigin https://github.com/zimeg/git-coverage.git (push)"; |
| 42 | + const remote = try origin(remotes); |
| 43 | + try std.testing.expectEqualStrings(remote, "https://github.com/zimeg/git-coverage.git"); |
| 44 | +} |
| 45 | + |
| 46 | +test "origin ssh" { |
| 47 | + const remotes = "origin git@github.com:zimeg/git-coverage.git (fetch)\norigin git@github.com:zimeg/git-coverage.git (push)"; |
| 48 | + const remote = try origin(remotes); |
| 49 | + try std.testing.expectEqualStrings(remote, "git@github.com:zimeg/git-coverage.git"); |
| 50 | +} |
| 51 | + |
| 52 | +fn repo(remote: []const u8) ![]const u8 { |
| 53 | + if (std.mem.startsWith(u8, remote, "https://github.com/")) { |
| 54 | + return remote[19..remote.len-4]; |
| 55 | + } |
| 56 | + if (std.mem.startsWith(u8, remote, "git@github.com:")) { |
| 57 | + return remote[15..remote.len-4]; |
| 58 | + } |
| 59 | + return error.GitProtocolMissing; |
| 60 | +} |
| 61 | + |
| 62 | +test "repo http" { |
| 63 | + const remote = "https://github.com/zimeg/git-coverage.git"; |
| 64 | + const project = try repo(remote); |
| 65 | + try std.testing.expectEqualStrings(project, "zimeg/git-coverage"); |
| 66 | +} |
| 67 | + |
| 68 | +test "repo ssh" { |
| 69 | + const remote = "git@github.com:zimeg/git-coverage.git"; |
| 70 | + const project = try repo(remote); |
| 71 | + try std.testing.expectEqualStrings(project, "zimeg/git-coverage"); |
| 72 | +} |
| 73 | + |
| 74 | +fn coverage(allocator: std.mem.Allocator, project: []const u8) ![]const u8 { |
| 75 | + return std.fmt.allocPrint( |
| 76 | + allocator, |
| 77 | + "https://app.codecov.io/gh/{s}", |
| 78 | + .{ project }, |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +test "coverage project" { |
| 83 | + const project = "zimeg/git-coverage"; |
| 84 | + const url = try coverage(std.testing.allocator, project); |
| 85 | + defer std.testing.allocator.free(url); |
| 86 | + try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage"); |
| 87 | +} |
0 commit comments