Something went wrong. Try again.
Sign git commits and tags with an OpenPGP key kept in Bitwarden.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! The summary: a line saying what is being signed, for the confirmation of//! a remote signature.//!//! git signs a commit or tag object: headers, a blank line, then the//! message. From that this makes `commit "Fix the frobnicator" by A U Thor//! <a@example.org>`, or `tag v1.0 "Release 1.0" by ...`. Anything else gets//! no summary. It is what the remote machine says, shown so that a//! person can recognize their own commit; the digest is what is signed, and//! nothing here is trusted.
const std = @import("std");const testing = std.testing;
/// Longest summary, in bytes.pub const max_len = 120;
/// The summary of `data`, the start of what is being signed, written into/// `out`; null when it is not a git commit or tag.pub fn summarize(data: []const u8, out: *[max_len]u8) ?[]const u8 { const header_end = std.mem.find(u8, data, "\n\n") orelse return null; const headers = data[0..header_end]; const is_commit = std.mem.startsWith(u8, headers, "tree "); const is_tag = std.mem.startsWith(u8, headers, "object "); if (!is_commit and !is_tag) return null;
var who: []const u8 = ""; var tag_name: []const u8 = ""; var lines = std.mem.splitScalar(u8, headers, '\n'); while (lines.next()) |line| { if (std.mem.startsWith(u8, line, if (is_commit) "author " else "tagger ")) { // `Name <email> 1767225600 +0000`: without the time and zone. var rest = line[7..]; for (0..2) |_| { const space = std.mem.findScalarLast(u8, rest, ' ') orelse break; rest = rest[0..space]; } who = rest; } else if (is_tag and std.mem.startsWith(u8, line, "tag ")) { tag_name = line[4..]; } }
const message = data[header_end + 2 ..]; const subject = std.mem.trim(u8, message[0 .. std.mem.findScalar(u8, message, '\n') orelse message.len], " \t\r");
var w: std.Io.Writer = .fixed(out); const wrote = blk: { if (is_commit) { w.print("commit \"{s}\"", .{subject}) catch break :blk false; } else { w.print("tag {s} \"{s}\"", .{ tag_name, subject }) catch break :blk false; } if (who.len > 0) w.print(" by {s}", .{who}) catch break :blk false; break :blk true; }; var text = w.buffered(); if (!wrote) { // Cut short, at a character boundary rather than inside one. var end = text.len; while (end > 0 and text[end - 1] & 0xc0 == 0x80) end -= 1; if (end > 0 and text[end - 1] >= 0xc0) end -= 1; text = text[0..end]; } // Nothing that could redraw or disguise the dialog's text. for (text) |*c| { if (c.* < 0x20 or c.* == 0x7f) c.* = ' '; } return text;}
test "a commit" { var buf: [max_len]u8 = undefined; const data = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n" ++ "parent 1111111111111111111111111111111111111111\n" ++ "author A U Thor <a@example.org> 1767225600 +0000\n" ++ "committer C O Mitter <c@example.org> 1767225600 +0000\n" ++ "\n" ++ "Fix the frobnicator\n\nLonger explanation.\n"; try testing.expectEqualStrings( "commit \"Fix the frobnicator\" by A U Thor <a@example.org>", summarize(data, &buf).?, );}
test "a tag" { var buf: [max_len]u8 = undefined; const data = "object 1111111111111111111111111111111111111111\n" ++ "type commit\n" ++ "tag v1.0\n" ++ "tagger T Agger <t@example.org> 1767225600 -0500\n" ++ "\n" ++ "Release 1.0\n"; try testing.expectEqualStrings( "tag v1.0 \"Release 1.0\" by T Agger <t@example.org>", summarize(data, &buf).?, );}
test "not git, control characters, and a long subject" { var buf: [max_len]u8 = undefined; try testing.expectEqual(@as(?[]const u8, null), summarize("just some file\n\ncontents", &buf)); try testing.expectEqual(@as(?[]const u8, null), summarize("tree abc\nno blank line", &buf));
const sneaky = "tree x\nauthor Me <m@e> 1 +0000\n\nlooks fine\x1b[2Jbut is not\n"; const s = summarize(sneaky, &buf).?; try testing.expect(std.mem.findScalar(u8, s, 0x1b) == null);
// A subject longer than the buffer, with a multi-byte character where // it would be cut. const subject: [100][2]u8 = @splat("é".*); const long = "tree x\n\n" ++ @as([200]u8, @bitCast(subject)) ++ "\n"; const cut = summarize(long, &buf).?; try testing.expect(cut.len <= max_len); try testing.expect(std.unicode.utf8ValidateSlice(cut));}