Skip to content

Commit 0f2a71d

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

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

refs.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ static unsigned char refname_disposition[256] = {
6262
*/
6363
#define REF_NEEDS_COMMIT 0x20
6464

65+
/*
66+
* 0x40 is REF_FORCE_CREATE_REFLOG, so skip it if you're adding a
67+
* value to ref_update::flags
68+
*/
69+
6570
/*
6671
* Try to read one refname component from the front of refname.
6772
* 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,
29142919
const unsigned char *sha1, struct strbuf *err);
29152920
static int commit_ref_update(struct ref_lock *lock,
29162921
const unsigned char *sha1, const char *logmsg,
2917-
struct strbuf *err);
2922+
int flags, struct strbuf *err);
29182923

29192924
int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
29202925
{
@@ -2976,7 +2981,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
29762981
hashcpy(lock->old_oid.hash, orig_sha1);
29772982

29782983
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)) {
29802985
error("unable to write current sha1 into %s: %s", newrefname, err.buf);
29812986
strbuf_release(&err);
29822987
goto rollback;
@@ -2995,7 +3000,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
29953000
flag = log_all_ref_updates;
29963001
log_all_ref_updates = 0;
29973002
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)) {
29993004
error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
30003005
strbuf_release(&err);
30013006
}
@@ -3152,15 +3157,16 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
31523157

31533158
static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
31543159
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)
31563162
{
31573163
int logfd, result, oflags = O_APPEND | O_WRONLY;
31583164
char *log_file;
31593165

31603166
if (log_all_ref_updates < 0)
31613167
log_all_ref_updates = !is_bare_repository();
31623168

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);
31643170

31653171
if (result)
31663172
return result;
@@ -3189,10 +3195,11 @@ static int log_ref_write_1(const char *refname, const unsigned char *old_sha1,
31893195

31903196
static int log_ref_write(const char *refname, const unsigned char *old_sha1,
31913197
const unsigned char *new_sha1, const char *msg,
3192-
struct strbuf *err)
3198+
int flags, struct strbuf *err)
31933199
{
31943200
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);
31963203
strbuf_release(&sb);
31973204
return ret;
31983205
}
@@ -3246,12 +3253,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
32463253
*/
32473254
static int commit_ref_update(struct ref_lock *lock,
32483255
const unsigned char *sha1, const char *logmsg,
3249-
struct strbuf *err)
3256+
int flags, struct strbuf *err)
32503257
{
32513258
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 ||
32533260
(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)) {
32553262
char *old_msg = strbuf_detach(err, NULL);
32563263
strbuf_addf(err, "Cannot update the ref '%s': %s",
32573264
lock->ref_name, old_msg);
@@ -3281,7 +3288,7 @@ static int commit_ref_update(struct ref_lock *lock,
32813288
!strcmp(head_ref, lock->ref_name)) {
32823289
struct strbuf log_err = STRBUF_INIT;
32833290
if (log_ref_write("HEAD", lock->old_oid.hash, sha1,
3284-
logmsg, &log_err)) {
3291+
logmsg, 0, &log_err)) {
32853292
error("%s", log_err.buf);
32863293
strbuf_release(&log_err);
32873294
}
@@ -3355,7 +3362,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
33553362
done:
33563363
#endif
33573364
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)) {
33593366
error("%s", err.buf);
33603367
strbuf_release(&err);
33613368
}
@@ -4032,7 +4039,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
40324039

40334040
if (update->flags & REF_NEEDS_COMMIT) {
40344041
if (commit_ref_update(update->lock,
4035-
update->new_sha1, update->msg, err)) {
4042+
update->new_sha1, update->msg,
4043+
update->flags, err)) {
40364044
/* freed by commit_ref_update(): */
40374045
update->lock = NULL;
40384046
ret = TRANSACTION_GENERIC_ERROR;

refs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ extern int peel_ref(const char *refname, unsigned char *sha1);
187187
* Other flags are reserved for internal use.
188188
*/
189189
#define REF_NODEREF 0x01
190+
#define REF_FORCE_CREATE_REFLOG 0x40
190191

191192
/*
192193
* Setup reflog before using. Fill in err and return -1 on failure.

0 commit comments

Comments
 (0)