Skip to content

Commit 44eb6da

Browse files
committed
package.git: remove Diagnostic
Logic moved to Unpack.
1 parent 5a985df commit 44eb6da

File tree

2 files changed

+18
-128
lines changed

2 files changed

+18
-128
lines changed

src/Package/Fetch/git.zig

Lines changed: 17 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,6 @@ test parseOid {
3636
try testing.expectError(error.InvalidOid, parseOid("HEAD"));
3737
}
3838

39-
pub const Diagnostics = struct {
40-
allocator: Allocator,
41-
errors: std.ArrayListUnmanaged(Error) = .{},
42-
43-
pub const Error = union(enum) {
44-
unable_to_create_sym_link: struct {
45-
code: anyerror,
46-
file_name: []const u8,
47-
link_name: []const u8,
48-
},
49-
};
50-
51-
pub fn deinit(d: *Diagnostics) void {
52-
for (d.errors.items) |item| {
53-
switch (item) {
54-
.unable_to_create_sym_link => |info| {
55-
d.allocator.free(info.file_name);
56-
d.allocator.free(info.link_name);
57-
},
58-
}
59-
}
60-
d.errors.deinit(d.allocator);
61-
d.* = undefined;
62-
}
63-
};
64-
6539
pub const Repository = struct {
6640
odb: Odb,
6741

@@ -76,85 +50,6 @@ pub const Repository = struct {
7650

7751
/// Checks out the repository at `commit_oid` to `worktree`.
7852
pub fn checkout(
79-
repository: *Repository,
80-
worktree: std.fs.Dir,
81-
commit_oid: Oid,
82-
diagnostics: *Diagnostics,
83-
) !void {
84-
try repository.odb.seekOid(commit_oid);
85-
const tree_oid = tree_oid: {
86-
const commit_object = try repository.odb.readObject();
87-
if (commit_object.type != .commit) return error.NotACommit;
88-
break :tree_oid try getCommitTree(commit_object.data);
89-
};
90-
try repository.checkoutTree(worktree, tree_oid, "", diagnostics);
91-
}
92-
93-
/// Checks out the tree at `tree_oid` to `worktree`.
94-
fn checkoutTree(
95-
repository: *Repository,
96-
dir: std.fs.Dir,
97-
tree_oid: Oid,
98-
current_path: []const u8,
99-
diagnostics: *Diagnostics,
100-
) !void {
101-
try repository.odb.seekOid(tree_oid);
102-
const tree_object = try repository.odb.readObject();
103-
if (tree_object.type != .tree) return error.NotATree;
104-
// The tree object may be evicted from the object cache while we're
105-
// iterating over it, so we can make a defensive copy here to make sure
106-
// it remains valid until we're done with it
107-
const tree_data = try repository.odb.allocator.dupe(u8, tree_object.data);
108-
defer repository.odb.allocator.free(tree_data);
109-
110-
var tree_iter: TreeIterator = .{ .data = tree_data };
111-
while (try tree_iter.next()) |entry| {
112-
switch (entry.type) {
113-
.directory => {
114-
try dir.makeDir(entry.name);
115-
var subdir = try dir.openDir(entry.name, .{});
116-
defer subdir.close();
117-
const sub_path = try std.fs.path.join(repository.odb.allocator, &.{ current_path, entry.name });
118-
defer repository.odb.allocator.free(sub_path);
119-
try repository.checkoutTree(subdir, entry.oid, sub_path, diagnostics);
120-
},
121-
.file => {
122-
var file = try dir.createFile(entry.name, .{});
123-
defer file.close();
124-
try repository.odb.seekOid(entry.oid);
125-
const file_object = try repository.odb.readObject();
126-
if (file_object.type != .blob) return error.InvalidFile;
127-
try file.writeAll(file_object.data);
128-
try file.sync();
129-
},
130-
.symlink => {
131-
try repository.odb.seekOid(entry.oid);
132-
const symlink_object = try repository.odb.readObject();
133-
if (symlink_object.type != .blob) return error.InvalidFile;
134-
const link_name = symlink_object.data;
135-
dir.symLink(link_name, entry.name, .{}) catch |e| {
136-
const file_name = try std.fs.path.join(diagnostics.allocator, &.{ current_path, entry.name });
137-
errdefer diagnostics.allocator.free(file_name);
138-
const link_name_dup = try diagnostics.allocator.dupe(u8, link_name);
139-
errdefer diagnostics.allocator.free(link_name_dup);
140-
try diagnostics.errors.append(diagnostics.allocator, .{ .unable_to_create_sym_link = .{
141-
.code = e,
142-
.file_name = file_name,
143-
.link_name = link_name_dup,
144-
} });
145-
};
146-
},
147-
.gitlink => {
148-
// Consistent with git archive behavior, create the directory but
149-
// do nothing else
150-
try dir.makeDir(entry.name);
151-
},
152-
}
153-
}
154-
}
155-
156-
/// Checks out the repository at `commit_oid` to `worktree`.
157-
pub fn checkout2(
15853
repository: *Repository,
15954
worktree: anytype,
16055
commit_oid: Oid,
@@ -165,11 +60,11 @@ pub const Repository = struct {
16560
if (commit_object.type != .commit) return error.NotACommit;
16661
break :tree_oid try getCommitTree(commit_object.data);
16762
};
168-
try repository.checkoutTree2(worktree, tree_oid, "");
63+
try repository.checkoutTree(worktree, tree_oid, "");
16964
}
17065

17166
/// Checks out the tree at `tree_oid` to `worktree`.
172-
fn checkoutTree2(
67+
fn checkoutTree(
17368
repository: *Repository,
17469
dir: anytype,
17570
tree_oid: Oid,
@@ -191,27 +86,32 @@ pub const Repository = struct {
19186
defer allocator.free(sub_path);
19287
switch (entry.type) {
19388
.directory => {
194-
try repository.checkoutTree2(dir, entry.oid, sub_path);
89+
try dir.makePath(sub_path);
90+
try repository.checkoutTree(dir, entry.oid, sub_path);
19591
},
19692
.file => {
19793
try repository.odb.seekOid(entry.oid);
19894
const file_object = try repository.odb.readObject();
19995
if (file_object.type != .blob) return error.InvalidFile;
20096

201-
if (try dir.createFile(sub_path)) |file| {
202-
defer file.close();
203-
try file.writeAll(file_object.data);
204-
try file.sync();
205-
}
97+
var file = dir.createFile(sub_path, .{}) catch |err| {
98+
if (err == error.Skip) continue;
99+
return err;
100+
};
101+
defer file.close();
102+
try file.writeAll(file_object.data);
103+
try file.sync();
206104
},
207105
.symlink => {
208106
try repository.odb.seekOid(entry.oid);
209107
const symlink_object = try repository.odb.readObject();
210108
if (symlink_object.type != .blob) return error.InvalidFile;
211109

212-
try dir.symLink(symlink_object.data, sub_path);
110+
try dir.symLink(symlink_object.data, sub_path, .{});
111+
},
112+
.gitlink => {
113+
try dir.makePath(sub_path);
213114
},
214-
.gitlink => {},
215115
}
216116
}
217117
}
@@ -1458,11 +1358,7 @@ test "packfile indexing and checkout" {
14581358
defer worktree.cleanup();
14591359

14601360
const commit_id = try parseOid("dd582c0720819ab7130b103635bd7271b9fd4feb");
1461-
1462-
var diagnostics: Diagnostics = .{ .allocator = testing.allocator };
1463-
defer diagnostics.deinit();
1464-
try repository.checkout(worktree.dir, commit_id, &diagnostics);
1465-
try testing.expect(diagnostics.errors.items.len == 0);
1361+
try repository.checkout(worktree.dir, commit_id);
14661362

14671363
const expected_files: []const []const u8 = &.{
14681364
"dir/file",
@@ -1552,11 +1448,5 @@ pub fn main() !void {
15521448
std.debug.print("Starting checkout...\n", .{});
15531449
var repository = try Repository.init(allocator, pack_file, index_file);
15541450
defer repository.deinit();
1555-
var diagnostics: Diagnostics = .{ .allocator = allocator };
1556-
defer diagnostics.deinit();
1557-
try repository.checkout(worktree, commit, &diagnostics);
1558-
1559-
for (diagnostics.errors.items) |err| {
1560-
std.debug.print("Diagnostic: {}\n", .{err});
1561-
}
1451+
try repository.checkout(worktree, commit);
15621452
}

src/Package/Unpack.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub fn gitPack(self: *Self, commit_oid: git.Oid, reader: anytype) !void {
123123
{
124124
var repository = try git.Repository.init(self.allocator, pack_file, index_file);
125125
defer repository.deinit();
126-
try repository.checkout2(inf, commit_oid);
126+
try repository.checkout(inf, commit_oid);
127127
}
128128

129129
try self.root.deleteTree(".git");

0 commit comments

Comments
 (0)