blob: c1c073b2f05a48772a45602cdc711eef6e211695 [file] [log] [blame]
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07001#include "cache.h"
2#include "refs.h"
3#include "object.h"
4#include "commit.h"
Junio C Hamanob614e3d2005-07-28 14:33:17 -07005#include "tag.h"
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07006
7/* refs */
8static FILE *info_ref_fp;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -07009
Junio C Hamano8da19772006-09-20 22:02:01 -070010static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070011{
Junio C Hamanof6b42a82005-10-13 18:57:40 -070012 struct object *o = parse_object(sha1);
Shawn O. Pearce76f8a302007-01-31 02:24:44 -050013 if (!o)
14 return -1;
Junio C Hamanof6b42a82005-10-13 18:57:40 -070015
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070016 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
Linus Torvalds19746322006-07-11 20:45:31 -070017 if (o->type == OBJ_TAG) {
Junio C Hamano9534f402005-11-02 15:19:13 -080018 o = deref_tag(o, path, 0);
19 if (o)
20 fprintf(info_ref_fp, "%s %s^{}\n",
21 sha1_to_hex(o->sha1), path);
Junio C Hamanof6b42a82005-10-13 18:57:40 -070022 }
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070023 return 0;
24}
25
26static int update_info_refs(int force)
27{
Shawn Pearce9befac42006-09-02 00:16:31 -040028 char *path0 = xstrdup(git_path("info/refs"));
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070029 int len = strlen(path0);
30 char *path1 = xmalloc(len + 2);
31
32 strcpy(path1, path0);
33 strcpy(path1 + len, "+");
34
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070035 safe_create_leading_directories(path0);
36 info_ref_fp = fopen(path1, "w");
37 if (!info_ref_fp)
André Goddard Rosa84ef0332007-11-21 18:59:14 -020038 return error("unable to update %s", path1);
Junio C Hamanocb5d7092006-09-20 21:47:42 -070039 for_each_ref(add_info_ref, NULL);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070040 fclose(info_ref_fp);
Johannes Schindelin83525222007-07-11 15:18:17 +010041 adjust_shared_perm(path1);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070042 rename(path1, path0);
43 free(path0);
44 free(path1);
45 return 0;
46}
47
48/* packs */
Linus Torvalds4d8fa912005-08-01 12:11:53 -070049static struct pack_info {
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070050 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];
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070056} **info;
57static int num_pack;
58static const char *objdir;
59static int objdirlen;
60
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070061static struct pack_info *find_pack_by_name(const char *name)
62{
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];
69 }
70 return NULL;
71}
72
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070073/* Returns non-zero when we detect that the info in the
74 * old file is useless.
75 */
76static int parse_pack_def(const char *line, int old_cnt)
77{
78 struct pack_info *i = find_pack_by_name(line + 2);
79 if (i) {
80 i->old_num = old_cnt;
81 return 0;
82 }
83 else {
Junio C Hamano6f42f892005-12-04 22:52:19 -080084 /* The file describes a pack that is no longer here */
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070085 return 1;
86 }
87}
88
Junio C Hamano8f3f9b02005-07-23 17:54:41 -070089/* Returns non-zero when we detect that the info in the
90 * old file is useless.
91 */
92static int read_pack_info_file(const char *infofile)
93{
94 FILE *fp;
95 char line[1000];
96 int old_cnt = 0;
97
98 fp = fopen(infofile, "r");
99 if (!fp)
Pavel Roskinaddf88e2006-07-09 03:44:30 -0400100 return 1; /* nonexistent is not an error. */
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700101
102 while (fgets(line, sizeof(line), fp)) {
103 int len = strlen(line);
Jim Meyering872c9302008-01-04 18:37:41 +0100104 if (len && line[len-1] == '\n')
Junio C Hamano8ac48382005-12-21 13:48:47 -0800105 line[--len] = 0;
106
107 if (!len)
108 continue;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700109
110 switch (line[0]) {
111 case 'P': /* P name */
112 if (parse_pack_def(line, old_cnt++))
113 goto out_stale;
114 break;
Junio C Hamano6f42f892005-12-04 22:52:19 -0800115 case 'D': /* we used to emit D but that was misguided. */
116 goto out_stale;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700117 break;
Junio C Hamano3e15c672005-12-04 23:12:36 -0800118 case 'T': /* we used to emit T but nobody uses it. */
119 goto out_stale;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700120 break;
121 default:
122 error("unrecognized: %s", line);
123 break;
124 }
125 }
126 fclose(fp);
127 return 0;
128 out_stale:
129 fclose(fp);
130 return 1;
131}
132
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700133static int compare_info(const void *a_, const void *b_)
134{
135 struct pack_info * const* a = a_;
136 struct pack_info * const* b = b_;
137
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;
147
Junio C Hamanod5eac492005-12-04 23:02:54 -0800148 /* then it does not matter but at least keep the comparison stable */
Junio C Hamano2dee5812005-12-08 17:29:11 -0800149 if ((*a)->p == (*b)->p)
150 return 0;
151 else if ((*a)->p < (*b)->p)
152 return -1;
153 else
154 return 1;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700155}
156
157static void init_pack_info(const char *infofile, int force)
158{
159 struct packed_git *p;
160 int stale;
161 int i = 0;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700162
163 objdir = get_object_directory();
164 objdirlen = strlen(objdir);
165
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.
170 */
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800171 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700172 continue;
173 i++;
174 }
175 num_pack = i;
176 info = xcalloc(num_pack, sizeof(struct pack_info *));
177 for (i = 0, p = packed_git; p; p = p->next) {
Junio C Hamanof13d7db2005-12-05 10:39:17 -0800178 if (!p->pack_local)
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700179 continue;
Junio C Hamano6f42f892005-12-04 22:52:19 -0800180 info[i] = xcalloc(1, sizeof(struct pack_info));
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700181 info[i]->p = p;
182 info[i]->old_num = -1;
183 i++;
184 }
185
186 if (infofile && !force)
187 stale = read_pack_info_file(infofile);
188 else
189 stale = 1;
190
191 for (i = 0; i < num_pack; i++) {
192 if (stale) {
193 info[i]->old_num = -1;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700194 info[i]->nr_heads = 0;
195 }
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700196 }
197
Junio C Hamano6f42f892005-12-04 22:52:19 -0800198 /* renumber them */
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700199 qsort(info, num_pack, sizeof(info[0]), compare_info);
200 for (i = 0; i < num_pack; i++)
201 info[i]->new_num = i;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700202}
203
204static void write_pack_info_file(FILE *fp)
205{
Junio C Hamano3e15c672005-12-04 23:12:36 -0800206 int i;
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700207 for (i = 0; i < num_pack; i++)
208 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
Junio C Hamano21b1ace2005-12-21 12:09:17 -0800209 fputc('\n', fp);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700210}
211
212static int update_info_packs(int force)
213{
214 char infofile[PATH_MAX];
215 char name[PATH_MAX];
216 int namelen;
217 FILE *fp;
218
219 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
220 strcpy(name, infofile);
221 strcpy(name + namelen, "+");
222
223 init_pack_info(infofile, force);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700224
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);
Johannes Schindelin83525222007-07-11 15:18:17 +0100231 adjust_shared_perm(name);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700232 rename(name, infofile);
233 return 0;
234}
235
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700236/* public */
237int update_server_info(int force)
238{
239 /* We would add more dumb-server support files later,
240 * including index of available pack files and their
241 * intended audiences.
242 */
243 int errs = 0;
244
245 errs = errs | update_info_refs(force);
246 errs = errs | update_info_packs(force);
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700247
Junio C Hamano6f42f892005-12-04 22:52:19 -0800248 /* remove leftover rev-cache file if there is any */
249 unlink(git_path("info/rev-cache"));
250
Junio C Hamano8f3f9b02005-07-23 17:54:41 -0700251 return errs;
252}