7b214c8138afa7ff8f3a274235c6be5dac32ef07
1#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
5#include "tag.h"
6
7/* refs */
8static FILE *info_ref_fp;
9
10static int add_info_ref(const char *path, const unsigned char *sha1)
11{
12 struct object *o = parse_object(sha1);
13
14 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
15 if (o->type == tag_type) {
16 o = deref_tag(o, path, 0);
17 if (o)
18 fprintf(info_ref_fp, "%s %s^{}\n",
19 sha1_to_hex(o->sha1), path);
20 }
21 return 0;
22}
23
24static int update_info_refs(int force)
25{
26 char *path0 = strdup(git_path("info/refs"));
27 int len = strlen(path0);
28 char *path1 = xmalloc(len + 2);
29
30 strcpy(path1, path0);
31 strcpy(path1 + len, "+");
32
33 safe_create_leading_directories(path0);
34 info_ref_fp = fopen(path1, "w");
35 if (!info_ref_fp)
36 return error("unable to update %s", path0);
37 for_each_ref(add_info_ref);
38 fclose(info_ref_fp);
39 rename(path1, path0);
40 free(path0);
41 free(path1);
42 return 0;
43}
44
45/* packs */
46static struct pack_info {
47 struct packed_git *p;
48 int old_num;
49 int new_num;
50 int nr_alloc;
51 int nr_heads;
52 unsigned char (*head)[20];
53} **info;
54static int num_pack;
55static const char *objdir;
56static int objdirlen;
57
58static struct object *parse_object_cheap(const unsigned char *sha1)
59{
60 struct object *o;
61
62 if ((o = parse_object(sha1)) == NULL)
63 return NULL;
64 if (o->type == commit_type) {
65 struct commit *commit = (struct commit *)o;
66 free(commit->buffer);
67 commit->buffer = NULL;
68 } else if (o->type == tree_type) {
69 struct tree *tree = (struct tree *)o;
70 struct tree_entry_list *e, *n;
71 for (e = tree->entries; e; e = n) {
72 free(e->name);
73 e->name = NULL;
74 n = e->next;
75 free(e);
76 }
77 tree->entries = NULL;
78 }
79 return o;
80}
81
82static struct pack_info *find_pack_by_name(const char *name)
83{
84 int i;
85 for (i = 0; i < num_pack; i++) {
86 struct packed_git *p = info[i]->p;
87 /* skip "/pack/" after ".git/objects" */
88 if (!strcmp(p->pack_name + objdirlen + 6, name))
89 return info[i];
90 }
91 return NULL;
92}
93
94static struct pack_info *find_pack_by_old_num(int old_num)
95{
96 int i;
97 for (i = 0; i < num_pack; i++)
98 if (info[i]->old_num == old_num)
99 return info[i];
100 return NULL;
101}
102
103/* Returns non-zero when we detect that the info in the
104 * old file is useless.
105 */
106static int parse_pack_def(const char *line, int old_cnt)
107{
108 struct pack_info *i = find_pack_by_name(line + 2);
109 if (i) {
110 i->old_num = old_cnt;
111 return 0;
112 }
113 else {
114 /* The file describes a pack that is no longer here */
115 return 1;
116 }
117}
118
119/* Returns non-zero when we detect that the info in the
120 * old file is useless.
121 */
122static int read_pack_info_file(const char *infofile)
123{
124 FILE *fp;
125 char line[1000];
126 int old_cnt = 0;
127
128 fp = fopen(infofile, "r");
129 if (!fp)
130 return 1; /* nonexisting is not an error. */
131
132 while (fgets(line, sizeof(line), fp)) {
133 int len = strlen(line);
134 if (line[len-1] == '\n')
135 line[len-1] = 0;
136
137 switch (line[0]) {
138 case 'P': /* P name */
139 if (parse_pack_def(line, old_cnt++))
140 goto out_stale;
141 break;
142 case 'D': /* we used to emit D but that was misguided. */
143 goto out_stale;
144 break;
145 case 'T': /* we used to emit T but nobody uses it. */
146 goto out_stale;
147 break;
148 default:
149 error("unrecognized: %s", line);
150 break;
151 }
152 }
153 fclose(fp);
154 return 0;
155 out_stale:
156 fclose(fp);
157 return 1;
158}
159
160static int compare_info(const void *a_, const void *b_)
161{
162 struct pack_info * const* a = a_;
163 struct pack_info * const* b = b_;
164
165 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
166 /* Keep the order in the original */
167 return (*a)->old_num - (*b)->old_num;
168 else if (0 <= (*a)->old_num)
169 /* Only A existed in the original so B is obviously newer */
170 return -1;
171 else if (0 <= (*b)->old_num)
172 /* The other way around. */
173 return 1;
174
175 /* then it does not matter but at least keep the comparison stable */
176 return (*a)->p - (*b)->p;
177}
178
179static void init_pack_info(const char *infofile, int force)
180{
181 struct packed_git *p;
182 int stale;
183 int i = 0;
184
185 objdir = get_object_directory();
186 objdirlen = strlen(objdir);
187
188 prepare_packed_git();
189 for (p = packed_git; p; p = p->next) {
190 /* we ignore things on alternate path since they are
191 * not available to the pullers in general.
192 */
193 if (!p->pack_local)
194 continue;
195 i++;
196 }
197 num_pack = i;
198 info = xcalloc(num_pack, sizeof(struct pack_info *));
199 for (i = 0, p = packed_git; p; p = p->next) {
200 if (!p->pack_local)
201 continue;
202 info[i] = xcalloc(1, sizeof(struct pack_info));
203 info[i]->p = p;
204 info[i]->old_num = -1;
205 i++;
206 }
207
208 if (infofile && !force)
209 stale = read_pack_info_file(infofile);
210 else
211 stale = 1;
212
213 for (i = 0; i < num_pack; i++) {
214 if (stale) {
215 info[i]->old_num = -1;
216 info[i]->nr_heads = 0;
217 }
218 }
219
220 /* renumber them */
221 qsort(info, num_pack, sizeof(info[0]), compare_info);
222 for (i = 0; i < num_pack; i++)
223 info[i]->new_num = i;
224}
225
226static void write_pack_info_file(FILE *fp)
227{
228 int i;
229 for (i = 0; i < num_pack; i++)
230 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
231}
232
233static int update_info_packs(int force)
234{
235 char infofile[PATH_MAX];
236 char name[PATH_MAX];
237 int namelen;
238 FILE *fp;
239
240 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
241 strcpy(name, infofile);
242 strcpy(name + namelen, "+");
243
244 init_pack_info(infofile, force);
245
246 safe_create_leading_directories(name);
247 fp = fopen(name, "w");
248 if (!fp)
249 return error("cannot open %s", name);
250 write_pack_info_file(fp);
251 fclose(fp);
252 rename(name, infofile);
253 return 0;
254}
255
256/* public */
257int update_server_info(int force)
258{
259 /* We would add more dumb-server support files later,
260 * including index of available pack files and their
261 * intended audiences.
262 */
263 int errs = 0;
264
265 errs = errs | update_info_refs(force);
266 errs = errs | update_info_packs(force);
267
268 /* remove leftover rev-cache file if there is any */
269 unlink(git_path("info/rev-cache"));
270
271 return errs;
272}