Skip to content

Commit 4da5883

Browse files
rsahlberggitster
authored andcommitted
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 <[email protected]> Acked-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1dc51c6 commit 4da5883

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

builtin/checkout.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,11 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
651651
}
652652
}
653653
if (old->path && old->name) {
654-
char log_file[PATH_MAX], ref_file[PATH_MAX];
654+
char ref_file[PATH_MAX];
655655

656-
git_snpath(log_file, sizeof(log_file), "logs/%s", old->path);
657656
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);
660659
}
661660
}
662661
remove_branch_state();

builtin/reflog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
369369
if (!lock)
370370
return error("cannot lock ref '%s'", ref);
371371
log_file = git_pathdup("logs/%s", ref);
372-
if (!file_exists(log_file))
372+
if (!reflog_exists(ref))
373373
goto finish;
374374
if (!cmd->dry_run) {
375375
newlog_path = git_pathdup("logs/%s.lock", ref);

refs.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,7 +1999,6 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
19991999

20002000
*log = NULL;
20012001
for (p = ref_rev_parse_rules; *p; p++) {
2002-
struct stat st;
20032002
unsigned char hash[20];
20042003
char path[PATH_MAX];
20052004
const char *ref, *it;
@@ -2008,12 +2007,9 @@ int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
20082007
ref = resolve_ref_unsafe(path, hash, 1, NULL);
20092008
if (!ref)
20102009
continue;
2011-
if (!stat(git_path("logs/%s", path), &st) &&
2012-
S_ISREG(st.st_mode))
2010+
if (reflog_exists(path))
20132011
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))
20172013
it = ref;
20182014
else
20192015
continue;
@@ -3046,6 +3042,19 @@ int read_ref_at(const char *refname, unsigned long at_time, int cnt,
30463042
return 1;
30473043
}
30483044

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+
30493058
static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
30503059
{
30513060
unsigned char osha1[20], nsha1[20];

refs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
173173
unsigned char *sha1, char **msg,
174174
unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
175175

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+
176182
/* iterate over reflog entries */
177183
typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
178184
int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);

0 commit comments

Comments
 (0)