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