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

Store peeled refs in packed-refs file.

This would speed up "show-ref -d" in a repository with mostly packed tags. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 19, 2006 at 13:22 UTC cf0adba7885342e1bbcf0689fece9d13e39784b4
4 files changed +117 -20
builtin-pack-refs.c
+20 -4
@@ -1,5 +1,7 @@
1 #include "cache.h"
2 #include "refs.h"
3 +#include "object.h"
4 +#include "tag.h"
5
6 static const char builtin_pack_refs_usage[] =
7 "git-pack-refs [--all] [--prune]";
@@ -29,12 +31,26 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
31 int flags, void *cb_data)
32 {
33 struct pack_refs_cb_data *cb = cb_data;
34 + int is_tag_ref;
35
33 - if (!cb->all && strncmp(path, "refs/tags/", 10))
34 - return 0;
36 /* Do not pack the symbolic refs */
36 - if (!(flags & REF_ISSYMREF))
37 - fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
37 + if ((flags & REF_ISSYMREF))
38 + return 0;
39 + is_tag_ref = !strncmp(path, "refs/tags/", 10);
40 + if (!cb->all && !is_tag_ref)
41 + return 0;
42 +
43 + fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
44 + if (is_tag_ref) {
45 + struct object *o = parse_object(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);
51 + }
52 + }
53 +
54 if (cb->prune && !do_not_prune(flags)) {
55 int namelen = strlen(path) + 1;
56 struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
builtin-show-ref.c
+27 -9
@@ -13,6 +13,7 @@ static int show_ref(const char *refname, const unsigned char *sha1, int flag, vo
13 {
14 struct object *obj;
15 const char *hex;
16 + unsigned char peeled[20];
17
18 if (tags_only || heads_only) {
19 int match;
@@ -44,12 +45,15 @@ static int show_ref(const char *refname, const unsigned char *sha1, int flag, vo
45
46 match:
47 found_match++;
47 - obj = parse_object(sha1);
48 - if (!obj) {
49 - if (quiet)
50 - return 0;
51 - die("git-show-ref: bad ref %s (%s)", refname, sha1_to_hex(sha1));
52 - }
48 +
49 + /* This changes the semantics slightly that even under quiet we
50 + * detect and return error if the repository is corrupt and
51 + * ref points at a nonexistent object.
52 + */
53 + if (!has_sha1_file(sha1))
54 + die("git-show-ref: bad ref %s (%s)", refname,
55 + sha1_to_hex(sha1));
56 +
57 if (quiet)
58 return 0;
59
@@ -58,11 +62,25 @@ match:
62 printf("%s\n", hex);
63 else
64 printf("%s %s\n", hex, refname);
61 - if (deref_tags && obj->type == OBJ_TAG) {
62 - obj = deref_tag(obj, refname, 0);
63 - hex = find_unique_abbrev(obj->sha1, abbrev);
65 +
66 + if (!deref_tags)
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);
72 }
73 + else {
74 + obj = parse_object(sha1);
75 + if (!obj)
76 + die("git-show-ref: bad ref %s (%s)", refname,
77 + sha1_to_hex(sha1));
78 + if (obj->type == OBJ_TAG) {
79 + obj = deref_tag(obj, refname, 0);
80 + hex = find_unique_abbrev(obj->sha1, abbrev);
81 + printf("%s %s^{}\n", hex, refname);
82 + }
83 + }
84 return 0;
85 }
86
refs.c
+66 -7
@@ -1,16 +1,18 @@
1 #include "refs.h"
2 #include "cache.h"
3 +#include "object.h"
4 +#include "tag.h"
5
6 #include <errno.h>
7
8 struct ref_list {
9 struct ref_list *next;
8 - unsigned char flag; /* ISSYMREF? ISPACKED? */
10 + unsigned char flag; /* ISSYMREF? ISPACKED? ISPEELED? */
11 unsigned char sha1[20];
12 char name[FLEX_ARRAY];
13 };
14
13 -static const char *parse_ref_line(char *line, unsigned char *sha1)
15 +static const char *parse_ref_line(char *line, unsigned char *sha1, int *flag)
16 {
17 /*
18 * 42: the answer to everything.
@@ -21,6 +23,7 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
23 * +1 (newline at the end of the line)
24 */
25 int len = strlen(line) - 42;
26 + int peeled = 0;
27
28 if (len <= 0)
29 return NULL;
@@ -29,11 +32,24 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
32 if (!isspace(line[40]))
33 return NULL;
34 line += 41;
32 - if (isspace(*line))
33 - return NULL;
35 +
36 + if (isspace(*line)) {
37 + /* "SHA-1 SP SP refs/tags/tagname^{} LF"? */
38 + line++;
39 + len--;
40 + peeled = 1;
41 + }
42 if (line[len] != '\n')
43 return NULL;
44 line[len] = 0;
45 +
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;
53 return line;
54 }
55
@@ -108,10 +124,12 @@ static struct ref_list *get_packed_refs(void)
124 char refline[PATH_MAX];
125 while (fgets(refline, sizeof(refline), f)) {
126 unsigned char sha1[20];
111 - const char *name = parse_ref_line(refline, sha1);
127 + int flag = REF_ISPACKED;
128 + const char *name =
129 + parse_ref_line(refline, sha1, &flag);
130 if (!name)
131 continue;
114 - list = add_ref(name, sha1, REF_ISPACKED, list);
132 + list = add_ref(name, sha1, flag, list);
133 }
134 fclose(f);
135 refs = list;
@@ -207,7 +225,8 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
225 if (lstat(path, &st) < 0) {
226 struct ref_list *list = get_packed_refs();
227 while (list) {
210 - if (!strcmp(ref, list->name)) {
228 + if (!(list->flag & REF_ISPEELED) &&
229 + !strcmp(ref, list->name)) {
230 hashcpy(sha1, list->sha1);
231 if (flag)
232 *flag |= REF_ISPACKED;
@@ -329,6 +348,8 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
348 return 0;
349 if (is_null_sha1(entry->sha1))
350 return 0;
351 + if (entry->flag & REF_ISPEELED)
352 + return 0;
353 if (!has_sha1_file(entry->sha1)) {
354 error("%s does not point to a valid object!", entry->name);
355 return 0;
@@ -336,6 +357,44 @@ static int do_one_ref(const char *base, each_ref_fn fn, int trim,
357 return fn(entry->name + trim, entry->sha1, entry->flag, cb_data);
358 }
359
360 +int peel_ref(const char *ref, unsigned char *sha1)
361 +{
362 + int flag;
363 + unsigned char base[20];
364 + struct object *o;
365 +
366 + if (!resolve_ref(ref, base, 1, &flag))
367 + return -1;
368 +
369 + if ((flag & REF_ISPACKED)) {
370 + struct ref_list *list = get_packed_refs();
371 + int len = strlen(ref);
372 +
373 + 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;
380 + }
381 + list = list->next;
382 + }
383 + /* older pack-refs did not leave peeled ones in */
384 + }
385 +
386 + /* otherwise ... */
387 + o = parse_object(base);
388 + if (o->type == OBJ_TAG) {
389 + o = deref_tag(o, ref, 0);
390 + if (o) {
391 + hashcpy(sha1, o->sha1);
392 + return 0;
393 + }
394 + }
395 + return -1;
396 +}
397 +
398 static int do_for_each_ref(const char *base, each_ref_fn fn, int trim,
399 void *cb_data)
400 {
refs.h
+4
@@ -16,6 +16,8 @@ struct ref_lock {
16 */
17 #define REF_ISSYMREF 01
18 #define REF_ISPACKED 02
19 +#define REF_ISPEELED 04 /* internal use */
20 +
21 typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
22 extern int head_ref(each_ref_fn, void *);
23 extern int for_each_ref(each_ref_fn, void *);
@@ -23,6 +25,8 @@ extern int for_each_tag_ref(each_ref_fn, void *);
25 extern int for_each_branch_ref(each_ref_fn, void *);
26 extern int for_each_remote_ref(each_ref_fn, void *);
27
28 +extern int peel_ref(const char *, unsigned char *);
29 +
30 /** Reads the refs file specified into sha1 **/
31 extern int get_ref_sha1(const char *ref, unsigned char *sha1);
32