git-send-email: Add --thread option
[git/aroben.git] / server-info.c
blobf9be5a7f60c1cc5208e7c91367ecb28014106f18
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 = xstrdup(git_path("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", 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;
47 /* packs */
48 static 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;
56 static int num_pack;
57 static const char *objdir;
58 static int objdirlen;
60 static struct pack_info *find_pack_by_name(const char *name)
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];
69 return NULL;
72 /* Returns non-zero when we detect that the info in the
73 * old file is useless.
75 static int parse_pack_def(const char *line, int old_cnt)
77 struct pack_info *i = find_pack_by_name(line + 2);
78 if (i) {
79 i->old_num = old_cnt;
80 return 0;
82 else {
83 /* The file describes a pack that is no longer here */
84 return 1;
88 /* Returns non-zero when we detect that the info in the
89 * old file is useless.
91 static int read_pack_info_file(const char *infofile)
93 FILE *fp;
94 char line[1000];
95 int old_cnt = 0;
97 fp = fopen(infofile, "r");
98 if (!fp)
99 return 1; /* nonexistent is not an error. */
101 while (fgets(line, sizeof(line), fp)) {
102 int len = strlen(line);
103 if (line[len-1] == '\n')
104 line[--len] = 0;
106 if (!len)
107 continue;
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;
125 fclose(fp);
126 return 0;
127 out_stale:
128 fclose(fp);
129 return 1;
132 static int compare_info(const void *a_, const void *b_)
134 struct pack_info * const* a = a_;
135 struct pack_info * const* b = b_;
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;
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;
156 static void init_pack_info(const char *infofile, int force)
158 struct packed_git *p;
159 int stale;
160 int i = 0;
162 objdir = get_object_directory();
163 objdirlen = strlen(objdir);
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.
170 if (!p->pack_local)
171 continue;
172 i++;
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++;
185 if (infofile && !force)
186 stale = read_pack_info_file(infofile);
187 else
188 stale = 1;
190 for (i = 0; i < num_pack; i++) {
191 if (stale) {
192 info[i]->old_num = -1;
193 info[i]->nr_heads = 0;
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;
203 static void write_pack_info_file(FILE *fp)
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);
211 static int update_info_packs(int force)
213 char infofile[PATH_MAX];
214 char name[PATH_MAX];
215 int namelen;
216 FILE *fp;
218 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
219 strcpy(name, infofile);
220 strcpy(name + namelen, "+");
222 init_pack_info(infofile, force);
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;
234 /* public */
235 int update_server_info(int force)
237 /* We would add more dumb-server support files later,
238 * including index of available pack files and their
239 * intended audiences.
241 int errs = 0;
243 errs = errs | update_info_refs(force);
244 errs = errs | update_info_packs(force);
246 /* remove leftover rev-cache file if there is any */
247 unlink(git_path("info/rev-cache"));
249 return errs;