Documentation: push/receive hook references.
[git/dkf.git] / server-info.c
blobcb67c1f2de95c2724a6aa8e7980f58dccd9aa30b
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)
12 struct object *o = parse_object(sha1);
14 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
15 if (o->type == tag_type) {
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);
21 return 0;
24 static int update_info_refs(int force)
26 char *path0 = strdup(git_path("info/refs"));
27 int len = strlen(path0);
28 char *path1 = xmalloc(len + 2);
30 strcpy(path1, path0);
31 strcpy(path1 + len, "+");
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;
45 /* packs */
46 static struct pack_info {
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];
53 } **info;
54 static int num_pack;
55 static const char *objdir;
56 static int objdirlen;
58 static struct object *parse_object_cheap(const unsigned char *sha1)
60 struct object *o;
62 if ((o = parse_object(sha1)) == NULL)
63 return NULL;
64 if (o->type == commit_type) {
65 struct commit *commit = (struct commit *)o;
66 free(commit->buffer);
67 commit->buffer = NULL;
68 } else if (o->type == tree_type) {
69 struct tree *tree = (struct tree *)o;
70 struct tree_entry_list *e, *n;
71 for (e = tree->entries; e; e = n) {
72 free(e->name);
73 e->name = NULL;
74 n = e->next;
75 free(e);
77 tree->entries = NULL;
79 return o;
82 static struct pack_info *find_pack_by_name(const char *name)
84 int i;
85 for (i = 0; i < num_pack; i++) {
86 struct packed_git *p = info[i]->p;
87 /* skip "/pack/" after ".git/objects" */
88 if (!strcmp(p->pack_name + objdirlen + 6, name))
89 return info[i];
91 return NULL;
94 static struct pack_info *find_pack_by_old_num(int old_num)
96 int i;
97 for (i = 0; i < num_pack; i++)
98 if (info[i]->old_num == old_num)
99 return info[i];
100 return NULL;
103 /* Returns non-zero when we detect that the info in the
104 * old file is useless.
106 static int parse_pack_def(const char *line, int old_cnt)
108 struct pack_info *i = find_pack_by_name(line + 2);
109 if (i) {
110 i->old_num = old_cnt;
111 return 0;
113 else {
114 /* The file describes a pack that is no longer here */
115 return 1;
119 /* Returns non-zero when we detect that the info in the
120 * old file is useless.
122 static int read_pack_info_file(const char *infofile)
124 FILE *fp;
125 char line[1000];
126 int old_cnt = 0;
128 fp = fopen(infofile, "r");
129 if (!fp)
130 return 1; /* nonexisting is not an error. */
132 while (fgets(line, sizeof(line), fp)) {
133 int len = strlen(line);
134 if (line[len-1] == '\n')
135 line[len-1] = 0;
137 switch (line[0]) {
138 case 'P': /* P name */
139 if (parse_pack_def(line, old_cnt++))
140 goto out_stale;
141 break;
142 case 'D': /* we used to emit D but that was misguided. */
143 goto out_stale;
144 break;
145 case 'T': /* we used to emit T but nobody uses it. */
146 goto out_stale;
147 break;
148 default:
149 error("unrecognized: %s", line);
150 break;
153 fclose(fp);
154 return 0;
155 out_stale:
156 fclose(fp);
157 return 1;
160 static int compare_info(const void *a_, const void *b_)
162 struct pack_info * const* a = a_;
163 struct pack_info * const* b = b_;
165 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
166 /* Keep the order in the original */
167 return (*a)->old_num - (*b)->old_num;
168 else if (0 <= (*a)->old_num)
169 /* Only A existed in the original so B is obviously newer */
170 return -1;
171 else if (0 <= (*b)->old_num)
172 /* The other way around. */
173 return 1;
175 /* then it does not matter but at least keep the comparison stable */
176 return (*a)->p - (*b)->p;
179 static void init_pack_info(const char *infofile, int force)
181 struct packed_git *p;
182 int stale;
183 int i = 0;
185 objdir = get_object_directory();
186 objdirlen = strlen(objdir);
188 prepare_packed_git();
189 for (p = packed_git; p; p = p->next) {
190 /* we ignore things on alternate path since they are
191 * not available to the pullers in general.
193 if (strncmp(p->pack_name, objdir, objdirlen) ||
194 strncmp(p->pack_name + objdirlen, "/pack/", 6))
195 continue;
196 i++;
198 num_pack = i;
199 info = xcalloc(num_pack, sizeof(struct pack_info *));
200 for (i = 0, p = packed_git; p; p = p->next) {
201 if (strncmp(p->pack_name, objdir, objdirlen) ||
202 p->pack_name[objdirlen] != '/')
203 continue;
204 info[i] = xcalloc(1, sizeof(struct pack_info));
205 info[i]->p = p;
206 info[i]->old_num = -1;
207 i++;
210 if (infofile && !force)
211 stale = read_pack_info_file(infofile);
212 else
213 stale = 1;
215 for (i = 0; i < num_pack; i++) {
216 if (stale) {
217 info[i]->old_num = -1;
218 info[i]->nr_heads = 0;
222 /* renumber them */
223 qsort(info, num_pack, sizeof(info[0]), compare_info);
224 for (i = 0; i < num_pack; i++)
225 info[i]->new_num = i;
228 static void write_pack_info_file(FILE *fp)
230 int i;
231 for (i = 0; i < num_pack; i++)
232 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
235 static int update_info_packs(int force)
237 char infofile[PATH_MAX];
238 char name[PATH_MAX];
239 int namelen;
240 FILE *fp;
242 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
243 strcpy(name, infofile);
244 strcpy(name + namelen, "+");
246 init_pack_info(infofile, force);
248 safe_create_leading_directories(name);
249 fp = fopen(name, "w");
250 if (!fp)
251 return error("cannot open %s", name);
252 write_pack_info_file(fp);
253 fclose(fp);
254 rename(name, infofile);
255 return 0;
258 /* public */
259 int update_server_info(int force)
261 /* We would add more dumb-server support files later,
262 * including index of available pack files and their
263 * intended audiences.
265 int errs = 0;
267 errs = errs | update_info_refs(force);
268 errs = errs | update_info_packs(force);
270 /* remove leftover rev-cache file if there is any */
271 unlink(git_path("info/rev-cache"));
273 return errs;