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