@matheusbsbs / matheus-bagliano / commits / e1e22e37f4
Fork 0

Start handling references internally as a sorted in-memory list

This also adds some very rudimentary support for the notion of packed refs. HOWEVER! At this point it isn't used to actually look up a ref yet, only for listing them (ie "for_each_ref()" and friends see the packed refs, but none of the other single-ref lookup routines). Note how we keep two separate lists: one for the loose refs, and one for the packed refs we read. That's so that we can easily keep the two apart, and read only one set or the other (and still always make sure that the loose refs take precedence). [ From this, it's not actually obvious why we'd keep the two separate lists, but it's important to have the packed refs on their own list later on, when I add support for looking up a single loose one. For that case, we will want to read _just_ the packed refs in case the single-ref lookup fails, yet we may end up needing the other list at some point in the future, so keeping them separated is important ] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Sep 11, 2006 at 16:37 UTC e1e22e37f47e3f4d741d28920e1d27e3775c31ad
5 files changed +218 -54
Makefile
+2 -1
@@ -301,7 +301,8 @@ BUILTIN_OBJS = \
301 builtin-upload-tar.o \
302 builtin-verify-pack.o \
303 builtin-write-tree.o \
304 - builtin-zip-tree.o
304 + builtin-zip-tree.o \
305 + builtin-pack-refs.o
306
307 GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
308 LIBS = $(GITLIBS) -lz
builtin-pack-refs.c new
+41
@@ -0,0 +1,41 @@
1 +#include "cache.h"
2 +#include "refs.h"
3 +
4 +static FILE *refs_file;
5 +static const char *result_path, *lock_path;
6 +
7 +static void remove_lock_file(void)
8 +{
9 + if (lock_path)
10 + unlink(lock_path);
11 +}
12 +
13 +static int handle_one_ref(const char *path, const unsigned char *sha1)
14 +{
15 + fprintf(refs_file, "%s %s\n", sha1_to_hex(sha1), path);
16 + return 0;
17 +}
18 +
19 +int cmd_pack_refs(int argc, const char **argv, const char *prefix)
20 +{
21 + int fd;
22 +
23 + result_path = xstrdup(git_path("packed-refs"));
24 + lock_path = xstrdup(mkpath("%s.lock", result_path));
25 +
26 + fd = open(lock_path, O_CREAT | O_EXCL | O_WRONLY, 0666);
27 + if (fd < 0)
28 + die("unable to create new ref-pack file (%s)", strerror(errno));
29 + atexit(remove_lock_file);
30 +
31 + refs_file = fdopen(fd, "w");
32 + if (!refs_file)
33 + die("unable to create ref-pack file structure (%s)", strerror(errno));
34 + for_each_ref(handle_one_ref);
35 + fsync(fd);
36 + fclose(refs_file);
37 + if (rename(lock_path, result_path) < 0)
38 + die("unable to overwrite old ref-pack file (%s)", strerror(errno));
39 + lock_path = NULL;
40 + return 0;
41 +}
builtin.h
+1
@@ -63,5 +63,6 @@ extern int cmd_version(int argc, const char **argv, const char *prefix);
63 extern int cmd_whatchanged(int argc, const char **argv, const char *prefix);
64 extern int cmd_write_tree(int argc, const char **argv, const char *prefix);
65 extern int cmd_verify_pack(int argc, const char **argv, const char *prefix);
66 +extern int cmd_pack_refs(int argc, const char **argv, const char *prefix);
67
68 #endif
git.c
+1
@@ -269,6 +269,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
269 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
270 { "write-tree", cmd_write_tree, RUN_SETUP },
271 { "verify-pack", cmd_verify_pack },
272 + { "pack-refs", cmd_pack_refs, RUN_SETUP },
273 };
274 int i;
275
refs.c
+173 -53
@@ -3,6 +3,145 @@
3
4 #include <errno.h>
5
6 +struct ref_list {
7 + struct ref_list *next;
8 + unsigned char sha1[20];
9 + char name[FLEX_ARRAY];
10 +};
11 +
12 +static const char *parse_ref_line(char *line, unsigned char *sha1)
13 +{
14 + /*
15 + * 42: the answer to everything.
16 + *
17 + * In this case, it happens to be the answer to
18 + * 40 (length of sha1 hex representation)
19 + * +1 (space in between hex and name)
20 + * +1 (newline at the end of the line)
21 + */
22 + int len = strlen(line) - 42;
23 +
24 + if (len <= 0)
25 + return NULL;
26 + if (get_sha1_hex(line, sha1) < 0)
27 + return NULL;
28 + if (!isspace(line[40]))
29 + return NULL;
30 + line += 41;
31 + if (line[len] != '\n')
32 + return NULL;
33 + line[len] = 0;
34 + return line;
35 +}
36 +
37 +static struct ref_list *add_ref(const char *name, const unsigned char *sha1, struct ref_list *list)
38 +{
39 + int len;
40 + struct ref_list **p = &list, *entry;
41 +
42 + /* Find the place to insert the ref into.. */
43 + while ((entry = *p) != NULL) {
44 + int cmp = strcmp(entry->name, name);
45 + if (cmp > 0)
46 + break;
47 +
48 + /* Same as existing entry? */
49 + if (!cmp)
50 + return list;
51 + p = &entry->next;
52 + }
53 +
54 + /* Allocate it and add it in.. */
55 + len = strlen(name) + 1;
56 + entry = xmalloc(sizeof(struct ref_list) + len);
57 + hashcpy(entry->sha1, sha1);
58 + memcpy(entry->name, name, len);
59 + entry->next = *p;
60 + *p = entry;
61 + return list;
62 +}
63 +
64 +static struct ref_list *get_packed_refs(void)
65 +{
66 + static int did_refs = 0;
67 + static struct ref_list *refs = NULL;
68 +
69 + if (!did_refs) {
70 + FILE *f = fopen(git_path("packed-refs"), "r");
71 + if (f) {
72 + struct ref_list *list = NULL;
73 + char refline[PATH_MAX];
74 + while (fgets(refline, sizeof(refline), f)) {
75 + unsigned char sha1[20];
76 + const char *name = parse_ref_line(refline, sha1);
77 + if (!name)
78 + continue;
79 + list = add_ref(name, sha1, list);
80 + }
81 + fclose(f);
82 + refs = list;
83 + }
84 + did_refs = 1;
85 + }
86 + return refs;
87 +}
88 +
89 +static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
90 +{
91 + DIR *dir = opendir(git_path("%s", base));
92 +
93 + if (dir) {
94 + struct dirent *de;
95 + int baselen = strlen(base);
96 + char *path = xmalloc(baselen + 257);
97 +
98 + memcpy(path, base, baselen);
99 + if (baselen && base[baselen-1] != '/')
100 + path[baselen++] = '/';
101 +
102 + while ((de = readdir(dir)) != NULL) {
103 + unsigned char sha1[20];
104 + struct stat st;
105 + int namelen;
106 +
107 + if (de->d_name[0] == '.')
108 + continue;
109 + namelen = strlen(de->d_name);
110 + if (namelen > 255)
111 + continue;
112 + if (has_extension(de->d_name, ".lock"))
113 + continue;
114 + memcpy(path + baselen, de->d_name, namelen+1);
115 + if (stat(git_path("%s", path), &st) < 0)
116 + continue;
117 + if (S_ISDIR(st.st_mode)) {
118 + list = get_ref_dir(path, list);
119 + continue;
120 + }
121 + if (read_ref(git_path("%s", path), sha1) < 0) {
122 + error("%s points nowhere!", path);
123 + continue;
124 + }
125 + list = add_ref(path, sha1, list);
126 + }
127 + free(path);
128 + closedir(dir);
129 + }
130 + return list;
131 +}
132 +
133 +static struct ref_list *get_loose_refs(void)
134 +{
135 + static int did_refs = 0;
136 + static struct ref_list *refs = NULL;
137 +
138 + if (!did_refs) {
139 + refs = get_ref_dir("refs", NULL);
140 + did_refs = 1;
141 + }
142 + return refs;
143 +}
144 +
145 /* We allow "recursive" symbolic refs. Only within reason, though */
146 #define MAXDEPTH 5
147
@@ -121,60 +260,41 @@ int read_ref(const char *filename, unsigned char *sha1)
260
261 static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
262 {
124 - int retval = 0;
125 - DIR *dir = opendir(git_path("%s", base));
126 -
127 - if (dir) {
128 - struct dirent *de;
129 - int baselen = strlen(base);
130 - char *path = xmalloc(baselen + 257);
131 -
132 - if (!strncmp(base, "./", 2)) {
133 - base += 2;
134 - baselen -= 2;
263 + int retval;
264 + struct ref_list *packed = get_packed_refs();
265 + struct ref_list *loose = get_loose_refs();
266 +
267 + while (packed && loose) {
268 + struct ref_list *entry;
269 + int cmp = strcmp(packed->name, loose->name);
270 + if (!cmp) {
271 + packed = packed->next;
272 + continue;
273 }
136 - memcpy(path, base, baselen);
137 - if (baselen && base[baselen-1] != '/')
138 - path[baselen++] = '/';
139 -
140 - while ((de = readdir(dir)) != NULL) {
141 - unsigned char sha1[20];
142 - struct stat st;
143 - int namelen;
274 + if (cmp > 0) {
275 + entry = loose;
276 + loose = loose->next;
277 + } else {
278 + entry = packed;
279 + packed = packed->next;
280 + }
281 + if (strncmp(base, entry->name, trim))
282 + continue;
283 + retval = fn(entry->name + trim, entry->sha1);
284 + if (retval)
285 + return retval;
286 + }
287
145 - if (de->d_name[0] == '.')
146 - continue;
147 - namelen = strlen(de->d_name);
148 - if (namelen > 255)
149 - continue;
150 - if (has_extension(de->d_name, ".lock"))
151 - continue;
152 - memcpy(path + baselen, de->d_name, namelen+1);
153 - if (stat(git_path("%s", path), &st) < 0)
154 - continue;
155 - if (S_ISDIR(st.st_mode)) {
156 - retval = do_for_each_ref(path, fn, trim);
157 - if (retval)
158 - break;
159 - continue;
160 - }
161 - if (read_ref(git_path("%s", path), sha1) < 0) {
162 - error("%s points nowhere!", path);
163 - continue;
164 - }
165 - if (!has_sha1_file(sha1)) {
166 - error("%s does not point to a valid "
167 - "commit object!", path);
168 - continue;
169 - }
170 - retval = fn(path + trim, sha1);
288 + packed = packed ? packed : loose;
289 + while (packed) {
290 + if (!strncmp(base, packed->name, trim)) {
291 + retval = fn(packed->name + trim, packed->sha1);
292 if (retval)
172 - break;
293 + return retval;
294 }
174 - free(path);
175 - closedir(dir);
295 + packed = packed->next;
296 }
177 - return retval;
297 + return 0;
298 }
299
300 int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
@@ -187,22 +307,22 @@ int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
307
308 int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
309 {
190 - return do_for_each_ref("refs", fn, 0);
310 + return do_for_each_ref("refs/", fn, 0);
311 }
312
313 int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
314 {
195 - return do_for_each_ref("refs/tags", fn, 10);
315 + return do_for_each_ref("refs/tags/", fn, 10);
316 }
317
318 int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
319 {
200 - return do_for_each_ref("refs/heads", fn, 11);
320 + return do_for_each_ref("refs/heads/", fn, 11);
321 }
322
323 int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
324 {
205 - return do_for_each_ref("refs/remotes", fn, 13);
325 + return do_for_each_ref("refs/remotes/", fn, 13);
326 }
327
328 int get_ref_sha1(const char *ref, unsigned char *sha1)