Skip to content

Commit 944d858

Browse files
author
Linus Torvalds
committed
Fix up "for_each_ref()" to be more usable, and use it in git-fsck-cache
It needed to take the GIT_DIR information into account, something that the original receive-pack usage just never cared about.
1 parent 7ec4e60 commit 944d858

File tree

4 files changed

+18
-63
lines changed

4 files changed

+18
-63
lines changed

fsck-cache.c

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "tree.h"
77
#include "blob.h"
88
#include "tag.h"
9+
#include "refs.h"
910
#include "pack.h"
1011

1112
#define REACHABLE 0x0001
@@ -303,76 +304,30 @@ static int fsck_dir(int i, char *path)
303304
return 0;
304305
}
305306

306-
static int read_sha1_reference(const char *path)
307+
static int default_refs = 0;
308+
309+
static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
307310
{
308-
char hexname[60];
309-
unsigned char sha1[20];
310-
int fd = open(path, O_RDONLY), len;
311311
struct object *obj;
312312

313-
if (fd < 0)
314-
return -1;
315-
316-
len = read(fd, hexname, sizeof(hexname));
317-
close(fd);
318-
if (len < 40)
319-
return -1;
320-
321-
if (get_sha1_hex(hexname, sha1) < 0)
322-
return -1;
323-
324313
obj = lookup_object(sha1);
325314
if (!obj) {
326315
if (!standalone && has_sha1_file(sha1))
327-
return 0; /* it is in pack */
328-
return error("%s: invalid sha1 pointer %.40s", path, hexname);
316+
return 0; /* it is in a pack */
317+
error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
318+
/* We'll continue with the rest despite the error.. */
319+
return 0;
329320
}
330-
321+
default_refs++;
331322
obj->used = 1;
332323
mark_reachable(obj, REACHABLE);
333324
return 0;
334325
}
335326

336-
static int find_file_objects(const char *base, const char *name)
337-
{
338-
int baselen = strlen(base);
339-
int namelen = strlen(name);
340-
char *path = xmalloc(baselen + namelen + 2);
341-
struct stat st;
342-
343-
memcpy(path, base, baselen);
344-
path[baselen] = '/';
345-
memcpy(path + baselen + 1, name, namelen+1);
346-
if (stat(path, &st) < 0)
347-
return 0;
348-
349-
/*
350-
* Recurse into directories
351-
*/
352-
if (S_ISDIR(st.st_mode)) {
353-
int count = 0;
354-
DIR *dir = opendir(path);
355-
if (dir) {
356-
struct dirent *de;
357-
while ((de = readdir(dir)) != NULL) {
358-
if (de->d_name[0] == '.')
359-
continue;
360-
count += find_file_objects(path, de->d_name);
361-
}
362-
closedir(dir);
363-
}
364-
return count;
365-
}
366-
if (S_ISREG(st.st_mode))
367-
return read_sha1_reference(path) == 0;
368-
return 0;
369-
}
370-
371327
static void get_default_heads(void)
372328
{
373-
char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
374-
int count = find_file_objects(git_dir, "refs");
375-
if (!count)
329+
for_each_ref(fsck_handle_ref);
330+
if (!default_refs)
376331
die("No default references");
377332
}
378333

receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
77

88
static const char *unpacker = "git-unpack-objects";
99

10-
static int show_ref(const char *path, unsigned char *sha1)
10+
static int show_ref(const char *path, const unsigned char *sha1)
1111
{
1212
packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
1313
return 0;

refs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static int read_ref(const char *path, unsigned char *sha1)
1717
return ret;
1818
}
1919

20-
static int do_for_each_ref(const char *base, int (*fn)(const char *path, unsigned char *sha1))
20+
static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
2121
{
2222
int retval = 0;
2323
DIR *dir = opendir(base);
@@ -27,6 +27,8 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, unsigne
2727
int baselen = strlen(base);
2828
char *path = xmalloc(baselen + 257);
2929
memcpy(path, base, baselen);
30+
if (baselen && base[baselen-1] != '/')
31+
path[baselen++] = '/';
3032

3133
while ((de = readdir(dir)) != NULL) {
3234
unsigned char sha1[20];
@@ -42,8 +44,6 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, unsigne
4244
if (lstat(path, &st) < 0)
4345
continue;
4446
if (S_ISDIR(st.st_mode)) {
45-
path[baselen + namelen] = '/';
46-
path[baselen + namelen + 1] = 0;
4747
retval = do_for_each_ref(path, fn);
4848
if (retval)
4949
break;
@@ -63,9 +63,9 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, unsigne
6363
return retval;
6464
}
6565

66-
int for_each_ref(int (*fn)(const char *path, unsigned char *sha1))
66+
int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
6767
{
68-
return do_for_each_ref("refs/", fn);
68+
return do_for_each_ref(get_refs_directory(), fn);
6969
}
7070

7171
static char *ref_file_name(const char *ref)

refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Calls the specified function for each ref file until it returns nonzero,
66
* and returns the value
77
*/
8-
extern int for_each_ref(int (*fn)(const char *path, unsigned char *sha1));
8+
extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
99

1010
/** Reads the refs file specified into sha1 **/
1111
extern int get_ref_sha1(const char *ref, unsigned char *sha1);

0 commit comments

Comments
 (0)