diff_populate_gitlink: use a strbuf
We allocate 100 bytes to hold the "Submodule commit ..." text. This is enough, but it's not immediately obvious that this is the case, and we have to repeat the magic 100 twice. We could get away with xstrfmt here, but we want to know the size, as well, so let's use a real strbuf. And while we're here, we can clean up the logic around size_only. It currently sets and clears the "data" field pointlessly, and leaves the "should_free" flag on even after we have cleared the data. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Feb 22, 2016 at 17:45 UTC
b1ddfb9151ca817c30825d1250d565b32c5fc0f5
1 file changed
+8
-8
diff.c
+8
-8
@@ -2704,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
2704
2705
static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
2706
{
2707
- int len;
2708
- char *data = xmalloc(100), *dirty = "";
2707
+ struct strbuf buf = STRBUF_INIT;
2708
+ char *dirty = "";
2709
2710
/* Are we looking at the work tree? */
2711
if (s->dirty_submodule)
2712
dirty = "-dirty";
2713
2714
- len = snprintf(data, 100,
2715
- "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
2716
- s->data = data;
2717
- s->size = len;
2718
- s->should_free = 1;
2714
+ strbuf_addf(&buf, "Subproject commit %s%s\n", sha1_to_hex(s->sha1), dirty);
2715
+ s->size = buf.len;
2716
if (size_only) {
2717
s->data = NULL;
2721
- free(data);
2718
+ strbuf_release(&buf);
2719
+ } else {
2720
+ s->data = strbuf_detach(&buf, NULL);
2721
+ s->should_free = 1;
2722
}
2723
return 0;
2724
}