]> git.ipfire.org Git - thirdparty/git.git/blame - server-info.c
[PATCH] quote.c: Make loop control more readable.
[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
8f3f9b09
JH
58static struct pack_info *find_pack_by_name(const char *name)
59{
60 int i;
61 for (i = 0; i < num_pack; i++) {
62 struct packed_git *p = info[i]->p;
63 /* skip "/pack/" after ".git/objects" */
64 if (!strcmp(p->pack_name + objdirlen + 6, name))
65 return info[i];
66 }
67 return NULL;
68}
69
8f3f9b09
JH
70/* Returns non-zero when we detect that the info in the
71 * old file is useless.
72 */
73static int parse_pack_def(const char *line, int old_cnt)
74{
75 struct pack_info *i = find_pack_by_name(line + 2);
76 if (i) {
77 i->old_num = old_cnt;
78 return 0;
79 }
80 else {
6f42f89c 81 /* The file describes a pack that is no longer here */
8f3f9b09
JH
82 return 1;
83 }
84}
85
8f3f9b09
JH
86/* Returns non-zero when we detect that the info in the
87 * old file is useless.
88 */
89static int read_pack_info_file(const char *infofile)
90{
91 FILE *fp;
92 char line[1000];
93 int old_cnt = 0;
94
95 fp = fopen(infofile, "r");
96 if (!fp)
97 return 1; /* nonexisting is not an error. */
98
99 while (fgets(line, sizeof(line), fp)) {
100 int len = strlen(line);
101 if (line[len-1] == '\n')
102 line[len-1] = 0;
103
104 switch (line[0]) {
105 case 'P': /* P name */
106 if (parse_pack_def(line, old_cnt++))
107 goto out_stale;
108 break;
6f42f89c
JH
109 case 'D': /* we used to emit D but that was misguided. */
110 goto out_stale;
8f3f9b09 111 break;
3e15c67c
JH
112 case 'T': /* we used to emit T but nobody uses it. */
113 goto out_stale;
8f3f9b09
JH
114 break;
115 default:
116 error("unrecognized: %s", line);
117 break;
118 }
119 }
120 fclose(fp);
121 return 0;
122 out_stale:
123 fclose(fp);
124 return 1;
125}
126
8f3f9b09
JH
127static int compare_info(const void *a_, const void *b_)
128{
129 struct pack_info * const* a = a_;
130 struct pack_info * const* b = b_;
131
132 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
133 /* Keep the order in the original */
134 return (*a)->old_num - (*b)->old_num;
135 else if (0 <= (*a)->old_num)
136 /* Only A existed in the original so B is obviously newer */
137 return -1;
138 else if (0 <= (*b)->old_num)
139 /* The other way around. */
140 return 1;
141
d5eac498 142 /* then it does not matter but at least keep the comparison stable */
2dee5816
JH
143 if ((*a)->p == (*b)->p)
144 return 0;
145 else if ((*a)->p < (*b)->p)
146 return -1;
147 else
148 return 1;
8f3f9b09
JH
149}
150
151static void init_pack_info(const char *infofile, int force)
152{
153 struct packed_git *p;
154 int stale;
155 int i = 0;
8f3f9b09
JH
156
157 objdir = get_object_directory();
158 objdirlen = strlen(objdir);
159
160 prepare_packed_git();
161 for (p = packed_git; p; p = p->next) {
162 /* we ignore things on alternate path since they are
163 * not available to the pullers in general.
164 */
f13d7db4 165 if (!p->pack_local)
8f3f9b09
JH
166 continue;
167 i++;
168 }
169 num_pack = i;
170 info = xcalloc(num_pack, sizeof(struct pack_info *));
171 for (i = 0, p = packed_git; p; p = p->next) {
f13d7db4 172 if (!p->pack_local)
8f3f9b09 173 continue;
6f42f89c 174 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b09
JH
175 info[i]->p = p;
176 info[i]->old_num = -1;
177 i++;
178 }
179
180 if (infofile && !force)
181 stale = read_pack_info_file(infofile);
182 else
183 stale = 1;
184
185 for (i = 0; i < num_pack; i++) {
186 if (stale) {
187 info[i]->old_num = -1;
8f3f9b09
JH
188 info[i]->nr_heads = 0;
189 }
8f3f9b09
JH
190 }
191
6f42f89c 192 /* renumber them */
8f3f9b09
JH
193 qsort(info, num_pack, sizeof(info[0]), compare_info);
194 for (i = 0; i < num_pack; i++)
195 info[i]->new_num = i;
8f3f9b09
JH
196}
197
198static void write_pack_info_file(FILE *fp)
199{
3e15c67c 200 int i;
8f3f9b09
JH
201 for (i = 0; i < num_pack; i++)
202 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
21b1aced 203 fputc('\n', fp);
8f3f9b09
JH
204}
205
206static int update_info_packs(int force)
207{
208 char infofile[PATH_MAX];
209 char name[PATH_MAX];
210 int namelen;
211 FILE *fp;
212
213 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
214 strcpy(name, infofile);
215 strcpy(name + namelen, "+");
216
217 init_pack_info(infofile, force);
8f3f9b09
JH
218
219 safe_create_leading_directories(name);
220 fp = fopen(name, "w");
221 if (!fp)
222 return error("cannot open %s", name);
223 write_pack_info_file(fp);
224 fclose(fp);
225 rename(name, infofile);
226 return 0;
227}
228
8f3f9b09
JH
229/* public */
230int update_server_info(int force)
231{
232 /* We would add more dumb-server support files later,
233 * including index of available pack files and their
234 * intended audiences.
235 */
236 int errs = 0;
237
238 errs = errs | update_info_refs(force);
239 errs = errs | update_info_packs(force);
8f3f9b09 240
6f42f89c
JH
241 /* remove leftover rev-cache file if there is any */
242 unlink(git_path("info/rev-cache"));
243
8f3f9b09
JH
244 return errs;
245}