Skip to content

Commit 80a6c20

Browse files
Ben Peartgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e333175 commit 80a6c20

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

refs.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,25 +786,21 @@ int delete_ref(const char *msg, const char *refname,
786786
old_oid, flags);
787787
}
788788

789-
int copy_reflog_msg(char *buf, const char *msg)
789+
void copy_reflog_msg(struct strbuf *sb, const char *msg)
790790
{
791-
char *cp = buf;
792791
char c;
793792
int wasspace = 1;
794793

795-
*cp++ = '\t';
794+
strbuf_addch(sb, '\t');
796795
while ((c = *msg++)) {
797796
if (wasspace && isspace(c))
798797
continue;
799798
wasspace = isspace(c);
800799
if (wasspace)
801800
c = ' ';
802-
*cp++ = c;
801+
strbuf_addch(sb, c);
803802
}
804-
while (buf < cp && isspace(cp[-1]))
805-
cp--;
806-
*cp++ = '\n';
807-
return cp - buf;
803+
strbuf_rtrim(sb);
808804
}
809805

810806
int should_autocreate_reflog(const char *refname)

refs/files-backend.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,26 +1582,17 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,
15821582
const struct object_id *new_oid,
15831583
const char *committer, const char *msg)
15841584
{
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;
16031587

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;
16051596
}
16061597

16071598
static int files_log_ref_write(struct files_ref_store *refs,

refs/refs-internal.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ enum peel_status {
9191
enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
9292

9393
/*
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.
9796
*/
98-
int copy_reflog_msg(char *buf, const char *msg);
97+
void copy_reflog_msg(struct strbuf *sb, const char *msg);
9998

10099
/**
101100
* Information needed for a single ref update. Set new_oid to the new

0 commit comments

Comments
 (0)