Skip to content

Commit 962ce8a

Browse files
authored
feat: jump to a specific path in the browser if the flag contains these detail (#13)
1 parent a9fed60 commit 962ce8a

File tree

4 files changed

+71
-14
lines changed

4 files changed

+71
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ to [Semantic Versioning][semver].
1414
- Include installation instruction for moving a release download into path.
1515
- Pick an upstream project according to the saved git remotes using a flag.
1616
- Fetch the changes of a branch when showing coverage with a specific flag.
17+
- Jump to a specific path in the browser if the flag contains these detail.
1718

1819
### Maintenance
1920

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Open test coverage uploaded to [`codecov`][codecov] in a web browser.
77
The following command uses `git` details:
88

99
```sh
10-
$ git coverage # https://app.codecov.io/gh/zimeg/git-coverage
10+
$ git coverage # https://app.codecov.io/gh/zimeg/git-coverage/tree/main
1111
```
1212

1313
### Flags
@@ -16,6 +16,7 @@ Options are available for customization:
1616

1717
- `--help`: boolean. Print these usage details. Default: `false`
1818
- `--branch`: string. Fetch changes to inspect. Default: `main`
19+
- `--path`: string. Show a specific file. Default: `.`
1920
- `--remote`: string. Pick an upstream project. Default: `origin`
2021

2122
## Installation

man/git-coverage.1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ The \fBgit-coverage\fP program opens test coverage results uploaded to \fBcodeco
1919
Print these usage details.
2020
.IP "--branch"
2121
Fetch changes to inspect. Default: "main"
22+
.IP "--path"
23+
.nf
24+
Show a specific file. Default: "."
2225
.IP "--remote"
2326
Pick an upstream project. Default: "origin"
2427
.
@@ -44,6 +47,12 @@ The latest release of \fBgit\fP is recommended.
4447
.IP "$ git coverage"
4548
Open test coverage results for the current project in a web browser.
4649
.
50+
.IP "$ git coverage --branch dev --path src/main.zig"
51+
Open test coverage results for a specific file on a certain branch.
52+
.
53+
.IP "$ git coverage --remote upstream"
54+
Open test coverage results for changes of some upstream repository.
55+
.
4756
.
4857
.SH REPORTING BUGS
4958
\fIhttps://github.com/zimeg/git-coverage/issues/\fP

src/main.zig

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ const std = @import("std");
33
const Flags = struct {
44
help: bool,
55
branch: []const u8,
6+
path: []const u8,
67
remote: []const u8,
78

89
pub fn init(allocator: std.mem.Allocator) !Flags {
910
var flags = Flags{
1011
.help = false,
1112
.branch = "main",
13+
.path = ".",
1214
.remote = "origin",
1315
};
1416
var args = try std.process.argsWithAllocator(allocator);
@@ -21,6 +23,8 @@ const Flags = struct {
2123
break;
2224
} else if (std.mem.eql(u8, arg, "--branch")) {
2325
flags.branch = try parse(&args);
26+
} else if (std.mem.eql(u8, arg, "--path")) {
27+
flags.path = try parse(&args);
2428
} else if (std.mem.eql(u8, arg, "--remote")) {
2529
flags.remote = try parse(&args);
2630
} else {
@@ -58,6 +62,7 @@ pub fn main() !void {
5862
\\
5963
\\ --help boolean print these usage details (default: false)
6064
\\ --branch string fetch changes to inspect (default: "main")
65+
\\ --path string show a specific file (default: ".")
6166
\\ --remote string pick an upstream project (default: "origin")
6267
\\
6368
, .{});
@@ -72,7 +77,7 @@ pub fn main() !void {
7277
}
7378
const remote = try origin(proc.stdout, flags.remote);
7479
const project = try repo(remote);
75-
const url = try coverage(allocator, project, flags.branch);
80+
const url = try coverage(allocator, project, flags.branch, flags.path);
7681
_ = std.process.Child.run(.{
7782
.allocator = allocator,
7883
.argv = &[_][]const u8{ "open", url },
@@ -165,24 +170,65 @@ test "repo ssh" {
165170
try std.testing.expectEqualStrings(project, "zimeg/git-coverage");
166171
}
167172

168-
fn coverage(allocator: std.mem.Allocator, project: []const u8, branch: []const u8) ![]const u8 {
169-
return std.fmt.allocPrint(
170-
allocator,
171-
"https://app.codecov.io/gh/{s}/tree/{s}",
172-
.{ project, branch },
173-
);
173+
fn coverage(allocator: std.mem.Allocator, project: []const u8, branch: []const u8, path: []const u8) ![]const u8 {
174+
const slashes = std.mem.count(u8, path, "/");
175+
const encoding = try allocator.alloc(u8, path.len + slashes * 2);
176+
defer allocator.free(encoding);
177+
_ = std.mem.replace(u8, path, "/", "%2F", encoding);
178+
const stat = try std.fs.cwd().statFile(path);
179+
switch (stat.kind) {
180+
.directory => return std.fmt.allocPrint(
181+
allocator,
182+
"https://app.codecov.io/gh/{s}/tree/{s}/{s}",
183+
.{ project, branch, encoding },
184+
),
185+
.file => return std.fmt.allocPrint(
186+
allocator,
187+
"https://app.codecov.io/gh/{s}/blob/{s}/{s}",
188+
.{ project, branch, encoding },
189+
),
190+
else => return error.FileKindMissing,
191+
}
192+
}
193+
194+
test "coverage project main root" {
195+
const project = "zimeg/git-coverage";
196+
const url = try coverage(std.testing.allocator, project, "main", ".");
197+
defer std.testing.allocator.free(url);
198+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/main/.");
199+
}
200+
201+
test "coverage project main dir" {
202+
const project = "zimeg/git-coverage";
203+
const url = try coverage(std.testing.allocator, project, "main", "src");
204+
defer std.testing.allocator.free(url);
205+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/main/src");
206+
}
207+
208+
test "coverage project main file" {
209+
const project = "zimeg/git-coverage";
210+
const url = try coverage(std.testing.allocator, project, "main", "src/main.zig");
211+
defer std.testing.allocator.free(url);
212+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/blob/main/src%2Fmain.zig");
213+
}
214+
215+
test "coverage project dev root" {
216+
const project = "zimeg/git-coverage";
217+
const url = try coverage(std.testing.allocator, project, "dev", ".");
218+
defer std.testing.allocator.free(url);
219+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/dev/.");
174220
}
175221

176-
test "coverage project main" {
222+
test "coverage project dev dir" {
177223
const project = "zimeg/git-coverage";
178-
const url = try coverage(std.testing.allocator, project, "main");
224+
const url = try coverage(std.testing.allocator, project, "dev", "src");
179225
defer std.testing.allocator.free(url);
180-
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/main");
226+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/dev/src");
181227
}
182228

183-
test "coverage project dev" {
229+
test "coverage project dev file" {
184230
const project = "zimeg/git-coverage";
185-
const url = try coverage(std.testing.allocator, project, "dev");
231+
const url = try coverage(std.testing.allocator, project, "dev", "src/main.zig");
186232
defer std.testing.allocator.free(url);
187-
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/tree/dev");
233+
try std.testing.expectEqualStrings(url, "https://app.codecov.io/gh/zimeg/git-coverage/blob/dev/src%2Fmain.zig");
188234
}

0 commit comments

Comments
 (0)