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
);
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 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(sha1
);
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;
229 retlen
= strlen(local_part
);
231 retlen
= next
- local_part
;
232 ret
= xmalloc(retlen
+ 1);
233 memcpy(ret
, local_part
, retlen
);
235 *force_p
= single_force
;
236 *not_for_merge_p
= not_for_merge
;
244 static int fetch_native_store(FILE *fp
,
246 const char *remote_nick
,
248 int verbose
, int force
)
253 signal(SIGINT
, remove_keep_on_signal
);
256 while (fgets(buffer
, sizeof(buffer
), stdin
)) {
260 int single_force
, not_for_merge
;
262 for (cp
= buffer
; *cp
&& !isspace(*cp
); cp
++)
267 if (len
&& cp
[len
-1] == '\n')
269 if (!strcmp(buffer
, "failed"))
270 die("Fetch failure: %s", remote
);
271 if (!strcmp(buffer
, "pack"))
273 if (!strcmp(buffer
, "keep")) {
274 char *od
= get_object_directory();
275 int len
= strlen(od
) + strlen(cp
) + 50;
277 sprintf(keep
, "%s/pack/pack-%s.keep", od
, cp
);
281 local_name
= find_local_name(cp
, refs
,
282 &single_force
, ¬_for_merge
);
285 err
|= append_fetch_head(fp
,
286 buffer
, remote
, cp
, remote_nick
,
287 local_name
, not_for_merge
,
288 verbose
, force
|| single_force
);
293 static int parse_reflist(const char *reflist
)
298 for (ref
= reflist
; ref
; ) {
300 while (*ref
&& isspace(*ref
))
304 for (next
= ref
; *next
&& !isspace(*next
); next
++)
306 printf("\n%.*s", (int)(next
- ref
), ref
);
312 for (ref
= reflist
; ref
; ) {
313 const char *next
, *colon
;
314 while (*ref
&& isspace(*ref
))
318 for (next
= ref
; *next
&& !isspace(*next
); next
++)
324 colon
= strchr(ref
, ':');
326 printf("%.*s", (int)((colon
? colon
: next
) - ref
), ref
);
333 static int expand_refs_wildcard(const char *ls_remote_result
, int numrefs
,
336 int i
, matchlen
, replacelen
;
338 const char *remote
= *refs
++;
342 fprintf(stderr
, "Nothing specified for fetching with remote.%s.fetch\n",
347 for (i
= 0; i
< numrefs
; i
++) {
348 const char *ref
= refs
[i
];
349 const char *lref
= ref
;
357 colon
= strchr(lref
, ':');
358 tail
= lref
+ strlen(lref
);
363 2 < tail
- (colon
+ 1) &&
368 printf("explicit\n");
377 /* lref to colon-2 is remote hierarchy name;
378 * colon+1 to tail-2 is local.
380 matchlen
= (colon
-1) - lref
;
381 replacelen
= (tail
-1) - (colon
+1);
382 for (ls
= ls_remote_result
; ls
; ls
= next
) {
384 unsigned char sha1
[20];
387 while (*ls
&& isspace(*ls
))
389 next
= strchr(ls
, '\n');
390 eol
= !next
? (ls
+ strlen(ls
)) : next
;
391 if (!memcmp("^{}", eol
-3, 3))
395 if (get_sha1_hex(ls
, sha1
))
398 while (ls
< eol
&& isspace(*ls
))
400 /* ls to next (or eol) is the name.
401 * is it identical to lref to colon-2?
403 if ((eol
- ls
) <= matchlen
||
404 strncmp(ls
, lref
, matchlen
))
407 /* Yes, it is a match */
411 printf("%.*s:%.*s%.*s\n",
413 replacelen
, colon
+ 1,
414 namelen
- matchlen
, ls
+ matchlen
);
420 static int pick_rref(int sha1_only
, const char *rref
, const char *ls_remote_result
)
423 int lrr_count
= lrr_count
, i
, pass
;
430 } *lrr_list
= lrr_list
;
432 for (pass
= 0; pass
< 2; pass
++) {
433 /* pass 0 counts and allocates, pass 1 fills... */
434 cp
= ls_remote_result
;
438 while (*cp
&& isspace(*cp
))
442 np
= strchr(cp
, '\n');
444 np
= cp
+ strlen(cp
);
446 lrr_list
[i
].line
= cp
;
447 lrr_list
[i
].name
= cp
+ 41;
448 lrr_list
[i
].namelen
= np
- (cp
+ 41);
455 lrr_list
= xcalloc(lrr_count
, sizeof(*lrr_list
));
464 while (*rref
&& isspace(*rref
))
468 next
= strchr(rref
, '\n');
470 next
= rref
+ strlen(rref
);
471 rreflen
= next
- rref
;
473 for (i
= 0; i
< lrr_count
; i
++) {
474 struct lrr
*lrr
= &(lrr_list
[i
]);
476 if (rreflen
== lrr
->namelen
&&
477 !memcmp(lrr
->name
, rref
, rreflen
)) {
480 sha1_only
? 40 : lrr
->namelen
+ 41,
486 if (lrr_count
<= i
) {
487 error("pick-rref: %.*s not found", rreflen
, rref
);
496 int cmd_fetch__tool(int argc
, const char **argv
, const char *prefix
)
503 const char *arg
= argv
[1];
504 if (!strcmp("-v", arg
))
506 else if (!strcmp("-f", arg
))
508 else if (!strcmp("-s", arg
))
517 return error("Missing subcommand");
519 if (!strcmp("append-fetch-head", argv
[1])) {
524 return error("append-fetch-head takes 6 args");
525 fp
= fopen(git_path("FETCH_HEAD"), "a");
526 result
= append_fetch_head(fp
, argv
[2], argv
[3],
528 argv
[6], !!argv
[7][0],
533 if (!strcmp("native-store", argv
[1])) {
538 return error("fetch-native-store takes 3 args");
539 fp
= fopen(git_path("FETCH_HEAD"), "a");
540 result
= fetch_native_store(fp
, argv
[2], argv
[3], argv
[4],
545 if (!strcmp("parse-reflist", argv
[1])) {
548 return error("parse-reflist takes 1 arg");
550 if (!strcmp(reflist
, "-"))
551 reflist
= get_stdin();
552 return parse_reflist(reflist
);
554 if (!strcmp("pick-rref", argv
[1])) {
555 const char *ls_remote_result
;
557 return error("pick-rref takes 2 args");
558 ls_remote_result
= argv
[3];
559 if (!strcmp(ls_remote_result
, "-"))
560 ls_remote_result
= get_stdin();
561 return pick_rref(sopt
, argv
[2], ls_remote_result
);
563 if (!strcmp("expand-refs-wildcard", argv
[1])) {
566 return error("expand-refs-wildcard takes at least 2 args");
568 if (!strcmp(reflist
, "-"))
569 reflist
= get_stdin();
570 return expand_refs_wildcard(reflist
, argc
- 3, argv
+ 3);
573 return error("Unknown subcommand: %s", argv
[1]);