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 sha1_array shallow
= SHA1_ARRAY_INIT
;
55 packet_trace_identity("fetch-pack");
57 memset(&args
, 0, sizeof(args
));
58 args
.uploadpack
= "git-upload-pack";
60 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
61 const char *arg
= argv
[i
];
63 if (starts_with(arg
, "--upload-pack=")) {
64 args
.uploadpack
= arg
+ 14;
67 if (starts_with(arg
, "--exec=")) {
68 args
.uploadpack
= arg
+ 7;
71 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
75 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
76 args
.lock_pack
= args
.keep_pack
;
80 if (!strcmp("--thin", arg
)) {
81 args
.use_thin_pack
= 1;
84 if (!strcmp("--include-tag", arg
)) {
88 if (!strcmp("--all", arg
)) {
92 if (!strcmp("--stdin", arg
)) {
96 if (!strcmp("--diag-url", arg
)) {
100 if (!strcmp("-v", arg
)) {
104 if (starts_with(arg
, "--depth=")) {
105 args
.depth
= strtol(arg
+ 8, NULL
, 0);
108 if (!strcmp("--no-progress", arg
)) {
109 args
.no_progress
= 1;
112 if (!strcmp("--stateless-rpc", arg
)) {
113 args
.stateless_rpc
= 1;
116 if (!strcmp("--lock-pack", arg
)) {
118 pack_lockfile_ptr
= &pack_lockfile
;
121 if (!strcmp("--check-self-contained-and-connected", arg
)) {
122 args
.check_self_contained_and_connected
= 1;
125 if (!strcmp("--cloning", arg
)) {
129 if (!strcmp("--update-shallow", arg
)) {
130 args
.update_shallow
= 1;
133 usage(fetch_pack_usage
);
139 usage(fetch_pack_usage
);
142 * Copy refs from cmdline to growable list, then append any
143 * refs from the standard input:
145 for (; i
< argc
; i
++)
146 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
147 if (args
.stdin_refs
) {
148 if (args
.stateless_rpc
) {
149 /* in stateless RPC mode we use pkt-line to read
150 * from stdin, until we get a flush packet
153 char *line
= packet_read_line(0, NULL
);
156 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
160 /* read from stdin one ref per line, until EOF */
161 struct strbuf line
= STRBUF_INIT
;
162 while (strbuf_getline_lf(&line
, stdin
) != EOF
)
163 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
164 strbuf_release(&line
);
168 if (args
.stateless_rpc
) {
173 int flags
= args
.verbose
? CONNECT_VERBOSE
: 0;
175 flags
|= CONNECT_DIAG_URL
;
176 conn
= git_connect(fd
, dest
, args
.uploadpack
,
179 return args
.diag_url
? 0 : 1;
181 get_remote_heads(fd
[0], NULL
, 0, &ref
, 0, NULL
, &shallow
);
183 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
, sought
, nr_sought
,
184 &shallow
, pack_lockfile_ptr
);
186 printf("lock %s\n", pack_lockfile
);
189 if (args
.check_self_contained_and_connected
&&
190 args
.self_contained_and_connected
) {
191 printf("connectivity-ok\n");
196 if (finish_connect(conn
))
202 * If the heads to pull were given, we should have consumed
203 * all of them by matching the remote. Otherwise, 'git fetch
204 * remote no-such-ref' would silently succeed without issuing
207 for (i
= 0; i
< nr_sought
; i
++) {
208 if (!sought
[i
] || sought
[i
]->matched
)
210 error("no such remote ref %s", sought
[i
]->name
);
216 oid_to_hex(&ref
->old_oid
), ref
->name
);