Skip to content

Commit e404f45

Browse files
mhaggergitster
authored andcommitted
log_ref_setup(): pass the open file descriptor back to the caller
This function will most often be called by log_ref_write_1(), which wants to append to the reflog file. In that case, it is silly to close the file only for the caller to reopen it immediately. So, in the case that the file was opened, pass the open file descriptor back to the caller. Signed-off-by: Michael Haggerty <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1fb0c80 commit e404f45

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

refs/files-backend.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,19 +2719,23 @@ static int open_or_create_logfile(const char *path, void *cb)
27192719
}
27202720

27212721
/*
2722-
* Create a reflog for a ref. If force_create = 0, the reflog will
2723-
* only be created for certain refs (those for which
2724-
* should_autocreate_reflog returns non-zero. Otherwise, create it
2725-
* regardless of the ref name. Fill in *err and return -1 on failure.
2722+
* Create a reflog for a ref. Store its path to *logfile. If
2723+
* force_create = 0, only create the reflog for certain refs (those
2724+
* for which should_autocreate_reflog returns non-zero). Otherwise,
2725+
* create it regardless of the reference name. If the logfile already
2726+
* existed or was created, return 0 and set *logfd to the file
2727+
* descriptor opened for appending to the file. If no logfile exists
2728+
* and we decided not to create one, return 0 and set *logfd to -1. On
2729+
* failure, fill in *err, set *logfd to -1, and return -1.
27262730
*/
2727-
static int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err, int force_create)
2731+
static int log_ref_setup(const char *refname,
2732+
struct strbuf *logfile, int *logfd,
2733+
struct strbuf *err, int force_create)
27282734
{
2729-
int logfd;
2730-
27312735
strbuf_git_path(logfile, "logs/%s", refname);
27322736

27332737
if (force_create || should_autocreate_reflog(refname)) {
2734-
if (raceproof_create_file(logfile->buf, open_or_create_logfile, &logfd)) {
2738+
if (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) {
27352739
if (errno == ENOENT)
27362740
strbuf_addf(err, "unable to create directory for '%s': "
27372741
"%s", logfile->buf, strerror(errno));
@@ -2745,8 +2749,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
27452749
return -1;
27462750
}
27472751
} else {
2748-
logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2749-
if (logfd < 0) {
2752+
*logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2753+
if (*logfd < 0) {
27502754
if (errno == ENOENT || errno == EISDIR) {
27512755
/*
27522756
* The logfile doesn't already exist,
@@ -2763,10 +2767,8 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
27632767
}
27642768
}
27652769

2766-
if (logfd >= 0) {
2770+
if (*logfd >= 0)
27672771
adjust_shared_perm(logfile->buf);
2768-
close(logfd);
2769-
}
27702772

27712773
return 0;
27722774
}
@@ -2777,11 +2779,14 @@ static int files_create_reflog(struct ref_store *ref_store,
27772779
{
27782780
int ret;
27792781
struct strbuf sb = STRBUF_INIT;
2782+
int fd;
27802783

27812784
/* Check validity (but we don't need the result): */
27822785
files_downcast(ref_store, 0, "create_reflog");
27832786

2784-
ret = log_ref_setup(refname, &sb, err, force_create);
2787+
ret = log_ref_setup(refname, &sb, &fd, err, force_create);
2788+
if (fd >= 0)
2789+
close(fd);
27852790
strbuf_release(&sb);
27862791
return ret;
27872792
}
@@ -2817,17 +2822,17 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
28172822
struct strbuf *logfile, int flags,
28182823
struct strbuf *err)
28192824
{
2820-
int logfd, result, oflags = O_APPEND | O_WRONLY;
2825+
int logfd, result;
28212826

28222827
if (log_all_ref_updates < 0)
28232828
log_all_ref_updates = !is_bare_repository();
28242829

2825-
result = log_ref_setup(refname, logfile, err, flags & REF_FORCE_CREATE_REFLOG);
2830+
result = log_ref_setup(refname, logfile, &logfd, err,
2831+
flags & REF_FORCE_CREATE_REFLOG);
28262832

28272833
if (result)
28282834
return result;
28292835

2830-
logfd = open(logfile->buf, oflags);
28312836
if (logfd < 0)
28322837
return 0;
28332838
result = log_ref_write_fd(logfd, old_sha1, new_sha1,

0 commit comments

Comments
 (0)