Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 250 lines (217 sloc) 5.263 kb
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
1 #include "cache.h"
2 #include "refs.h"
3 #include "object.h"
4 #include "commit.h"
b614e3d server-info: do not complain if a tag points at a non-commit.
Junio C Hamano authored
5 #include "tag.h"
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
6
7 /* refs */
8 static FILE *info_ref_fp;
9
8da1977 Tell between packed, unpacked and symbolic refs.
Junio C Hamano authored
10 static int add_info_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
11 {
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
12 struct object *o = parse_object(sha1);
76f8a30 @spearce Don't coredump on bad refs in update-server-info.
spearce authored
13 if (!o)
14 return -1;
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
15
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
16 fprintf(info_ref_fp, "%s %s\n", sha1_to_hex(sha1), path);
1974632 Remove TYPE_* constant macros and use object_type enums consistently.
Linus Torvalds authored
17 if (o->type == OBJ_TAG) {
9534f40 Be careful when dereferencing tags.
Junio C Hamano authored
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);
f6b42a8 Show peeled onion from upload-pack and server-info.
Junio C Hamano authored
22 }
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
23 return 0;
24 }
25
26 static int update_info_refs(int force)
27 {
a4f34cb @raalkml Use git_pathdup instead of xstrdup(git_path(...))
raalkml authored
28 char *path0 = git_pathdup("info/refs");
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
29 int len = strlen(path0);
30 char *path1 = xmalloc(len + 2);
31
32 strcpy(path1, path0);
33 strcpy(path1 + len, "+");
34
35 safe_create_leading_directories(path0);
36 info_ref_fp = fopen(path1, "w");
37 if (!info_ref_fp)
84ef033 @andre-rosa Print the real filename that we failed to open.
andre-rosa authored
38 return error("unable to update %s", path1);
cb5d709 Add callback data to for_each_ref() family.
Junio C Hamano authored
39 for_each_ref(add_info_ref, NULL);
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
40 fclose(info_ref_fp);
8352522 @dscho Fix core.sharedRepository = 2
dscho authored
41 adjust_shared_perm(path1);
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
42 rename(path1, path0);
43 free(path0);
44 free(path1);
45 return 0;
46 }
47
48 /* packs */
4d8fa91 [PATCH] Fix sparse warnings
Linus Torvalds authored
49 static struct pack_info {
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
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;
60
61 static 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
73 /* Returns non-zero when we detect that the info in the
74 * old file is useless.
75 */
76 static 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 {
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
84 /* The file describes a pack that is no longer here */
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
85 return 1;
86 }
87 }
88
89 /* Returns non-zero when we detect that the info in the
90 * old file is useless.
91 */
92 static 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)
addf88e @proski Assorted typo fixes
proski authored
100 return 1; /* nonexistent is not an error. */
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
101
102 while (fgets(line, sizeof(line), fp)) {
103 int len = strlen(line);
872c930 @meyering Don't access line[-1] for a zero-length "line" from fgets.
meyering authored
104 if (len && line[len-1] == '\n')
8ac4838 server-info: skip empty lines.
Junio C Hamano authored
105 line[--len] = 0;
106
107 if (!len)
108 continue;
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
109
110 switch (line[0]) {
111 case 'P': /* P name */
112 if (parse_pack_def(line, old_cnt++))
113 goto out_stale;
114 break;
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
115 case 'D': /* we used to emit D but that was misguided. */
3e15c67 server-info: throw away T computation as well.
Junio C Hamano authored
116 case 'T': /* we used to emit T but nobody uses it. */
117 goto out_stale;
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
118 default:
119 error("unrecognized: %s", line);
120 break;
121 }
122 }
123 fclose(fp);
124 return 0;
125 out_stale:
126 fclose(fp);
127 return 1;
128 }
129
130 static int compare_info(const void *a_, const void *b_)
131 {
4b25d09 @felipec Fix a bunch of pointer declarations (codestyle)
felipec authored
132 struct pack_info *const *a = a_;
133 struct pack_info *const *b = b_;
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
134
135 if (0 <= (*a)->old_num && 0 <= (*b)->old_num)
136 /* Keep the order in the original */
137 return (*a)->old_num - (*b)->old_num;
138 else if (0 <= (*a)->old_num)
139 /* Only A existed in the original so B is obviously newer */
140 return -1;
141 else if (0 <= (*b)->old_num)
142 /* The other way around. */
143 return 1;
144
d5eac49 server-info: stop sorting packs by latest date.
Junio C Hamano authored
145 /* then it does not matter but at least keep the comparison stable */
2dee581 qsort(): ptrdiff_t may be larger than int
Junio C Hamano authored
146 if ((*a)->p == (*b)->p)
147 return 0;
148 else if ((*a)->p < (*b)->p)
149 return -1;
150 else
151 return 1;
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
152 }
153
154 static void init_pack_info(const char *infofile, int force)
155 {
156 struct packed_git *p;
157 int stale;
158 int i = 0;
159
160 objdir = get_object_directory();
161 objdirlen = strlen(objdir);
162
163 prepare_packed_git();
164 for (p = packed_git; p; p = p->next) {
165 /* we ignore things on alternate path since they are
166 * not available to the pullers in general.
167 */
f13d7db server-info.c: use pack_local like everybody else.
Junio C Hamano authored
168 if (!p->pack_local)
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
169 continue;
170 i++;
171 }
172 num_pack = i;
173 info = xcalloc(num_pack, sizeof(struct pack_info *));
174 for (i = 0, p = packed_git; p; p = p->next) {
f13d7db server-info.c: use pack_local like everybody else.
Junio C Hamano authored
175 if (!p->pack_local)
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
176 continue;
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
177 info[i] = xcalloc(1, sizeof(struct pack_info));
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
178 info[i]->p = p;
179 info[i]->old_num = -1;
180 i++;
181 }
182
183 if (infofile && !force)
184 stale = read_pack_info_file(infofile);
185 else
186 stale = 1;
187
188 for (i = 0; i < num_pack; i++) {
189 if (stale) {
190 info[i]->old_num = -1;
191 info[i]->nr_heads = 0;
192 }
193 }
194
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
195 /* renumber them */
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
196 qsort(info, num_pack, sizeof(info[0]), compare_info);
197 for (i = 0; i < num_pack; i++)
198 info[i]->new_num = i;
199 }
200
201 static void write_pack_info_file(FILE *fp)
202 {
3e15c67 server-info: throw away T computation as well.
Junio C Hamano authored
203 int i;
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
204 for (i = 0; i < num_pack; i++)
205 fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6);
21b1ace objects/info/packs: work around bug in http-fetch.c::fetch_indices()
Junio C Hamano authored
206 fputc('\n', fp);
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
207 }
208
209 static int update_info_packs(int force)
210 {
211 char infofile[PATH_MAX];
212 char name[PATH_MAX];
213 int namelen;
214 FILE *fp;
215
216 namelen = sprintf(infofile, "%s/info/packs", get_object_directory());
217 strcpy(name, infofile);
218 strcpy(name + namelen, "+");
219
220 init_pack_info(infofile, force);
221
222 safe_create_leading_directories(name);
223 fp = fopen(name, "w");
224 if (!fp)
225 return error("cannot open %s", name);
226 write_pack_info_file(fp);
227 fclose(fp);
8352522 @dscho Fix core.sharedRepository = 2
dscho authored
228 adjust_shared_perm(name);
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
229 rename(name, infofile);
230 return 0;
231 }
232
233 /* public */
234 int update_server_info(int force)
235 {
236 /* We would add more dumb-server support files later,
237 * including index of available pack files and their
238 * intended audiences.
239 */
240 int errs = 0;
241
242 errs = errs | update_info_refs(force);
243 errs = errs | update_info_packs(force);
244
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
245 /* remove leftover rev-cache file if there is any */
691f1a2 @raalkml replace direct calls to unlink(2) with unlink_or_warn
raalkml authored
246 unlink_or_warn(git_path("info/rev-cache"));
6f42f89 server-info.c: drop unused D lines.
Junio C Hamano authored
247
8f3f9b0 [PATCH] Add update-server-info.
Junio C Hamano authored
248 return errs;
249 }
Something went wrong with that request. Please try again.