]> git.ipfire.org Git - thirdparty/git.git/blame - server-info.c
server-info: throw away T computation as well.
[thirdparty/git.git] / server-info.c
CommitLineData
8f3f9b09
JH
1#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
b614e3d7 5#include "tag.h"
8f3f9b09
JH
6
7/* refs */
8static FILE *info_ref_fp;
8f3f9b09
JH
9
10static int add_info_ref(const char *path, const unsigned char *sha1)
11{
f6b42a81
JH
12 struct object *o = parse_object(sha1);
13
8f3f9b09 14 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
f6b42a81 15 if (o->type == tag_type) {
9534f40b
JH
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);
f6b42a81 20 }
8f3f9b09
JH
21 return 0;
22}
23
24static int update_info_refs(int force)
25{
8f3f9b09
JH
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
8f3f9b09
JH
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 */
4d8fa916 46static struct pack_info {
8f3f9b09
JH
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];
8f3f9b09
JH
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;
f22ca7c5
RF
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;
8f3f9b09
JH
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
8f3f9b09
JH
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 {
6f42f89c 114 /* The file describes a pack that is no longer here */
8f3f9b09
JH
115 return 1;
116 }
117}
118
8f3f9b09
JH
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;
6f42f89c
JH
142 case 'D': /* we used to emit D but that was misguided. */
143 goto out_stale;
8f3f9b09 144 break;
3e15c67c
JH
145 case 'T': /* we used to emit T but nobody uses it. */
146 goto out_stale;
8f3f9b09
JH
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
8f3f9b09
JH
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
d5eac498
JH
175 /* then it does not matter but at least keep the comparison stable */
176 return (*a)->p - (*b)->p;
8f3f9b09
JH
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;
8f3f9b09
JH
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 (strncmp(p->pack_name, objdir, objdirlen) ||
194 strncmp(p->pack_name + objdirlen, "/pack/", 6))
195 continue;
196 i++;
197 }
198 num_pack = i;
199 info = xcalloc(num_pack, sizeof(struct pack_info *));
200 for (i = 0, p = packed_git; p; p = p->next) {
201 if (strncmp(p->pack_name, objdir, objdirlen) ||
202 p->pack_name[objdirlen] != '/')
203 continue;
6f42f89c 204 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
205 info[i]->p = p;
206 info[i]->old_num = -1;
207 i++;
208 }
209
210 if (infofile && !force)
211 stale = read_pack_info_file(infofile);
212 else
213 stale = 1;
214
215 for (i = 0; i < num_pack; i++) {
216 if (stale) {
217 info[i]->old_num = -1;
8f3f9b09
JH
218 info[i]->nr_heads = 0;
219 }
8f3f9b09
JH
220 }
221
6f42f89c 222 /* renumber them */
8f3f9b09
JH
223 qsort(info, num_pack, sizeof(info[0]), compare_info);
224 for (i = 0; i < num_pack; i++)
225 info[i]->new_num = i;
8f3f9b09
JH
226}
227
228static void write_pack_info_file(FILE *fp)
229{
3e15c67c 230 int i;
8f3f9b09
JH
231 for (i = 0; i < num_pack; i++)
232 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
8f3f9b09
JH
233}
234
235static int update_info_packs(int force)
236{
237 char infofile[PATH_MAX];
238 char name[PATH_MAX];
239 int namelen;
240 FILE *fp;
241
242 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
243 strcpy(name, infofile);
244 strcpy(name + namelen, "+");
245
246 init_pack_info(infofile, force);
8f3f9b09
JH
247
248 safe_create_leading_directories(name);
249 fp = fopen(name, "w");
250 if (!fp)
251 return error("cannot open %s", name);
252 write_pack_info_file(fp);
253 fclose(fp);
254 rename(name, infofile);
255 return 0;
256}
257
8f3f9b09
JH
258/* public */
259int update_server_info(int force)
260{
261 /* We would add more dumb-server support files later,
262 * including index of available pack files and their
263 * intended audiences.
264 */
265 int errs = 0;
266
267 errs = errs | update_info_refs(force);
268 errs = errs | update_info_packs(force);
8f3f9b09 269
6f42f89c
JH
270 /* remove leftover rev-cache file if there is any */
271 unlink(git_path("info/rev-cache"));
272
8f3f9b09
JH
273 return errs;
274}