3 #include "fetch-pack.h"
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>...]";
12 static void add_sought_entry_mem(struct ref
***sought
, int *nr
, int *alloc
,
13 const char *name
, int namelen
)
15 struct ref
*ref
= xcalloc(1, sizeof(*ref
) + namelen
+ 1);
16 unsigned char sha1
[20];
18 if (namelen
> 41 && name
[40] == ' ' && !get_sha1_hex(name
, sha1
)) {
19 hashcpy(ref
->old_sha1
, sha1
);
24 memcpy(ref
->name
, name
, namelen
);
25 ref
->name
[namelen
] = '\0';
27 ALLOC_GROW(*sought
, *nr
, *alloc
);
28 (*sought
)[*nr
- 1] = ref
;
31 static void add_sought_entry(struct ref
***sought
, int *nr
, int *alloc
,
34 add_sought_entry_mem(sought
, nr
, alloc
, string
, strlen(string
));
37 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
40 struct ref
*ref
= NULL
;
41 const char *dest
= NULL
;
42 struct ref
**sought
= NULL
;
43 int nr_sought
= 0, alloc_sought
= 0;
45 char *pack_lockfile
= NULL
;
46 char **pack_lockfile_ptr
= NULL
;
47 struct child_process
*conn
;
48 struct fetch_pack_args args
;
50 packet_trace_identity("fetch-pack");
52 memset(&args
, 0, sizeof(args
));
53 args
.uploadpack
= "git-upload-pack";
55 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
56 const char *arg
= argv
[i
];
58 if (!prefixcmp(arg
, "--upload-pack=")) {
59 args
.uploadpack
= arg
+ 14;
62 if (!prefixcmp(arg
, "--exec=")) {
63 args
.uploadpack
= arg
+ 7;
66 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
70 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
71 args
.lock_pack
= args
.keep_pack
;
75 if (!strcmp("--thin", arg
)) {
76 args
.use_thin_pack
= 1;
79 if (!strcmp("--include-tag", arg
)) {
83 if (!strcmp("--all", arg
)) {
87 if (!strcmp("--stdin", arg
)) {
91 if (!strcmp("-v", arg
)) {
95 if (!prefixcmp(arg
, "--depth=")) {
96 args
.depth
= strtol(arg
+ 8, NULL
, 0);
99 if (!strcmp("--no-progress", arg
)) {
100 args
.no_progress
= 1;
103 if (!strcmp("--stateless-rpc", arg
)) {
104 args
.stateless_rpc
= 1;
107 if (!strcmp("--lock-pack", arg
)) {
109 pack_lockfile_ptr
= &pack_lockfile
;
112 if (!strcmp("--check-self-contained-and-connected", arg
)) {
113 args
.check_self_contained_and_connected
= 1;
116 usage(fetch_pack_usage
);
122 usage(fetch_pack_usage
);
125 * Copy refs from cmdline to growable list, then append any
126 * refs from the standard input:
128 for (; i
< argc
; i
++)
129 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, argv
[i
]);
130 if (args
.stdin_refs
) {
131 if (args
.stateless_rpc
) {
132 /* in stateless RPC mode we use pkt-line to read
133 * from stdin, until we get a flush packet
136 char *line
= packet_read_line(0, NULL
);
139 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
);
143 /* read from stdin one ref per line, until EOF */
144 struct strbuf line
= STRBUF_INIT
;
145 while (strbuf_getline(&line
, stdin
, '\n') != EOF
)
146 add_sought_entry(&sought
, &nr_sought
, &alloc_sought
, line
.buf
);
147 strbuf_release(&line
);
151 if (args
.stateless_rpc
) {
156 conn
= git_connect(fd
, dest
, args
.uploadpack
,
157 args
.verbose
? CONNECT_VERBOSE
: 0);
160 get_remote_heads(fd
[0], NULL
, 0, &ref
, 0, NULL
, NULL
);
162 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
,
163 sought
, nr_sought
, NULL
, pack_lockfile_ptr
);
165 printf("lock %s\n", pack_lockfile
);
168 if (args
.check_self_contained_and_connected
&&
169 args
.self_contained_and_connected
) {
170 printf("connectivity-ok\n");
175 if (finish_connect(conn
))
181 * If the heads to pull were given, we should have consumed
182 * all of them by matching the remote. Otherwise, 'git fetch
183 * remote no-such-ref' would silently succeed without issuing
186 for (i
= 0; i
< nr_sought
; i
++) {
187 if (!sought
[i
] || sought
[i
]->matched
)
189 error("no such remote ref %s", sought
[i
]->name
);
195 sha1_to_hex(ref
->old_sha1
), ref
->name
);