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