]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-fetch--tool.c
6 #define CHUNK_SIZE 1024
8 static char *get_stdin(void)
11 char *data
= xmalloc(CHUNK_SIZE
);
14 ssize_t cnt
= xread(0, data
+ offset
, CHUNK_SIZE
);
16 die("error reading standard input: %s",
23 data
= xrealloc(data
, offset
+ CHUNK_SIZE
);
28 static void show_new(enum object_type type
, unsigned char *sha1_new
)
30 fprintf(stderr
, " %s: %s\n", typename(type
),
31 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
34 static int update_ref(const char *action
,
37 unsigned char *oldval
)
40 char *rla
= getenv("GIT_REFLOG_ACTION");
41 static struct ref_lock
*lock
;
44 rla
= "(reflog update)";
45 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
46 lock
= lock_any_ref_for_update(refname
, oldval
, 0);
49 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
54 static int update_local_ref(const char *name
,
57 int verbose
, int force
)
59 unsigned char sha1_old
[20], sha1_new
[20];
60 char oldh
[41], newh
[41];
61 struct commit
*current
, *updated
;
62 enum object_type type
;
64 if (get_sha1_hex(new_head
, sha1_new
))
65 die("malformed object name %s", new_head
);
67 type
= sha1_object_info(sha1_new
, NULL
);
69 die("object %s not found", new_head
);
74 fprintf(stderr
, "* fetched %s\n", note
);
75 show_new(type
, sha1_new
);
80 if (get_sha1(name
, sha1_old
)) {
84 if (!strncmp(name
, "refs/tags/", 10))
88 fprintf(stderr
, "* %s: storing %s\n",
90 show_new(type
, sha1_new
);
91 return update_ref(msg
, name
, sha1_new
, NULL
);
94 if (!hashcmp(sha1_old
, sha1_new
)) {
96 fprintf(stderr
, "* %s: same as %s\n", name
, note
);
97 show_new(type
, sha1_new
);
102 if (!strncmp(name
, "refs/tags/", 10)) {
103 fprintf(stderr
, "* %s: updating with %s\n", name
, note
);
104 show_new(type
, sha1_new
);
105 return update_ref("updating tag", name
, sha1_new
, NULL
);
108 current
= lookup_commit_reference(sha1_old
);
109 updated
= lookup_commit_reference(sha1_new
);
110 if (!current
|| !updated
)
113 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
114 strcpy(newh
, find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
116 if (in_merge_bases(current
, &updated
, 1)) {
117 fprintf(stderr
, "* %s: fast forward to %s\n",
119 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
120 return update_ref("fast forward", name
, sha1_new
, sha1_old
);
124 "* %s: not updating to non-fast forward %s\n",
127 " old...new: %s...%s\n", oldh
, newh
);
131 "* %s: forcing update to non-fast forward %s\n",
133 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
134 return update_ref("forced-update", name
, sha1_new
, sha1_old
);
137 static int append_fetch_head(FILE *fp
,
138 const char *head
, const char *remote
,
139 const char *remote_name
, const char *remote_nick
,
140 const char *local_name
, int not_for_merge
,
141 int verbose
, int force
)
143 struct commit
*commit
;
144 int remote_len
, i
, note_len
;
145 unsigned char sha1
[20];
147 const char *what
, *kind
;
149 if (get_sha1(head
, sha1
))
150 return error("Not a valid object name: %s", head
);
151 commit
= lookup_commit_reference(sha1
);
155 if (!strcmp(remote_name
, "HEAD")) {
159 else if (!strncmp(remote_name
, "refs/heads/", 11)) {
161 what
= remote_name
+ 11;
163 else if (!strncmp(remote_name
, "refs/tags/", 10)) {
165 what
= remote_name
+ 10;
167 else if (!strncmp(remote_name
, "refs/remotes/", 13)) {
168 kind
= "remote branch";
169 what
= remote_name
+ 13;
176 remote_len
= strlen(remote
);
177 for (i
= remote_len
- 1; remote
[i
] == '/' && 0 <= i
; i
--)
180 if (4 < i
&& !strncmp(".git", remote
+ i
- 3, 4))
186 note_len
+= sprintf(note
+ note_len
, "%s ", kind
);
187 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
189 note_len
+= sprintf(note
+ note_len
, "%.*s", remote_len
, remote
);
190 fprintf(fp
, "%s\t%s\t%s\n",
191 sha1_to_hex(commit
? commit
->object
.sha1
: sha1
),
192 not_for_merge
? "not-for-merge" : "",
194 return update_local_ref(local_name
, head
, note
, verbose
, force
);
198 static void remove_keep(void)
204 static void remove_keep_on_signal(int signo
)
207 signal(SIGINT
, SIG_DFL
);
211 static char *find_local_name(const char *remote_name
, const char *refs
,
212 int *force_p
, int *not_for_merge_p
)
214 const char *ref
= refs
;
215 int len
= strlen(remote_name
);
219 int single_force
, not_for_merge
;
225 next
= strchr(ref
, '\n');
227 single_force
= not_for_merge
= 0;
240 if (!strncmp(remote_name
, ref
, len
) && ref
[len
] == ':') {
241 const char *local_part
= ref
+ len
+ 1;
246 retlen
= strlen(local_part
);
248 retlen
= next
- local_part
;
249 ret
= xmalloc(retlen
+ 1);
250 memcpy(ret
, local_part
, retlen
);
252 *force_p
= single_force
;
253 *not_for_merge_p
= not_for_merge
;
261 static int fetch_native_store(FILE *fp
,
263 const char *remote_nick
,
265 int verbose
, int force
)
270 signal(SIGINT
, remove_keep_on_signal
);
273 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
277 int single_force
, not_for_merge
;
279 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
284 if (len
&& cp
[len
-1] == '\n')
286 if (!strcmp(buffer
, "failed"))
287 die("Fetch failure: %s", remote
);
288 if (!strcmp(buffer
, "pack"))
290 if (!strcmp(buffer
, "keep")) {
291 char *od
= get_object_directory();
292 int len
= strlen(od
) + strlen(cp
) + 50;
294 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
298 local_name
= find_local_name(cp
, refs
,
299 &single_force
, ¬_for_merge
);
302 err
|= append_fetch_head(fp
,
303 buffer
, remote
, cp
, remote_nick
,
304 local_name
, not_for_merge
,
305 verbose
, force
|| single_force
);
310 static int parse_reflist(const char *reflist
)
315 for (ref
= reflist
; ref
; ) {
317 while (*ref
&& isspace(*ref
))
321 for (next
= ref
; *next
&& !isspace(*next
); next
++)
323 printf("\n%.*s", (int)(next
- ref
), ref
);
329 for (ref
= reflist
; ref
; ) {
330 const char *next
, *colon
;
331 while (*ref
&& isspace(*ref
))
335 for (next
= ref
; *next
&& !isspace(*next
); next
++)
341 colon
= strchr(ref
, ':');
343 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
350 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
353 int i
, matchlen
, replacelen
;
355 const char *remote
= *refs
++;
359 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
364 for (i
= 0; i
< numrefs
; i
++) {
365 const char *ref
= refs
[i
];
366 const char *lref
= ref
;
374 colon
= strchr(lref
, ':');
375 tail
= lref
+ strlen(lref
);
380 2 < tail
- (colon
+ 1) &&
385 printf("explicit\n");
394 /* lref to colon-2 is remote hierarchy name;
395 * colon+1 to tail-2 is local.
397 matchlen
= (colon
-1) - lref
;
398 replacelen
= (tail
-1) - (colon
+1);
399 for (ls
= ls_remote_result
; ls
; ls
= next
) {
401 unsigned char sha1
[20];
404 while (*ls
&& isspace(*ls
))
406 next
= strchr(ls
, '\n');
407 eol
= !next
? (ls
+ strlen(ls
)) : next
;
408 if (!memcmp("^{}", eol
-3, 3))
412 if (get_sha1_hex(ls
, sha1
))
415 while (ls
< eol
&& isspace(*ls
))
417 /* ls to next (or eol) is the name.
418 * is it identical to lref to colon-2?
420 if ((eol
- ls
) <= matchlen
||
421 strncmp(ls
, lref
, matchlen
))
424 /* Yes, it is a match */
428 printf("%.*s:%.*s%.*s\n",
430 replacelen
, colon
+ 1,
431 namelen
- matchlen
, ls
+ matchlen
);
437 static int pick_rref(int sha1_only
, const char *rref
, const char *ls_remote_result
)
440 int lrr_count
= lrr_count
, i
, pass
;
447 } *lrr_list
= lrr_list
;
449 for (pass
= 0; pass
< 2; pass
++) {
450 /* pass 0 counts and allocates, pass 1 fills... */
451 cp
= ls_remote_result
;
455 while (*cp
&& isspace(*cp
))
459 np
= strchr(cp
, '\n');
461 np
= cp
+ strlen(cp
);
463 lrr_list
[i
].line
= cp
;
464 lrr_list
[i
].name
= cp
+ 41;
465 lrr_list
[i
].namelen
= np
- (cp
+ 41);
472 lrr_list
= xcalloc(lrr_count
, sizeof(*lrr_list
));
481 while (*rref
&& isspace(*rref
))
485 next
= strchr(rref
, '\n');
487 next
= rref
+ strlen(rref
);
488 rreflen
= next
- rref
;
490 for (i
= 0; i
< lrr_count
; i
++) {
491 struct lrr
*lrr
= &(lrr_list
[i
]);
493 if (rreflen
== lrr
->namelen
&&
494 !memcmp(lrr
->name
, rref
, rreflen
)) {
497 sha1_only
? 40 : lrr
->namelen
+ 41,
503 if (lrr_count
<= i
) {
504 error("pick-rref: %.*s not found", rreflen
, rref
);
513 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
520 const char *arg
= argv
[1];
521 if (!strcmp("-v", arg
))
523 else if (!strcmp("-f", arg
))
525 else if (!strcmp("-s", arg
))
534 return error("Missing subcommand");
536 if (!strcmp("append-fetch-head", argv
[1])) {
541 return error("append-fetch-head takes 6 args");
542 fp
= fopen(git_path("FETCH_HEAD"), "a");
543 result
= append_fetch_head(fp
, argv
[2], argv
[3],
545 argv
[6], !!argv
[7][0],
550 if (!strcmp("native-store", argv
[1])) {
555 return error("fetch-native-store takes 3 args");
556 fp
= fopen(git_path("FETCH_HEAD"), "a");
557 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
562 if (!strcmp("parse-reflist", argv
[1])) {
565 return error("parse-reflist takes 1 arg");
567 if (!strcmp(reflist
, "-"))
568 reflist
= get_stdin();
569 return parse_reflist(reflist
);
571 if (!strcmp("pick-rref", argv
[1])) {
572 const char *ls_remote_result
;
574 return error("pick-rref takes 2 args");
575 ls_remote_result
= argv
[3];
576 if (!strcmp(ls_remote_result
, "-"))
577 ls_remote_result
= get_stdin();
578 return pick_rref(sopt
, argv
[2], ls_remote_result
);
580 if (!strcmp("expand-refs-wildcard", argv
[1])) {
583 return error("expand-refs-wildcard takes at least 2 args");
585 if (!strcmp(reflist
, "-"))
586 reflist
= get_stdin();
587 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
590 return error("Unknown subcommand: %s", argv
[1]);