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