// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! A line saying what is being signed, for the confirmation shown when a //! signature is asked for over SSH. //! //! 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 //! `, or `tag v1.0 "Release 1.0" by ...`. Anything else gets //! no summary. It is what the requesting 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 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 1767225600 +0000\n" ++ "committer C O Mitter 1767225600 +0000\n" ++ "\n" ++ "Fix the frobnicator\n\nLonger explanation.\n"; try testing.expectEqualStrings( "commit \"Fix the frobnicator\" by A U Thor ", 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 1767225600 -0500\n" ++ "\n" ++ "Release 1.0\n"; try testing.expectEqualStrings( "tag v1.0 \"Release 1.0\" by T Agger ", 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 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 long = "tree x\n\n" ++ ("é" ** 100) ++ "\n"; const cut = summarize(long, &buf).?; try testing.expect(cut.len <= max_len); try testing.expect(std.unicode.utf8ValidateSlice(cut)); }