refs.c: add new functions reflog_exists and delete_reflog

Add two new functions, reflog_exists and delete_reflog, to hide the internal reflog implementation (that they are files under .git/logs/...) from callers. Update checkout.c to use these functions in update_refs_for_switch instead of building pathnames and calling out to file access functions. Update reflog.c to use these to check if the reflog exists. Now there are still many places in reflog.c where we are still leaking the reflog storage implementation but this at least reduces the number of such dependencies by one. Finally change two places in refs.c itself to use the new function to check if a ref exists or not isntead of build-path-and-stat(). Now, this is strictly not all that important since these are in parts of refs that are implementing the actual file storage backend but on the other hand it will not hurt either. Signed-off-by: Ronnie Sahlberg <sahlberg@google.com> Acked-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ronnie Sahlberg committed May 6, 2014 at 15:45 UTC 4da588357a4a8b73f6a8d9c24435dabee74d0a7e
4 files changed +25 -11
builtin/checkout.c
+3 -4
@@ -651,12 +651,11 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
651 }
652 }
653 if (old->path && old->name) {
654 - char log_file[PATH_MAX], ref_file[PATH_MAX];
654 + char ref_file[PATH_MAX];
655
656 - git_snpath(log_file, sizeof(log_file), "logs/%s", old->path);
656 git_snpath(ref_file, sizeof(ref_file), "%s", old->path);
658 - if (!file_exists(ref_file) && file_exists(log_file))
659 - remove_path(log_file);
657 + if (!file_exists(ref_file) && reflog_exists(old->path))
658 + delete_reflog(old->path);
659 }
660 }
661 remove_branch_state();
builtin/reflog.c
+1 -1
@@ -369,7 +369,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
369 if (!lock)
370 return error("cannot lock ref '%s'", ref);
371 log_file = git_pathdup("logs/%s", ref);
372 - if (!file_exists(log_file))
372 + if (!reflog_exists(ref))
373 goto finish;
374 if (!cmd->dry_run) {
375 newlog_path = git_pathdup("logs/%s.lock", ref);
refs.c
+15 -6
@@ -1999,7 +1999,6 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
1999
2000 *log = NULL;
2001 for (p = ref_rev_parse_rules; *p; p++) {
2002 - struct stat st;
2002 unsigned char hash[20];
2003 char path[PATH_MAX];
2004 const char *ref, *it;
@@ -2008,12 +2007,9 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
2007 ref = resolve_ref_unsafe(path, hash, 1, NULL);
2008 if (!ref)
2009 continue;
2011 - if (!stat(git_path("logs/%s", path), &st) &&
2012 - S_ISREG(st.st_mode))
2010 + if (reflog_exists(path))
2011 it = path;
2014 - else if (strcmp(ref, path) &&
2015 - !stat(git_path("logs/%s", ref), &st) &&
2016 - S_ISREG(st.st_mode))
2012 + else if (strcmp(ref, path) && reflog_exists(ref))
2013 it = ref;
2014 else
2015 continue;
@@ -3046,6 +3042,19 @@ int read_ref_at(const char *refname, unsigned long at_time, int cnt,
3042 return 1;
3043 }
3044
3045 +int reflog_exists(const char *refname)
3046 +{
3047 + struct stat st;
3048 +
3049 + return !lstat(git_path("logs/%s", refname), &st) &&
3050 + S_ISREG(st.st_mode);
3051 +}
3052 +
3053 +int delete_reflog(const char *refname)
3054 +{
3055 + return remove_path(git_path("logs/%s", refname));
3056 +}
3057 +
3058 static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
3059 {
3060 unsigned char osha1[20], nsha1[20];
refs.h
+6
@@ -173,6 +173,12 @@ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
173 unsigned char *sha1, char **msg,
174 unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
175
176 +/** Check if a particular reflog exists */
177 +extern int reflog_exists(const char *refname);
178 +
179 +/** Delete a reflog */
180 +extern int delete_reflog(const char *refname);
181 +
182 /* iterate over reflog entries */
183 typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
184 int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);