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