7 static char *get_stdin(void)
9 struct strbuf buf
= STRBUF_INIT
;
10 if (strbuf_read(&buf
, 0, 1024) < 0) {
11 die_errno("error reading standard input");
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,
35 UPDATE_REFS_QUIET_ON_ERR
);
38 static int update_local_ref(const char *name
,
41 int verbose
, int force
)
43 unsigned char sha1_old
[20], sha1_new
[20];
44 char oldh
[41], newh
[41];
45 struct commit
*current
, *updated
;
46 enum object_type type
;
48 if (get_sha1_hex(new_head
, sha1_new
))
49 die("malformed object name %s", new_head
);
51 type
= sha1_object_info(sha1_new
, NULL
);
53 die("object %s not found", new_head
);
58 fprintf(stderr
, "* fetched %s\n", note
);
59 show_new(type
, sha1_new
);
64 if (get_sha1(name
, sha1_old
)) {
68 if (!strncmp(name
, "refs/tags/", 10))
72 fprintf(stderr
, "* %s: storing %s\n",
74 show_new(type
, sha1_new
);
75 return update_ref_env(msg
, name
, sha1_new
, NULL
);
78 if (!hashcmp(sha1_old
, sha1_new
)) {
80 fprintf(stderr
, "* %s: same as %s\n", name
, note
);
81 show_new(type
, sha1_new
);
86 if (!strncmp(name
, "refs/tags/", 10)) {
87 fprintf(stderr
, "* %s: updating with %s\n", name
, note
);
88 show_new(type
, sha1_new
);
89 return update_ref_env("updating tag", name
, sha1_new
, NULL
);
92 current
= lookup_commit_reference(sha1_old
);
93 updated
= lookup_commit_reference(sha1_new
);
94 if (!current
|| !updated
)
97 strcpy(oldh
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
98 strcpy(newh
, find_unique_abbrev(sha1_new
, DEFAULT_ABBREV
));
100 if (in_merge_bases(current
, updated
)) {
101 fprintf(stderr
, "* %s: fast-forward to %s\n",
103 fprintf(stderr
, " old..new: %s..%s\n", oldh
, newh
);
104 return update_ref_env("fast-forward", name
, sha1_new
, sha1_old
);
108 "* %s: not updating to non-fast-forward %s\n",
111 " old...new: %s...%s\n", oldh
, newh
);
115 "* %s: forcing update to non-fast-forward %s\n",
117 fprintf(stderr
, " old...new: %s...%s\n", oldh
, newh
);
118 return update_ref_env("forced-update", name
, sha1_new
, sha1_old
);
121 static int append_fetch_head(FILE *fp
,
122 const char *head
, const char *remote
,
123 const char *remote_name
, const char *remote_nick
,
124 const char *local_name
, int not_for_merge
,
125 int verbose
, int force
)
127 struct commit
*commit
;
128 int remote_len
, i
, note_len
;
129 unsigned char sha1
[20];
131 const char *what
, *kind
;
133 if (get_sha1(head
, sha1
))
134 return error("Not a valid object name: %s", head
);
135 commit
= lookup_commit_reference_gently(sha1
, 1);
139 if (!strcmp(remote_name
, "HEAD")) {
143 else if (!strncmp(remote_name
, "refs/heads/", 11)) {
145 what
= remote_name
+ 11;
147 else if (!strncmp(remote_name
, "refs/tags/", 10)) {
149 what
= remote_name
+ 10;
151 else if (!strncmp(remote_name
, "refs/remotes/", 13)) {
152 kind
= "remote-tracking branch";
153 what
= remote_name
+ 13;
160 remote_len
= strlen(remote
);
161 for (i
= remote_len
- 1; remote
[i
] == '/' && 0 <= i
; i
--)
164 if (4 < i
&& !strncmp(".git", remote
+ i
- 3, 4))
170 note_len
+= sprintf(note
+ note_len
, "%s ", kind
);
171 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
173 note_len
+= sprintf(note
+ note_len
, "%.*s", remote_len
, remote
);
174 fprintf(fp
, "%s\t%s\t%s\n",
175 sha1_to_hex(commit
? commit
->object
.sha1
: sha1
),
176 not_for_merge
? "not-for-merge" : "",
178 return update_local_ref(local_name
, head
, note
, verbose
, force
);
182 static void remove_keep(void)
188 static void remove_keep_on_signal(int signo
)
195 static char *find_local_name(const char *remote_name
, const char *refs
,
196 int *force_p
, int *not_for_merge_p
)
198 const char *ref
= refs
;
199 int len
= strlen(remote_name
);
203 int single_force
, not_for_merge
;
209 next
= strchr(ref
, '\n');
211 single_force
= not_for_merge
= 0;
224 if (!strncmp(remote_name
, ref
, len
) && ref
[len
] == ':') {
225 const char *local_part
= ref
+ len
+ 1;
229 retlen
= strlen(local_part
);
231 retlen
= next
- local_part
;
232 *force_p
= single_force
;
233 *not_for_merge_p
= not_for_merge
;
234 return xmemdupz(local_part
, retlen
);
241 static int fetch_native_store(FILE *fp
,
243 const char *remote_nick
,
245 int verbose
, int force
)
250 sigchain_push_common(remove_keep_on_signal
);
253 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
257 int single_force
, not_for_merge
;
259 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
264 if (len
&& cp
[len
-1] == '\n')
266 if (!strcmp(buffer
, "failed"))
267 die("Fetch failure: %s", remote
);
268 if (!strcmp(buffer
, "pack"))
270 if (!strcmp(buffer
, "keep")) {
271 char *od
= get_object_directory();
272 int len
= strlen(od
) + strlen(cp
) + 50;
274 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
278 local_name
= find_local_name(cp
, refs
,
279 &single_force
, ¬_for_merge
);
282 err
|= append_fetch_head(fp
,
283 buffer
, remote
, cp
, remote_nick
,
284 local_name
, not_for_merge
,
285 verbose
, force
|| single_force
);
290 static int parse_reflist(const char *reflist
)
295 for (ref
= reflist
; ref
; ) {
297 while (*ref
&& isspace(*ref
))
301 for (next
= ref
; *next
&& !isspace(*next
); next
++)
303 printf("\n%.*s", (int)(next
- ref
), ref
);
309 for (ref
= reflist
; ref
; ) {
310 const char *next
, *colon
;
311 while (*ref
&& isspace(*ref
))
315 for (next
= ref
; *next
&& !isspace(*next
); next
++)
321 colon
= strchr(ref
, ':');
323 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
330 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
333 int i
, matchlen
, replacelen
;
335 const char *remote
= *refs
++;
339 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
344 for (i
= 0; i
< numrefs
; i
++) {
345 const char *ref
= refs
[i
];
346 const char *lref
= ref
;
354 colon
= strchr(lref
, ':');
355 tail
= lref
+ strlen(lref
);
360 2 < tail
- (colon
+ 1) &&
365 printf("explicit\n");
374 /* lref to colon-2 is remote hierarchy name;
375 * colon+1 to tail-2 is local.
377 matchlen
= (colon
-1) - lref
;
378 replacelen
= (tail
-1) - (colon
+1);
379 for (ls
= ls_remote_result
; ls
; ls
= next
) {
381 unsigned char sha1
[20];
384 while (*ls
&& isspace(*ls
))
386 next
= strchr(ls
, '\n');
387 eol
= !next
? (ls
+ strlen(ls
)) : next
;
388 if (!memcmp("^{}", eol
-3, 3))
392 if (get_sha1_hex(ls
, sha1
))
395 while (ls
< eol
&& isspace(*ls
))
397 /* ls to next (or eol) is the name.
398 * is it identical to lref to colon-2?
400 if ((eol
- ls
) <= matchlen
||
401 strncmp(ls
, lref
, matchlen
))
404 /* Yes, it is a match */
408 printf("%.*s:%.*s%.*s\n",
410 replacelen
, colon
+ 1,
411 namelen
- matchlen
, ls
+ matchlen
);
417 static int pick_rref(int sha1_only
, const char *rref
, const char *ls_remote_result
)
420 int lrr_count
= lrr_count
, i
, pass
;
427 } *lrr_list
= lrr_list
;
429 for (pass
= 0; pass
< 2; pass
++) {
430 /* pass 0 counts and allocates, pass 1 fills... */
431 cp
= ls_remote_result
;
435 while (*cp
&& isspace(*cp
))
439 np
= strchrnul(cp
, '\n');
441 lrr_list
[i
].line
= cp
;
442 lrr_list
[i
].name
= cp
+ 41;
443 lrr_list
[i
].namelen
= np
- (cp
+ 41);
450 lrr_list
= xcalloc(lrr_count
, sizeof(*lrr_list
));
459 while (*rref
&& isspace(*rref
))
463 next
= strchrnul(rref
, '\n');
464 rreflen
= next
- rref
;
466 for (i
= 0; i
< lrr_count
; i
++) {
467 struct lrr
*lrr
= &(lrr_list
[i
]);
469 if (rreflen
== lrr
->namelen
&&
470 !memcmp(lrr
->name
, rref
, rreflen
)) {
473 sha1_only
? 40 : lrr
->namelen
+ 41,
479 if (lrr_count
<= i
) {
480 error("pick-rref: %.*s not found", rreflen
, rref
);
489 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
496 const char *arg
= argv
[1];
497 if (!strcmp("-v", arg
))
499 else if (!strcmp("-f", arg
))
501 else if (!strcmp("-s", arg
))
510 return error("Missing subcommand");
512 if (!strcmp("append-fetch-head", argv
[1])) {
518 return error("append-fetch-head takes 6 args");
519 filename
= git_path("FETCH_HEAD");
520 fp
= fopen(filename
, "a");
522 return error("cannot open %s: %s", filename
, strerror(errno
));
523 result
= append_fetch_head(fp
, argv
[2], argv
[3],
525 argv
[6], !!argv
[7][0],
530 if (!strcmp("native-store", argv
[1])) {
536 return error("fetch-native-store takes 3 args");
537 filename
= git_path("FETCH_HEAD");
538 fp
= fopen(filename
, "a");
540 return error("cannot open %s: %s", filename
, strerror(errno
));
541 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
546 if (!strcmp("parse-reflist", argv
[1])) {
549 return error("parse-reflist takes 1 arg");
551 if (!strcmp(reflist
, "-"))
552 reflist
= get_stdin();
553 return parse_reflist(reflist
);
555 if (!strcmp("pick-rref", argv
[1])) {
556 const char *ls_remote_result
;
558 return error("pick-rref takes 2 args");
559 ls_remote_result
= argv
[3];
560 if (!strcmp(ls_remote_result
, "-"))
561 ls_remote_result
= get_stdin();
562 return pick_rref(sopt
, argv
[2], ls_remote_result
);
564 if (!strcmp("expand-refs-wildcard", argv
[1])) {
567 return error("expand-refs-wildcard takes at least 2 args");
569 if (!strcmp(reflist
, "-"))
570 reflist
= get_stdin();
571 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
574 return error("Unknown subcommand: %s", argv
[1]);