Skip to content

Commit

Permalink
Don't rely on specific Git attribute order (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondyck authored Oct 29, 2024
1 parent 1c4b291 commit afebddd
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions build/Git.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,27 +136,27 @@ fn setAdditionalMetadata(git: *Git, arena: Allocator, git_dir: std.fs.Dir) !void

var attributes = std.mem.splitScalar(u8, data, '\n');

_ = attributes.next(); // tree hash
_ = attributes.next(); // parent commit hash
_ = attributes.next(); // author

if (attributes.next()) |committer| {
const @"<_index" = std.mem.indexOfScalar(u8, committer, '<').?;
const @">_index" = std.mem.indexOfScalar(u8, committer, '>').?;

git.author_name = committer[10 .. @"<_index" - 1];
git.author_email = committer[@"<_index" + 1 .. @">_index"];

const unix_time = try std.fmt.parseInt(i64, committer[@">_index" + 2 .. committer.len - 6], 10);
const offset_hour = try std.fmt.parseInt(i64, committer[committer.len - 4 .. committer.len - 2], 10);
const offset = switch (committer[committer.len - 5]) {
'-' => -offset_hour,
'+' => offset_hour,
else => unreachable,
};
git.commit_date = .{ .unix = unix_time + offset * 3600 };
while (attributes.next()) |attribute| {
const committerHeading = "committer ";
if (std.mem.startsWith(u8, attribute, committerHeading)) {
const @"<_index" = std.mem.indexOfScalar(u8, attribute, '<').?;
const @">_index" = std.mem.indexOfScalar(u8, attribute, '>').?;

git.author_name = attribute[10 .. @"<_index" - 1];
git.author_email = attribute[@"<_index" + 1 .. @">_index"];

const unix_time = try std.fmt.parseInt(i64, attribute[@">_index" + 2 .. attribute.len - 6], 10);
const offset_hour = try std.fmt.parseInt(i64, attribute[attribute.len - 4 .. attribute.len - 2], 10);
const offset = switch (attribute[attribute.len - 5]) {
'-' => -offset_hour,
'+' => offset_hour,
else => unreachable,
};
git.commit_date = .{ .unix = unix_time + offset * 3600 };
}
if (attribute.len == 0) {
break;
}
}

_ = attributes.next(); // empty line
git.commit_message = attributes.rest();
}

0 comments on commit afebddd

Please sign in to comment.