3 #include "fetch-pack.h"
5 static const char fetch_pack_usage
[] =
6 "git fetch-pack [--all] [--stdin] [--quiet|-q] [--keep|-k] [--thin] "
7 "[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] "
8 "[--no-progress] [-v] [<host>:]<directory> [<refs>...]";
10 int cmd_fetch_pack(int argc
, const char **argv
, const char *prefix
)
13 struct ref
*ref
= NULL
;
14 const char *dest
= NULL
;
15 struct string_list sought
= STRING_LIST_INIT_DUP
;
17 char *pack_lockfile
= NULL
;
18 char **pack_lockfile_ptr
= NULL
;
19 struct child_process
*conn
;
20 struct fetch_pack_args args
;
22 packet_trace_identity("fetch-pack");
24 memset(&args
, 0, sizeof(args
));
25 args
.uploadpack
= "git-upload-pack";
27 for (i
= 1; i
< argc
&& *argv
[i
] == '-'; i
++) {
28 const char *arg
= argv
[i
];
30 if (!prefixcmp(arg
, "--upload-pack=")) {
31 args
.uploadpack
= arg
+ 14;
34 if (!prefixcmp(arg
, "--exec=")) {
35 args
.uploadpack
= arg
+ 7;
38 if (!strcmp("--quiet", arg
) || !strcmp("-q", arg
)) {
42 if (!strcmp("--keep", arg
) || !strcmp("-k", arg
)) {
43 args
.lock_pack
= args
.keep_pack
;
47 if (!strcmp("--thin", arg
)) {
48 args
.use_thin_pack
= 1;
51 if (!strcmp("--include-tag", arg
)) {
55 if (!strcmp("--all", arg
)) {
59 if (!strcmp("--stdin", arg
)) {
63 if (!strcmp("-v", arg
)) {
67 if (!prefixcmp(arg
, "--depth=")) {
68 args
.depth
= strtol(arg
+ 8, NULL
, 0);
71 if (!strcmp("--no-progress", arg
)) {
75 if (!strcmp("--stateless-rpc", arg
)) {
76 args
.stateless_rpc
= 1;
79 if (!strcmp("--lock-pack", arg
)) {
81 pack_lockfile_ptr
= &pack_lockfile
;
84 usage(fetch_pack_usage
);
90 usage(fetch_pack_usage
);
93 * Copy refs from cmdline to growable list, then append any
94 * refs from the standard input:
97 string_list_append(&sought
, xstrdup(argv
[i
]));
98 if (args
.stdin_refs
) {
99 if (args
.stateless_rpc
) {
100 /* in stateless RPC mode we use pkt-line to read
101 * from stdin, until we get a flush packet
103 static char line
[1000];
105 int n
= packet_read_line(0, line
, sizeof(line
));
108 if (line
[n
-1] == '\n')
110 string_list_append(&sought
, xmemdupz(line
, n
));
114 /* read from stdin one ref per line, until EOF */
115 struct strbuf line
= STRBUF_INIT
;
116 while (strbuf_getline(&line
, stdin
, '\n') != EOF
)
117 string_list_append(&sought
, strbuf_detach(&line
, NULL
));
118 strbuf_release(&line
);
122 if (args
.stateless_rpc
) {
127 conn
= git_connect(fd
, dest
, args
.uploadpack
,
128 args
.verbose
? CONNECT_VERBOSE
: 0);
131 get_remote_heads(fd
[0], &ref
, 0, NULL
);
133 ref
= fetch_pack(&args
, fd
, conn
, ref
, dest
,
134 &sought
, pack_lockfile_ptr
);
136 printf("lock %s\n", pack_lockfile
);
141 if (finish_connect(conn
))
144 ret
= !ref
|| sought
.nr
;
147 * If the heads to pull were given, we should have consumed
148 * all of them by matching the remote. Otherwise, 'git fetch
149 * remote no-such-ref' would silently succeed without issuing
152 for (i
= 0; i
< sought
.nr
; i
++)
153 error("no such remote ref %s", sought
.items
[i
].string
);
156 sha1_to_hex(ref
->old_sha1
), ref
->name
);