convert log_ref_write_fd() to use strbuf
Since we don't care about how many bytes were written, simplify the return value logic. log_ref_write_fd() was written long before strbuf was fleshed out. Remove the old manual buffer management code and replace it with strbuf(). Also update copy_reflog_msg() which is called only by log_ref_write_fd() to use strbuf as it keeps things consistent. Signed-off-by: Ben Peart <Ben.Peart@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ben Peart committed
Jul 10, 2018 at 21:08 UTC
80a6c2073b4d90be3e1be21f86afe7a47d1ac7bc
3 files changed
+17
-31
refs.c
+4
-8
@@ -786,25 +786,21 @@ int delete_ref(const char *msg, const char *refname,
786
old_oid, flags);
787
}
788
789
-int copy_reflog_msg(char *buf, const char *msg)
789
+void copy_reflog_msg(struct strbuf *sb, const char *msg)
790
{
791
- char *cp = buf;
791
char c;
792
int wasspace = 1;
793
795
- *cp++ = '\t';
794
+ strbuf_addch(sb, '\t');
795
while ((c = *msg++)) {
796
if (wasspace && isspace(c))
797
continue;
798
wasspace = isspace(c);
799
if (wasspace)
800
c = ' ';
802
- *cp++ = c;
801
+ strbuf_addch(sb, c);
802
}
804
- while (buf < cp && isspace(cp[-1]))
805
- cp--;
806
- *cp++ = '\n';
807
- return cp - buf;
803
+ strbuf_rtrim(sb);
804
}
805
806
int should_autocreate_reflog(const char *refname)
refs/files-backend.c
+10
-19
@@ -1582,26 +1582,17 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,
1582
const struct object_id *new_oid,
1583
const char *committer, const char *msg)
1584
{
1585
- int msglen, written;
1586
- unsigned maxlen, len;
1587
- char *logrec;
1588
-
1589
- msglen = msg ? strlen(msg) : 0;
1590
- maxlen = strlen(committer) + msglen + 100;
1591
- logrec = xmalloc(maxlen);
1592
- len = xsnprintf(logrec, maxlen, "%s %s %s\n",
1593
- oid_to_hex(old_oid),
1594
- oid_to_hex(new_oid),
1595
- committer);
1596
- if (msglen)
1597
- len += copy_reflog_msg(logrec + len - 1, msg) - 1;
1598
-
1599
- written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
1600
- free(logrec);
1601
- if (written < 0)
1602
- return -1;
1585
+ struct strbuf sb = STRBUF_INIT;
1586
+ int ret = 0;
1587
1604
- return 0;
1588
+ strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
1589
+ if (msg && *msg)
1590
+ copy_reflog_msg(&sb, msg);
1591
+ strbuf_addch(&sb, '\n');
1592
+ if (write_in_full(fd, sb.buf, sb.len) < 0)
1593
+ ret = -1;
1594
+ strbuf_release(&sb);
1595
+ return ret;
1596
}
1597
1598
static int files_log_ref_write(struct files_ref_store *refs,
refs/refs-internal.h
+3
-4
@@ -91,11 +91,10 @@ enum peel_status {
91
enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
92
93
/*
94
- * Copy the reflog message msg to buf, which has been allocated sufficiently
95
- * large, while cleaning up the whitespaces. Especially, convert LF to space,
96
- * because reflog file is one line per entry.
94
+ * Copy the reflog message msg to sb while cleaning up the whitespaces.
95
+ * Especially, convert LF to space, because reflog file is one line per entry.
96
*/
98
-int copy_reflog_msg(char *buf, const char *msg);
97
+void copy_reflog_msg(struct strbuf *sb, const char *msg);
98
99
/**
100
* Information needed for a single ref update. Set new_oid to the new