blob: fffda592670e667e81eb92af218f9a4d95d7614d [file] [log] [blame]
Elijah Newrenb6fdc442023-04-11 00:41:54 -07001#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Stefan Bellerd807c4a2018-04-10 14:26:18 -07003#include "exec-cmd.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Tay Ray Chuan888692b2010-03-02 18:49:29 +08006#include "http.h"
Daniel Barkalow30ae7642007-09-10 23:02:45 -04007#include "walker.h"
Elijah Newrene38da482023-03-21 06:26:05 +00008#include "setup.h"
Jonathan Tan27e35ba2021-02-22 11:20:07 -08009#include "strvec.h"
Ivan Frade0ba558f2021-11-10 23:51:29 +000010#include "urlmatch.h"
Jonathan Tan7abb43c2022-12-12 14:46:30 -080011#include "trace2.h"
Daniel Barkalow30ae7642007-09-10 23:02:45 -040012
Jonathan Nieder616f86d2009-11-09 09:04:59 -060013static const char http_fetch_usage[] = "git http-fetch "
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070014"[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
Jonathan Nieder616f86d2009-11-09 09:04:59 -060015
Jonathan Tan8e6adb62020-06-10 13:57:17 -070016static int fetch_using_walker(const char *raw_url, int get_verbosely,
17 int get_recover, int commits, char **commit_id,
18 const char **write_ref, int commits_on_stdin)
19{
20 char *url = NULL;
21 struct walker *walker;
22 int rc;
23
24 str_end_url_with_slash(raw_url, &url);
25
26 http_init(NULL, url, 0);
27
28 walker = get_http_walker(url);
29 walker->get_verbosely = get_verbosely;
30 walker->get_recover = get_recover;
31 walker->get_progress = 0;
32
33 rc = walker_fetch(walker, commits, commit_id, write_ref, url);
34
35 if (commits_on_stdin)
36 walker_targets_free(commits, commit_id, write_ref);
37
38 if (walker->corrupt_object_found) {
39 fprintf(stderr,
40"Some loose object were found to be corrupt, but they might be just\n"
41"a false '404 Not Found' error message sent with incorrect HTTP\n"
42"status code. Suggest running 'git fsck'.\n");
43 }
44
45 walker_free(walker);
46 http_cleanup();
47 free(url);
48
49 return rc;
50}
51
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070052static void fetch_single_packfile(struct object_id *packfile_hash,
Jonathan Tan27e35ba2021-02-22 11:20:07 -080053 const char *url,
54 const char **index_pack_args) {
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070055 struct http_pack_request *preq;
56 struct slot_results results;
57 int ret;
58
59 http_init(NULL, url, 0);
60
61 preq = new_direct_http_pack_request(packfile_hash->hash, xstrdup(url));
Junio C Hamanoafe8a902022-05-02 09:50:37 -070062 if (!preq)
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070063 die("couldn't create http pack request");
64 preq->slot->results = &results;
Jonathan Tan726b25a2021-02-22 11:20:06 -080065 preq->index_pack_args = index_pack_args;
66 preq->preserve_index_pack_stdout = 1;
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070067
68 if (start_active_slot(preq->slot)) {
69 run_active_slot(preq->slot);
70 if (results.curl_result != CURLE_OK) {
Ivan Frade0ba558f2021-11-10 23:51:29 +000071 struct url_info url;
72 char *nurl = url_normalize(preq->url, &url);
73 if (!nurl || !git_env_bool("GIT_TRACE_REDACT", 1)) {
74 die("unable to get pack file '%s'\n%s", preq->url,
75 curl_errorstr);
76 } else {
77 die("failed to get '%.*s' url from '%.*s' "
78 "(full URL redacted due to GIT_TRACE_REDACT setting)\n%s",
79 (int)url.scheme_len, url.url,
80 (int)url.host_len, &url.url[url.host_off], curl_errorstr);
81 }
Jonathan Tan8d5d2a32020-06-10 13:57:18 -070082 }
83 } else {
84 die("Unable to start request");
85 }
86
87 if ((ret = finish_http_pack_request(preq)))
88 die("finish_http_pack_request gave result %d", ret);
89
90 release_http_pack_request(preq);
91 http_cleanup();
92}
93
Jeff King3f2e2292016-07-01 01:58:58 -040094int cmd_main(int argc, const char **argv)
Daniel Barkalow30ae7642007-09-10 23:02:45 -040095{
Daniel Barkalow30ae7642007-09-10 23:02:45 -040096 int commits_on_stdin = 0;
97 int commits;
98 const char **write_ref = NULL;
99 char **commit_id;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400100 int arg = 1;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400101 int get_verbosely = 0;
102 int get_recover = 0;
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700103 int packfile = 0;
brian m. carlson439d3a12020-07-29 23:14:18 +0000104 int nongit;
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700105 struct object_id packfile_hash;
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800106 struct strvec index_pack_args = STRVEC_INIT;
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400107
brian m. carlson439d3a12020-07-29 23:14:18 +0000108 setup_git_directory_gently(&nongit);
109
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400110 while (arg < argc && argv[arg][0] == '-') {
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700111 const char *p;
112
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400113 if (argv[arg][1] == 't') {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400114 } else if (argv[arg][1] == 'c') {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400115 } else if (argv[arg][1] == 'a') {
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400116 } else if (argv[arg][1] == 'v') {
117 get_verbosely = 1;
118 } else if (argv[arg][1] == 'w') {
119 write_ref = &argv[arg + 1];
120 arg++;
Jonathan Nieder616f86d2009-11-09 09:04:59 -0600121 } else if (argv[arg][1] == 'h') {
122 usage(http_fetch_usage);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400123 } else if (!strcmp(argv[arg], "--recover")) {
124 get_recover = 1;
125 } else if (!strcmp(argv[arg], "--stdin")) {
126 commits_on_stdin = 1;
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700127 } else if (skip_prefix(argv[arg], "--packfile=", &p)) {
128 const char *end;
129
130 packfile = 1;
131 if (parse_oid_hex(p, &packfile_hash, &end) || *end)
132 die(_("argument to --packfile must be a valid hash (got '%s')"), p);
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800133 } else if (skip_prefix(argv[arg], "--index-pack-arg=", &p)) {
134 strvec_push(&index_pack_args, p);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400135 }
136 arg++;
137 }
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700138 if (argc != arg + 2 - (commits_on_stdin || packfile))
Jonathan Nieder616f86d2009-11-09 09:04:59 -0600139 usage(http_fetch_usage);
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700140
brian m. carlson439d3a12020-07-29 23:14:18 +0000141 if (nongit)
142 die(_("not a git repository"));
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700143
Jonathan Tan7abb43c2022-12-12 14:46:30 -0800144 trace2_cmd_name("http-fetch");
145
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700146 git_config(git_default_config, NULL);
147
148 if (packfile) {
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800149 if (!index_pack_args.nr)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000150 die(_("the option '%s' requires '%s'"), "--packfile", "--index-pack-args");
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800151
152 fetch_single_packfile(&packfile_hash, argv[arg],
153 index_pack_args.v);
154
Jonathan Tan8d5d2a32020-06-10 13:57:18 -0700155 return 0;
156 }
157
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800158 if (index_pack_args.nr)
Jean-Noël Avila6fa00ee2022-01-05 20:02:19 +0000159 die(_("the option '%s' requires '%s'"), "--index-pack-args", "--packfile");
Jonathan Tan27e35ba2021-02-22 11:20:07 -0800160
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400161 if (commits_on_stdin) {
162 commits = walker_targets_stdin(&commit_id, &write_ref);
163 } else {
164 commit_id = (char **) &argv[arg++];
165 commits = 1;
166 }
Jonathan Tan8e6adb62020-06-10 13:57:17 -0700167 return fetch_using_walker(argv[arg], get_verbosely, get_recover,
168 commits, commit_id, write_ref,
169 commits_on_stdin);
Daniel Barkalow30ae7642007-09-10 23:02:45 -0400170}