8 static struct remote *remote;
9 static const char *url;
10 static struct walker *walker;
15 unsigned progress : 1,
18 static struct options options;
20 static void init_walker(void)
23 walker = get_http_walker(url, remote);
26 static int set_option(const char *name, const char *value)
28 if (!strcmp(name, "verbosity")) {
30 int v = strtol(value, &end, 10);
31 if (value == end || *end)
33 options.verbosity = v;
36 else if (!strcmp(name, "progress")) {
37 if (!strcmp(value, "true"))
39 else if (!strcmp(value, "false"))
43 return 1 /* TODO implement later */;
45 else if (!strcmp(name, "depth")) {
47 unsigned long v = strtoul(value, &end, 10);
48 if (value == end || *end)
51 return 1 /* TODO implement later */;
53 else if (!strcmp(name, "followtags")) {
54 if (!strcmp(value, "true"))
55 options.followtags = 1;
56 else if (!strcmp(value, "false"))
57 options.followtags = 0;
60 return 1 /* TODO implement later */;
63 return 1 /* unsupported */;
67 static struct ref *get_refs(void)
69 struct strbuf buffer = STRBUF_INIT;
70 char *data, *start, *mid;
76 struct ref *refs = NULL;
77 struct ref *ref = NULL;
78 struct ref *last_ref = NULL;
80 refs_url = xmalloc(strlen(url) + 11);
81 sprintf(refs_url, "%s/info/refs", url);
84 http_ret = http_get_strbuf(refs_url, &buffer, HTTP_NO_CACHE);
88 case HTTP_MISSING_TARGET:
89 die("%s not found: did you run git update-server-info on the"
90 " server?", refs_url);
92 http_error(refs_url, http_ret);
93 die("HTTP request failed");
99 while (i < buffer.len) {
105 if (data[i] == '\n') {
108 ref = xmalloc(sizeof(struct ref) +
109 strlen(ref_name) + 1);
110 memset(ref, 0, sizeof(struct ref));
111 strcpy(ref->name, ref_name);
112 get_sha1_hex(start, ref->old_sha1);
116 last_ref->next = ref;
123 strbuf_release(&buffer);
125 ref = alloc_ref("HEAD");
126 if (!walker->fetch_ref(walker, ref) &&
127 !resolve_remote_symref(ref, refs)) {
134 strbuf_release(&buffer);
139 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
141 char **targets = xmalloc(nr_heads * sizeof(char*));
144 for (i = 0; i < nr_heads; i++)
145 targets[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
149 walker->get_tree = 1;
150 walker->get_history = 1;
151 walker->get_verbosely = options.verbosity >= 3;
152 walker->get_recover = 0;
153 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
155 for (i = 0; i < nr_heads; i++)
159 return ret ? error("Fetch failed.") : 0;
162 static void parse_fetch(struct strbuf *buf)
164 struct ref **to_fetch = NULL;
165 struct ref *list_head = NULL;
166 struct ref **list = &list_head;
167 int alloc_heads = 0, nr_heads = 0;
170 if (!prefixcmp(buf->buf, "fetch ")) {
171 char *p = buf->buf + strlen("fetch ");
174 unsigned char old_sha1[20];
176 if (strlen(p) < 40 || get_sha1_hex(p, old_sha1))
177 die("protocol error: expected sha/ref, got %s'", p);
183 die("protocol error: expected sha/ref, got %s'", p);
185 ref = alloc_ref(name);
186 hashcpy(ref->old_sha1, old_sha1);
191 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
192 to_fetch[nr_heads++] = ref;
195 die("http transport does not support %s", buf->buf);
198 if (strbuf_getline(buf, stdin, '\n') == EOF)
204 if (fetch_dumb(nr_heads, to_fetch))
205 exit(128); /* error already reported */
206 free_refs(list_head);
214 int main(int argc, const char **argv)
216 struct strbuf buf = STRBUF_INIT;
218 git_extract_argv0_path(argv[0]);
219 setup_git_directory();
221 fprintf(stderr, "Remote needed\n");
225 options.verbosity = 1;
226 options.progress = !!isatty(2);
228 remote = remote_get(argv[1]);
233 url = remote->url[0];
237 if (strbuf_getline(&buf, stdin, '\n') == EOF)
239 if (!prefixcmp(buf.buf, "fetch ")) {
242 } else if (!strcmp(buf.buf, "list")) {
243 struct ref *refs = get_refs();
245 for (posn = refs; posn; posn = posn->next) {
247 printf("@%s %s\n", posn->symref, posn->name);
249 printf("%s %s\n", sha1_to_hex(posn->old_sha1), posn->name);
253 } else if (!prefixcmp(buf.buf, "option ")) {
254 char *name = buf.buf + strlen("option ");
255 char *value = strchr(name, ' ');
263 result = set_option(name, value);
267 printf("error invalid value\n");
269 printf("unsupported\n");
272 } else if (!strcmp(buf.buf, "capabilities")) {