3 #include "fetch-pack.h"
6 #include "sha1-array.h"
8 static const char fetch_pack_usage
[] =
9 "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
10 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
11 "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]";
13 static void add_sought_entry(struct ref
***sought
, int *nr
, int *alloc
,
19 if (!get_oid_hex(name
, &oid
)) {
20 if (name
[GIT_SHA1_HEXSZ
] == ' ') {
21 /* <sha1> <ref>, find refname */
22 name
+= GIT_SHA1_HEXSZ
+ 1;
23 } else if (name
[GIT_SHA1_HEXSZ
] == '\0') {
24 ; /* <sha1>, leave sha1 as name */
26 /* <ref>, clear cruft from oid */
30 /* <ref>, clear cruft from get_oid_hex */
34 ref
= alloc_ref(name
);
35 oidcpy(&ref
->old_oid
, &oid
);
37 ALLOC_GROW(*sought
, *nr
, *alloc
);
38 (*sought
)[*nr
- 1] = ref
;
41 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
44 struct ref
*ref
= NULL
;
45 const char *dest
= NULL
;
46 struct ref
**sought
= NULL
;
47 int nr_sought
= 0, alloc_sought
= 0;
49 char *pack_lockfile
= NULL
;
50 char **pack_lockfile_ptr
= NULL
;
51 struct child_process
*conn
;
52 struct fetch_pack_args args
;
53 struct oid_array shallow
= OID_ARRAY_INIT
;
54 struct string_list deepen_not
= STRING_LIST_INIT_DUP
;
56 packet_trace_identity("fetch-pack");
58 memset(&args
, 0, sizeof(args
));
59 args
.uploadpack
= "git-upload-pack";
61 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
62 const char *arg
= argv
[i
];
64 if (skip_prefix(arg
, "--upload-pack=", &arg
)) {
65 args
.uploadpack
= arg
;
68 if (skip_prefix(arg
, "--exec=", &arg
)) {
69 args
.uploadpack
= arg
;
72 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
76 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
77 args
.lock_pack
= args
.keep_pack
;
81 if (!strcmp("--thin", arg
)) {
82 args
.use_thin_pack
= 1;
85 if (!strcmp("--include-tag", arg
)) {
89 if (!strcmp("--all", arg
)) {
93 if (!strcmp("--stdin", arg
)) {
97 if (!strcmp("--diag-url", arg
)) {
101 if (!strcmp("-v", arg
)) {
105 if (skip_prefix(arg
, "--depth=", &arg
)) {
106 args
.depth
= strtol(arg
, NULL
, 0);
109 if (skip_prefix(arg
, "--shallow-since=", &arg
)) {
110 args
.deepen_since
= xstrdup(arg
);
113 if (skip_prefix(arg
, "--shallow-exclude=", &arg
)) {
114 string_list_append(&deepen_not
, arg
);
117 if (!strcmp(arg
, "--deepen-relative")) {
118 args
.deepen_relative
= 1;
121 if (!strcmp("--no-progress", arg
)) {
122 args
.no_progress
= 1;
125 if (!strcmp("--stateless-rpc", arg
)) {
126 args
.stateless_rpc
= 1;
129 if (!strcmp("--lock-pack", arg
)) {
131 pack_lockfile_ptr
= &pack_lockfile
;
134 if (!strcmp("--check-self-contained-and-connected", arg
)) {
135 args
.check_self_contained_and_connected
= 1;
138 if (!strcmp("--cloning", arg
)) {
142 if (!strcmp("--update-shallow", arg
)) {
143 args
.update_shallow
= 1;
146 usage(fetch_pack_usage
);
149 args
.deepen_not
= &deepen_not
;
154 usage(fetch_pack_usage
);
157 * Copy refs from cmdline to growable list, then append any
158 * refs from the standard input:
160 for (; i
< argc
; i
++)
161 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
162 if (args
.stdin_refs
) {
163 if (args
.stateless_rpc
) {
164 /* in stateless RPC mode we use pkt-line to read
165 * from stdin, until we get a flush packet
168 char *line
= packet_read_line(0, NULL
);
171 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
175 /* read from stdin one ref per line, until EOF */
176 struct strbuf line
= STRBUF_INIT
;
177 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
178 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
179 strbuf_release(&line
);
183 if (args
.stateless_rpc
) {
188 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
190 flags
|= CONNECT_DIAG_URL
;
191 conn
= git_connect(fd
, dest
, args
.uploadpack
,
194 return args
.diag_url
? 0 : 1;
196 get_remote_heads(fd
[0], NULL
, 0, &ref
, 0, NULL
, &shallow
);
198 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
, sought
, nr_sought
,
199 &shallow
, pack_lockfile_ptr
);
201 printf("lock %s\n", pack_lockfile
);
204 if (args
.check_self_contained_and_connected
&&
205 args
.self_contained_and_connected
) {
206 printf("connectivity-ok\n");
211 if (finish_connect(conn
))
217 * If the heads to pull were given, we should have consumed
218 * all of them by matching the remote. Otherwise, 'git fetch
219 * remote no-such-ref' would silently succeed without issuing
222 ret
|= report_unmatched_refs(sought
, nr_sought
);
226 oid_to_hex(&ref
->old_oid
), ref
->name
);