refs: new public ref function: safe_create_reflog
The safe_create_reflog function creates a reflog, if it does not already exist. The log_ref_setup function becomes private and gains a force_create parameter to force the creation of a reflog even if log_all_ref_updates is false or the refname is not one of the special refnames. The new parameter also reduces the need to store, modify, and restore the log_all_ref_updates global before reflog creation. In a moment, we will use this to add reflog creation commands to git-reflog. Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
David Turner committed
Jul 21, 2015 at 17:04 UTC
abd0cd3a3018e1c4e9e4437fb3911d6658e99fec
3 files changed
+26
-14
builtin/checkout.c
+5
-9
@@ -619,24 +619,20 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
619
if (opts->new_branch) {
620
if (opts->new_orphan_branch) {
621
if (opts->new_branch_log && !log_all_ref_updates) {
622
- int temp;
623
- struct strbuf log_file = STRBUF_INIT;
622
int ret;
625
- const char *ref_name;
623
+ char *refname;
624
struct strbuf err = STRBUF_INIT;
625
628
- ref_name = mkpath("refs/heads/%s", opts->new_orphan_branch);
629
- temp = log_all_ref_updates;
630
- log_all_ref_updates = 1;
631
- ret = log_ref_setup(ref_name, &log_file, &err);
632
- log_all_ref_updates = temp;
633
- strbuf_release(&log_file);
626
+ refname = mkpathdup("refs/heads/%s", opts->new_orphan_branch);
627
+ ret = safe_create_reflog(refname, 1, &err);
628
+ free(refname);
629
if (ret) {
630
fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
631
opts->new_orphan_branch, err.buf);
632
strbuf_release(&err);
633
return;
634
}
635
+ strbuf_release(&err);
636
}
637
}
638
else
refs.c
+20
-4
@@ -3063,8 +3063,13 @@ static int should_autocreate_reflog(const char *refname)
3063
!strcmp(refname, "HEAD");
3064
}
3065
3066
-/* This function will fill in *err and return -1 on failure */
3067
-int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err)
3066
+/*
3067
+ * Create a reflog for a ref. If force_create = 0, the reflog will
3068
+ * only be created for certain refs (those for which
3069
+ * should_autocreate_reflog returns non-zero. Otherwise, create it
3070
+ * regardless of the ref name. Fill in *err and return -1 on failure.
3071
+ */
3072
+static int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf *err, int force_create)
3073
{
3074
int logfd, oflags = O_APPEND | O_WRONLY;
3075
char *logfile;
@@ -3073,7 +3078,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
3078
logfile = sb_logfile->buf;
3079
/* make sure the rest of the function can't change "logfile" */
3080
sb_logfile = NULL;
3076
- if (should_autocreate_reflog(refname)) {
3081
+ if (force_create || should_autocreate_reflog(refname)) {
3082
if (safe_create_leading_directories(logfile) < 0) {
3083
strbuf_addf(err, "unable to create directory for %s: "
3084
"%s", logfile, strerror(errno));
@@ -3108,6 +3113,17 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
3113
return 0;
3114
}
3115
3116
+
3117
+int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
3118
+{
3119
+ int ret;
3120
+ struct strbuf sb = STRBUF_INIT;
3121
+
3122
+ ret = log_ref_setup(refname, &sb, err, force_create);
3123
+ strbuf_release(&sb);
3124
+ return ret;
3125
+}
3126
+
3127
static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
3128
const unsigned char *new_sha1,
3129
const char *committer, const char *msg)
@@ -3144,7 +3160,7 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3160
if (log_all_ref_updates < 0)
3161
log_all_ref_updates = !is_bare_repository();
3162
3147
- result = log_ref_setup(refname, sb_log_file, err);
3163
+ result = log_ref_setup(refname, sb_log_file, err, 0);
3164
3165
if (result)
3166
return result;
refs.h
+1
-1
@@ -191,7 +191,7 @@ extern int peel_ref(const char *refname, unsigned char *sha1);
191
/*
192
* Setup reflog before using. Fill in err and return -1 on failure.
193
*/
194
-int log_ref_setup(const char *refname, struct strbuf *logfile, struct strbuf *err);
194
+int safe_create_reflog(const char *refname, int force_create, struct strbuf *err);
195
196
/** Reads log for the value of ref during at_time. **/
197
extern int read_ref_at(const char *refname, unsigned int flags,