Skip to content

Commit 4533e53

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 87b21e0 commit 4533e53

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

refs/files-backend.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,37 +2719,36 @@ static int open_or_create_logfile(const char *path, void *cb)
27192719
}
27202720

27212721
/*
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.
2722+
* Create a reflog for a ref. If force_create = 0, only create the
2723+
* reflog for certain refs (those for which should_autocreate_reflog
2724+
* returns non-zero). Otherwise, create it regardless of the reference
2725+
* name. If the logfile already existed or was created, return 0 and
2726+
* set *logfd to the file descriptor opened for appending to the file.
2727+
* If no logfile exists and we decided not to create one, return 0 and
2728+
* set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
2729+
* return -1.
27302730
*/
2731-
static int log_ref_setup(const char *refname,
2732-
struct strbuf *logfile, int *logfd,
2733-
struct strbuf *err, int force_create)
2731+
static int log_ref_setup(const char *refname, int force_create,
2732+
int *logfd, struct strbuf *err)
27342733
{
2735-
strbuf_git_path(logfile, "logs/%s", refname);
2734+
char *logfile = git_pathdup("logs/%s", refname);
27362735

27372736
if (force_create || should_autocreate_reflog(refname)) {
2738-
if (raceproof_create_file(logfile->buf, open_or_create_logfile, logfd)) {
2737+
if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
27392738
if (errno == ENOENT)
27402739
strbuf_addf(err, "unable to create directory for '%s': "
2741-
"%s", logfile->buf, strerror(errno));
2740+
"%s", logfile, strerror(errno));
27422741
else if (errno == EISDIR)
27432742
strbuf_addf(err, "there are still logs under '%s'",
2744-
logfile->buf);
2743+
logfile);
27452744
else
27462745
strbuf_addf(err, "unable to append to '%s': %s",
2747-
logfile->buf, strerror(errno));
2746+
logfile, strerror(errno));
27482747

2749-
return -1;
2748+
goto error;
27502749
}
27512750
} else {
2752-
*logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2751+
*logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
27532752
if (*logfd < 0) {
27542753
if (errno == ENOENT || errno == EISDIR) {
27552754
/*
@@ -2761,34 +2760,39 @@ static int log_ref_setup(const char *refname,
27612760
;
27622761
} else {
27632762
strbuf_addf(err, "unable to append to '%s': %s",
2764-
logfile->buf, strerror(errno));
2765-
return -1;
2763+
logfile, strerror(errno));
2764+
goto error;
27662765
}
27672766
}
27682767
}
27692768

27702769
if (*logfd >= 0)
2771-
adjust_shared_perm(logfile->buf);
2770+
adjust_shared_perm(logfile);
27722771

2772+
free(logfile);
27732773
return 0;
2774+
2775+
error:
2776+
free(logfile);
2777+
return -1;
27742778
}
27752779

27762780
static int files_create_reflog(struct ref_store *ref_store,
27772781
const char *refname, int force_create,
27782782
struct strbuf *err)
27792783
{
2780-
int ret;
2781-
struct strbuf sb = STRBUF_INIT;
27822784
int fd;
27832785

27842786
/* Check validity (but we don't need the result): */
27852787
files_downcast(ref_store, 0, "create_reflog");
27862788

2787-
ret = log_ref_setup(refname, &sb, &fd, err, force_create);
2789+
if (log_ref_setup(refname, force_create, &fd, err))
2790+
return -1;
2791+
27882792
if (fd >= 0)
27892793
close(fd);
2790-
strbuf_release(&sb);
2791-
return ret;
2794+
2795+
return 0;
27922796
}
27932797

27942798
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,
28192823

28202824
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
28212825
const unsigned char *new_sha1, const char *msg,
2822-
struct strbuf *logfile, int flags,
2823-
struct strbuf *err)
2826+
int flags, struct strbuf *err)
28242827
{
28252828
int logfd, result;
28262829

28272830
if (log_all_ref_updates < 0)
28282831
log_all_ref_updates = !is_bare_repository();
28292832

2830-
result = log_ref_setup(refname, logfile, &logfd, err,
2831-
flags & REF_FORCE_CREATE_REFLOG);
2833+
result = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,
2834+
&logfd, err);
28322835

28332836
if (result)
28342837
return result;
@@ -2859,11 +2862,7 @@ int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
28592862
const unsigned char *new_sha1, const char *msg,
28602863
int flags, struct strbuf *err)
28612864
{
2862-
struct strbuf sb = STRBUF_INIT;
2863-
int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
2864-
err);
2865-
strbuf_release(&sb);
2866-
return ret;
2865+
return log_ref_write_1(refname, old_sha1, new_sha1, msg, flags, err);
28672866
}
28682867

28692868
/*

0 commit comments

Comments
 (0)