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
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))
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)