Skip to content

Commit f7f349e

Browse files
committed
Merge branch 'rs/reflog-exists'
* rs/reflog-exists: checkout.c: use ref_exists instead of file_exist refs.c: add new functions reflog_exists and delete_reflog
2 parents 43eb7cb + 482b8f3 commit f7f349e

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

builtin/checkout.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,12 +651,8 @@ 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];
655-
656-
git_snpath(log_file, sizeof(log_file), "logs/%s", old->path);
657-
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);
654+
if (!ref_exists(old->path) && reflog_exists(old->path))
655+
delete_reflog(old->path);
660656
}
661657
}
662658
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
@@ -161,6 +161,12 @@ extern int read_ref_at(const char *refname, unsigned long at_time, int cnt,
161161
unsigned char *sha1, char **msg,
162162
unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt);
163163

164+
/** Check if a particular reflog exists */
165+
extern int reflog_exists(const char *refname);
166+
167+
/** Delete a reflog */
168+
extern int delete_reflog(const char *refname);
169+
164170
/* iterate over reflog entries */
165171
typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
166172
int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);

t/t1410-reflog.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,12 @@ test_expect_success 'gc.reflogexpire=false' '
245245
246246
'
247247

248+
test_expect_success 'checkout should not delete log for packed ref' '
249+
test $(git reflog master | wc -l) = 4 &&
250+
git branch foo &&
251+
git pack-refs --all &&
252+
git checkout foo &&
253+
test $(git reflog master | wc -l) = 4
254+
'
255+
248256
test_done

0 commit comments

Comments
 (0)