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>

Nguyễn Thái Ngọc Duy committed Mar 26, 2017 at 09:42 UTC 802de3da0752f3c3aa40e8089f27797857009392
1 file changed +86 -56
refs/files-backend.c
+86 -56
@@ -165,7 +165,8 @@ static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
165 const char *dirname, size_t len,
166 int incomplete);
167 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,
170 const unsigned char *new_sha1, const char *msg,
171 int flags, struct strbuf *err);
172
@@ -1167,6 +1168,18 @@ static const char *files_packed_refs_path(struct files_ref_store *refs)
1168 return refs->packed_refs_path;
1169 }
1170
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 +
1183 /*
1184 * Get the packed_ref_cache for the specified files_ref_store,
1185 * creating it if necessary.
@@ -2313,7 +2326,9 @@ enum {
2326 * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
2327 * REMOVE_EMPTY_PARENTS_REFLOG.
2328 */
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)
2332 {
2333 struct strbuf buf = STRBUF_INIT;
2334 struct strbuf sb = STRBUF_INIT;
@@ -2345,7 +2360,7 @@ static void try_remove_empty_parents(const char *refname, unsigned int flags)
2360 flags &= ~REMOVE_EMPTY_PARENTS_REF;
2361
2362 strbuf_reset(&sb);
2348 - strbuf_git_path(&sb, "logs/%s", buf.buf);
2363 + files_reflog_path(refs, &sb, buf.buf);
2364 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
2365 flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
2366 }
@@ -2538,15 +2553,15 @@ static int rename_tmp_log_callback(const char *path, void *cb_data)
2553 }
2554 }
2555
2541 -static int rename_tmp_log(const char *newrefname)
2556 +static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
2557 {
2558 struct strbuf path = STRBUF_INIT;
2559 struct strbuf tmp = STRBUF_INIT;
2560 struct rename_cb cb;
2561 int ret;
2562
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);
2565 cb.tmp_renamed_log = tmp.buf;
2566 ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
2567 if (ret) {
@@ -2606,9 +2621,9 @@ static int files_rename_ref(struct ref_store *ref_store,
2621 int log, ret;
2622 struct strbuf err = STRBUF_INIT;
2623
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);
2627
2628 log = !lstat(sb_oldref.buf, &loginfo);
2629 if (log && S_ISLNK(loginfo.st_mode)) {
@@ -2671,7 +2686,7 @@ static int files_rename_ref(struct ref_store *ref_store,
2686 }
2687 }
2688
2674 - if (log && rename_tmp_log(newrefname))
2689 + if (log && rename_tmp_log(refs, newrefname))
2690 goto rollback;
2691
2692 logmoved = log;
@@ -2786,10 +2801,15 @@ static int open_or_create_logfile(const char *path, void *cb)
2801 * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
2802 * return -1.
2803 */
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,
2806 int *logfd, struct strbuf *err)
2807 {
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);
2813
2814 if (force_create || should_autocreate_reflog(refname)) {
2815 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
@@ -2839,12 +2859,11 @@ static int files_create_reflog(struct ref_store *ref_store,
2859 const char *refname, int force_create,
2860 struct strbuf *err)
2861 {
2862 + struct files_ref_store *refs =
2863 + files_downcast(ref_store, 0, "create_reflog");
2864 int fd;
2865
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))
2867 return -1;
2868
2869 if (fd >= 0)
@@ -2879,7 +2898,8 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2898 return 0;
2899 }
2900
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,
2903 const unsigned char *new_sha1, const char *msg,
2904 int flags, struct strbuf *err)
2905 {
@@ -2888,7 +2908,8 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
2908 if (log_all_ref_updates == LOG_REFS_UNSET)
2909 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
2910
2891 - result = log_ref_setup(refname, flags & REF_FORCE_CREATE_REFLOG,
2911 + result = log_ref_setup(refs, refname,
2912 + flags & REF_FORCE_CREATE_REFLOG,
2913 &logfd, err);
2914
2915 if (result)
@@ -2902,7 +2923,7 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
2923 struct strbuf sb = STRBUF_INIT;
2924 int save_errno = errno;
2925
2905 - strbuf_git_path(&sb, "logs/%s", refname);
2926 + files_reflog_path(refs, &sb, refname);
2927 strbuf_addf(err, "unable to append to '%s': %s",
2928 sb.buf, strerror(save_errno));
2929 strbuf_release(&sb);
@@ -2913,7 +2934,7 @@ static int files_log_ref_write(const char *refname, const unsigned char *old_sha
2934 struct strbuf sb = STRBUF_INIT;
2935 int save_errno = errno;
2936
2916 - strbuf_git_path(&sb, "logs/%s", refname);
2937 + files_reflog_path(refs, &sb, refname);
2938 strbuf_addf(err, "unable to append to '%s': %s",
2939 sb.buf, strerror(save_errno));
2940 strbuf_release(&sb);
@@ -2974,7 +2995,8 @@ static int commit_ref_update(struct files_ref_store *refs,
2995 files_assert_main_repository(refs, "commit_ref_update");
2996
2997 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,
3000 logmsg, 0, err)) {
3001 char *old_msg = strbuf_detach(err, NULL);
3002 strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -3006,8 +3028,9 @@ static int commit_ref_update(struct files_ref_store *refs,
3028 if (head_ref && (head_flag & REF_ISSYMREF) &&
3029 !strcmp(head_ref, lock->ref_name)) {
3030 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)) {
3034 error("%s", log_err.buf);
3035 strbuf_release(&log_err);
3036 }
@@ -3039,24 +3062,26 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target)
3062 return ret;
3063 }
3064
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,
3067 const char *target, const char *logmsg)
3068 {
3069 struct strbuf err = STRBUF_INIT;
3070 unsigned char new_sha1[20];
3071 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)) {
3074 error("%s", err.buf);
3075 strbuf_release(&err);
3076 }
3077 }
3078
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,
3081 const char *target, const char *logmsg)
3082 {
3083 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);
3085 return 0;
3086 }
3087
@@ -3064,7 +3089,7 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,
3089 return error("unable to fdopen %s: %s",
3090 lock->lk->tempfile.filename.buf, strerror(errno));
3091
3067 - update_symref_reflog(lock, refname, target, logmsg);
3092 + update_symref_reflog(refs, lock, refname, target, logmsg);
3093
3094 /* no error check; commit_ref will check ferror */
3095 fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
@@ -3093,13 +3118,20 @@ static int files_create_symref(struct ref_store *ref_store,
3118 return -1;
3119 }
3120
3096 - ret = create_symref_locked(lock, refname, target, logmsg);
3121 + ret = create_symref_locked(refs, lock, refname, target, logmsg);
3122 unlock_ref(lock);
3123 return ret;
3124 }
3125
3126 int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
3127 {
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 +
3135 static struct lock_file head_lock;
3136 struct ref_lock *lock;
3137 struct strbuf head_path = STRBUF_INIT;
@@ -3126,7 +3158,7 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
3158 lock->lk = &head_lock;
3159 lock->ref_name = xstrdup(head_rel);
3160
3129 - ret = create_symref_locked(lock, head_rel, target, logmsg);
3161 + ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
3162
3163 unlock_ref(lock); /* will free lock */
3164 strbuf_release(&head_path);
@@ -3136,14 +3168,13 @@ int set_worktree_head_symref(const char *gitdir, const char *target, const char
3168 static int files_reflog_exists(struct ref_store *ref_store,
3169 const char *refname)
3170 {
3171 + struct files_ref_store *refs =
3172 + files_downcast(ref_store, 0, "reflog_exists");
3173 struct strbuf sb = STRBUF_INIT;
3174 struct stat st;
3175 int ret;
3176
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);
3178 ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
3179 strbuf_release(&sb);
3180 return ret;
@@ -3152,13 +3183,12 @@ static int files_reflog_exists(struct ref_store *ref_store,
3183 static int files_delete_reflog(struct ref_store *ref_store,
3184 const char *refname)
3185 {
3186 + struct files_ref_store *refs =
3187 + files_downcast(ref_store, 0, "delete_reflog");
3188 struct strbuf sb = STRBUF_INIT;
3189 int ret;
3190
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);
3192 ret = remove_path(sb.buf);
3193 strbuf_release(&sb);
3194 return ret;
@@ -3209,15 +3239,14 @@ static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
3239 each_reflog_ent_fn fn,
3240 void *cb_data)
3241 {
3242 + struct files_ref_store *refs =
3243 + files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
3244 struct strbuf sb = STRBUF_INIT;
3245 FILE *logfp;
3246 long pos;
3247 int ret = 0, at_tail = 1;
3248
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);
3250 logfp = fopen(sb.buf, "r");
3251 strbuf_release(&sb);
3252 if (!logfp)
@@ -3318,14 +3347,13 @@ static int files_for_each_reflog_ent(struct ref_store *ref_store,
3347 const char *refname,
3348 each_reflog_ent_fn fn, void *cb_data)
3349 {
3350 + struct files_ref_store *refs =
3351 + files_downcast(ref_store, 0, "for_each_reflog_ent");
3352 FILE *logfp;
3353 struct strbuf sb = STRBUF_INIT;
3354 int ret = 0;
3355
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);
3357 logfp = fopen(sb.buf, "r");
3358 strbuf_release(&sb);
3359 if (!logfp)
@@ -3407,15 +3435,14 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
3435
3436 static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
3437 {
3438 + struct files_ref_store *refs =
3439 + files_downcast(ref_store, 0, "reflog_iterator_begin");
3440 struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
3441 struct ref_iterator *ref_iterator = &iter->base;
3442 struct strbuf sb = STRBUF_INIT;
3443
3414 - /* Check validity (but we don't need the result): */
3415 - files_downcast(ref_store, 0, "reflog_iterator_begin");
3416 -
3444 base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
3418 - strbuf_git_path(&sb, "logs");
3445 + files_reflog_path(refs, &sb, NULL);
3446 iter->dir_iterator = dir_iterator_begin(sb.buf);
3447 strbuf_release(&sb);
3448 return ref_iterator;
@@ -3840,7 +3867,8 @@ static int files_transaction_commit(struct ref_store *ref_store,
3867
3868 if (update->flags & REF_NEEDS_COMMIT ||
3869 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,
3872 lock->old_oid.hash,
3873 update->new_sha1,
3874 update->msg, update->flags,
@@ -3900,9 +3928,9 @@ static int files_transaction_commit(struct ref_store *ref_store,
3928 /* Delete the reflogs of any references that were deleted: */
3929 for_each_string_list_item(ref_to_delete, &refs_to_delete) {
3930 strbuf_reset(&sb);
3903 - strbuf_git_path(&sb, "logs/%s", ref_to_delete->string);
3931 + files_reflog_path(refs, &sb, ref_to_delete->string);
3932 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,
3934 REMOVE_EMPTY_PARENTS_REFLOG);
3935 }
3936
@@ -3926,7 +3954,7 @@ cleanup:
3954 * can only work because we have already
3955 * removed the lockfile.)
3956 */
3929 - try_remove_empty_parents(update->refname,
3957 + try_remove_empty_parents(refs, update->refname,
3958 REMOVE_EMPTY_PARENTS_REF);
3959 }
3960 }
@@ -4077,6 +4105,7 @@ static int files_reflog_expire(struct ref_store *ref_store,
4105 static struct lock_file reflog_lock;
4106 struct expire_reflog_cb cb;
4107 struct ref_lock *lock;
4108 + struct strbuf log_file_sb = STRBUF_INIT;
4109 char *log_file;
4110 int status = 0;
4111 int type;
@@ -4105,7 +4134,8 @@ static int files_reflog_expire(struct ref_store *ref_store,
4134 return 0;
4135 }
4136
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);
4139 if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
4140 /*
4141 * Even though holding $GIT_DIR/logs/$reflog.lock has