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>
Michael Haggerty committed
Jan 6, 2017 at 17:22 UTC
4533e5343bb1171b932f787e345d6cff920a4e70
1 file changed
+34
-35
refs/files-backend.c
+34
-35
@@ -2719,37 +2719,36 @@ static int open_or_create_logfile(const char *path, void *cb)
2719
}
2720
2721
/*
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.
2730
*/
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)
2733
{
2735
- strbuf_git_path(logfile, "logs/%s", refname);
2734
+ char *logfile = git_pathdup("logs/%s", refname);
2735
2736
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)) {
2738
if (errno == ENOENT)
2739
strbuf_addf(err, "unable to create directory for '%s': "
2741
- "%s", logfile->buf, strerror(errno));
2740
+ "%s", logfile, strerror(errno));
2741
else if (errno == EISDIR)
2742
strbuf_addf(err, "there are still logs under '%s'",
2744
- logfile->buf);
2743
+ logfile);
2744
else
2745
strbuf_addf(err, "unable to append to '%s': %s",
2747
- logfile->buf, strerror(errno));
2746
+ logfile, strerror(errno));
2747
2749
- return -1;
2748
+ goto error;
2749
}
2750
} else {
2752
- *logfd = open(logfile->buf, O_APPEND | O_WRONLY, 0666);
2751
+ *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
2752
if (*logfd < 0) {
2753
if (errno == ENOENT || errno == EISDIR) {
2754
/*
@@ -2761,34 +2760,39 @@ static int log_ref_setup(const char *refname,
2760
;
2761
} else {
2762
strbuf_addf(err, "unable to append to '%s': %s",
2764
- logfile->buf, strerror(errno));
2765
- return -1;
2763
+ logfile, strerror(errno));
2764
+ goto error;
2765
}
2766
}
2767
}
2768
2769
if (*logfd >= 0)
2771
- adjust_shared_perm(logfile->buf);
2770
+ adjust_shared_perm(logfile);
2771
2772
+ free(logfile);
2773
return 0;
2774
+
2775
+error:
2776
+ free(logfile);
2777
+ return -1;
2778
}
2779
2780
static int files_create_reflog(struct ref_store *ref_store,
2781
const char *refname, int force_create,
2782
struct strbuf *err)
2783
{
2780
- int ret;
2781
- struct strbuf sb = STRBUF_INIT;
2784
int fd;
2785
2786
/* Check validity (but we don't need the result): */
2787
files_downcast(ref_store, 0, "create_reflog");
2788
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
+
2792
if (fd >= 0)
2793
close(fd);
2790
- strbuf_release(&sb);
2791
- return ret;
2794
+
2795
+ return 0;
2796
}
2797
2798
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,
2823
2824
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
2825
const unsigned char *new_sha1, const char *msg,
2822
- struct strbuf *logfile, int flags,
2823
- struct strbuf *err)
2826
+ int flags, struct strbuf *err)
2827
{
2828
int logfd, result;
2829
2830
if (log_all_ref_updates < 0)
2831
log_all_ref_updates = !is_bare_repository();
2832
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);
2835
2836
if (result)
2837
return result;
@@ -2859,11 +2862,7 @@ int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
2862
const unsigned char *new_sha1, const char *msg,
2863
int flags, struct strbuf *err)
2864
{
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);
2866
}
2867
2868
/*