refs: add REF_FORCE_CREATE_REFLOG flag

Add a flag to allow forcing the creation of a reflog even if the ref name and core.logAllRefUpdates setting would not ordinarily cause ref creation. In a moment, we will use this to add options to git tag and git update-ref to force reflog creation. 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 0f2a71d9923b4cf010ad0801eb71ee3666798204
2 files changed +22 -13
refs.c
+21 -13
@@ -62,6 +62,11 @@ static unsigned char refname_disposition[256] = {
62 */
63 #define REF_NEEDS_COMMIT 0x20
64
65 +/*
66 + * 0x40 is REF_FORCE_CREATE_REFLOG, so skip it if you're adding a
67 + * value to ref_update::flags
68 + */
69 +
70 /*
71 * Try to read one refname component from the front of refname.
72 * Return the length of the component found, or -1 if the component is
@@ -2914,7 +2919,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
2919 const unsigned char *sha1, struct strbuf *err);
2920 static int commit_ref_update(struct ref_lock *lock,
2921 const unsigned char *sha1, const char *logmsg,
2917 - struct strbuf *err);
2922 + int flags, struct strbuf *err);
2923
2924 int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
2925 {
@@ -2976,7 +2981,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
2981 hashcpy(lock->old_oid.hash, orig_sha1);
2982
2983 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
2979 - commit_ref_update(lock, orig_sha1, logmsg, &err)) {
2984 + commit_ref_update(lock, orig_sha1, logmsg, 0, &err)) {
2985 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
2986 strbuf_release(&err);
2987 goto rollback;
@@ -2995,7 +3000,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
3000 flag = log_all_ref_updates;
3001 log_all_ref_updates = 0;
3002 if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
2998 - commit_ref_update(lock, orig_sha1, NULL, &err)) {
3003 + commit_ref_update(lock, orig_sha1, NULL, 0, &err)) {
3004 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
3005 strbuf_release(&err);
3006 }
@@ -3152,7 +3157,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
3157
3158 static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3159 const unsigned char *new_sha1, const char *msg,
3155 - struct strbuf *sb_log_file, struct strbuf *err)
3160 + struct strbuf *sb_log_file, int flags,
3161 + struct strbuf *err)
3162 {
3163 int logfd, result, oflags = O_APPEND | O_WRONLY;
3164 char *log_file;
@@ -3160,7 +3166,7 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3166 if (log_all_ref_updates < 0)
3167 log_all_ref_updates = !is_bare_repository();
3168
3163 - result = log_ref_setup(refname, sb_log_file, err, 0);
3169 + result = log_ref_setup(refname, sb_log_file, err, flags & REF_FORCE_CREATE_REFLOG);
3170
3171 if (result)
3172 return result;
@@ -3189,10 +3195,11 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
3195
3196 static int log_ref_write(const char *refname, const unsigned char *old_sha1,
3197 const unsigned char *new_sha1, const char *msg,
3192 - struct strbuf *err)
3198 + int flags, struct strbuf *err)
3199 {
3200 struct strbuf sb = STRBUF_INIT;
3195 - int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, err);
3201 + int ret = log_ref_write_1(refname, old_sha1, new_sha1, msg, &sb, flags,
3202 + err);
3203 strbuf_release(&sb);
3204 return ret;
3205 }
@@ -3246,12 +3253,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
3253 */
3254 static int commit_ref_update(struct ref_lock *lock,
3255 const unsigned char *sha1, const char *logmsg,
3249 - struct strbuf *err)
3256 + int flags, struct strbuf *err)
3257 {
3258 clear_loose_ref_cache(&ref_cache);
3252 - if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, err) < 0 ||
3259 + if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0 ||
3260 (strcmp(lock->ref_name, lock->orig_ref_name) &&
3254 - log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, err) < 0)) {
3261 + log_ref_write(lock->orig_ref_name, lock->old_oid.hash, sha1, logmsg, flags, err) < 0)) {
3262 char *old_msg = strbuf_detach(err, NULL);
3263 strbuf_addf(err, "Cannot update the ref '%s': %s",
3264 lock->ref_name, old_msg);
@@ -3281,7 +3288,7 @@ static int commit_ref_update(struct ref_lock *lock,
3288 !strcmp(head_ref, lock->ref_name)) {
3289 struct strbuf log_err = STRBUF_INIT;
3290 if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
3284 - logmsg, &log_err)) {
3291 + logmsg, 0, &log_err)) {
3292 error("%s", log_err.buf);
3293 strbuf_release(&log_err);
3294 }
@@ -3355,7 +3362,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
3362 done:
3363 #endif
3364 if (logmsg && !read_ref(refs_heads_master, new_sha1) &&
3358 - log_ref_write(ref_target, old_sha1, new_sha1, logmsg, &err)) {
3365 + log_ref_write(ref_target, old_sha1, new_sha1, logmsg, 0, &err)) {
3366 error("%s", err.buf);
3367 strbuf_release(&err);
3368 }
@@ -4032,7 +4039,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
4039
4040 if (update->flags & REF_NEEDS_COMMIT) {
4041 if (commit_ref_update(update->lock,
4035 - update->new_sha1, update->msg, err)) {
4042 + update->new_sha1, update->msg,
4043 + update->flags, err)) {
4044 /* freed by commit_ref_update(): */
4045 update->lock = NULL;
4046 ret = TRANSACTION_GENERIC_ERROR;
refs.h
+1
@@ -187,6 +187,7 @@ extern int peel_ref(const char *refname, unsigned char *sha1);
187 * Other flags are reserved for internal use.
188 */
189 #define REF_NODEREF 0x01
190 +#define REF_FORCE_CREATE_REFLOG 0x40
191
192 /*
193 * Setup reflog before using. Fill in err and return -1 on failure.