scan reflogs independently from refs
Currently, the search for all reflogs depends on the existence of corresponding refs under the .git/refs/ directory. Let's scan the .git/logs/ directory directly instead. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Feb 3, 2007 at 13:25 UTC
eb8381c88518b10d683a29deea1d43ed671f14ec
5 files changed
+67
-9
builtin-reflog.c
+2
-5
@@ -245,14 +245,11 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
245
char *log_file, *newlog_path = NULL;
246
int status = 0;
247
248
- if (strncmp(ref, "refs/", 5))
249
- return error("not a ref '%s'", ref);
250
-
248
memset(&cb, 0, sizeof(cb));
249
/* we take the lock for the ref itself to prevent it from
250
* getting updated.
251
*/
255
- lock = lock_ref_sha1(ref + 5, sha1);
252
+ lock = lock_any_ref_for_update(ref, sha1);
253
if (!lock)
254
return error("cannot lock ref '%s'", ref);
255
log_file = xstrdup(git_path("logs/%s", ref));
@@ -353,7 +350,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
350
}
351
352
if (do_all)
356
- status |= for_each_ref(expire_reflog, &cb);
353
+ status |= for_each_reflog(expire_reflog, &cb);
354
while (i < argc) {
355
const char *ref = argv[i++];
356
unsigned char sha1[20];
fsck-objects.c
+7
-2
@@ -477,6 +477,12 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
477
return 0;
478
}
479
480
+static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
481
+{
482
+ for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
483
+ return 0;
484
+}
485
+
486
static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
487
{
488
struct object *obj;
@@ -495,14 +501,13 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
501
obj->used = 1;
502
mark_reachable(obj, REACHABLE);
503
498
- for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL);
499
-
504
return 0;
505
}
506
507
static void get_default_heads(void)
508
{
509
for_each_ref(fsck_handle_ref, NULL);
510
+ for_each_reflog(fsck_handle_reflog, NULL);
511
512
/*
513
* Not having any default heads isn't really fatal, but
reachable.c
+2
-2
@@ -188,9 +188,9 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog)
188
/* Add all external refs */
189
for_each_ref(add_one_ref, revs);
190
191
- /* Add all reflog info from refs */
191
+ /* Add all reflog info */
192
if (mark_reflog)
193
- for_each_ref(add_one_reflog, revs);
193
+ for_each_reflog(add_one_reflog, revs);
194
195
/*
196
* Set up the revision walk - this will move all commits
refs.c
+50
@@ -1201,3 +1201,53 @@ int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data)
1201
return ret;
1202
}
1203
1204
+static int do_for_each_reflog(const char *base, each_ref_fn fn, void *cb_data)
1205
+{
1206
+ DIR *dir = opendir(git_path("logs/%s", base));
1207
+ int retval = errno;
1208
+
1209
+ if (dir) {
1210
+ struct dirent *de;
1211
+ int baselen = strlen(base);
1212
+ char *log = xmalloc(baselen + 257);
1213
+
1214
+ memcpy(log, base, baselen);
1215
+ if (baselen && base[baselen-1] != '/')
1216
+ log[baselen++] = '/';
1217
+
1218
+ while ((de = readdir(dir)) != NULL) {
1219
+ struct stat st;
1220
+ int namelen;
1221
+
1222
+ if (de->d_name[0] == '.')
1223
+ continue;
1224
+ namelen = strlen(de->d_name);
1225
+ if (namelen > 255)
1226
+ continue;
1227
+ if (has_extension(de->d_name, ".lock"))
1228
+ continue;
1229
+ memcpy(log + baselen, de->d_name, namelen+1);
1230
+ if (stat(git_path("logs/%s", log), &st) < 0)
1231
+ continue;
1232
+ if (S_ISDIR(st.st_mode)) {
1233
+ retval = do_for_each_reflog(log, fn, cb_data);
1234
+ } else {
1235
+ unsigned char sha1[20];
1236
+ if (!resolve_ref(log, sha1, 0, NULL))
1237
+ retval = error("bad ref for %s", log);
1238
+ else
1239
+ retval = fn(log, sha1, 0, cb_data);
1240
+ }
1241
+ if (retval)
1242
+ break;
1243
+ }
1244
+ free(log);
1245
+ closedir(dir);
1246
+ }
1247
+ return retval;
1248
+}
1249
+
1250
+int for_each_reflog(each_ref_fn fn, void *cb_data)
1251
+{
1252
+ return do_for_each_reflog("", fn, cb_data);
1253
+}
refs.h
+6
@@ -48,6 +48,12 @@ extern int read_ref_at(const char *ref, unsigned long at_time, int cnt, unsigned
48
typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
49
int for_each_reflog_ent(const char *ref, each_reflog_ent_fn fn, void *cb_data);
50
51
+/*
52
+ * Calls the specified function for each reflog file until it returns nonzero,
53
+ * and returns the value
54
+ */
55
+extern int for_each_reflog(each_ref_fn, void *);
56
+
57
/** Returns 0 if target has the right format for a ref. **/
58
extern int check_ref_format(const char *target);
59