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
)
39 char *rla
= getenv("GIT_REFLOG_ACTION");
40 static struct ref_lock
*lock
;
43 rla
= "(reflog update)";
44 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
45 lock
= lock_any_ref_for_update(refname
, oldval
);
48 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
53 static int update_local_ref(const char *name
,
56 int verbose
, int force
)
58 unsigned char sha1_old
[20], sha1_new
[20];
59 char oldh
[41], newh
[41];
60 struct commit
*current
, *updated
;
61 enum object_type type
;
63 if (get_sha1_hex(new_head
, sha1_new
))
64 die("malformed object name %s", new_head
);
66 type
= sha1_object_info(sha1_new
, NULL
);
68 die("object %s not found", new_head
);
73 fprintf(stderr
, "* fetched %s\n", note
);
74 show_new(type
, sha1_new
);
79 if (get_sha1(name
, sha1_old
)) {
83 if (!strncmp(name
, "refs/tags/", 10))
87 fprintf(stderr
, "* %s: storing %s\n",
89 show_new(type
, sha1_new
);
90 return update_ref(msg
, name
, sha1_new
, NULL
);
93 if (!hashcmp(sha1_old
, sha1_new
)) {
95 fprintf(stderr
, "* %s: same as %s\n", name
, note
);
96 show_new(type
, sha1_new
);
101 if (!strncmp(name
, "refs/tags/", 10)) {
102 fprintf(stderr
, "* %s: updating with %s\n", name
, note
);
103 show_new(type
, sha1_new
);
104 return update_ref("updating tag", name
, sha1_new
, NULL
);
107 current
= lookup_commit_reference(sha1_old
);
108 updated
= lookup_commit_reference(sha1_new
);
109 if (!current
|| !updated
)
112 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
113 strcpy(newh
, find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
115 if (in_merge_bases(current
, &updated
, 1)) {
116 fprintf(stderr
, "* %s: fast forward to %s\n",
118 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
119 return update_ref("fast forward", name
, sha1_new
, sha1_old
);
123 "* %s: not updating to non-fast forward %s\n",
126 " old...new: %s...%s\n", oldh
, newh
);
130 "* %s: forcing update to non-fast forward %s\n",
132 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
133 return update_ref("forced-update", name
, sha1_new
, sha1_old
);
136 static int append_fetch_head(FILE *fp
,
137 const char *head
, const char *remote
,
138 const char *remote_name
, const char *remote_nick
,
139 const char *local_name
, int not_for_merge
,
140 int verbose
, int force
)
142 struct commit
*commit
;
143 int remote_len
, i
, note_len
;
144 unsigned char sha1
[20];
146 const char *what
, *kind
;
148 if (get_sha1(head
, sha1
))
149 return error("Not a valid object name: %s", head
);
150 commit
= lookup_commit_reference(sha1
);
154 if (!strcmp(remote_name
, "HEAD")) {
158 else if (!strncmp(remote_name
, "refs/heads/", 11)) {
160 what
= remote_name
+ 11;
162 else if (!strncmp(remote_name
, "refs/tags/", 10)) {
164 what
= remote_name
+ 10;
166 else if (!strncmp(remote_name
, "refs/remotes/", 13)) {
167 kind
= "remote branch";
168 what
= remote_name
+ 13;
175 remote_len
= strlen(remote
);
176 for (i
= remote_len
- 1; remote
[i
] == '/' && 0 <= i
; i
--)
179 if (4 < i
&& !strncmp(".git", remote
+ i
- 3, 4))
185 note_len
+= sprintf(note
+ note_len
, "%s ", kind
);
186 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
188 note_len
+= sprintf(note
+ note_len
, "%.*s", remote_len
, remote
);
189 fprintf(fp
, "%s\t%s\t%s\n",
190 sha1_to_hex(commit
? commit
->object
.sha1
: sha1
),
191 not_for_merge
? "not-for-merge" : "",
193 return update_local_ref(local_name
, head
, note
, verbose
, force
);
197 static void remove_keep(void)
203 static void remove_keep_on_signal(int signo
)
206 signal(SIGINT
, SIG_DFL
);
210 static char *find_local_name(const char *remote_name
, const char *refs
,
211 int *force_p
, int *not_for_merge_p
)
213 const char *ref
= refs
;
214 int len
= strlen(remote_name
);
218 int single_force
, not_for_merge
;
224 next
= strchr(ref
, '\n');
226 single_force
= not_for_merge
= 0;
239 if (!strncmp(remote_name
, ref
, len
) && ref
[len
] == ':') {
240 const char *local_part
= ref
+ len
+ 1;
245 retlen
= strlen(local_part
);
247 retlen
= next
- local_part
;
248 ret
= xmalloc(retlen
+ 1);
249 memcpy(ret
, local_part
, retlen
);
251 *force_p
= single_force
;
252 *not_for_merge_p
= not_for_merge
;
260 static int fetch_native_store(FILE *fp
,
262 const char *remote_nick
,
264 int verbose
, int force
)
269 signal(SIGINT
, remove_keep_on_signal
);
272 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
276 int single_force
, not_for_merge
;
278 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
283 if (len
&& cp
[len
-1] == '\n')
285 if (!strcmp(buffer
, "failed"))
286 die("Fetch failure: %s", remote
);
287 if (!strcmp(buffer
, "pack"))
289 if (!strcmp(buffer
, "keep")) {
290 char *od
= get_object_directory();
291 int len
= strlen(od
) + strlen(cp
) + 50;
293 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
297 local_name
= find_local_name(cp
, refs
,
298 &single_force
, ¬_for_merge
);
301 err
|= append_fetch_head(fp
,
302 buffer
, remote
, cp
, remote_nick
,
303 local_name
, not_for_merge
,
304 verbose
, force
|| single_force
);
309 static int parse_reflist(const char *reflist
)
314 for (ref
= reflist
; ref
; ) {
316 while (*ref
&& isspace(*ref
))
320 for (next
= ref
; *next
&& !isspace(*next
); next
++)
322 printf("\n%.*s", (int)(next
- ref
), ref
);
328 for (ref
= reflist
; ref
; ) {
329 const char *next
, *colon
;
330 while (*ref
&& isspace(*ref
))
334 for (next
= ref
; *next
&& !isspace(*next
); next
++)
340 colon
= strchr(ref
, ':');
342 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
349 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
352 int i
, matchlen
, replacelen
;
354 const char *remote
= *refs
++;
358 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
363 for (i
= 0; i
< numrefs
; i
++) {
364 const char *ref
= refs
[i
];
365 const char *lref
= ref
;
373 colon
= strchr(lref
, ':');
374 tail
= lref
+ strlen(lref
);
379 2 < tail
- (colon
+ 1) &&
384 printf("explicit\n");
393 /* lref to colon-2 is remote hierarchy name;
394 * colon+1 to tail-2 is local.
396 matchlen
= (colon
-1) - lref
;
397 replacelen
= (tail
-1) - (colon
+1);
398 for (ls
= ls_remote_result
; ls
; ls
= next
) {
400 unsigned char sha1
[20];
403 while (*ls
&& isspace(*ls
))
405 next
= strchr(ls
, '\n');
406 eol
= !next
? (ls
+ strlen(ls
)) : next
;
407 if (!memcmp("^{}", eol
-3, 3))
411 if (get_sha1_hex(ls
, sha1
))
414 while (ls
< eol
&& isspace(*ls
))
416 /* ls to next (or eol) is the name.
417 * is it identical to lref to colon-2?
419 if ((eol
- ls
) <= matchlen
||
420 strncmp(ls
, lref
, matchlen
))
423 /* Yes, it is a match */
427 printf("%.*s:%.*s%.*s\n",
429 replacelen
, colon
+ 1,
430 namelen
- matchlen
, ls
+ matchlen
);
436 static int pick_rref(int sha1_only
, const char *rref
, const char *ls_remote_result
)
439 int lrr_count
= lrr_count
, i
, pass
;
446 } *lrr_list
= lrr_list
;
448 for (pass
= 0; pass
< 2; pass
++) {
449 /* pass 0 counts and allocates, pass 1 fills... */
450 cp
= ls_remote_result
;
454 while (*cp
&& isspace(*cp
))
458 np
= strchr(cp
, '\n');
460 np
= cp
+ strlen(cp
);
462 lrr_list
[i
].line
= cp
;
463 lrr_list
[i
].name
= cp
+ 41;
464 lrr_list
[i
].namelen
= np
- (cp
+ 41);
471 lrr_list
= xcalloc(lrr_count
, sizeof(*lrr_list
));
480 while (*rref
&& isspace(*rref
))
484 next
= strchr(rref
, '\n');
486 next
= rref
+ strlen(rref
);
487 rreflen
= next
- rref
;
489 for (i
= 0; i
< lrr_count
; i
++) {
490 struct lrr
*lrr
= &(lrr_list
[i
]);
492 if (rreflen
== lrr
->namelen
&&
493 !memcmp(lrr
->name
, rref
, rreflen
)) {
496 sha1_only
? 40 : lrr
->namelen
+ 41,
502 if (lrr_count
<= i
) {
503 error("pick-rref: %.*s not found", rreflen
, rref
);
512 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
519 const char *arg
= argv
[1];
520 if (!strcmp("-v", arg
))
522 else if (!strcmp("-f", arg
))
524 else if (!strcmp("-s", arg
))
533 return error("Missing subcommand");
535 if (!strcmp("append-fetch-head", argv
[1])) {
540 return error("append-fetch-head takes 6 args");
541 fp
= fopen(git_path("FETCH_HEAD"), "a");
542 result
= append_fetch_head(fp
, argv
[2], argv
[3],
544 argv
[6], !!argv
[7][0],
549 if (!strcmp("native-store", argv
[1])) {
554 return error("fetch-native-store takes 3 args");
555 fp
= fopen(git_path("FETCH_HEAD"), "a");
556 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
561 if (!strcmp("parse-reflist", argv
[1])) {
564 return error("parse-reflist takes 1 arg");
566 if (!strcmp(reflist
, "-"))
567 reflist
= get_stdin();
568 return parse_reflist(reflist
);
570 if (!strcmp("pick-rref", argv
[1])) {
571 const char *ls_remote_result
;
573 return error("pick-rref takes 2 args");
574 ls_remote_result
= argv
[3];
575 if (!strcmp(ls_remote_result
, "-"))
576 ls_remote_result
= get_stdin();
577 return pick_rref(sopt
, argv
[2], ls_remote_result
);
579 if (!strcmp("expand-refs-wildcard", argv
[1])) {
582 return error("expand-refs-wildcard takes at least 2 args");
584 if (!strcmp(reflist
, "-"))
585 reflist
= get_stdin();
586 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
589 return error("Unknown subcommand: %s", argv
[1]);