1#include "cache.h"
2#include "refs.h"
3#include "commit.h"
45
#define CHUNK_SIZE 1024
67
static char *get_stdin(void)
8{
9int offset = 0;
10char *data = xmalloc(CHUNK_SIZE);
1112
while (1) {
13int cnt = xread(0, data + offset, CHUNK_SIZE);
14if (cnt < 0)
15die("error reading standard input: %s",
16strerror(errno));
17if (cnt == 0) {
18data[offset] = 0;
19break;
20}
21offset += cnt;
22data = xrealloc(data, offset + CHUNK_SIZE);
23}
24return data;
25}
2627
static void show_new(enum object_type type, unsigned char *sha1_new)
28{
29fprintf(stderr, " %s: %s\n", typename(type),
30find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
31}
3233
static int update_ref(const char *action,
34const char *refname,
35unsigned char *sha1,
36unsigned char *oldval)
37{
38int len;
39char msg[1024];
40char *rla = getenv("GIT_REFLOG_ACTION");
41static struct ref_lock *lock;
4243
if (!rla)
44rla = "(reflog update)";
45len = snprintf(msg, sizeof(msg), "%s: %s", rla, action);
46if (sizeof(msg) <= len)
47die("insanely long action");
48lock = lock_any_ref_for_update(refname, oldval);
49if (!lock)
50return 1;
51if (write_ref_sha1(lock, sha1, msg) < 0)
52return 1;
53return 0;
54}
5556
static int update_local_ref(const char *name,
57const char *new_head,
58const char *note,
59int verbose, int force)
60{
61unsigned char sha1_old[20], sha1_new[20];
62char oldh[41], newh[41];
63struct commit *current, *updated;
64enum object_type type;
6566
if (get_sha1_hex(new_head, sha1_new))
67die("malformed object name %s", new_head);
6869
type = sha1_object_info(sha1_new, NULL);
70if (type < 0)
71die("object %s not found", new_head);
7273
if (!*name) {
74/* Not storing */
75if (verbose) {
76fprintf(stderr, "* fetched %s\n", note);
77show_new(type, sha1_new);
78}
79return 0;
80}
8182
if (get_sha1(name, sha1_old)) {
83char *msg;
84just_store:
85/* new ref */
86if (!strncmp(name, "refs/tags/", 10))
87msg = "storing tag";
88else
89msg = "storing head";
90fprintf(stderr, "* %s: storing %s\n",
91name, note);
92show_new(type, sha1_new);
93return update_ref(msg, name, sha1_new, NULL);
94}
9596
if (!hashcmp(sha1_old, sha1_new)) {
97if (verbose) {
98fprintf(stderr, "* %s: same as %s\n", name, note);
99show_new(type, sha1_new);
100}
101return 0;
102}
103104
if (!strncmp(name, "refs/tags/", 10)) {
105fprintf(stderr, "* %s: updating with %s\n", name, note);
106show_new(type, sha1_new);
107return update_ref("updating tag", name, sha1_new, NULL);
108}
109110
current = lookup_commit_reference(sha1_old);
111updated = lookup_commit_reference(sha1_new);
112if (!current || !updated)
113goto just_store;
114115
strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
116strcpy(newh, find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
117118
if (in_merge_bases(current, &updated, 1)) {
119fprintf(stderr, "* %s: fast forward to %s\n",
120name, note);
121fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
122return update_ref("fast forward", name, sha1_new, sha1_old);
123}
124if (!force) {
125fprintf(stderr,
126"* %s: not updating to non-fast forward %s\n",
127name, note);
128fprintf(stderr,
129" old...new: %s...%s\n", oldh, newh);
130return 1;
131}
132fprintf(stderr,
133"* %s: forcing update to non-fast forward %s\n",
134name, note);
135fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
136return update_ref("forced-update", name, sha1_new, sha1_old);
137}
138139
static int append_fetch_head(FILE *fp,
140const char *head, const char *remote,
141const char *remote_name, const char *remote_nick,
142const char *local_name, int not_for_merge,
143int verbose, int force)
144{
145struct commit *commit;
146int remote_len, i, note_len;
147unsigned char sha1[20];
148char note[1024];
149const char *what, *kind;
150151
if (get_sha1(head, sha1))
152return error("Not a valid object name: %s", head);
153commit = lookup_commit_reference(sha1);
154if (!commit)
155not_for_merge = 1;
156157
if (!strcmp(remote_name, "HEAD")) {
158kind = "";
159what = "";
160}
161else if (!strncmp(remote_name, "refs/heads/", 11)) {
162kind = "branch";
163what = remote_name + 11;
164}
165else if (!strncmp(remote_name, "refs/tags/", 10)) {
166kind = "tag";
167what = remote_name + 10;
168}
169else if (!strncmp(remote_name, "refs/remotes/", 13)) {
170kind = "remote branch";
171what = remote_name + 13;
172}
173else {
174kind = "";
175what = remote_name;
176}
177178
remote_len = strlen(remote);
179for (i = remote_len - 1; remote[i] == '/' && 0 <= i; i--)
180;
181remote_len = i + 1;
182if (4 < i && !strncmp(".git", remote + i - 3, 4))
183remote_len = i - 3;
184185
note_len = 0;
186if (*what) {
187if (*kind)
188note_len += sprintf(note + note_len, "%s ", kind);
189note_len += sprintf(note + note_len, "'%s' of ", what);
190}
191note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
192fprintf(fp, "%s\t%s\t%s\n",
193sha1_to_hex(commit ? commit->object.sha1 : sha1),
194not_for_merge ? "not-for-merge" : "",
195note);
196return update_local_ref(local_name, head, note, verbose, force);
197}
198199
static char *keep;
200static void remove_keep(void)
201{
202if (keep && *keep)
203unlink(keep);
204}
205206
static void remove_keep_on_signal(int signo)
207{
208remove_keep();
209signal(SIGINT, SIG_DFL);
210raise(signo);
211}
212213
static char *find_local_name(const char *remote_name, const char *refs,
214int *force_p, int *not_for_merge_p)
215{
216const char *ref = refs;
217int len = strlen(remote_name);
218219
while (ref) {
220const char *next;
221int single_force, not_for_merge;
222223
while (*ref == '\n')
224ref++;
225if (!*ref)
226break;
227next = strchr(ref, '\n');
228229
single_force = not_for_merge = 0;
230if (*ref == '+') {
231single_force = 1;
232ref++;
233}
234if (*ref == '.') {
235not_for_merge = 1;
236ref++;
237if (*ref == '+') {
238single_force = 1;
239ref++;
240}
241}
242if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
243const char *local_part = ref + len + 1;
244char *ret;
245int retlen;
246247
if (!next)
248retlen = strlen(local_part);
249else
250retlen = next - local_part;
251ret = xmalloc(retlen + 1);
252memcpy(ret, local_part, retlen);
253ret[retlen] = 0;
254*force_p = single_force;
255*not_for_merge_p = not_for_merge;
256return ret;
257}
258ref = next;
259}
260return NULL;
261}
262263
static int fetch_native_store(FILE *fp,
264const char *remote,
265const char *remote_nick,
266const char *refs,
267int verbose, int force)
268{
269char buffer[1024];
270int err = 0;
271272
signal(SIGINT, remove_keep_on_signal);
273atexit(remove_keep);
274275
while (fgets(buffer, sizeof(buffer), stdin)) {
276int len;
277char *cp;
278char *local_name;
279int single_force, not_for_merge;
280281
for (cp = buffer; *cp && !isspace(*cp); cp++)
282;
283if (*cp)
284*cp++ = 0;
285len = strlen(cp);
286if (len && cp[len-1] == '\n')
287cp[--len] = 0;
288if (!strcmp(buffer, "failed"))
289die("Fetch failure: %s", remote);
290if (!strcmp(buffer, "pack"))
291continue;
292if (!strcmp(buffer, "keep")) {
293char *od = get_object_directory();
294int len = strlen(od) + strlen(cp) + 50;
295keep = xmalloc(len);
296sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
297continue;
298}
299300
local_name = find_local_name(cp, refs,
301&single_force, ¬_for_merge);
302if (!local_name)
303continue;
304err |= append_fetch_head(fp,
305buffer, remote, cp, remote_nick,
306local_name, not_for_merge,
307verbose, force || single_force);
308}
309return err;
310}
311312
static int parse_reflist(const char *reflist)
313{
314const char *ref;
315316
printf("refs='");
317for (ref = reflist; ref; ) {
318const char *next;
319while (*ref && isspace(*ref))
320ref++;
321if (!*ref)
322break;
323for (next = ref; *next && !isspace(*next); next++)
324;
325printf("\n%.*s", (int)(next - ref), ref);
326ref = next;
327}
328printf("'\n");
329330
printf("rref='");
331for (ref = reflist; ref; ) {
332const char *next, *colon;
333while (*ref && isspace(*ref))
334ref++;
335if (!*ref)
336break;
337for (next = ref; *next && !isspace(*next); next++)
338;
339if (*ref == '.')
340ref++;
341if (*ref == '+')
342ref++;
343colon = strchr(ref, ':');
344putchar('\n');
345printf("%.*s", (int)((colon ? colon : next) - ref), ref);
346ref = next;
347}
348printf("'\n");
349return 0;
350}
351352
static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
353const char **refs)
354{
355int i, matchlen, replacelen;
356int found_one = 0;
357const char *remote = *refs++;
358numrefs--;
359360
if (numrefs == 0) {
361fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
362remote);
363printf("empty\n");
364}
365366
for (i = 0; i < numrefs; i++) {
367const char *ref = refs[i];
368const char *lref = ref;
369const char *colon;
370const char *tail;
371const char *ls;
372const char *next;
373374
if (*lref == '+')
375lref++;
376colon = strchr(lref, ':');
377tail = lref + strlen(lref);
378if (!(colon &&
3792 < colon - lref &&
380colon[-1] == '*' &&
381colon[-2] == '/' &&
3822 < tail - (colon + 1) &&
383tail[-1] == '*' &&
384tail[-2] == '/')) {
385/* not a glob */
386if (!found_one++)
387printf("explicit\n");
388printf("%s\n", ref);
389continue;
390}
391392
/* glob */
393if (!found_one++)
394printf("glob\n");
395396
/* lref to colon-2 is remote hierarchy name;
397* colon+1 to tail-2 is local.
398*/
399matchlen = (colon-1) - lref;
400replacelen = (tail-1) - (colon+1);
401for (ls = ls_remote_result; ls; ls = next) {
402const char *eol;
403unsigned char sha1[20];
404int namelen;
405406
while (*ls && isspace(*ls))
407ls++;
408next = strchr(ls, '\n');
409eol = !next ? (ls + strlen(ls)) : next;
410if (!memcmp("^{}", eol-3, 3))
411continue;
412if (eol - ls < 40)
413continue;
414if (get_sha1_hex(ls, sha1))
415continue;
416ls += 40;
417while (ls < eol && isspace(*ls))
418ls++;
419/* ls to next (or eol) is the name.
420* is it identical to lref to colon-2?
421*/
422if ((eol - ls) <= matchlen ||
423strncmp(ls, lref, matchlen))
424continue;
425426
/* Yes, it is a match */
427namelen = eol - ls;
428if (lref != ref)
429putchar('+');
430printf("%.*s:%.*s%.*s\n",
431namelen, ls,
432replacelen, colon + 1,
433namelen - matchlen, ls + matchlen);
434}
435}
436return 0;
437}
438439
int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
440{
441int verbose = 0;
442int force = 0;
443444
while (1 < argc) {
445const char *arg = argv[1];
446if (!strcmp("-v", arg))
447verbose = 1;
448else if (!strcmp("-f", arg))
449force = 1;
450else
451break;
452argc--;
453argv++;
454}
455456
if (argc <= 1)
457return error("Missing subcommand");
458459
if (!strcmp("append-fetch-head", argv[1])) {
460int result;
461FILE *fp;
462463
if (argc != 8)
464return error("append-fetch-head takes 6 args");
465fp = fopen(git_path("FETCH_HEAD"), "a");
466result = append_fetch_head(fp, argv[2], argv[3],
467argv[4], argv[5],
468argv[6], !!argv[7][0],
469verbose, force);
470fclose(fp);
471return result;
472}
473if (!strcmp("native-store", argv[1])) {
474int result;
475FILE *fp;
476477
if (argc != 5)
478return error("fetch-native-store takes 3 args");
479fp = fopen(git_path("FETCH_HEAD"), "a");
480result = fetch_native_store(fp, argv[2], argv[3], argv[4],
481verbose, force);
482fclose(fp);
483return result;
484}
485if (!strcmp("parse-reflist", argv[1])) {
486const char *reflist;
487if (argc != 3)
488return error("parse-reflist takes 1 arg");
489reflist = argv[2];
490if (!strcmp(reflist, "-"))
491reflist = get_stdin();
492return parse_reflist(reflist);
493}
494if (!strcmp("expand-refs-wildcard", argv[1])) {
495const char *reflist;
496if (argc < 4)
497return error("expand-refs-wildcard takes at least 2 args");
498reflist = argv[2];
499if (!strcmp(reflist, "-"))
500reflist = get_stdin();
501return expand_refs_wildcard(reflist, argc - 3, argv + 3);
502}
503504
return error("Unknown subcommand: %s", argv[1]);
505}