6 static char *get_stdin(void)
10 if (strbuf_read(&buf
, 0, 1024) < 0) {
11 die("error reading standard input: %s", strerror(errno
));
13 return strbuf_detach(&buf
, NULL
);
16 static void show_new(enum object_type type
, unsigned char *sha1_new
)
18 fprintf(stderr
, " %s: %s\n", typename(type
),
19 find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
22 static int update_ref_env(const char *action
,
25 unsigned char *oldval
)
28 const char *rla
= getenv("GIT_REFLOG_ACTION");
31 rla
= "(reflog update)";
32 if (snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
) >= sizeof(msg
))
33 warning("reflog message too long: %.*s...", 50, msg
);
34 return update_ref(msg
, refname
, sha1
, oldval
, 0, QUIET_ON_ERR
);
37 static int update_local_ref(const char *name
,
40 int verbose
, int force
)
42 unsigned char sha1_old
[20], sha1_new
[20];
43 char oldh
[41], newh
[41];
44 struct commit
*current
, *updated
;
45 enum object_type type
;
47 if (get_sha1_hex(new_head
, sha1_new
))
48 die("malformed object name %s", new_head
);
50 type
= sha1_object_info(sha1_new
, NULL
);
52 die("object %s not found", new_head
);
57 fprintf(stderr
, "* fetched %s\n", note
);
58 show_new(type
, sha1_new
);
63 if (get_sha1(name
, sha1_old
)) {
67 if (!strncmp(name
, "refs/tags/", 10))
71 fprintf(stderr
, "* %s: storing %s\n",
73 show_new(type
, sha1_new
);
74 return update_ref_env(msg
, name
, sha1_new
, NULL
);
77 if (!hashcmp(sha1_old
, sha1_new
)) {
79 fprintf(stderr
, "* %s: same as %s\n", name
, note
);
80 show_new(type
, sha1_new
);
85 if (!strncmp(name
, "refs/tags/", 10)) {
86 fprintf(stderr
, "* %s: updating with %s\n", name
, note
);
87 show_new(type
, sha1_new
);
88 return update_ref_env("updating tag", name
, sha1_new
, NULL
);
91 current
= lookup_commit_reference(sha1_old
);
92 updated
= lookup_commit_reference(sha1_new
);
93 if (!current
|| !updated
)
96 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
97 strcpy(newh
, find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
99 if (in_merge_bases(current
, &updated
, 1)) {
100 fprintf(stderr
, "* %s: fast forward to %s\n",
102 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
103 return update_ref_env("fast forward", name
, sha1_new
, sha1_old
);
107 "* %s: not updating to non-fast forward %s\n",
110 " old...new: %s...%s\n", oldh
, newh
);
114 "* %s: forcing update to non-fast forward %s\n",
116 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
117 return update_ref_env("forced-update", name
, sha1_new
, sha1_old
);
120 static int append_fetch_head(FILE *fp
,
121 const char *head
, const char *remote
,
122 const char *remote_name
, const char *remote_nick
,
123 const char *local_name
, int not_for_merge
,
124 int verbose
, int force
)
126 struct commit
*commit
;
127 int remote_len
, i
, note_len
;
128 unsigned char sha1
[20];
130 const char *what
, *kind
;
132 if (get_sha1(head
, sha1
))
133 return error("Not a valid object name: %s", head
);
134 commit
= lookup_commit_reference_gently(sha1
, 1);
138 if (!strcmp(remote_name
, "HEAD")) {
142 else if (!strncmp(remote_name
, "refs/heads/", 11)) {
144 what
= remote_name
+ 11;
146 else if (!strncmp(remote_name
, "refs/tags/", 10)) {
148 what
= remote_name
+ 10;
150 else if (!strncmp(remote_name
, "refs/remotes/", 13)) {
151 kind
= "remote branch";
152 what
= remote_name
+ 13;
159 remote_len
= strlen(remote
);
160 for (i
= remote_len
- 1; remote
[i
] == '/' && 0 <= i
; i
--)
163 if (4 < i
&& !strncmp(".git", remote
+ i
- 3, 4))
169 note_len
+= sprintf(note
+ note_len
, "%s ", kind
);
170 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
172 note_len
+= sprintf(note
+ note_len
, "%.*s", remote_len
, remote
);
173 fprintf(fp
, "%s\t%s\t%s\n",
174 sha1_to_hex(commit
? commit
->object
.sha1
: sha1
),
175 not_for_merge
? "not-for-merge" : "",
177 return update_local_ref(local_name
, head
, note
, verbose
, force
);
181 static void remove_keep(void)
187 static void remove_keep_on_signal(int signo
)
190 signal(SIGINT
, SIG_DFL
);
194 static char *find_local_name(const char *remote_name
, const char *refs
,
195 int *force_p
, int *not_for_merge_p
)
197 const char *ref
= refs
;
198 int len
= strlen(remote_name
);
202 int single_force
, not_for_merge
;
208 next
= strchr(ref
, '\n');
210 single_force
= not_for_merge
= 0;
223 if (!strncmp(remote_name
, ref
, len
) && ref
[len
] == ':') {
224 const char *local_part
= ref
+ len
+ 1;
228 retlen
= strlen(local_part
);
230 retlen
= next
- local_part
;
231 *force_p
= single_force
;
232 *not_for_merge_p
= not_for_merge
;
233 return xmemdupz(local_part
, retlen
);
240 static int fetch_native_store(FILE *fp
,
242 const char *remote_nick
,
244 int verbose
, int force
)
249 signal(SIGINT
, remove_keep_on_signal
);
252 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
256 int single_force
, not_for_merge
;
258 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
263 if (len
&& cp
[len
-1] == '\n')
265 if (!strcmp(buffer
, "failed"))
266 die("Fetch failure: %s", remote
);
267 if (!strcmp(buffer
, "pack"))
269 if (!strcmp(buffer
, "keep")) {
270 char *od
= get_object_directory();
271 int len
= strlen(od
) + strlen(cp
) + 50;
273 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
277 local_name
= find_local_name(cp
, refs
,
278 &single_force
, ¬_for_merge
);
281 err
|= append_fetch_head(fp
,
282 buffer
, remote
, cp
, remote_nick
,
283 local_name
, not_for_merge
,
284 verbose
, force
|| single_force
);
289 static int parse_reflist(const char *reflist
)
294 for (ref
= reflist
; ref
; ) {
296 while (*ref
&& isspace(*ref
))
300 for (next
= ref
; *next
&& !isspace(*next
); next
++)
302 printf("\n%.*s", (int)(next
- ref
), ref
);
308 for (ref
= reflist
; ref
; ) {
309 const char *next
, *colon
;
310 while (*ref
&& isspace(*ref
))
314 for (next
= ref
; *next
&& !isspace(*next
); next
++)
320 colon
= strchr(ref
, ':');
322 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
329 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
332 int i
, matchlen
, replacelen
;
334 const char *remote
= *refs
++;
338 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
343 for (i
= 0; i
< numrefs
; i
++) {
344 const char *ref
= refs
[i
];
345 const char *lref
= ref
;
353 colon
= strchr(lref
, ':');
354 tail
= lref
+ strlen(lref
);
359 2 < tail
- (colon
+ 1) &&
364 printf("explicit\n");
373 /* lref to colon-2 is remote hierarchy name;
374 * colon+1 to tail-2 is local.
376 matchlen
= (colon
-1) - lref
;
377 replacelen
= (tail
-1) - (colon
+1);
378 for (ls
= ls_remote_result
; ls
; ls
= next
) {
380 unsigned char sha1
[20];
383 while (*ls
&& isspace(*ls
))
385 next
= strchr(ls
, '\n');
386 eol
= !next
? (ls
+ strlen(ls
)) : next
;
387 if (!memcmp("^{}", eol
-3, 3))
391 if (get_sha1_hex(ls
, sha1
))
394 while (ls
< eol
&& isspace(*ls
))
396 /* ls to next (or eol) is the name.
397 * is it identical to lref to colon-2?
399 if ((eol
- ls
) <= matchlen
||
400 strncmp(ls
, lref
, matchlen
))
403 /* Yes, it is a match */
407 printf("%.*s:%.*s%.*s\n",
409 replacelen
, colon
+ 1,
410 namelen
- matchlen
, ls
+ matchlen
);
416 static int pick_rref(int sha1_only
, const char *rref
, const char *ls_remote_result
)
419 int lrr_count
= lrr_count
, i
, pass
;
426 } *lrr_list
= lrr_list
;
428 for (pass
= 0; pass
< 2; pass
++) {
429 /* pass 0 counts and allocates, pass 1 fills... */
430 cp
= ls_remote_result
;
434 while (*cp
&& isspace(*cp
))
438 np
= strchr(cp
, '\n');
440 np
= cp
+ strlen(cp
);
442 lrr_list
[i
].line
= cp
;
443 lrr_list
[i
].name
= cp
+ 41;
444 lrr_list
[i
].namelen
= np
- (cp
+ 41);
451 lrr_list
= xcalloc(lrr_count
, sizeof(*lrr_list
));
460 while (*rref
&& isspace(*rref
))
464 next
= strchr(rref
, '\n');
466 next
= rref
+ strlen(rref
);
467 rreflen
= next
- rref
;
469 for (i
= 0; i
< lrr_count
; i
++) {
470 struct lrr
*lrr
= &(lrr_list
[i
]);
472 if (rreflen
== lrr
->namelen
&&
473 !memcmp(lrr
->name
, rref
, rreflen
)) {
476 sha1_only
? 40 : lrr
->namelen
+ 41,
482 if (lrr_count
<= i
) {
483 error("pick-rref: %.*s not found", rreflen
, rref
);
492 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
499 const char *arg
= argv
[1];
500 if (!strcmp("-v", arg
))
502 else if (!strcmp("-f", arg
))
504 else if (!strcmp("-s", arg
))
513 return error("Missing subcommand");
515 if (!strcmp("append-fetch-head", argv
[1])) {
520 return error("append-fetch-head takes 6 args");
521 fp
= fopen(git_path("FETCH_HEAD"), "a");
522 result
= append_fetch_head(fp
, argv
[2], argv
[3],
524 argv
[6], !!argv
[7][0],
529 if (!strcmp("native-store", argv
[1])) {
534 return error("fetch-native-store takes 3 args");
535 fp
= fopen(git_path("FETCH_HEAD"), "a");
536 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
541 if (!strcmp("parse-reflist", argv
[1])) {
544 return error("parse-reflist takes 1 arg");
546 if (!strcmp(reflist
, "-"))
547 reflist
= get_stdin();
548 return parse_reflist(reflist
);
550 if (!strcmp("pick-rref", argv
[1])) {
551 const char *ls_remote_result
;
553 return error("pick-rref takes 2 args");
554 ls_remote_result
= argv
[3];
555 if (!strcmp(ls_remote_result
, "-"))
556 ls_remote_result
= get_stdin();
557 return pick_rref(sopt
, argv
[2], ls_remote_result
);
559 if (!strcmp("expand-refs-wildcard", argv
[1])) {
562 return error("expand-refs-wildcard takes at least 2 args");
564 if (!strcmp(reflist
, "-"))
565 reflist
= get_stdin();
566 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
569 return error("Unknown subcommand: %s", argv
[1]);