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