]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin-fetch--tool.c
5 #define CHUNK_SIZE 1024
7 static char *get_stdin(void)
10 char *data
= xmalloc(CHUNK_SIZE
);
13 int cnt
= xread(0, data
+ offset
, CHUNK_SIZE
);
15 die("error reading standard input: %s",
22 data
= xrealloc(data
, offset
+ CHUNK_SIZE
);
27 static void show_new(enum object_type type
, unsigned char *sha1_new
)
29 fprintf(stderr
, " %s: %s\n", typename(type
),
30 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
33 static int update_ref(const char *action
,
36 unsigned char *oldval
)
40 char *rla
= getenv("GIT_REFLOG_ACTION");
41 static struct ref_lock
*lock
;
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
);
51 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
56 static int update_local_ref(const char *name
,
59 int verbose
, int force
)
61 unsigned char sha1_old
[20], sha1_new
[20];
62 char oldh
[41], newh
[41];
63 struct commit
*current
, *updated
;
64 enum object_type type
;
66 if (get_sha1_hex(new_head
, sha1_new
))
67 die("malformed object name %s", new_head
);
69 type
= sha1_object_info(sha1_new
, NULL
);
71 die("object %s not found", new_head
);
76 fprintf(stderr
, "* fetched %s\n", note
);
77 show_new(type
, sha1_new
);
82 if (get_sha1(name
, sha1_old
)) {
86 if (!strncmp(name
, "refs/tags/", 10))
90 fprintf(stderr
, "* %s: storing %s\n",
92 show_new(type
, sha1_new
);
93 return update_ref(msg
, name
, sha1_new
, NULL
);
96 if (!hashcmp(sha1_old
, sha1_new
)) {
98 fprintf(stderr
, "* %s: same as %s\n", name
, note
);
99 show_new(type
, sha1_new
);
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
);
110 current
= lookup_commit_reference(sha1_old
);
111 updated
= lookup_commit_reference(sha1_new
);
112 if (!current
|| !updated
)
115 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
116 strcpy(newh
, find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
118 if (in_merge_bases(current
, &updated
, 1)) {
119 fprintf(stderr
, "* %s: fast forward to %s\n",
121 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
122 return update_ref("fast forward", name
, sha1_new
, sha1_old
);
126 "* %s: not updating to non-fast forward %s\n",
129 " old...new: %s...%s\n", oldh
, newh
);
133 "* %s: forcing update to non-fast forward %s\n",
135 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
136 return update_ref("forced-update", name
, sha1_new
, sha1_old
);
139 static 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
)
145 struct commit
*commit
;
146 int remote_len
, i
, note_len
;
147 unsigned char sha1
[20];
149 const char *what
, *kind
;
151 if (get_sha1(head
, sha1
))
152 return error("Not a valid object name: %s", head
);
153 commit
= lookup_commit_reference(sha1
);
157 if (!strcmp(remote_name
, "HEAD")) {
161 else if (!strncmp(remote_name
, "refs/heads/", 11)) {
163 what
= remote_name
+ 11;
165 else if (!strncmp(remote_name
, "refs/tags/", 10)) {
167 what
= remote_name
+ 10;
169 else if (!strncmp(remote_name
, "refs/remotes/", 13)) {
170 kind
= "remote branch";
171 what
= remote_name
+ 13;
178 remote_len
= strlen(remote
);
179 for (i
= remote_len
- 1; remote
[i
] == '/' && 0 <= i
; i
--)
182 if (4 < i
&& !strncmp(".git", remote
+ i
- 3, 4))
188 note_len
+= sprintf(note
+ note_len
, "%s ", kind
);
189 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
191 note_len
+= sprintf(note
+ note_len
, "%.*s", remote_len
, remote
);
192 fprintf(fp
, "%s\t%s\t%s\n",
193 sha1_to_hex(commit
? commit
->object
.sha1
: sha1
),
194 not_for_merge
? "not-for-merge" : "",
196 return update_local_ref(local_name
, head
, note
, verbose
, force
);
200 static void remove_keep(void)
206 static void remove_keep_on_signal(int signo
)
209 signal(SIGINT
, SIG_DFL
);
213 static char *find_local_name(const char *remote_name
, const char *refs
,
214 int *force_p
, int *not_for_merge_p
)
216 const char *ref
= refs
;
217 int len
= strlen(remote_name
);
221 int single_force
, not_for_merge
;
227 next
= strchr(ref
, '\n');
229 single_force
= not_for_merge
= 0;
242 if (!strncmp(remote_name
, ref
, len
) && ref
[len
] == ':') {
243 const char *local_part
= ref
+ len
+ 1;
248 retlen
= strlen(local_part
);
250 retlen
= next
- local_part
;
251 ret
= xmalloc(retlen
+ 1);
252 memcpy(ret
, local_part
, retlen
);
254 *force_p
= single_force
;
255 *not_for_merge_p
= not_for_merge
;
263 static int fetch_native_store(FILE *fp
,
265 const char *remote_nick
,
267 int verbose
, int force
)
272 signal(SIGINT
, remove_keep_on_signal
);
275 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
279 int single_force
, not_for_merge
;
281 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
286 if (len
&& cp
[len
-1] == '\n')
288 if (!strcmp(buffer
, "failed"))
289 die("Fetch failure: %s", remote
);
290 if (!strcmp(buffer
, "pack"))
292 if (!strcmp(buffer
, "keep")) {
293 char *od
= get_object_directory();
294 int len
= strlen(od
) + strlen(cp
) + 50;
296 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
300 local_name
= find_local_name(cp
, refs
,
301 &single_force
, ¬_for_merge
);
304 err
|= append_fetch_head(fp
,
305 buffer
, remote
, cp
, remote_nick
,
306 local_name
, not_for_merge
,
307 verbose
, force
|| single_force
);
312 static int parse_reflist(const char *reflist
)
317 for (ref
= reflist
; ref
; ) {
319 while (*ref
&& isspace(*ref
))
323 for (next
= ref
; *next
&& !isspace(*next
); next
++)
325 printf("\n%.*s", (int)(next
- ref
), ref
);
331 for (ref
= reflist
; ref
; ) {
332 const char *next
, *colon
;
333 while (*ref
&& isspace(*ref
))
337 for (next
= ref
; *next
&& !isspace(*next
); next
++)
343 colon
= strchr(ref
, ':');
345 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
352 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
355 int i
, matchlen
, replacelen
;
357 const char *remote
= *refs
++;
361 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
366 for (i
= 0; i
< numrefs
; i
++) {
367 const char *ref
= refs
[i
];
368 const char *lref
= ref
;
376 colon
= strchr(lref
, ':');
377 tail
= lref
+ strlen(lref
);
382 2 < tail
- (colon
+ 1) &&
387 printf("explicit\n");
396 /* lref to colon-2 is remote hierarchy name;
397 * colon+1 to tail-2 is local.
399 matchlen
= (colon
-1) - lref
;
400 replacelen
= (tail
-1) - (colon
+1);
401 for (ls
= ls_remote_result
; ls
; ls
= next
) {
403 unsigned char sha1
[20];
406 while (*ls
&& isspace(*ls
))
408 next
= strchr(ls
, '\n');
409 eol
= !next
? (ls
+ strlen(ls
)) : next
;
410 if (!memcmp("^{}", eol
-3, 3))
414 if (get_sha1_hex(ls
, sha1
))
417 while (ls
< eol
&& isspace(*ls
))
419 /* ls to next (or eol) is the name.
420 * is it identical to lref to colon-2?
422 if ((eol
- ls
) <= matchlen
||
423 strncmp(ls
, lref
, matchlen
))
426 /* Yes, it is a match */
430 printf("%.*s:%.*s%.*s\n",
432 replacelen
, colon
+ 1,
433 namelen
- matchlen
, ls
+ matchlen
);
439 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
445 const char *arg
= argv
[1];
446 if (!strcmp("-v", arg
))
448 else if (!strcmp("-f", arg
))
457 return error("Missing subcommand");
459 if (!strcmp("append-fetch-head", argv
[1])) {
464 return error("append-fetch-head takes 6 args");
465 fp
= fopen(git_path("FETCH_HEAD"), "a");
466 result
= append_fetch_head(fp
, argv
[2], argv
[3],
468 argv
[6], !!argv
[7][0],
473 if (!strcmp("native-store", argv
[1])) {
478 return error("fetch-native-store takes 3 args");
479 fp
= fopen(git_path("FETCH_HEAD"), "a");
480 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
485 if (!strcmp("parse-reflist", argv
[1])) {
488 return error("parse-reflist takes 1 arg");
490 if (!strcmp(reflist
, "-"))
491 reflist
= get_stdin();
492 return parse_reflist(reflist
);
494 if (!strcmp("expand-refs-wildcard", argv
[1])) {
497 return error("expand-refs-wildcard takes at least 2 args");
499 if (!strcmp(reflist
, "-"))
500 reflist
= get_stdin();
501 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
504 return error("Unknown subcommand: %s", argv
[1]);