commit 6ec49e90ef1b2a051c73cc185fa0fdbf77c3eb00
parent b7c4702aba7684a5380945dc54d40a44750fbf2d
Author: Martin Ashby <martin@ashbysoft.com>
Date: Fri, 6 Sep 2024 14:31:47 +0100
implement creating non-annotated tags
Diffstat:
M | src/root.zig | | | 42 | +++++++++++++++++++++++------------------- |
1 file changed, 23 insertions(+), 19 deletions(-)
diff --git a/src/root.zig b/src/root.zig
@@ -59,8 +59,8 @@ pub fn doMain() !void {
try tag.addPositional(&tag_name);
var tag_object: argparse.Positional = .{ .name = "object", .description = "the object to add a tag for" };
try tag.addPositional(&tag_object);
- var tag_kind: argparse.Flag = .{ .long = "annotated", .short = "a", .description = "whether to create a tag object", .hasarg = false };
- try tag.addFlag(&tag_kind);
+ var tag_annotated: argparse.Flag = .{ .long = "annotate", .short = "a", .description = "whether to create an annotated tag", .hasarg = false };
+ try tag.addFlag(&tag_annotated);
if (!try ap.parseOrHelp()) {
return;
@@ -118,7 +118,7 @@ pub fn doMain() !void {
try doShowRef(a, std.fs.cwd(), std.io.getStdOut().writer());
} else if (tag.wasExecuted) {
if (tag_name.value) |tagname| {
- try doAddTag(a, std.fs.cwd(), tagname, tag_object.value);
+ try doAddTag(a, std.fs.cwd(), tagname, tag_object.value, tag_annotated.waspresent);
} else {
try doListTags(a, std.fs.cwd(), std.io.getStdOut().writer());
}
@@ -149,33 +149,37 @@ fn doListTags(a: std.mem.Allocator, dir: std.fs.Dir, writer: anytype) !void {
}
}
-fn doAddTag(a: std.mem.Allocator, dir: std.fs.Dir, tagname: []const u8, tagobj: ?[]const u8) !void {
+fn doAddTag(a: std.mem.Allocator, dir: std.fs.Dir, tagname: []const u8, maybe_ref: ?[]const u8, create_tag_obj: bool) !void {
var repo = try repo_find(a, dir);
defer repo.deinit();
const ref: [20]u8 =
- if (tagobj) |to|
- try repo.resolve(a, to)
+ if (maybe_ref) |rr|
+ try repo.resolve(a, rr)
else
try repo.head(a);
- const ref_str = std.fmt.bytesToHex(ref, .lower);
- var al = std.ArrayList(u8).init(a);
- defer al.deinit();
- const writer = al.writer();
- try std.fmt.format(writer, "object {s}\n", .{&ref_str});
- try std.fmt.format(writer, "type tag\n", .{});
- try std.fmt.format(writer, "tag {s}\n", .{tagname});
- try std.fmt.format(writer, "tagger \"Martin Ashby\"\n", .{});
- try std.fmt.format(writer, "\n{s}\n", .{"Foo Message"});
- var fbs = std.io.fixedBufferStream(al.items);
- const res = try repo.write_object(a, al.items.len, fbs.reader(), .tag, true);
- defer a.free(res);
+ var ref_str = std.fmt.bytesToHex(ref, .lower);
+
+ if (create_tag_obj) {
+ var al = std.ArrayList(u8).init(a);
+ defer al.deinit();
+ const writer = al.writer();
+ try std.fmt.format(writer, "object {s}\n", .{&ref_str});
+ try std.fmt.format(writer, "type commit\n", .{});
+ try std.fmt.format(writer, "tag {s}\n", .{tagname});
+ try std.fmt.format(writer, "tagger \"Martin Ashby\"\n", .{});
+ try std.fmt.format(writer, "\n{s}\n", .{"Foo Message"});
+ var fbs = std.io.fixedBufferStream(al.items);
+ const res = try repo.write_object(a, al.items.len, fbs.reader(), .tag, true);
+ defer a.free(res);
+ std.mem.copyForwards(u8, &ref_str, res);
+ }
const tagfilepath = try std.fs.path.join(a, &.{ "refs", "tags", tagname });
defer a.free(tagfilepath);
var tagfile = try repo.gitdir.createFile(tagfilepath, .{});
defer tagfile.close();
var wtr = tagfile.writer();
- try wtr.writeAll(res);
+ try wtr.writeAll(&ref_str);
try wtr.writeByte('\n');
}