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