Updates to draft release notes to 1.6.4
[tgit.git] / server-info.c
blob4098ca2b5c166c32cfe4aea9d1bff2e6593e9a60
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "commit.h"
5 #include "tag.h"
7 /* refs */
8 static FILE *info_ref_fp;
10 static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
12 struct object *o = parse_object(sha1);
13 if (!o)
14 return -1;
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);
23 return 0;
26 static int update_info_refs(int force)
28 char *path0 = git_pathdup("info/refs");
29 int len = strlen(path0);
30 char *path1 = xmalloc(len + 2);
32 strcpy(path1, path0);
33 strcpy(path1 + len, "+");
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", path1);
39 for_each_ref(add_info_ref, NULL);
40 fclose(info_ref_fp);
41 adjust_shared_perm(path1);
42 rename(path1, path0);
43 free(path0);
44 free(path1);
45 return 0;
48 /* packs */
49 static struct pack_info {
50 struct packed_git *p;
51 int old_num;
52 int new_num;
53 int nr_alloc;
54 int nr_heads;
55 unsigned char (*head)[20];
56 } **info;
57 static int num_pack;
58 static const char *objdir;
59 static int objdirlen;
61 static struct pack_info *find_pack_by_name(const char *name)
63 int i;
64 for (i = 0; i < num_pack; i++) {
65 struct packed_git *p = info[i]->p;
66 /* skip "/pack/" after ".git/objects" */
67 if (!strcmp(p->pack_name + objdirlen + 6, name))
68 return info[i];
70 return NULL;
73 /* Returns non-zero when we detect that the info in the
74 * old file is useless.
76 static int parse_pack_def(const char *line, int old_cnt)
78 struct pack_info *i = find_pack_by_name(line + 2);
79 if (i) {
80 i->old_num = old_cnt;
81 return 0;
83 else {
84 /* The file describes a pack that is no longer here */
85 return 1;
89 /* Returns non-zero when we detect that the info in the
90 * old file is useless.
92 static int read_pack_info_file(const char *infofile)
94 FILE *fp;
95 char line[1000];
96 int old_cnt = 0;
98 fp = fopen(infofile, "r");
99 if (!fp)
100 return 1; /* nonexistent is not an error. */
102 while (fgets(line, sizeof(line), fp)) {
103 int len = strlen(line);
104 if (len && line[len-1] == '\n')
105 line[--len] = 0;
107 if (!len)
108 continue;
110 switch (line[0]) {
111 case 'P': /* P name */
112 if (parse_pack_def(line, old_cnt++))
113 goto out_stale;
114 break;
115 case 'D': /* we used to emit D but that was misguided. */
116 goto out_stale;
117 break;
118 case 'T': /* we used to emit T but nobody uses it. */
119 goto out_stale;
120 break;
121 default:
122 error("unrecognized: %s", line);
123 break;
126 fclose(fp);
127 return 0;
128 out_stale:
129 fclose(fp);
130 return 1;
133 static int compare_info(const void *a_, const void *b_)
135 struct pack_info *const *a = a_;
136 struct pack_info *const *b = b_;
138 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
139 /* Keep the order in the original */
140 return (*a)->old_num - (*b)->old_num;
141 else if (0 <= (*a)->old_num)
142 /* Only A existed in the original so B is obviously newer */
143 return -1;
144 else if (0 <= (*b)->old_num)
145 /* The other way around. */
146 return 1;
148 /* then it does not matter but at least keep the comparison stable */
149 if ((*a)->p == (*b)->p)
150 return 0;
151 else if ((*a)->p < (*b)->p)
152 return -1;
153 else
154 return 1;
157 static void init_pack_info(const char *infofile, int force)
159 struct packed_git *p;
160 int stale;
161 int i = 0;
163 objdir = get_object_directory();
164 objdirlen = strlen(objdir);
166 prepare_packed_git();
167 for (p = packed_git; p; p = p->next) {
168 /* we ignore things on alternate path since they are
169 * not available to the pullers in general.
171 if (!p->pack_local)
172 continue;
173 i++;
175 num_pack = i;
176 info = xcalloc(num_pack, sizeof(struct pack_info *));
177 for (i = 0, p = packed_git; p; p = p->next) {
178 if (!p->pack_local)
179 continue;
180 info[i] = xcalloc(1, sizeof(struct pack_info));
181 info[i]->p = p;
182 info[i]->old_num = -1;
183 i++;
186 if (infofile && !force)
187 stale = read_pack_info_file(infofile);
188 else
189 stale = 1;
191 for (i = 0; i < num_pack; i++) {
192 if (stale) {
193 info[i]->old_num = -1;
194 info[i]->nr_heads = 0;
198 /* renumber them */
199 qsort(info, num_pack, sizeof(info[0]), compare_info);
200 for (i = 0; i < num_pack; i++)
201 info[i]->new_num = i;
204 static void write_pack_info_file(FILE *fp)
206 int i;
207 for (i = 0; i < num_pack; i++)
208 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
209 fputc('\n', fp);
212 static int update_info_packs(int force)
214 char infofile[PATH_MAX];
215 char name[PATH_MAX];
216 int namelen;
217 FILE *fp;
219 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
220 strcpy(name, infofile);
221 strcpy(name + namelen, "+");
223 init_pack_info(infofile, force);
225 safe_create_leading_directories(name);
226 fp = fopen(name, "w");
227 if (!fp)
228 return error("cannot open %s", name);
229 write_pack_info_file(fp);
230 fclose(fp);
231 adjust_shared_perm(name);
232 rename(name, infofile);
233 return 0;
236 /* public */
237 int update_server_info(int force)
239 /* We would add more dumb-server support files later,
240 * including index of available pack files and their
241 * intended audiences.
243 int errs = 0;
245 errs = errs | update_info_refs(force);
246 errs = errs | update_info_packs(force);
248 /* remove leftover rev-cache file if there is any */
249 unlink_or_warn(git_path("info/rev-cache"));
251 return errs;