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, int flag, void *cb_data)
11{
12 struct object *o = parse_object(sha1);
13 if (!o)
14 return -1;
15
16 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
17 if (o->type == OBJ_TAG) {
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);
22 }
23 return 0;
24}
25
26static int update_info_refs(int force)
27{
28 char *path0 = xstrdup(git_path("info/refs"));
29 int len = strlen(path0);
30 char *path1 = xmalloc(len + 2);
31
32 strcpy(path1, path0);
33 strcpy(path1 + len, "+");
34
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);
39 for_each_ref(add_info_ref, NULL);
40 fclose(info_ref_fp);
41 rename(path1, path0);
42 free(path0);
43 free(path1);
44 return 0;
45}
46
47/* packs */
48static struct pack_info {
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];
55} **info;
56static int num_pack;
57static const char *objdir;
58static int objdirlen;
59
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
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 {
83 /* The file describes a pack that is no longer here */
84 return 1;
85 }
86}
87
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)
99 return 1; /* nonexistent is not an error. */
100
101 while (fgets(line, sizeof(line), fp)) {
102 int len = strlen(line);
103 if (line[len-1] == '\n')
104 line[--len] = 0;
105
106 if (!len)
107 continue;
108
109 switch (line[0]) {
110 case 'P': /* P name */
111 if (parse_pack_def(line, old_cnt++))
112 goto out_stale;
113 break;
114 case 'D': /* we used to emit D but that was misguided. */
115 goto out_stale;
116 break;
117 case 'T': /* we used to emit T but nobody uses it. */
118 goto out_stale;
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
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
147 /* then it does not matter but at least keep the comparison stable */
148 if ((*a)->p == (*b)->p)
149 return 0;
150 else if ((*a)->p < (*b)->p)
151 return -1;
152 else
153 return 1;
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;
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 */
170 if (!p->pack_local)
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) {
177 if (!p->pack_local)
178 continue;
179 info[i] = xcalloc(1, sizeof(struct pack_info));
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;
193 info[i]->nr_heads = 0;
194 }
195 }
196
197 /* renumber them */
198 qsort(info, num_pack, sizeof(info[0]), compare_info);
199 for (i = 0; i < num_pack; i++)
200 info[i]->new_num = i;
201}
202
203static void write_pack_info_file(FILE *fp)
204{
205 int i;
206 for (i = 0; i < num_pack; i++)
207 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
208 fputc('\n', fp);
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);
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
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);
245
246 /* remove leftover rev-cache file if there is any */
247 unlink(git_path("info/rev-cache"));
248
249 return errs;
250}