@@ -2,10 +2,12 @@ const std = @import("std");
22
33const Flags = struct {
44 help : bool ,
5+ remote : []const u8 ,
56
67 pub fn init (allocator : std.mem.Allocator ) ! Flags {
78 var flags = Flags {
89 .help = false ,
10+ .remote = "origin" ,
911 };
1012 var args = try std .process .argsWithAllocator (allocator );
1113 _ = args .next ();
@@ -15,6 +17,8 @@ const Flags = struct {
1517 if (std .mem .eql (u8 , arg , "-h" ) or std .mem .eql (u8 , arg , "--help" )) {
1618 flags .help = true ;
1719 break ;
20+ } else if (std .mem .eql (u8 , arg , "--remote" )) {
21+ flags .remote = try parse (& args );
1822 } else {
1923 return error .FlagOptionMissing ;
2024 }
@@ -24,6 +28,15 @@ const Flags = struct {
2428 }
2529 return flags ;
2630 }
31+
32+ fn parse (args : * std.process.ArgIterator ) ! []const u8 {
33+ const path = args .next ();
34+ if (path ) | value | {
35+ return value ;
36+ } else {
37+ return error .FlagValueMissing ;
38+ }
39+ }
2740};
2841
2942pub fn main () ! void {
@@ -40,6 +53,7 @@ pub fn main() !void {
4053 \\Open test coverage uploaded to codecov in a web browser.
4154 \\
4255 \\ --help boolean print these usage details (default: false)
56+ \\ --remote string pick an upstream project (default: "origin")
4357 \\
4458 , .{});
4559 return ;
@@ -51,7 +65,7 @@ pub fn main() !void {
5165 if (proc .stdout .len <= 0 ) {
5266 return error .GitRemoteMissing ;
5367 }
54- const remote = try origin (proc .stdout );
68+ const remote = try origin (proc .stdout , flags . remote );
5569 const project = try repo (remote );
5670 const url = try coverage (allocator , project );
5771 _ = std .process .Child .run (.{
@@ -62,32 +76,68 @@ pub fn main() !void {
6276 };
6377}
6478
65- fn origin (remotes : []const u8 ) ! []const u8 {
79+ fn origin (remotes : []const u8 , upstream : [] const u8 ) ! []const u8 {
6680 var remote = std .mem .splitScalar (u8 , remotes , '\n ' );
6781 while (true ) {
6882 const line = remote .next ();
6983 if (line ) | val | {
70- if (std .mem .startsWith (u8 , val , "origin" ) and std .mem .endsWith (u8 , val , "(push)" )) {
71- return std .mem .trim (u8 , val [7 .. val .len - 6 ], " " );
84+ if (std .mem .startsWith (u8 , val , upstream ) and std .mem .endsWith (u8 , val , "(push)" )) {
85+ return std .mem .trim (u8 , val [upstream . len + 1 .. val .len - 6 ], " " );
7286 }
7387 } else {
7488 return error .GitOriginMissing ;
7589 }
7690 }
7791}
7892
79- test "origin http" {
80- const remotes = "origin https://github.com/zimeg/git-coverage.git (fetch)\n origin https://github.com/zimeg/git-coverage.git (push)" ;
81- const remote = try origin (remotes );
93+ test "origin http default" {
94+ const remotes =
95+ \\origin https://github.com/example/git-coverage.git (fetch)
96+ \\origin https://github.com/example/git-coverage.git (push)
97+ \\upstream https://github.com/zimeg/git-coverage.git (fetch)
98+ \\upstream https://github.com/zimeg/git-coverage.git (push)
99+ \\
100+ ;
101+ const remote = try origin (remotes , "origin" );
102+ try std .testing .expectEqualStrings (remote , "https://github.com/example/git-coverage.git" );
103+ }
104+
105+ test "origin http custom" {
106+ const remotes =
107+ \\origin https://github.com/example/git-coverage.git (fetch)
108+ \\origin https://github.com/example/git-coverage.git (push)
109+ \\upstream https://github.com/zimeg/git-coverage.git (fetch)
110+ \\upstream https://github.com/zimeg/git-coverage.git (push)
111+ \\
112+ ;
113+ const remote = try origin (remotes , "upstream" );
82114 try std .testing .expectEqualStrings (remote , "https://github.com/zimeg/git-coverage.git" );
83115}
84116
85- test "origin ssh" {
86- const remotes = "origin git@github.com:zimeg/git-coverage.git (fetch)\n origin git@github.com:zimeg/git-coverage.git (push)" ;
87- const remote = try origin (remotes );
117+ test "origin ssh default" {
118+ const remotes =
119+ \\fork git@github.com:example/git-coverage.git (fetch)
120+ \\fork git@github.com:example/git-coverage.git (push)
121+ \\origin git@github.com:zimeg/git-coverage.git (fetch)
122+ \\origin git@github.com:zimeg/git-coverage.git (push)
123+ \\
124+ ;
125+ const remote = try origin (remotes , "origin" );
88126 try std .testing .expectEqualStrings (remote , "git@github.com:zimeg/git-coverage.git" );
89127}
90128
129+ test "origin ssh custom" {
130+ const remotes =
131+ \\fork git@github.com:example/git-coverage.git (fetch)
132+ \\fork git@github.com:example/git-coverage.git (push)
133+ \\origin git@github.com:zimeg/git-coverage.git (fetch)
134+ \\origin git@github.com:zimeg/git-coverage.git (push)
135+ \\
136+ ;
137+ const remote = try origin (remotes , "fork" );
138+ try std .testing .expectEqualStrings (remote , "git@github.com:example/git-coverage.git" );
139+ }
140+
91141fn repo (remote : []const u8 ) ! []const u8 {
92142 if (std .mem .startsWith (u8 , remote , "https://github.com/" )) {
93143 return remote [19 .. remote .len - 4 ];
0 commit comments