Skip to content

Commit 802de3d

Browse files
pcloudsgitster
authored andcommitted
files-backend: add and use files_reflog_path()
Keep repo-related path handling in one place. This will make it easier to add submodule/multiworktree support later. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a5c1efd commit 802de3d

File tree

1 file changed

+86
-56
lines changed

1 file changed

+86
-56
lines changed

refs/files-backend.c

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
165165
const char *dirname, size_t len,
166166
int incomplete);
167167
static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
168-
static int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
168+
static int files_log_ref_write(struct files_ref_store *refs,
169+
const char *refname, const unsigned char *old_sha1,
169170
const unsigned char *new_sha1, const char *msg,
170171
int flags, struct strbuf *err);
171172

@@ -1167,6 +1168,18 @@ static const char *files_packed_refs_path(struct files_ref_store *refs)
11671168
return refs->packed_refs_path;
11681169
}
11691170

1171+
static void files_reflog_path(struct files_ref_store *refs,
1172+
struct strbuf *sb,
1173+
const char *refname)
1174+
{
1175+
if (!refname) {
1176+
strbuf_git_path(sb, "logs");
1177+
return;
1178+
}
1179+
1180+
strbuf_git_path(sb, "logs/%s", refname);
1181+
}
1182+
11701183
/*
11711184
* Get the packed_ref_cache for the specified files_ref_store,
11721185
* creating it if necessary.
@@ -2313,7 +2326,9 @@ enum {
23132326
* subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
23142327
* REMOVE_EMPTY_PARENTS_REFLOG.
23152328
*/
2316-
static void try_remove_empty_parents(const char *refname, unsigned int flags)
2329+
static void try_remove_empty_parents(struct files_ref_store *refs,
2330+
const char *refname,
2331+
unsigned int flags)
23172332
{
23182333
struct strbuf buf = STRBUF_INIT;
23192334
struct strbuf sb = STRBUF_INIT;
@@ -2345,7 +2360,7 @@ static void try_remove_empty_parents(const char *refname, unsigned int flags)
23452360
flags &= ~REMOVE_EMPTY_PARENTS_REF;
23462361

23472362
strbuf_reset(&sb);
2348-
strbuf_git_path(&sb, "logs/%s", buf.buf);
2363+
files_reflog_path(refs, &sb, buf.buf);
23492364
if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
23502365
flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
23512366
}
@@ -2538,15 +2553,15 @@ static int rename_tmp_log_callback(const char *path, void *cb_data)
25382553
}
25392554
}
25402555

2541-
static int rename_tmp_log(const char *newrefname)
2556+
static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
25422557
{
25432558
struct strbuf path = STRBUF_INIT;
25442559
struct strbuf tmp = STRBUF_INIT;
25452560
struct rename_cb cb;
25462561
int ret;
25472562

2548-
strbuf_git_path(&path, "logs/%s", newrefname);
2549-
strbuf_git_path(&tmp, "logs/%s", TMP_RENAMED_LOG);
2563+
files_reflog_path(refs, &path, newrefname);
2564+
files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
25502565
cb.tmp_renamed_log = tmp.buf;
25512566
ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
25522567
if (ret) {
@@ -2606,9 +2621,9 @@ static int files_rename_ref(struct ref_store *ref_store,
26062621
int log, ret;
26072622
struct strbuf err = STRBUF_INIT;
26082623

2609-
strbuf_git_path(&sb_oldref, "logs/%s", oldrefname);
2610-
strbuf_git_path(&sb_newref, "logs/%s", newrefname);
2611-
strbuf_git_path(&tmp_renamed_log, "logs/%s", TMP_RENAMED_LOG);
2624+
files_reflog_path(refs, &sb_oldref, oldrefname);
2625+
files_reflog_path(refs, &sb_newref, newrefname);
2626+
files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
26122627

26132628
log = !lstat(sb_oldref.buf, &loginfo);
26142629
if (log && S_ISLNK(loginfo.st_mode)) {
@@ -2671,7 +2686,7 @@ static int files_rename_ref(struct ref_store *ref_store,
26712686
}
26722687
}
26732688

2674-
if (log && rename_tmp_log(newrefname))
2689+
if (log && rename_tmp_log(refs, newrefname))
26752690
goto rollback;
26762691

26772692
logmoved = log;
@@ -2786,10 +2801,15 @@ static int open_or_create_logfile(const char *path, void *cb)
27862801
* set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
27872802
* return -1.
27882803
*/
2789-
static int log_ref_setup(const char *refname, int force_create,
2804+
static int log_ref_setup(struct files_ref_store *refs,
2805+
const char *refname, int force_create,
27902806
int *logfd, struct strbuf *err)
27912807
{
2792-
char *logfile = git_pathdup("logs/%s", refname);
2808+
struct strbuf logfile_sb = STRBUF_INIT;
2809+
char *logfile;
2810+
2811+
files_reflog_path(refs, &logfile_sb, refname);
2812+
logfile = strbuf_detach(&logfile_sb, NULL);
27932813

27942814
if (force_create || should_autocreate_reflog(refname)) {
27952815
if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
@@ -2839,12 +2859,11 @@ static int files_create_reflog(struct ref_store *ref_store,
28392859
const char *refname, int force_create,
28402860
struct strbuf *err)
28412861
{
2862+
struct files_ref_store *refs =
2863+
files_downcast(ref_store, 0, "create_reflog");
28422864
int fd;
28432865

2844-
/* Check validity (but we don't need the result): */
2845-
files_downcast(ref_store, 0, "create_reflog");
2846-
2847-
if (log_ref_setup(refname, force_create, &fd, err))
2866+
if (log_ref_setup(refs, refname, force_create, &fd, err))
28482867
return -1;
28492868

28502869
if (fd >= 0)
@@ -2879,7 +2898,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
28792898
return 0;
28802899
}
28812900

2882-
static int files_log_ref_write(const char *refname, const unsigned char *old_sha1,
2901+
static int files_log_ref_write(struct files_ref_store *refs,
2902+
const char *refname, const unsigned char *old_sha1,
28832903
const unsigned char *new_sha1, const char *msg,
28842904
int flags, struct strbuf *err)
28852905
{
@@ -2888,7 +2908,8 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
28882908
if (log_all_ref_updates == LOG_REFS_UNSET)
28892909
log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
28902910

2891-
result = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,
2911+
result = log_ref_setup(refs, refname,
2912+
flags & REF_FORCE_CREATE_REFLOG,
28922913
&logfd, err);
28932914

28942915
if (result)
@@ -2902,7 +2923,7 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
29022923
struct strbuf sb = STRBUF_INIT;
29032924
int save_errno = errno;
29042925

2905-
strbuf_git_path(&sb, "logs/%s", refname);
2926+
files_reflog_path(refs, &sb, refname);
29062927
strbuf_addf(err, "unable to append to '%s': %s",
29072928
sb.buf, strerror(save_errno));
29082929
strbuf_release(&sb);
@@ -2913,7 +2934,7 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
29132934
struct strbuf sb = STRBUF_INIT;
29142935
int save_errno = errno;
29152936

2916-
strbuf_git_path(&sb, "logs/%s", refname);
2937+
files_reflog_path(refs, &sb, refname);
29172938
strbuf_addf(err, "unable to append to '%s': %s",
29182939
sb.buf, strerror(save_errno));
29192940
strbuf_release(&sb);
@@ -2974,7 +2995,8 @@ static int commit_ref_update(struct files_ref_store *refs,
29742995
files_assert_main_repository(refs, "commit_ref_update");
29752996

29762997
clear_loose_ref_cache(refs);
2977-
if (files_log_ref_write(lock->ref_name, lock->old_oid.hash, sha1,
2998+
if (files_log_ref_write(refs, lock->ref_name,
2999+
lock->old_oid.hash, sha1,
29783000
logmsg, 0, err)) {
29793001
char *old_msg = strbuf_detach(err, NULL);
29803002
strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -3006,8 +3028,9 @@ static int commit_ref_update(struct files_ref_store *refs,
30063028
if (head_ref && (head_flag & REF_ISSYMREF) &&
30073029
!strcmp(head_ref, lock->ref_name)) {
30083030
struct strbuf log_err = STRBUF_INIT;
3009-
if (files_log_ref_write("HEAD", lock->old_oid.hash, sha1,
3010-
logmsg, 0, &log_err)) {
3031+
if (files_log_ref_write(refs, "HEAD",
3032+
lock->old_oid.hash, sha1,
3033+
logmsg, 0, &log_err)) {
30113034
error("%s", log_err.buf);
30123035
strbuf_release(&log_err);
30133036
}
@@ -3039,32 +3062,34 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
30393062
return ret;
30403063
}
30413064

3042-
static void update_symref_reflog(struct ref_lock *lock, const char *refname,
3065+
static void update_symref_reflog(struct files_ref_store *refs,
3066+
struct ref_lock *lock, const char *refname,
30433067
const char *target, const char *logmsg)
30443068
{
30453069
struct strbuf err = STRBUF_INIT;
30463070
unsigned char new_sha1[20];
30473071
if (logmsg && !read_ref(target, new_sha1) &&
3048-
files_log_ref_write(refname, lock->old_oid.hash, new_sha1,
3049-
logmsg, 0, &err)) {
3072+
files_log_ref_write(refs, refname, lock->old_oid.hash,
3073+
new_sha1, logmsg, 0, &err)) {
30503074
error("%s", err.buf);
30513075
strbuf_release(&err);
30523076
}
30533077
}
30543078

3055-
static int create_symref_locked(struct ref_lock *lock, const char *refname,
3079+
static int create_symref_locked(struct files_ref_store *refs,
3080+
struct ref_lock *lock, const char *refname,
30563081
const char *target, const char *logmsg)
30573082
{
30583083
if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
3059-
update_symref_reflog(lock, refname, target, logmsg);
3084+
update_symref_reflog(refs, lock, refname, target, logmsg);
30603085
return 0;
30613086
}
30623087

30633088
if (!fdopen_lock_file(lock->lk, "w"))
30643089
return error("unable to fdopen %s: %s",
30653090
lock->lk->tempfile.filename.buf, strerror(errno));
30663091

3067-
update_symref_reflog(lock, refname, target, logmsg);
3092+
update_symref_reflog(refs, lock, refname, target, logmsg);
30683093

30693094
/* no error check; commit_ref will check ferror */
30703095
fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
@@ -3093,13 +3118,20 @@ static int files_create_symref(struct ref_store *ref_store,
30933118
return -1;
30943119
}
30953120

3096-
ret = create_symref_locked(lock, refname, target, logmsg);
3121+
ret = create_symref_locked(refs, lock, refname, target, logmsg);
30973122
unlock_ref(lock);
30983123
return ret;
30993124
}
31003125

31013126
int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
31023127
{
3128+
/*
3129+
* FIXME: this obviously will not work well for future refs
3130+
* backends. This function needs to die.
3131+
*/
3132+
struct files_ref_store *refs =
3133+
files_downcast(get_ref_store(NULL), 0, "set_head_symref");
3134+
31033135
static struct lock_file head_lock;
31043136
struct ref_lock *lock;
31053137
struct strbuf head_path = STRBUF_INIT;
@@ -3126,7 +3158,7 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
31263158
lock->lk = &head_lock;
31273159
lock->ref_name = xstrdup(head_rel);
31283160

3129-
ret = create_symref_locked(lock, head_rel, target, logmsg);
3161+
ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
31303162

31313163
unlock_ref(lock); /* will free lock */
31323164
strbuf_release(&head_path);
@@ -3136,14 +3168,13 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
31363168
static int files_reflog_exists(struct ref_store *ref_store,
31373169
const char *refname)
31383170
{
3171+
struct files_ref_store *refs =
3172+
files_downcast(ref_store, 0, "reflog_exists");
31393173
struct strbuf sb = STRBUF_INIT;
31403174
struct stat st;
31413175
int ret;
31423176

3143-
/* Check validity (but we don't need the result): */
3144-
files_downcast(ref_store, 0, "reflog_exists");
3145-
3146-
strbuf_git_path(&sb, "logs/%s", refname);
3177+
files_reflog_path(refs, &sb, refname);
31473178
ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
31483179
strbuf_release(&sb);
31493180
return ret;
@@ -3152,13 +3183,12 @@ static int files_reflog_exists(struct ref_store *ref_store,
31523183
static int files_delete_reflog(struct ref_store *ref_store,
31533184
const char *refname)
31543185
{
3186+
struct files_ref_store *refs =
3187+
files_downcast(ref_store, 0, "delete_reflog");
31553188
struct strbuf sb = STRBUF_INIT;
31563189
int ret;
31573190

3158-
/* Check validity (but we don't need the result): */
3159-
files_downcast(ref_store, 0, "delete_reflog");
3160-
3161-
strbuf_git_path(&sb, "logs/%s", refname);
3191+
files_reflog_path(refs, &sb, refname);
31623192
ret = remove_path(sb.buf);
31633193
strbuf_release(&sb);
31643194
return ret;
@@ -3209,15 +3239,14 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
32093239
each_reflog_ent_fn fn,
32103240
void *cb_data)
32113241
{
3242+
struct files_ref_store *refs =
3243+
files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
32123244
struct strbuf sb = STRBUF_INIT;
32133245
FILE *logfp;
32143246
long pos;
32153247
int ret = 0, at_tail = 1;
32163248

3217-
/* Check validity (but we don't need the result): */
3218-
files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
3219-
3220-
strbuf_git_path(&sb, "logs/%s", refname);
3249+
files_reflog_path(refs, &sb, refname);
32213250
logfp = fopen(sb.buf, "r");
32223251
strbuf_release(&sb);
32233252
if (!logfp)
@@ -3318,14 +3347,13 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
33183347
const char *refname,
33193348
each_reflog_ent_fn fn, void *cb_data)
33203349
{
3350+
struct files_ref_store *refs =
3351+
files_downcast(ref_store, 0, "for_each_reflog_ent");
33213352
FILE *logfp;
33223353
struct strbuf sb = STRBUF_INIT;
33233354
int ret = 0;
33243355

3325-
/* Check validity (but we don't need the result): */
3326-
files_downcast(ref_store, 0, "for_each_reflog_ent");
3327-
3328-
strbuf_git_path(&sb, "logs/%s", refname);
3356+
files_reflog_path(refs, &sb, refname);
33293357
logfp = fopen(sb.buf, "r");
33303358
strbuf_release(&sb);
33313359
if (!logfp)
@@ -3407,15 +3435,14 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
34073435

34083436
static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
34093437
{
3438+
struct files_ref_store *refs =
3439+
files_downcast(ref_store, 0, "reflog_iterator_begin");
34103440
struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
34113441
struct ref_iterator *ref_iterator = &iter->base;
34123442
struct strbuf sb = STRBUF_INIT;
34133443

3414-
/* Check validity (but we don't need the result): */
3415-
files_downcast(ref_store, 0, "reflog_iterator_begin");
3416-
34173444
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
3418-
strbuf_git_path(&sb, "logs");
3445+
files_reflog_path(refs, &sb, NULL);
34193446
iter->dir_iterator = dir_iterator_begin(sb.buf);
34203447
strbuf_release(&sb);
34213448
return ref_iterator;
@@ -3840,7 +3867,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
38403867

38413868
if (update->flags & REF_NEEDS_COMMIT ||
38423869
update->flags & REF_LOG_ONLY) {
3843-
if (files_log_ref_write(lock->ref_name,
3870+
if (files_log_ref_write(refs,
3871+
lock->ref_name,
38443872
lock->old_oid.hash,
38453873
update->new_sha1,
38463874
update->msg, update->flags,
@@ -3900,9 +3928,9 @@ static int files_transaction_commit(struct ref_store *ref_store,
39003928
/* Delete the reflogs of any references that were deleted: */
39013929
for_each_string_list_item(ref_to_delete, &refs_to_delete) {
39023930
strbuf_reset(&sb);
3903-
strbuf_git_path(&sb, "logs/%s", ref_to_delete->string);
3931+
files_reflog_path(refs, &sb, ref_to_delete->string);
39043932
if (!unlink_or_warn(sb.buf))
3905-
try_remove_empty_parents(ref_to_delete->string,
3933+
try_remove_empty_parents(refs, ref_to_delete->string,
39063934
REMOVE_EMPTY_PARENTS_REFLOG);
39073935
}
39083936

@@ -3926,7 +3954,7 @@ static int files_transaction_commit(struct ref_store *ref_store,
39263954
* can only work because we have already
39273955
* removed the lockfile.)
39283956
*/
3929-
try_remove_empty_parents(update->refname,
3957+
try_remove_empty_parents(refs, update->refname,
39303958
REMOVE_EMPTY_PARENTS_REF);
39313959
}
39323960
}
@@ -4077,6 +4105,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
40774105
static struct lock_file reflog_lock;
40784106
struct expire_reflog_cb cb;
40794107
struct ref_lock *lock;
4108+
struct strbuf log_file_sb = STRBUF_INIT;
40804109
char *log_file;
40814110
int status = 0;
40824111
int type;
@@ -4105,7 +4134,8 @@ static int files_reflog_expire(struct ref_store *ref_store,
41054134
return 0;
41064135
}
41074136

4108-
log_file = git_pathdup("logs/%s", refname);
4137+
files_reflog_path(refs, &log_file_sb, refname);
4138+
log_file = strbuf_detach(&log_file_sb, NULL);
41094139
if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
41104140
/*
41114141
* Even though holding $GIT_DIR/logs/$reflog.lock has

0 commit comments

Comments
 (0)