]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-fetch--tool.c
fetch--tool: fix uninitialized buffer when reading from stdin
[thirdparty/git.git] / builtin-fetch--tool.c
CommitLineData
d4289fff
JH
1#include "cache.h"
2#include "refs.h"
3#include "commit.h"
4
dec56c8c 5#define CHUNK_SIZE 1024
46ce8b6d
JP
6
7static char *get_stdin(void)
8{
dec56c8c 9 int offset = 0;
46ce8b6d 10 char *data = xmalloc(CHUNK_SIZE);
dec56c8c
JH
11
12 while (1) {
13 int cnt = xread(0, data + offset, CHUNK_SIZE);
14 if (cnt < 0)
15 die("error reading standard input: %s",
16 strerror(errno));
17 if (cnt == 0) {
18 data[offset] = 0;
19 break;
20 }
21 offset += cnt;
46ce8b6d 22 data = xrealloc(data, offset + CHUNK_SIZE);
46ce8b6d
JP
23 }
24 return data;
25}
26
dcf01c6e 27static void show_new(enum object_type type, unsigned char *sha1_new)
d4289fff 28{
dcf01c6e 29 fprintf(stderr, " %s: %s\n", typename(type),
d4289fff
JH
30 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
31}
32
33static int update_ref(const char *action,
34 const char *refname,
35 unsigned char *sha1,
36 unsigned char *oldval)
37{
38 int len;
39 char msg[1024];
40 char *rla = getenv("GIT_REFLOG_ACTION");
41 static struct ref_lock *lock;
42
43 if (!rla)
44 rla = "(reflog update)";
45 len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
46 if (sizeof(msg) <= len)
47 die("insanely long action");
48 lock = lock_any_ref_for_update(refname, oldval);
49 if (!lock)
50 return 1;
51 if (write_ref_sha1(lock, sha1, msg) < 0)
52 return 1;
53 return 0;
54}
55
56static int update_local_ref(const char *name,
57 const char *new_head,
58 const char *note,
59 int verbose, int force)
60{
d4289fff
JH
61 unsigned char sha1_old[20], sha1_new[20];
62 char oldh[41], newh[41];
63 struct commit *current, *updated;
dcf01c6e 64 enum object_type type;
d4289fff
JH
65
66 if (get_sha1_hex(new_head, sha1_new))
67 die("malformed object name %s", new_head);
dcf01c6e
JH
68
69 type = sha1_object_info(sha1_new, NULL);
70 if (type < 0)
d4289fff
JH
71 die("object %s not found", new_head);
72
73 if (!*name) {
74 /* Not storing */
75 if (verbose) {
76 fprintf(stderr, "* fetched %s\n", note);
77 show_new(type, sha1_new);
78 }
79 return 0;
80 }
81
82 if (get_sha1(name, sha1_old)) {
83 char *msg;
84 just_store:
85 /* new ref */
86 if (!strncmp(name, "refs/tags/", 10))
87 msg = "storing tag";
88 else
89 msg = "storing head";
90 fprintf(stderr, "* %s: storing %s\n",
91 name, note);
92 show_new(type, sha1_new);
93 return update_ref(msg, name, sha1_new, NULL);
94 }
95
96 if (!hashcmp(sha1_old, sha1_new)) {
97 if (verbose) {
98 fprintf(stderr, "* %s: same as %s\n", name, note);
99 show_new(type, sha1_new);
100 }
101 return 0;
102 }
103
104 if (!strncmp(name, "refs/tags/", 10)) {
105 fprintf(stderr, "* %s: updating with %s\n", name, note);
106 show_new(type, sha1_new);
107 return update_ref("updating tag", name, sha1_new, NULL);
108 }
109
110 current = lookup_commit_reference(sha1_old);
111 updated = lookup_commit_reference(sha1_new);
112 if (!current || !updated)
113 goto just_store;
114
115 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
116 strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
117
118 if (in_merge_bases(current, &updated, 1)) {
119 fprintf(stderr, "* %s: fast forward to %s\n",
120 name, note);
121 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
122 return update_ref("fast forward", name, sha1_new, sha1_old);
123 }
124 if (!force) {
125 fprintf(stderr,
126 "* %s: not updating to non-fast forward %s\n",
127 name, note);
128 fprintf(stderr,
129 " old...new: %s...%s\n", oldh, newh);
130 return 1;
131 }
132 fprintf(stderr,
133 "* %s: forcing update to non-fast forward %s\n",
134 name, note);
135 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
136 return update_ref("forced-update", name, sha1_new, sha1_old);
137}
138
139static int append_fetch_head(FILE *fp,
140 const char *head, const char *remote,
141 const char *remote_name, const char *remote_nick,
142 const char *local_name, int not_for_merge,
143 int verbose, int force)
144{
145 struct commit *commit;
146 int remote_len, i, note_len;
147 unsigned char sha1[20];
148 char note[1024];
149 const char *what, *kind;
150
151 if (get_sha1(head, sha1))
152 return error("Not a valid object name: %s", head);
153 commit = lookup_commit_reference(sha1);
154 if (!commit)
155 not_for_merge = 1;
156
157 if (!strcmp(remote_name, "HEAD")) {
158 kind = "";
159 what = "";
160 }
161 else if (!strncmp(remote_name, "refs/heads/", 11)) {
162 kind = "branch";
163 what = remote_name + 11;
164 }
165 else if (!strncmp(remote_name, "refs/tags/", 10)) {
166 kind = "tag";
167 what = remote_name + 10;
168 }
169 else if (!strncmp(remote_name, "refs/remotes/", 13)) {
170 kind = "remote branch";
171 what = remote_name + 13;
172 }
173 else {
174 kind = "";
175 what = remote_name;
176 }
177
178 remote_len = strlen(remote);
179 for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
180 ;
181 remote_len = i + 1;
182 if (4 < i && !strncmp(".git", remote + i - 3, 4))
183 remote_len = i - 3;
184 note_len = sprintf(note, "%s\t%s\t",
185 sha1_to_hex(commit ? commit->object.sha1 : sha1),
186 not_for_merge ? "not-for-merge" : "");
187 if (*what) {
188 if (*kind)
189 note_len += sprintf(note + note_len, "%s ", kind);
190 note_len += sprintf(note + note_len, "'%s' of ", what);
191 }
192 note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
193 fprintf(fp, "%s\n", note);
194 return update_local_ref(local_name, head, note, verbose, force);
195}
196
fbe2687e
JH
197static char *keep;
198static void remove_keep(void)
199{
200 if (keep && *keep)
201 unlink(keep);
202}
203
204static void remove_keep_on_signal(int signo)
205{
206 remove_keep();
207 signal(SIGINT, SIG_DFL);
208 raise(signo);
209}
210
211static char *find_local_name(const char *remote_name, const char *refs,
212 int *force_p, int *not_for_merge_p)
213{
214 const char *ref = refs;
215 int len = strlen(remote_name);
216
217 while (ref) {
218 const char *next;
219 int single_force, not_for_merge;
220
221 while (*ref == '\n')
222 ref++;
223 if (!*ref)
224 break;
225 next = strchr(ref, '\n');
226
227 single_force = not_for_merge = 0;
228 if (*ref == '+') {
229 single_force = 1;
230 ref++;
231 }
232 if (*ref == '.') {
233 not_for_merge = 1;
234 ref++;
235 if (*ref == '+') {
236 single_force = 1;
237 ref++;
238 }
239 }
240 if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
241 const char *local_part = ref + len + 1;
242 char *ret;
243 int retlen;
244
245 if (!next)
246 retlen = strlen(local_part);
247 else
248 retlen = next - local_part;
249 ret = xmalloc(retlen + 1);
250 memcpy(ret, local_part, retlen);
251 ret[retlen] = 0;
252 *force_p = single_force;
253 *not_for_merge_p = not_for_merge;
254 return ret;
255 }
256 ref = next;
257 }
258 return NULL;
259}
260
261static int fetch_native_store(FILE *fp,
262 const char *remote,
263 const char *remote_nick,
264 const char *refs,
265 int verbose, int force)
266{
267 char buffer[1024];
268 int err = 0;
269
270 signal(SIGINT, remove_keep_on_signal);
271 atexit(remove_keep);
272
273 while (fgets(buffer, sizeof(buffer), stdin)) {
274 int len;
275 char *cp;
276 char *local_name;
277 int single_force, not_for_merge;
278
279 for (cp = buffer; *cp && !isspace(*cp); cp++)
280 ;
281 if (*cp)
282 *cp++ = 0;
283 len = strlen(cp);
284 if (len && cp[len-1] == '\n')
285 cp[--len] = 0;
286 if (!strcmp(buffer, "failed"))
287 die("Fetch failure: %s", remote);
288 if (!strcmp(buffer, "pack"))
289 continue;
290 if (!strcmp(buffer, "keep")) {
291 char *od = get_object_directory();
292 int len = strlen(od) + strlen(cp) + 50;
293 keep = xmalloc(len);
294 sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
295 continue;
296 }
297
298 local_name = find_local_name(cp, refs,
299 &single_force, &not_for_merge);
300 if (!local_name)
301 continue;
302 err |= append_fetch_head(fp,
303 buffer, remote, cp, remote_nick,
304 local_name, not_for_merge,
305 verbose, force || single_force);
306 }
307 return err;
308}
309
d1e0ef6c
JH
310static int parse_reflist(const char *reflist)
311{
312 const char *ref;
313
314 printf("refs='");
315 for (ref = reflist; ref; ) {
316 const char *next;
317 while (*ref && isspace(*ref))
318 ref++;
319 if (!*ref)
320 break;
321 for (next = ref; *next && !isspace(*next); next++)
322 ;
323 printf("\n%.*s", (int)(next - ref), ref);
324 ref = next;
325 }
326 printf("'\n");
327
328 printf("rref='");
329 for (ref = reflist; ref; ) {
330 const char *next, *colon;
331 while (*ref && isspace(*ref))
332 ref++;
333 if (!*ref)
334 break;
335 for (next = ref; *next && !isspace(*next); next++)
336 ;
337 if (*ref == '.')
338 ref++;
339 if (*ref == '+')
340 ref++;
341 colon = strchr(ref, ':');
342 putchar('\n');
343 printf("%.*s", (int)((colon ? colon : next) - ref), ref);
344 ref = next;
345 }
346 printf("'\n");
347 return 0;
348}
349
86551586
JH
350static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
351 const char **refs)
352{
353 int i, matchlen, replacelen;
354 int found_one = 0;
355 const char *remote = *refs++;
356 numrefs--;
357
358 if (numrefs == 0) {
359 fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
360 remote);
361 printf("empty\n");
362 }
363
364 for (i = 0; i < numrefs; i++) {
365 const char *ref = refs[i];
366 const char *lref = ref;
367 const char *colon;
368 const char *tail;
369 const char *ls;
370 const char *next;
371
372 if (*lref == '+')
373 lref++;
374 colon = strchr(lref, ':');
375 tail = lref + strlen(lref);
376 if (!(colon &&
377 2 < colon - lref &&
378 colon[-1] == '*' &&
379 colon[-2] == '/' &&
380 2 < tail - (colon + 1) &&
381 tail[-1] == '*' &&
382 tail[-2] == '/')) {
383 /* not a glob */
384 if (!found_one++)
385 printf("explicit\n");
386 printf("%s\n", ref);
387 continue;
388 }
389
390 /* glob */
391 if (!found_one++)
392 printf("glob\n");
393
394 /* lref to colon-2 is remote hierarchy name;
395 * colon+1 to tail-2 is local.
396 */
397 matchlen = (colon-1) - lref;
398 replacelen = (tail-1) - (colon+1);
399 for (ls = ls_remote_result; ls; ls = next) {
400 const char *eol;
401 unsigned char sha1[20];
402 int namelen;
403
404 while (*ls && isspace(*ls))
405 ls++;
406 next = strchr(ls, '\n');
407 eol = !next ? (ls + strlen(ls)) : next;
408 if (!memcmp("^{}", eol-3, 3))
409 continue;
410 if (get_sha1_hex(ls, sha1))
411 continue;
412 ls += 40;
413 while (ls < eol && isspace(*ls))
414 ls++;
415 /* ls to next (or eol) is the name.
416 * is it identical to lref to colon-2?
417 */
418 if ((eol - ls) <= matchlen ||
419 strncmp(ls, lref, matchlen))
420 continue;
421
422 /* Yes, it is a match */
423 namelen = eol - ls;
424 if (lref != ref)
425 putchar('+');
426 printf("%.*s:%.*s%.*s\n",
427 namelen, ls,
428 replacelen, colon + 1,
429 namelen - matchlen, ls + matchlen);
430 }
431 }
432 return 0;
433}
434
d4289fff
JH
435int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
436{
437 int verbose = 0;
438 int force = 0;
439
440 while (1 < argc) {
441 const char *arg = argv[1];
442 if (!strcmp("-v", arg))
443 verbose = 1;
444 else if (!strcmp("-f", arg))
445 force = 1;
446 else
447 break;
448 argc--;
449 argv++;
450 }
451
452 if (argc <= 1)
453 return error("Missing subcommand");
454
455 if (!strcmp("append-fetch-head", argv[1])) {
456 int result;
457 FILE *fp;
458
459 if (argc != 8)
460 return error("append-fetch-head takes 6 args");
461 fp = fopen(git_path("FETCH_HEAD"), "a");
462 result = append_fetch_head(fp, argv[2], argv[3],
463 argv[4], argv[5],
464 argv[6], !!argv[7][0],
465 verbose, force);
466 fclose(fp);
467 return result;
468 }
469 if (!strcmp("update-local-ref", argv[1])) {
470 if (argc != 5)
471 return error("update-local-ref takes 3 args");
472 return update_local_ref(argv[2], argv[3], argv[4],
473 verbose, force);
474 }
fbe2687e
JH
475 if (!strcmp("native-store", argv[1])) {
476 int result;
477 FILE *fp;
478
479 if (argc != 5)
480 return error("fetch-native-store takes 3 args");
481 fp = fopen(git_path("FETCH_HEAD"), "a");
482 result = fetch_native_store(fp, argv[2], argv[3], argv[4],
483 verbose, force);
484 fclose(fp);
485 return result;
486 }
d1e0ef6c 487 if (!strcmp("parse-reflist", argv[1])) {
46ce8b6d 488 const char *reflist;
d1e0ef6c
JH
489 if (argc != 3)
490 return error("parse-reflist takes 1 arg");
46ce8b6d
JP
491 reflist = argv[2];
492 if (!strcmp(reflist, "-"))
493 reflist = get_stdin();
494 return parse_reflist(reflist);
d1e0ef6c 495 }
86551586 496 if (!strcmp("expand-refs-wildcard", argv[1])) {
46ce8b6d 497 const char *reflist;
86551586
JH
498 if (argc < 4)
499 return error("expand-refs-wildcard takes at least 2 args");
46ce8b6d
JP
500 reflist = argv[2];
501 if (!strcmp(reflist, "-"))
502 reflist = get_stdin();
503 return expand_refs_wildcard(reflist, argc - 3, argv + 3);
86551586 504 }
d1e0ef6c 505
d4289fff
JH
506 return error("Unknown subcommand: %s", argv[1]);
507}