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;
184note_len = sprintf(note, "%s\t%s\t",
185sha1_to_hex(commit ? commit->object.sha1 : sha1),
186not_for_merge ? "not-for-merge" : "");
187if (*what) {
188if (*kind)
189note_len += sprintf(note + note_len, "%s ", kind);
190note_len += sprintf(note + note_len, "'%s' of ", what);
191}
192note_len += sprintf(note + note_len, "%.*s", remote_len, remote);
193fprintf(fp, "%s\n", note);
194return update_local_ref(local_name, head, note, verbose, force);
195}
196197
static char *keep;
198static void remove_keep(void)
199{
200if (keep && *keep)
201unlink(keep);
202}
203204
static void remove_keep_on_signal(int signo)
205{
206remove_keep();
207signal(SIGINT, SIG_DFL);
208raise(signo);
209}
210211
static char *find_local_name(const char *remote_name, const char *refs,
212int *force_p, int *not_for_merge_p)
213{
214const char *ref = refs;
215int len = strlen(remote_name);
216217
while (ref) {
218const char *next;
219int single_force, not_for_merge;
220221
while (*ref == '\n')
222ref++;
223if (!*ref)
224break;
225next = strchr(ref, '\n');
226227
single_force = not_for_merge = 0;
228if (*ref == '+') {
229single_force = 1;
230ref++;
231}
232if (*ref == '.') {
233not_for_merge = 1;
234ref++;
235if (*ref == '+') {
236single_force = 1;
237ref++;
238}
239}
240if (!strncmp(remote_name, ref, len) && ref[len] == ':') {
241const char *local_part = ref + len + 1;
242char *ret;
243int retlen;
244245
if (!next)
246retlen = strlen(local_part);
247else
248retlen = next - local_part;
249ret = xmalloc(retlen + 1);
250memcpy(ret, local_part, retlen);
251ret[retlen] = 0;
252*force_p = single_force;
253*not_for_merge_p = not_for_merge;
254return ret;
255}
256ref = next;
257}
258return NULL;
259}
260261
static int fetch_native_store(FILE *fp,
262const char *remote,
263const char *remote_nick,
264const char *refs,
265int verbose, int force)
266{
267char buffer[1024];
268int err = 0;
269270
signal(SIGINT, remove_keep_on_signal);
271atexit(remove_keep);
272273
while (fgets(buffer, sizeof(buffer), stdin)) {
274int len;
275char *cp;
276char *local_name;
277int single_force, not_for_merge;
278279
for (cp = buffer; *cp && !isspace(*cp); cp++)
280;
281if (*cp)
282*cp++ = 0;
283len = strlen(cp);
284if (len && cp[len-1] == '\n')
285cp[--len] = 0;
286if (!strcmp(buffer, "failed"))
287die("Fetch failure: %s", remote);
288if (!strcmp(buffer, "pack"))
289continue;
290if (!strcmp(buffer, "keep")) {
291char *od = get_object_directory();
292int len = strlen(od) + strlen(cp) + 50;
293keep = xmalloc(len);
294sprintf(keep, "%s/pack/pack-%s.keep", od, cp);
295continue;
296}
297298
local_name = find_local_name(cp, refs,
299&single_force, ¬_for_merge);
300if (!local_name)
301continue;
302err |= append_fetch_head(fp,
303buffer, remote, cp, remote_nick,
304local_name, not_for_merge,
305verbose, force || single_force);
306}
307return err;
308}
309310
static int parse_reflist(const char *reflist)
311{
312const char *ref;
313314
printf("refs='");
315for (ref = reflist; ref; ) {
316const char *next;
317while (*ref && isspace(*ref))
318ref++;
319if (!*ref)
320break;
321for (next = ref; *next && !isspace(*next); next++)
322;
323printf("\n%.*s", (int)(next - ref), ref);
324ref = next;
325}
326printf("'\n");
327328
printf("rref='");
329for (ref = reflist; ref; ) {
330const char *next, *colon;
331while (*ref && isspace(*ref))
332ref++;
333if (!*ref)
334break;
335for (next = ref; *next && !isspace(*next); next++)
336;
337if (*ref == '.')
338ref++;
339if (*ref == '+')
340ref++;
341colon = strchr(ref, ':');
342putchar('\n');
343printf("%.*s", (int)((colon ? colon : next) - ref), ref);
344ref = next;
345}
346printf("'\n");
347return 0;
348}
349350
static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
351const char **refs)
352{
353int i, matchlen, replacelen;
354int found_one = 0;
355const char *remote = *refs++;
356numrefs--;
357358
if (numrefs == 0) {
359fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
360remote);
361printf("empty\n");
362}
363364
for (i = 0; i < numrefs; i++) {
365const char *ref = refs[i];
366const char *lref = ref;
367const char *colon;
368const char *tail;
369const char *ls;
370const char *next;
371372
if (*lref == '+')
373lref++;
374colon = strchr(lref, ':');
375tail = lref + strlen(lref);
376if (!(colon &&
3772 < colon - lref &&
378colon[-1] == '*' &&
379colon[-2] == '/' &&
3802 < tail - (colon + 1) &&
381tail[-1] == '*' &&
382tail[-2] == '/')) {
383/* not a glob */
384if (!found_one++)
385printf("explicit\n");
386printf("%s\n", ref);
387continue;
388}
389390
/* glob */
391if (!found_one++)
392printf("glob\n");
393394
/* lref to colon-2 is remote hierarchy name;
395* colon+1 to tail-2 is local.
396*/
397matchlen = (colon-1) - lref;
398replacelen = (tail-1) - (colon+1);
399for (ls = ls_remote_result; ls; ls = next) {
400const char *eol;
401unsigned char sha1[20];
402int namelen;
403404
while (*ls && isspace(*ls))
405ls++;
406next = strchr(ls, '\n');
407eol = !next ? (ls + strlen(ls)) : next;
408if (!memcmp("^{}", eol-3, 3))
409continue;
410if (get_sha1_hex(ls, sha1))
411continue;
412ls += 40;
413while (ls < eol && isspace(*ls))
414ls++;
415/* ls to next (or eol) is the name.
416* is it identical to lref to colon-2?
417*/
418if ((eol - ls) <= matchlen ||
419strncmp(ls, lref, matchlen))
420continue;
421422
/* Yes, it is a match */
423namelen = eol - ls;
424if (lref != ref)
425putchar('+');
426printf("%.*s:%.*s%.*s\n",
427namelen, ls,
428replacelen, colon + 1,
429namelen - matchlen, ls + matchlen);
430}
431}
432return 0;
433}
434435
int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
436{
437int verbose = 0;
438int force = 0;
439440
while (1 < argc) {
441const char *arg = argv[1];
442if (!strcmp("-v", arg))
443verbose = 1;
444else if (!strcmp("-f", arg))
445force = 1;
446else
447break;
448argc--;
449argv++;
450}
451452
if (argc <= 1)
453return error("Missing subcommand");
454455
if (!strcmp("append-fetch-head", argv[1])) {
456int result;
457FILE *fp;
458459
if (argc != 8)
460return error("append-fetch-head takes 6 args");
461fp = fopen(git_path("FETCH_HEAD"), "a");
462result = append_fetch_head(fp, argv[2], argv[3],
463argv[4], argv[5],
464argv[6], !!argv[7][0],
465verbose, force);
466fclose(fp);
467return result;
468}
469if (!strcmp("update-local-ref", argv[1])) {
470if (argc != 5)
471return error("update-local-ref takes 3 args");
472return update_local_ref(argv[2], argv[3], argv[4],
473verbose, force);
474}
475if (!strcmp("native-store", argv[1])) {
476int result;
477FILE *fp;
478479
if (argc != 5)
480return error("fetch-native-store takes 3 args");
481fp = fopen(git_path("FETCH_HEAD"), "a");
482result = fetch_native_store(fp, argv[2], argv[3], argv[4],
483verbose, force);
484fclose(fp);
485return result;
486}
487if (!strcmp("parse-reflist", argv[1])) {
488const char *reflist;
489if (argc != 3)
490return error("parse-reflist takes 1 arg");
491reflist = argv[2];
492if (!strcmp(reflist, "-"))
493reflist = get_stdin();
494return parse_reflist(reflist);
495}
496if (!strcmp("expand-refs-wildcard", argv[1])) {
497const char *reflist;
498if (argc < 4)
499return error("expand-refs-wildcard takes at least 2 args");
500reflist = argv[2];
501if (!strcmp(reflist, "-"))
502reflist = get_stdin();
503return expand_refs_wildcard(reflist, argc - 3, argv + 3);
504}
505506
return error("Unknown subcommand: %s", argv[1]);
507}