log_ref_setup(): manage the name of the reflog file internally

Instead of writing the name of the reflog file into a strbuf that is
supplied by the caller but not needed there, write it into a local
temporary buffer and remove the strbuf parameter entirely.

And while we're adjusting the function signature, reorder the arguments
to move the input parameters before the output parameters.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michael Haggerty
2017-01-06 17:22:36 +01:00
committed by Junio C Hamano
parent 87b21e05ed
commit 4533e5343b

View File

@ -2719,37 +2719,36 @@ static int open_or_create_logfile(const char *path, void *cb)
} }
/* /*
* Create a reflog for a ref. Store its path to *logfile. If * Create a reflog for a ref. If force_create = 0, only create the
* force_create = 0, only create the reflog for certain refs (those * reflog for certain refs (those for which should_autocreate_reflog
* for which should_autocreate_reflog returns non-zero). Otherwise, * returns non-zero). Otherwise, create it regardless of the reference
* create it regardless of the reference name. If the logfile already * name. If the logfile already existed or was created, return 0 and
* existed or was created, return 0 and set *logfd to the file * set *logfd to the file descriptor opened for appending to the file.
* descriptor opened for appending to the file. If no logfile exists * If no logfile exists and we decided not to create one, return 0 and
* and we decided not to create one, return 0 and set *logfd to -1. On * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
* failure, fill in *err, set *logfd to -1, and return -1. * return -1.
*/ */
static int log_ref_setup(const char *refname, static int log_ref_setup(const char *refname, int force_create,
struct strbuf *logfile, int *logfd, int *logfd, struct strbuf *err)
struct strbuf *err, int force_create)
{ {
strbuf_git_path(logfile, "logs/%s", refname); char *logfile = git_pathdup("logs/%s", refname);
if (force_create || should_autocreate_reflog(refname)) { if (force_create || should_autocreate_reflog(refname)) {
if (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) { if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
if (errno == ENOENT) if (errno == ENOENT)
strbuf_addf(err, "unable to create directory for '%s': " strbuf_addf(err, "unable to create directory for '%s': "
"%s", logfile->buf, strerror(errno)); "%s", logfile, strerror(errno));
else if (errno == EISDIR) else if (errno == EISDIR)
strbuf_addf(err, "there are still logs under '%s'", strbuf_addf(err, "there are still logs under '%s'",
logfile->buf); logfile);
else else
strbuf_addf(err, "unable to append to '%s': %s", strbuf_addf(err, "unable to append to '%s': %s",
logfile->buf, strerror(errno)); logfile, strerror(errno));
return -1; goto error;
} }
} else { } else {
*logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666); *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
if (*logfd < 0) { if (*logfd < 0) {
if (errno == ENOENT || errno == EISDIR) { if (errno == ENOENT || errno == EISDIR) {
/* /*
@ -2761,34 +2760,39 @@ static int log_ref_setup(const char *refname,
; ;
} else { } else {
strbuf_addf(err, "unable to append to '%s': %s", strbuf_addf(err, "unable to append to '%s': %s",
logfile->buf, strerror(errno)); logfile, strerror(errno));
return -1; goto error;
} }
} }
} }
if (*logfd >= 0) if (*logfd >= 0)
adjust_shared_perm(logfile->buf); adjust_shared_perm(logfile);
free(logfile);
return 0; return 0;
error:
free(logfile);
return -1;
} }
static int files_create_reflog(struct ref_store *ref_store, static int files_create_reflog(struct ref_store *ref_store,
const char *refname, int force_create, const char *refname, int force_create,
struct strbuf *err) struct strbuf *err)
{ {
int ret;
struct strbuf sb = STRBUF_INIT;
int fd; int fd;
/* Check validity (but we don't need the result): */ /* Check validity (but we don't need the result): */
files_downcast(ref_store, 0, "create_reflog"); files_downcast(ref_store, 0, "create_reflog");
ret = log_ref_setup(refname, &sb, &fd, err, force_create); if (log_ref_setup(refname, force_create, &fd, err))
return -1;
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
strbuf_release(&sb);
return ret; return 0;
} }
static int log_ref_write_fd(int fd, const unsigned char *old_sha1, static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
@ -2819,16 +2823,15 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1, static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
const unsigned char *new_sha1, const char *msg, const unsigned char *new_sha1, const char *msg,
struct strbuf *logfile, int flags, int flags, struct strbuf *err)
struct strbuf *err)
{ {
int logfd, result; int logfd, result;
if (log_all_ref_updates < 0) if (log_all_ref_updates < 0)
log_all_ref_updates = !is_bare_repository(); log_all_ref_updates = !is_bare_repository();
result = log_ref_setup(refname, logfile, &logfd, err, result = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,
flags & REF_FORCE_CREATE_REFLOG); &logfd, err);
if (result) if (result)
return result; return result;
@ -2859,11 +2862,7 @@ int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
const unsigned char *new_sha1, const char *msg, const unsigned char *new_sha1, const char *msg,
int flags, struct strbuf *err) int flags, struct strbuf *err)
{ {
struct strbuf sb = STRBUF_INIT; return log_ref_write_1(refname, old_sha1, new_sha1, msg, flags, err);
int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
err);
strbuf_release(&sb);
return ret;
} }
/* /*