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

Store peeled refs in packed-refs (take 2).

This fixes the previous implementation which failed to optimize repositories with tons of lightweight tags. The updated packed-refs format begins with "# packed-refs with:" line that lists the kind of extended data the file records. Currently, there is only one such extension defined, "peeled". This stores the "peeled tag" on a line that immediately follows a line for a tag object itself in the format "^<sha-1>". The header line itself and any extended data are ignored by older implementation, so packed-refs file generated with this version can still be used by older git. packed-refs made by older git can of course be used with this version. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 21, 2006 at 23:36 UTC f4204ab9f6a192cdb9a68150e031d7183688bfeb
4 files changed +77 -55
builtin-pack-refs.c
+6 -2
@@ -46,8 +46,8 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
46 if (o->type == OBJ_TAG) {
47 o = deref_tag(o, path, 0);
48 if (o)
49 - fprintf(cb->refs_file, "%s %s^{}\n",
50 - sha1_to_hex(o->sha1), path);
49 + fprintf(cb->refs_file, "^%s\n",
50 + sha1_to_hex(o->sha1));
51 }
52 }
53
@@ -111,6 +111,10 @@ int cmd_pack_refs(int argc, const char **argv, const char *prefix)
111 if (!cbdata.refs_file)
112 die("unable to create ref-pack file structure (%s)",
113 strerror(errno));
114 +
115 + /* perhaps other traits later as well */
116 + fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
117 +
118 for_each_ref(handle_one_ref, &cbdata);
119 fflush(cbdata.refs_file);
120 fsync(fd);
builtin-show-ref.c
+4 -2
@@ -67,8 +67,10 @@ match:
67 return 0;
68
69 if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) {
70 - hex = find_unique_abbrev(peeled, abbrev);
71 - printf("%s %s^{}\n", hex, refname);
70 + if (!is_null_sha1(peeled)) {
71 + hex = find_unique_abbrev(peeled, abbrev);
72 + printf("%s %s^{}\n", hex, refname);
73 + }
74 }
75 else {
76 obj = parse_object(sha1);
refs.c
+64 -47
@@ -5,14 +5,18 @@
5
6 #include <errno.h>
7
8 +/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
9 +#define REF_KNOWS_PEELED 04
10 +
11 struct ref_list {
12 struct ref_list *next;
10 - unsigned char flag; /* ISSYMREF? ISPACKED? ISPEELED? */
13 + unsigned char flag; /* ISSYMREF? ISPACKED? */
14 unsigned char sha1[20];
15 + unsigned char peeled[20];
16 char name[FLEX_ARRAY];
17 };
18
15 -static const char *parse_ref_line(char *line, unsigned char *sha1, int *flag)
19 +static const char *parse_ref_line(char *line, unsigned char *sha1)
20 {
21 /*
22 * 42: the answer to everything.
@@ -23,7 +27,6 @@ static const char *parse_ref_line(char *line, unsigned char *sha1, int *flag)
27 * +1 (newline at the end of the line)
28 */
29 int len = strlen(line) - 42;
26 - int peeled = 0;
30
31 if (len <= 0)
32 return NULL;
@@ -32,29 +35,18 @@ static const char *parse_ref_line(char *line, unsigned char *sha1, int *flag)
35 if (!isspace(line[40]))
36 return NULL;
37 line += 41;
35 -
36 - if (isspace(*line)) {
37 - /* "SHA-1 SP SP refs/tags/tagname^{} LF"? */
38 - line++;
39 - len--;
40 - peeled = 1;
41 - }
38 + if (isspace(*line))
39 + return NULL;
40 if (line[len] != '\n')
41 return NULL;
42 line[len] = 0;
43
46 - if (peeled && (len < 3 || strcmp(line + len - 3, "^{}")))
47 - return NULL;
48 -
49 - if (!peeled)
50 - *flag &= ~REF_ISPEELED;
51 - else
52 - *flag |= REF_ISPEELED;
44 return line;
45 }
46
47 static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
57 - int flag, struct ref_list *list)
48 + int flag, struct ref_list *list,
49 + struct ref_list **new_entry)
50 {
51 int len;
52 struct ref_list **p = &list, *entry;
@@ -66,8 +58,11 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
58 break;
59
60 /* Same as existing entry? */
69 - if (!cmp)
61 + if (!cmp) {
62 + if (new_entry)
63 + *new_entry = entry;
64 return list;
65 + }
66 p = &entry->next;
67 }
68
@@ -75,10 +70,13 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
70 len = strlen(name) + 1;
71 entry = xmalloc(sizeof(struct ref_list) + len);
72 hashcpy(entry->sha1, sha1);
73 + hashclr(entry->peeled);
74 memcpy(entry->name, name, len);
75 entry->flag = flag;
76 entry->next = *p;
77 *p = entry;
78 + if (new_entry)
79 + *new_entry = entry;
80 return list;
81 }
82
@@ -114,27 +112,50 @@ static void invalidate_cached_refs(void)
112 ca->did_loose = ca->did_packed = 0;
113 }
114
115 +static void read_packed_refs(FILE *f, struct cached_refs *cached_refs)
116 +{
117 + struct ref_list *list = NULL;
118 + struct ref_list *last = NULL;
119 + char refline[PATH_MAX];
120 + int flag = REF_ISPACKED;
121 +
122 + while (fgets(refline, sizeof(refline), f)) {
123 + unsigned char sha1[20];
124 + const char *name;
125 + static const char header[] = "# pack-refs with:";
126 +
127 + if (!strncmp(refline, header, sizeof(header)-1)) {
128 + const char *traits = refline + sizeof(header) - 1;
129 + if (strstr(traits, " peeled "))
130 + flag |= REF_KNOWS_PEELED;
131 + /* perhaps other traits later as well */
132 + continue;
133 + }
134 +
135 + name = parse_ref_line(refline, sha1);
136 + if (name) {
137 + list = add_ref(name, sha1, flag, list, &last);
138 + continue;
139 + }
140 + if (last &&
141 + refline[0] == '^' &&
142 + strlen(refline) == 42 &&
143 + refline[41] == '\n' &&
144 + !get_sha1_hex(refline + 1, sha1))
145 + hashcpy(last->peeled, sha1);
146 + }
147 + cached_refs->packed = list;
148 +}
149 +
150 static struct ref_list *get_packed_refs(void)
151 {
152 if (!cached_refs.did_packed) {
120 - struct ref_list *refs = NULL;
153 FILE *f = fopen(git_path("packed-refs"), "r");
154 + cached_refs.packed = NULL;
155 if (f) {
123 - struct ref_list *list = NULL;
124 - char refline[PATH_MAX];
125 - while (fgets(refline, sizeof(refline), f)) {
126 - unsigned char sha1[20];
127 - int flag = REF_ISPACKED;
128 - const char *name =
129 - parse_ref_line(refline, sha1, &flag);
130 - if (!name)
131 - continue;
132 - list = add_ref(name, sha1, flag, list);
133 - }
156 + read_packed_refs(f, &cached_refs);
157 fclose(f);
135 - refs = list;
158 }
137 - cached_refs.packed = refs;
159 cached_refs.did_packed = 1;
160 }
161 return cached_refs.packed;
@@ -177,7 +198,7 @@ static struct ref_list *get_ref_dir(const char *base, struct ref_list *list)
198 error("%s points nowhere!", ref);
199 continue;
200 }
180 - list = add_ref(ref, sha1, flag, list);
201 + list = add_ref(ref, sha1, flag, list, NULL);
202 }
203 free(ref);
204 closedir(dir);
@@ -225,8 +246,7 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
246 if (lstat(path, &st) < 0) {
247 struct ref_list *list = get_packed_refs();
248 while (list) {
228 - if (!(list->flag & REF_ISPEELED) &&
229 - !strcmp(ref, list->name)) {
249 + if (!strcmp(ref, list->name)) {
250 hashcpy(sha1, list->sha1);
251 if (flag)
252 *flag |= REF_ISPACKED;
@@ -348,8 +368,6 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
368 return 0;
369 if (is_null_sha1(entry->sha1))
370 return 0;
351 - if (entry->flag & REF_ISPEELED)
352 - return 0;
371 if (!has_sha1_file(entry->sha1)) {
372 error("%s does not point to a valid object!", entry->name);
373 return 0;
@@ -368,22 +386,21 @@ int peel_ref(const char *ref, unsigned char *sha1)
386
387 if ((flag & REF_ISPACKED)) {
388 struct ref_list *list = get_packed_refs();
371 - int len = strlen(ref);
389
390 while (list) {
374 - if ((list->flag & REF_ISPEELED) &&
375 - !strncmp(list->name, ref, len) &&
376 - strlen(list->name) == len + 3 &&
377 - !strcmp(list->name + len, "^{}")) {
378 - hashcpy(sha1, list->sha1);
379 - return 0;
391 + if (!strcmp(list->name, ref)) {
392 + if (list->flag & REF_KNOWS_PEELED) {
393 + hashcpy(sha1, list->peeled);
394 + return 0;
395 + }
396 + /* older pack-refs did not leave peeled ones */
397 + break;
398 }
399 list = list->next;
400 }
383 - /* older pack-refs did not leave peeled ones in */
401 }
402
386 - /* otherwise ... */
403 + /* fallback - callers should not call this for unpacked refs */
404 o = parse_object(base);
405 if (o->type == OBJ_TAG) {
406 o = deref_tag(o, ref, 0);
refs.h
+3 -4
@@ -10,14 +10,13 @@ struct ref_lock {
10 int force_write;
11 };
12
13 +#define REF_ISSYMREF 01
14 +#define REF_ISPACKED 02
15 +
16 /*
17 * Calls the specified function for each ref file until it returns nonzero,
18 * and returns the value
19 */
17 -#define REF_ISSYMREF 01
18 -#define REF_ISPACKED 02
19 -#define REF_ISPEELED 04 /* internal use */
20 -
20 typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
21 extern int head_ref(each_ref_fn, void *);
22 extern int for_each_ref(each_ref_fn, void *);