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