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