blob: 79a611fda1f8b344ce619dd9318695a69eebb695 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Linus Torvaldsdef88e92005-07-04 13:26:53 -07002#include "pkt-line.h"
Daniel Barkalow2d4177c2007-09-10 23:03:00 -04003#include "fetch-pack.h"
Junio C Hamano47a59182013-07-08 13:56:53 -07004#include "remote.h"
5#include "connect.h"
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +07006#include "sha1-array.h"
Shawn O. Pearcefa740522007-09-19 00:49:35 -04007
Junio C Hamano33b83032005-08-12 02:08:29 -07008static const char fetch_pack_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -07009"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
Ivan Todoroski078b8952012-04-02 17:13:48 +020010"[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +010011"[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
Linus Torvaldsdef88e92005-07-04 13:26:53 -070012
Jeff King5545f052016-02-22 17:44:50 -050013static void add_sought_entry(struct ref ***sought, int *nr, int *alloc,
14 const char *name)
Junio C Hamanof2db8542013-01-29 14:02:15 -080015{
Jeff King5545f052016-02-22 17:44:50 -050016 struct ref *ref;
brian m. carlson854ecb92015-11-10 02:22:21 +000017 struct object_id oid;
Nguyễn Thái Ngọc Duy58f2ed02013-12-05 20:02:49 +070018
Jeff King5545f052016-02-22 17:44:50 -050019 if (!get_oid_hex(name, &oid) && name[GIT_SHA1_HEXSZ] == ' ')
20 name += GIT_SHA1_HEXSZ + 1;
21 else
22 oidclr(&oid);
Junio C Hamanof2db8542013-01-29 14:02:15 -080023
Jeff King5545f052016-02-22 17:44:50 -050024 ref = alloc_ref(name);
25 oidcpy(&ref->old_oid, &oid);
Junio C Hamanof2db8542013-01-29 14:02:15 -080026 (*nr)++;
27 ALLOC_GROW(*sought, *nr, *alloc);
28 (*sought)[*nr - 1] = ref;
29}
30
Daniel Barkalow2d4177c2007-09-10 23:03:00 -040031int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
Linus Torvaldsdef88e92005-07-04 13:26:53 -070032{
Michael Haggerty57e6fc692012-05-21 09:59:59 +020033 int i, ret;
Daniel Barkalowba227852008-02-04 13:26:23 -050034 struct ref *ref = NULL;
Michael Haggerty9d19c6e2012-05-21 09:59:56 +020035 const char *dest = NULL;
Junio C Hamanof2db8542013-01-29 14:02:15 -080036 struct ref **sought = NULL;
37 int nr_sought = 0, alloc_sought = 0;
Daniel Barkalowba227852008-02-04 13:26:23 -050038 int fd[2];
Shawn O. Pearce249b2002009-10-30 17:47:42 -070039 char *pack_lockfile = NULL;
40 char **pack_lockfile_ptr = NULL;
Daniel Barkalowba227852008-02-04 13:26:23 -050041 struct child_process *conn;
Nguyễn Thái Ngọc Duyf8eb3032012-10-26 22:53:54 +070042 struct fetch_pack_args args;
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +070043 struct sha1_array shallow = SHA1_ARRAY_INIT;
Linus Torvaldsdef88e92005-07-04 13:26:53 -070044
Jeff Kingbbc30f92011-02-24 09:30:19 -050045 packet_trace_identity("fetch-pack");
46
Nguyễn Thái Ngọc Duyf8eb3032012-10-26 22:53:54 +070047 memset(&args, 0, sizeof(args));
48 args.uploadpack = "git-upload-pack";
49
Michael Haggertyff22ff92012-05-21 09:59:58 +020050 for (i = 1; i < argc && *argv[i] == '-'; i++) {
Daniel Barkalow2d4177c2007-09-10 23:03:00 -040051 const char *arg = argv[i];
Linus Torvaldsdef88e92005-07-04 13:26:53 -070052
Christian Couder59556542013-11-30 21:55:40 +010053 if (starts_with(arg, "--upload-pack=")) {
Michael Haggertyff22ff92012-05-21 09:59:58 +020054 args.uploadpack = arg + 14;
55 continue;
Linus Torvaldsdef88e92005-07-04 13:26:53 -070056 }
Christian Couder59556542013-11-30 21:55:40 +010057 if (starts_with(arg, "--exec=")) {
Michael Haggertyff22ff92012-05-21 09:59:58 +020058 args.uploadpack = arg + 7;
59 continue;
60 }
61 if (!strcmp("--quiet", arg) || !strcmp("-q", arg)) {
62 args.quiet = 1;
63 continue;
64 }
65 if (!strcmp("--keep", arg) || !strcmp("-k", arg)) {
66 args.lock_pack = args.keep_pack;
67 args.keep_pack = 1;
68 continue;
69 }
70 if (!strcmp("--thin", arg)) {
71 args.use_thin_pack = 1;
72 continue;
73 }
74 if (!strcmp("--include-tag", arg)) {
75 args.include_tag = 1;
76 continue;
77 }
78 if (!strcmp("--all", arg)) {
79 args.fetch_all = 1;
80 continue;
81 }
82 if (!strcmp("--stdin", arg)) {
83 args.stdin_refs = 1;
84 continue;
85 }
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +010086 if (!strcmp("--diag-url", arg)) {
87 args.diag_url = 1;
88 continue;
89 }
Michael Haggertyff22ff92012-05-21 09:59:58 +020090 if (!strcmp("-v", arg)) {
91 args.verbose = 1;
92 continue;
93 }
Christian Couder59556542013-11-30 21:55:40 +010094 if (starts_with(arg, "--depth=")) {
Michael Haggertyff22ff92012-05-21 09:59:58 +020095 args.depth = strtol(arg + 8, NULL, 0);
96 continue;
97 }
98 if (!strcmp("--no-progress", arg)) {
99 args.no_progress = 1;
100 continue;
101 }
102 if (!strcmp("--stateless-rpc", arg)) {
103 args.stateless_rpc = 1;
104 continue;
105 }
106 if (!strcmp("--lock-pack", arg)) {
107 args.lock_pack = 1;
108 pack_lockfile_ptr = &pack_lockfile;
109 continue;
110 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700111 if (!strcmp("--check-self-contained-and-connected", arg)) {
112 args.check_self_contained_and_connected = 1;
113 continue;
114 }
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700115 if (!strcmp("--cloning", arg)) {
116 args.cloning = 1;
117 continue;
118 }
119 if (!strcmp("--update-shallow", arg)) {
120 args.update_shallow = 1;
121 continue;
122 }
Michael Haggertyff22ff92012-05-21 09:59:58 +0200123 usage(fetch_pack_usage);
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700124 }
Michael Haggerty4cc00fc2012-05-21 09:59:57 +0200125
126 if (i < argc)
127 dest = argv[i++];
128 else
Linus Torvaldsdef88e92005-07-04 13:26:53 -0700129 usage(fetch_pack_usage);
Daniel Barkalow2d4177c2007-09-10 23:03:00 -0400130
Michael Haggerty57e6fc692012-05-21 09:59:59 +0200131 /*
132 * Copy refs from cmdline to growable list, then append any
133 * refs from the standard input:
134 */
Michael Haggerty57e6fc692012-05-21 09:59:59 +0200135 for (; i < argc; i++)
Junio C Hamanof2db8542013-01-29 14:02:15 -0800136 add_sought_entry(&sought, &nr_sought, &alloc_sought, argv[i]);
Ivan Todoroski078b8952012-04-02 17:13:48 +0200137 if (args.stdin_refs) {
Ivan Todoroski078b8952012-04-02 17:13:48 +0200138 if (args.stateless_rpc) {
139 /* in stateless RPC mode we use pkt-line to read
140 * from stdin, until we get a flush packet
141 */
Ivan Todoroski078b8952012-04-02 17:13:48 +0200142 for (;;) {
Jeff King74543a02013-02-20 15:02:57 -0500143 char *line = packet_read_line(0, NULL);
144 if (!line)
Ivan Todoroski078b8952012-04-02 17:13:48 +0200145 break;
Junio C Hamanoe013bda2013-04-01 08:59:37 -0700146 add_sought_entry(&sought, &nr_sought, &alloc_sought, line);
Ivan Todoroski078b8952012-04-02 17:13:48 +0200147 }
148 }
149 else {
150 /* read from stdin one ref per line, until EOF */
151 struct strbuf line = STRBUF_INIT;
Junio C Hamano8f309ae2016-01-13 15:31:17 -0800152 while (strbuf_getline_lf(&line, stdin) != EOF)
Junio C Hamanof2db8542013-01-29 14:02:15 -0800153 add_sought_entry(&sought, &nr_sought, &alloc_sought, line.buf);
Ivan Todoroski078b8952012-04-02 17:13:48 +0200154 strbuf_release(&line);
155 }
156 }
157
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700158 if (args.stateless_rpc) {
159 conn = NULL;
160 fd[0] = 0;
161 fd[1] = 1;
Daniel Barkalowba227852008-02-04 13:26:23 -0500162 } else {
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100163 int flags = args.verbose ? CONNECT_VERBOSE : 0;
164 if (args.diag_url)
165 flags |= CONNECT_DIAG_URL;
Michael Haggerty9d19c6e2012-05-21 09:59:56 +0200166 conn = git_connect(fd, dest, args.uploadpack,
Torsten Bögershausen5610b7c2013-11-28 20:49:17 +0100167 flags);
168 if (!conn)
169 return args.diag_url ? 0 : 1;
Daniel Barkalowba227852008-02-04 13:26:23 -0500170 }
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700171 get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700172
Nguyễn Thái Ngọc Duy16094882013-12-05 20:02:50 +0700173 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
174 &shallow, pack_lockfile_ptr);
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700175 if (pack_lockfile) {
176 printf("lock %s\n", pack_lockfile);
177 fflush(stdout);
178 }
Nguyễn Thái Ngọc Duy9ba38042013-07-21 15:18:05 +0700179 if (args.check_self_contained_and_connected &&
180 args.self_contained_and_connected) {
181 printf("connectivity-ok\n");
182 fflush(stdout);
183 }
Shawn O. Pearce249b2002009-10-30 17:47:42 -0700184 close(fd[0]);
185 close(fd[1]);
186 if (finish_connect(conn))
Michael Haggerty7418f1a2012-09-09 08:19:46 +0200187 return 1;
Junio C Hamano9e5d2b42005-11-06 00:09:59 -0800188
Junio C Hamanof2db8542013-01-29 14:02:15 -0800189 ret = !ref;
Michael Haggertyb2856682012-09-09 08:19:48 +0200190
191 /*
192 * If the heads to pull were given, we should have consumed
193 * all of them by matching the remote. Otherwise, 'git fetch
194 * remote no-such-ref' would silently succeed without issuing
195 * an error.
196 */
Junio C Hamanof2db8542013-01-29 14:02:15 -0800197 for (i = 0; i < nr_sought; i++) {
198 if (!sought[i] || sought[i]->matched)
199 continue;
200 error("no such remote ref %s", sought[i]->name);
201 ret = 1;
202 }
203
Daniel Barkalowba227852008-02-04 13:26:23 -0500204 while (ref) {
205 printf("%s %s\n",
brian m. carlsonf4e54d02015-11-10 02:22:20 +0000206 oid_to_hex(&ref->old_oid), ref->name);
Daniel Barkalowba227852008-02-04 13:26:23 -0500207 ref = ref->next;
208 }
Junio C Hamano9e5d2b42005-11-06 00:09:59 -0800209
Daniel Barkalowba227852008-02-04 13:26:23 -0500210 return ret;
211}