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
This commit is contained in:
Junio C Hamano 2018-07-24 14:50:47 -07:00
commit 9cb10ca9df
3 changed files with 17 additions and 31 deletions

12
refs.c
View File

@ -787,25 +787,21 @@ int delete_ref(const char *msg, const char *refname,
old_oid, flags); old_oid, flags);
} }
int copy_reflog_msg(char *buf, const char *msg) void copy_reflog_msg(struct strbuf *sb, const char *msg)
{ {
char *cp = buf;
char c; char c;
int wasspace = 1; int wasspace = 1;
*cp++ = '\t'; strbuf_addch(sb, '\t');
while ((c = *msg++)) { while ((c = *msg++)) {
if (wasspace && isspace(c)) if (wasspace && isspace(c))
continue; continue;
wasspace = isspace(c); wasspace = isspace(c);
if (wasspace) if (wasspace)
c = ' '; c = ' ';
*cp++ = c; strbuf_addch(sb, c);
} }
while (buf < cp && isspace(cp[-1])) strbuf_rtrim(sb);
cp--;
*cp++ = '\n';
return cp - buf;
} }
int should_autocreate_reflog(const char *refname) int should_autocreate_reflog(const char *refname)

View File

@ -1582,26 +1582,17 @@ static int log_ref_write_fd(int fd, const struct object_id *old_oid,
const struct object_id *new_oid, const struct object_id *new_oid,
const char *committer, const char *msg) const char *committer, const char *msg)
{ {
int msglen, written; struct strbuf sb = STRBUF_INIT;
unsigned maxlen, len; int ret = 0;
char *logrec;
msglen = msg ? strlen(msg) : 0; strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
maxlen = strlen(committer) + msglen + 100; if (msg && *msg)
logrec = xmalloc(maxlen); copy_reflog_msg(&sb, msg);
len = xsnprintf(logrec, maxlen, "%s %s %s\n", strbuf_addch(&sb, '\n');
oid_to_hex(old_oid), if (write_in_full(fd, sb.buf, sb.len) < 0)
oid_to_hex(new_oid), ret = -1;
committer); strbuf_release(&sb);
if (msglen) return ret;
len += copy_reflog_msg(logrec + len - 1, msg) - 1;
written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
free(logrec);
if (written < 0)
return -1;
return 0;
} }
static int files_log_ref_write(struct files_ref_store *refs, static int files_log_ref_write(struct files_ref_store *refs,

View File

@ -93,11 +93,10 @@ enum peel_status {
enum peel_status peel_object(const struct object_id *name, struct object_id *oid); enum peel_status peel_object(const struct object_id *name, struct object_id *oid);
/* /*
* Copy the reflog message msg to buf, which has been allocated sufficiently * Copy the reflog message msg to sb while cleaning up the whitespaces.
* large, while cleaning up the whitespaces. Especially, convert LF to space, * Especially, convert LF to space, because reflog file is one line per entry.
* because reflog file is one line per entry.
*/ */
int copy_reflog_msg(char *buf, const char *msg); void copy_reflog_msg(struct strbuf *sb, const char *msg);
/** /**
* Information needed for a single ref update. Set new_oid to the new * Information needed for a single ref update. Set new_oid to the new