Skip to content

Commit abd0cd3

Browse files
dturner-twgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e2bef5 commit abd0cd3

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

builtin/checkout.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,24 +619,20 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
619619
if (opts->new_branch) {
620620
if (opts->new_orphan_branch) {
621621
if (opts->new_branch_log && !log_all_ref_updates) {
622-
int temp;
623-
struct strbuf log_file = STRBUF_INIT;
624622
int ret;
625-
const char *ref_name;
623+
char *refname;
626624
struct strbuf err = STRBUF_INIT;
627625

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);
634629
if (ret) {
635630
fprintf(stderr, _("Can not do reflog for '%s': %s\n"),
636631
opts->new_orphan_branch, err.buf);
637632
strbuf_release(&err);
638633
return;
639634
}
635+
strbuf_release(&err);
640636
}
641637
}
642638
else

refs.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,8 +3063,13 @@ static int should_autocreate_reflog(const char *refname)
30633063
!strcmp(refname, "HEAD");
30643064
}
30653065

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)
30683073
{
30693074
int logfd, oflags = O_APPEND | O_WRONLY;
30703075
char *logfile;
@@ -3073,7 +3078,7 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
30733078
logfile = sb_logfile->buf;
30743079
/* make sure the rest of the function can't change "logfile" */
30753080
sb_logfile = NULL;
3076-
if (should_autocreate_reflog(refname)) {
3081+
if (force_create || should_autocreate_reflog(refname)) {
30773082
if (safe_create_leading_directories(logfile) < 0) {
30783083
strbuf_addf(err, "unable to create directory for %s: "
30793084
"%s", logfile, strerror(errno));
@@ -3108,6 +3113,17 @@ int log_ref_setup(const char *refname, struct strbuf *sb_logfile, struct strbuf
31083113
return 0;
31093114
}
31103115

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+
31113127
static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
31123128
const unsigned char *new_sha1,
31133129
const char *committer, const char *msg)
@@ -3144,7 +3160,7 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
31443160
if (log_all_ref_updates < 0)
31453161
log_all_ref_updates = !is_bare_repository();
31463162

3147-
result = log_ref_setup(refname, sb_log_file, err);
3163+
result = log_ref_setup(refname, sb_log_file, err, 0);
31483164

31493165
if (result)
31503166
return result;

refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ extern int peel_ref(const char *refname, unsigned char *sha1);
191191
/*
192192
* Setup reflog before using. Fill in err and return -1 on failure.
193193
*/
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);
195195

196196
/** Reads log for the value of ref during at_time. **/
197197
extern int read_ref_at(const char *refname, unsigned int flags,

0 commit comments

Comments
 (0)