1 #include "git-compat-util.h"
13 static const char http_fetch_usage
[] = "git http-fetch "
14 "[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
16 static int fetch_using_walker(const char *raw_url
, int get_verbosely
,
17 int get_recover
, int commits
, char **commit_id
,
18 const char **write_ref
, int commits_on_stdin
)
21 struct walker
*walker
;
24 str_end_url_with_slash(raw_url
, &url
);
26 http_init(NULL
, url
, 0);
28 walker
= get_http_walker(url
);
29 walker
->get_verbosely
= get_verbosely
;
30 walker
->get_recover
= get_recover
;
31 walker
->get_progress
= 0;
33 rc
= walker_fetch(walker
, commits
, commit_id
, write_ref
, url
);
36 walker_targets_free(commits
, commit_id
, write_ref
);
38 if (walker
->corrupt_object_found
) {
40 "Some loose object were found to be corrupt, but they might be just\n"
41 "a false '404 Not Found' error message sent with incorrect HTTP\n"
42 "status code. Suggest running 'git fsck'.\n");
52 static void fetch_single_packfile(struct object_id
*packfile_hash
,
54 const char **index_pack_args
) {
55 struct http_pack_request
*preq
;
56 struct slot_results results
;
59 http_init(NULL
, url
, 0);
61 preq
= new_direct_http_pack_request(packfile_hash
->hash
, xstrdup(url
));
63 die("couldn't create http pack request");
64 preq
->slot
->results
= &results
;
65 preq
->index_pack_args
= index_pack_args
;
66 preq
->preserve_index_pack_stdout
= 1;
68 if (start_active_slot(preq
->slot
)) {
69 run_active_slot(preq
->slot
);
70 if (results
.curl_result
!= CURLE_OK
) {
72 char *nurl
= url_normalize(preq
->url
, &url
);
73 if (!nurl
|| !git_env_bool("GIT_TRACE_REDACT", 1)) {
74 die("unable to get pack file '%s'\n%s", preq
->url
,
77 die("failed to get '%.*s' url from '%.*s' "
78 "(full URL redacted due to GIT_TRACE_REDACT setting)\n%s",
79 (int)url
.scheme_len
, url
.url
,
80 (int)url
.host_len
, &url
.url
[url
.host_off
], curl_errorstr
);
84 die("Unable to start request");
87 if ((ret
= finish_http_pack_request(preq
)))
88 die("finish_http_pack_request gave result %d", ret
);
90 release_http_pack_request(preq
);
94 int cmd_main(int argc
, const char **argv
)
96 int commits_on_stdin
= 0;
98 const char **write_ref
= NULL
;
101 int get_verbosely
= 0;
105 struct object_id packfile_hash
;
106 struct strvec index_pack_args
= STRVEC_INIT
;
108 setup_git_directory_gently(&nongit
);
110 while (arg
< argc
&& argv
[arg
][0] == '-') {
113 if (argv
[arg
][1] == 't') {
114 } else if (argv
[arg
][1] == 'c') {
115 } else if (argv
[arg
][1] == 'a') {
116 } else if (argv
[arg
][1] == 'v') {
118 } else if (argv
[arg
][1] == 'w') {
119 write_ref
= &argv
[arg
+ 1];
121 } else if (argv
[arg
][1] == 'h') {
122 usage(http_fetch_usage
);
123 } else if (!strcmp(argv
[arg
], "--recover")) {
125 } else if (!strcmp(argv
[arg
], "--stdin")) {
126 commits_on_stdin
= 1;
127 } else if (skip_prefix(argv
[arg
], "--packfile=", &p
)) {
131 if (parse_oid_hex(p
, &packfile_hash
, &end
) || *end
)
132 die(_("argument to --packfile must be a valid hash (got '%s')"), p
);
133 } else if (skip_prefix(argv
[arg
], "--index-pack-arg=", &p
)) {
134 strvec_push(&index_pack_args
, p
);
138 if (argc
!= arg
+ 2 - (commits_on_stdin
|| packfile
))
139 usage(http_fetch_usage
);
142 die(_("not a git repository"));
144 trace2_cmd_name("http-fetch");
146 git_config(git_default_config
, NULL
);
149 if (!index_pack_args
.nr
)
150 die(_("the option '%s' requires '%s'"), "--packfile", "--index-pack-args");
152 fetch_single_packfile(&packfile_hash
, argv
[arg
],
158 if (index_pack_args
.nr
)
159 die(_("the option '%s' requires '%s'"), "--index-pack-args", "--packfile");
161 if (commits_on_stdin
) {
162 commits
= walker_targets_stdin(&commit_id
, &write_ref
);
164 commit_id
= (char **) &argv
[arg
++];
167 return fetch_using_walker(argv
[arg
], get_verbosely
, get_recover
,
168 commits
, commit_id
, write_ref
,