1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
15 static const char http_fetch_usage
[] = "git http-fetch "
16 "[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
18 static int fetch_using_walker(const char *raw_url
, int get_verbosely
,
19 int get_recover
, int commits
, char **commit_id
,
20 const char **write_ref
, int commits_on_stdin
)
23 struct walker
*walker
;
26 str_end_url_with_slash(raw_url
, &url
);
28 http_init(NULL
, url
, 0);
30 walker
= get_http_walker(url
);
31 walker
->get_verbosely
= get_verbosely
;
32 walker
->get_recover
= get_recover
;
33 walker
->get_progress
= 0;
35 rc
= walker_fetch(walker
, commits
, commit_id
, write_ref
, url
);
38 walker_targets_free(commits
, commit_id
, write_ref
);
40 if (walker
->corrupt_object_found
) {
42 "Some loose object were found to be corrupt, but they might be just\n"
43 "a false '404 Not Found' error message sent with incorrect HTTP\n"
44 "status code. Suggest running 'git fsck'.\n");
54 static void fetch_single_packfile(struct object_id
*packfile_hash
,
56 const char **index_pack_args
) {
57 struct http_pack_request
*preq
;
58 struct slot_results results
;
61 http_init(NULL
, url
, 0);
63 preq
= new_direct_http_pack_request(packfile_hash
->hash
, xstrdup(url
));
65 die("couldn't create http pack request");
66 preq
->slot
->results
= &results
;
67 preq
->index_pack_args
= index_pack_args
;
68 preq
->preserve_index_pack_stdout
= 1;
70 if (start_active_slot(preq
->slot
)) {
71 run_active_slot(preq
->slot
);
72 if (results
.curl_result
!= CURLE_OK
) {
74 char *nurl
= url_normalize(preq
->url
, &url
);
75 if (!nurl
|| !git_env_bool("GIT_TRACE_REDACT", 1)) {
76 die("unable to get pack file '%s'\n%s", preq
->url
,
79 die("failed to get '%.*s' url from '%.*s' "
80 "(full URL redacted due to GIT_TRACE_REDACT setting)\n%s",
81 (int)url
.scheme_len
, url
.url
,
82 (int)url
.host_len
, &url
.url
[url
.host_off
], curl_errorstr
);
86 die("Unable to start request");
89 if ((ret
= finish_http_pack_request(preq
)))
90 die("finish_http_pack_request gave result %d", ret
);
92 release_http_pack_request(preq
);
96 int cmd_main(int argc
, const char **argv
)
98 int commits_on_stdin
= 0;
100 const char **write_ref
= NULL
;
103 int get_verbosely
= 0;
107 struct object_id packfile_hash
;
108 struct strvec index_pack_args
= STRVEC_INIT
;
110 setup_git_directory_gently(&nongit
);
112 while (arg
< argc
&& argv
[arg
][0] == '-') {
115 if (argv
[arg
][1] == 't') {
116 } else if (argv
[arg
][1] == 'c') {
117 } else if (argv
[arg
][1] == 'a') {
118 } else if (argv
[arg
][1] == 'v') {
120 } else if (argv
[arg
][1] == 'w') {
121 write_ref
= &argv
[arg
+ 1];
123 } else if (argv
[arg
][1] == 'h') {
124 usage(http_fetch_usage
);
125 } else if (!strcmp(argv
[arg
], "--recover")) {
127 } else if (!strcmp(argv
[arg
], "--stdin")) {
128 commits_on_stdin
= 1;
129 } else if (skip_prefix(argv
[arg
], "--packfile=", &p
)) {
133 die(_("not a git repository"));
136 if (parse_oid_hex_algop(p
, &packfile_hash
, &end
,
137 the_repository
->hash_algo
) || *end
)
138 die(_("argument to --packfile must be a valid hash (got '%s')"), p
);
139 } else if (skip_prefix(argv
[arg
], "--index-pack-arg=", &p
)) {
140 strvec_push(&index_pack_args
, p
);
144 if (argc
!= arg
+ 2 - (commits_on_stdin
|| packfile
))
145 usage(http_fetch_usage
);
148 die(_("not a git repository"));
150 trace2_cmd_name("http-fetch");
152 git_config(git_default_config
, NULL
);
155 if (!index_pack_args
.nr
)
156 die(_("the option '%s' requires '%s'"), "--packfile", "--index-pack-args");
158 fetch_single_packfile(&packfile_hash
, argv
[arg
],
164 if (index_pack_args
.nr
)
165 die(_("the option '%s' requires '%s'"), "--index-pack-args", "--packfile");
167 if (commits_on_stdin
) {
168 commits
= walker_targets_stdin(&commit_id
, &write_ref
);
170 commit_id
= (char **) &argv
[arg
++];
173 return fetch_using_walker(argv
[arg
], get_verbosely
, get_recover
,
174 commits
, commit_id
, write_ref
,