git
/
http-fetch.c
176 строк · 4.5 Кб
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "git-compat-util.h"
4#include "config.h"
5#include "gettext.h"
6#include "hex.h"
7#include "http.h"
8#include "walker.h"
9#include "setup.h"
10#include "strvec.h"
11#include "url.h"
12#include "urlmatch.h"
13#include "trace2.h"
14
15static const char http_fetch_usage[] = "git http-fetch "
16"[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
17
18static int fetch_using_walker(const char *raw_url, int get_verbosely,
19int get_recover, int commits, char **commit_id,
20const char **write_ref, int commits_on_stdin)
21{
22char *url = NULL;
23struct walker *walker;
24int rc;
25
26str_end_url_with_slash(raw_url, &url);
27
28http_init(NULL, url, 0);
29
30walker = get_http_walker(url);
31walker->get_verbosely = get_verbosely;
32walker->get_recover = get_recover;
33walker->get_progress = 0;
34
35rc = walker_fetch(walker, commits, commit_id, write_ref, url);
36
37if (commits_on_stdin)
38walker_targets_free(commits, commit_id, write_ref);
39
40if (walker->corrupt_object_found) {
41fprintf(stderr,
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");
45}
46
47walker_free(walker);
48http_cleanup();
49free(url);
50
51return rc;
52}
53
54static void fetch_single_packfile(struct object_id *packfile_hash,
55const char *url,
56const char **index_pack_args) {
57struct http_pack_request *preq;
58struct slot_results results;
59int ret;
60
61http_init(NULL, url, 0);
62
63preq = new_direct_http_pack_request(packfile_hash->hash, xstrdup(url));
64if (!preq)
65die("couldn't create http pack request");
66preq->slot->results = &results;
67preq->index_pack_args = index_pack_args;
68preq->preserve_index_pack_stdout = 1;
69
70if (start_active_slot(preq->slot)) {
71run_active_slot(preq->slot);
72if (results.curl_result != CURLE_OK) {
73struct url_info url;
74char *nurl = url_normalize(preq->url, &url);
75if (!nurl || !git_env_bool("GIT_TRACE_REDACT", 1)) {
76die("unable to get pack file '%s'\n%s", preq->url,
77curl_errorstr);
78} else {
79die("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);
83}
84}
85} else {
86die("Unable to start request");
87}
88
89if ((ret = finish_http_pack_request(preq)))
90die("finish_http_pack_request gave result %d", ret);
91
92release_http_pack_request(preq);
93http_cleanup();
94}
95
96int cmd_main(int argc, const char **argv)
97{
98int commits_on_stdin = 0;
99int commits;
100const char **write_ref = NULL;
101char **commit_id;
102int arg = 1;
103int get_verbosely = 0;
104int get_recover = 0;
105int packfile = 0;
106int nongit;
107struct object_id packfile_hash;
108struct strvec index_pack_args = STRVEC_INIT;
109
110setup_git_directory_gently(&nongit);
111
112while (arg < argc && argv[arg][0] == '-') {
113const char *p;
114
115if (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') {
119get_verbosely = 1;
120} else if (argv[arg][1] == 'w') {
121write_ref = &argv[arg + 1];
122arg++;
123} else if (argv[arg][1] == 'h') {
124usage(http_fetch_usage);
125} else if (!strcmp(argv[arg], "--recover")) {
126get_recover = 1;
127} else if (!strcmp(argv[arg], "--stdin")) {
128commits_on_stdin = 1;
129} else if (skip_prefix(argv[arg], "--packfile=", &p)) {
130const char *end;
131
132if (nongit)
133die(_("not a git repository"));
134
135packfile = 1;
136if (parse_oid_hex_algop(p, &packfile_hash, &end,
137the_repository->hash_algo) || *end)
138die(_("argument to --packfile must be a valid hash (got '%s')"), p);
139} else if (skip_prefix(argv[arg], "--index-pack-arg=", &p)) {
140strvec_push(&index_pack_args, p);
141}
142arg++;
143}
144if (argc != arg + 2 - (commits_on_stdin || packfile))
145usage(http_fetch_usage);
146
147if (nongit)
148die(_("not a git repository"));
149
150trace2_cmd_name("http-fetch");
151
152git_config(git_default_config, NULL);
153
154if (packfile) {
155if (!index_pack_args.nr)
156die(_("the option '%s' requires '%s'"), "--packfile", "--index-pack-args");
157
158fetch_single_packfile(&packfile_hash, argv[arg],
159index_pack_args.v);
160
161return 0;
162}
163
164if (index_pack_args.nr)
165die(_("the option '%s' requires '%s'"), "--index-pack-args", "--packfile");
166
167if (commits_on_stdin) {
168commits = walker_targets_stdin(&commit_id, &write_ref);
169} else {
170commit_id = (char **) &argv[arg++];
171commits = 1;
172}
173return fetch_using_walker(argv[arg], get_verbosely, get_recover,
174commits, commit_id, write_ref,
175commits_on_stdin);
176}
177