cache.h: move remote/connect API out of it
[git] / builtin / fetch-pack.c
1 #include "builtin.h"
2 #include "pkt-line.h"
3 #include "fetch-pack.h"
4 #include "remote.h"
5 #include "connect.h"
6
7 static const char fetch_pack_usage[] =
8 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
9 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
10 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
11
12 static void add_sought_entry_mem(struct ref ***sought, int *nr, int *alloc,
13                                  const char *name, int namelen)
14 {
15         struct ref *ref = xcalloc(1, sizeof(*ref) + namelen + 1);
16
17         memcpy(ref->name, name, namelen);
18         ref->name[namelen] = '\0';
19         (*nr)++;
20         ALLOC_GROW(*sought, *nr, *alloc);
21         (*sought)[*nr - 1] = ref;
22 }
23
24 static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
25                              const char *string)
26 {
27         add_sought_entry_mem(sought, nr, alloc, string, strlen(string));
28 }
29
30 int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
31 {
32         int i, ret;
33         struct ref *ref = NULL;
34         const char *dest = NULL;
35         struct ref **sought = NULL;
36         int nr_sought = 0, alloc_sought = 0;
37         int fd[2];
38         char *pack_lockfile = NULL;
39         char **pack_lockfile_ptr = NULL;
40         struct child_process *conn;
41         struct fetch_pack_args args;
42
43         packet_trace_identity("fetch-pack");
44
45         memset(&args, 0, sizeof(args));
46         args.uploadpack = "git-upload-pack";
47
48         for (i = 1; i < argc && *argv[i] == '-'; i++) {
49                 const char *arg = argv[i];
50
51                 if (!prefixcmp(arg, "--upload-pack=")) {
52                         args.uploadpack = arg + 14;
53                         continue;
54                 }
55                 if (!prefixcmp(arg, "--exec=")) {
56                         args.uploadpack = arg + 7;
57                         continue;
58                 }
59                 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
60                         args.quiet = 1;
61                         continue;
62                 }
63                 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
64                         args.lock_pack = args.keep_pack;
65                         args.keep_pack = 1;
66                         continue;
67                 }
68                 if (!strcmp("--thin", arg)) {
69                         args.use_thin_pack = 1;
70                         continue;
71                 }
72                 if (!strcmp("--include-tag", arg)) {
73                         args.include_tag = 1;
74                         continue;
75                 }
76                 if (!strcmp("--all", arg)) {
77                         args.fetch_all = 1;
78                         continue;
79                 }
80                 if (!strcmp("--stdin", arg)) {
81                         args.stdin_refs = 1;
82                         continue;
83                 }
84                 if (!strcmp("-v", arg)) {
85                         args.verbose = 1;
86                         continue;
87                 }
88                 if (!prefixcmp(arg, "--depth=")) {
89                         args.depth = strtol(arg + 8, NULL, 0);
90                         continue;
91                 }
92                 if (!strcmp("--no-progress", arg)) {
93                         args.no_progress = 1;
94                         continue;
95                 }
96                 if (!strcmp("--stateless-rpc", arg)) {
97                         args.stateless_rpc = 1;
98                         continue;
99                 }
100                 if (!strcmp("--lock-pack", arg)) {
101                         args.lock_pack = 1;
102                         pack_lockfile_ptr = &pack_lockfile;
103                         continue;
104                 }
105                 usage(fetch_pack_usage);
106         }
107
108         if (i < argc)
109                 dest = argv[i++];
110         else
111                 usage(fetch_pack_usage);
112
113         /*
114          * Copy refs from cmdline to growable list, then append any
115          * refs from the standard input:
116          */
117         for (; i < argc; i++)
118                 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
119         if (args.stdin_refs) {
120                 if (args.stateless_rpc) {
121                         /* in stateless RPC mode we use pkt-line to read
122                          * from stdin, until we get a flush packet
123                          */
124                         for (;;) {
125                                 char *line = packet_read_line(0, NULL);
126                                 if (!line)
127                                         break;
128                                 add_sought_entry(&sought, &nr_sought,  &alloc_sought, line);
129                         }
130                 }
131                 else {
132                         /* read from stdin one ref per line, until EOF */
133                         struct strbuf line = STRBUF_INIT;
134                         while (strbuf_getline(&line, stdin, '\n') != EOF)
135                                 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
136                         strbuf_release(&line);
137                 }
138         }
139
140         if (args.stateless_rpc) {
141                 conn = NULL;
142                 fd[0] = 0;
143                 fd[1] = 1;
144         } else {
145                 conn = git_connect(fd, dest, args.uploadpack,
146                                    args.verbose ? CONNECT_VERBOSE : 0);
147         }
148
149         get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL);
150
151         ref = fetch_pack(&args, fd, conn, ref, dest,
152                          sought, nr_sought, pack_lockfile_ptr);
153         if (pack_lockfile) {
154                 printf("lock %s\n", pack_lockfile);
155                 fflush(stdout);
156         }
157         close(fd[0]);
158         close(fd[1]);
159         if (finish_connect(conn))
160                 return 1;
161
162         ret = !ref;
163
164         /*
165          * If the heads to pull were given, we should have consumed
166          * all of them by matching the remote.  Otherwise, 'git fetch
167          * remote no-such-ref' would silently succeed without issuing
168          * an error.
169          */
170         for (i = 0; i < nr_sought; i++) {
171                 if (!sought[i] || sought[i]->matched)
172                         continue;
173                 error("no such remote ref %s", sought[i]->name);
174                 ret = 1;
175         }
176
177         while (ref) {
178                 printf("%s %s\n",
179                        sha1_to_hex(ref->old_sha1), ref->name);
180                 ref = ref->next;
181         }
182
183         return ret;
184 }