Skip to content

Commit 9cb10ca

Browse files
committed
Merge branch 'bp/log-ref-write-fd-with-strbuf'
Code clean-up. * bp/log-ref-write-fd-with-strbuf: convert log_ref_write_fd() to use strbuf
2 parents 8fa8a4f + 80a6c20 commit 9cb10ca

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
@@ -787,25 +787,21 @@ int delete_ref(const char *msg, const char *refname,
787787
old_oid, flags);
788788
}
789789

790-
int copy_reflog_msg(char *buf, const char *msg)
790+
void copy_reflog_msg(struct strbuf *sb, const char *msg)
791791
{
792-
char *cp = buf;
793792
char c;
794793
int wasspace = 1;
795794

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

811807
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
@@ -93,11 +93,10 @@ enum peel_status {
9393
enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
9494

9595
/*
96-
* Copy the reflog message msg to buf, which has been allocated sufficiently
97-
* large, while cleaning up the whitespaces. Especially, convert LF to space,
98-
* because reflog file is one line per entry.
96+
* Copy the reflog message msg to sb while cleaning up the whitespaces.
97+
* Especially, convert LF to space, because reflog file is one line per entry.
9998
*/
100-
int copy_reflog_msg(char *buf, const char *msg);
99+
void copy_reflog_msg(struct strbuf *sb, const char *msg);
101100

102101
/**
103102
* Information needed for a single ref update. Set new_oid to the new

0 commit comments

Comments
 (0)