🌐 AI搜索 & 代理 主页
blob: c4ccc5fea93cff445785bb101c8438b9374d1b74 [file] [log] [blame]
Daniel Barkalow30ae7642007-09-11 03:02:451#include "cache.h"
Brandon Williamsb2141fc2017-06-14 18:07:362#include "config.h"
Stefan Bellerd807c4a2018-04-10 21:26:183#include "exec-cmd.h"
Tay Ray Chuan888692b2010-03-02 10:49:294#include "http.h"
Daniel Barkalow30ae7642007-09-11 03:02:455#include "walker.h"
6
Jonathan Nieder616f86d2009-11-09 15:04:597static const char http_fetch_usage[] = "git http-fetch "
Jonathan Tan8d5d2a32020-06-10 20:57:188"[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
Jonathan Nieder616f86d2009-11-09 15:04:599
Jonathan Tan8e6adb62020-06-10 20:57:1710static int fetch_using_walker(const char *raw_url, int get_verbosely,
11 int get_recover, int commits, char **commit_id,
12 const char **write_ref, int commits_on_stdin)
13{
14 char *url = NULL;
15 struct walker *walker;
16 int rc;
17
18 str_end_url_with_slash(raw_url, &url);
19
20 http_init(NULL, url, 0);
21
22 walker = get_http_walker(url);
23 walker->get_verbosely = get_verbosely;
24 walker->get_recover = get_recover;
25 walker->get_progress = 0;
26
27 rc = walker_fetch(walker, commits, commit_id, write_ref, url);
28
29 if (commits_on_stdin)
30 walker_targets_free(commits, commit_id, write_ref);
31
32 if (walker->corrupt_object_found) {
33 fprintf(stderr,
34"Some loose object were found to be corrupt, but they might be just\n"
35"a false '404 Not Found' error message sent with incorrect HTTP\n"
36"status code. Suggest running 'git fsck'.\n");
37 }
38
39 walker_free(walker);
40 http_cleanup();
41 free(url);
42
43 return rc;
44}
45
Jonathan Tan8d5d2a32020-06-10 20:57:1846static void fetch_single_packfile(struct object_id *packfile_hash,
47 const char *url) {
48 struct http_pack_request *preq;
49 struct slot_results results;
50 int ret;
51
52 http_init(NULL, url, 0);
53
54 preq = new_direct_http_pack_request(packfile_hash->hash, xstrdup(url));
55 if (preq == NULL)
56 die("couldn't create http pack request");
57 preq->slot->results = &results;
58 preq->generate_keep = 1;
59
60 if (start_active_slot(preq->slot)) {
61 run_active_slot(preq->slot);
62 if (results.curl_result != CURLE_OK) {
63 die("Unable to get pack file %s\n%s", preq->url,
64 curl_errorstr);
65 }
66 } else {
67 die("Unable to start request");
68 }
69
70 if ((ret = finish_http_pack_request(preq)))
71 die("finish_http_pack_request gave result %d", ret);
72
73 release_http_pack_request(preq);
74 http_cleanup();
75}
76
Jeff King3f2e2292016-07-01 05:58:5877int cmd_main(int argc, const char **argv)
Daniel Barkalow30ae7642007-09-11 03:02:4578{
Daniel Barkalow30ae7642007-09-11 03:02:4579 int commits_on_stdin = 0;
80 int commits;
81 const char **write_ref = NULL;
82 char **commit_id;
Daniel Barkalow30ae7642007-09-11 03:02:4583 int arg = 1;
Daniel Barkalow30ae7642007-09-11 03:02:4584 int get_verbosely = 0;
85 int get_recover = 0;
Jonathan Tan8d5d2a32020-06-10 20:57:1886 int packfile = 0;
brian m. carlson439d3a12020-07-29 23:14:1887 int nongit;
Jonathan Tan8d5d2a32020-06-10 20:57:1888 struct object_id packfile_hash;
Daniel Barkalow30ae7642007-09-11 03:02:4589
brian m. carlson439d3a12020-07-29 23:14:1890 setup_git_directory_gently(&nongit);
91
Daniel Barkalow30ae7642007-09-11 03:02:4592 while (arg < argc && argv[arg][0] == '-') {
Jonathan Tan8d5d2a32020-06-10 20:57:1893 const char *p;
94
Daniel Barkalow30ae7642007-09-11 03:02:4595 if (argv[arg][1] == 't') {
Daniel Barkalow30ae7642007-09-11 03:02:4596 } else if (argv[arg][1] == 'c') {
Daniel Barkalow30ae7642007-09-11 03:02:4597 } else if (argv[arg][1] == 'a') {
Daniel Barkalow30ae7642007-09-11 03:02:4598 } else if (argv[arg][1] == 'v') {
99 get_verbosely = 1;
100 } else if (argv[arg][1] == 'w') {
101 write_ref = &argv[arg + 1];
102 arg++;
Jonathan Nieder616f86d2009-11-09 15:04:59103 } else if (argv[arg][1] == 'h') {
104 usage(http_fetch_usage);
Daniel Barkalow30ae7642007-09-11 03:02:45105 } else if (!strcmp(argv[arg], "--recover")) {
106 get_recover = 1;
107 } else if (!strcmp(argv[arg], "--stdin")) {
108 commits_on_stdin = 1;
Jonathan Tan8d5d2a32020-06-10 20:57:18109 } else if (skip_prefix(argv[arg], "--packfile=", &p)) {
110 const char *end;
111
112 packfile = 1;
113 if (parse_oid_hex(p, &packfile_hash, &end) || *end)
114 die(_("argument to --packfile must be a valid hash (got '%s')"), p);
Daniel Barkalow30ae7642007-09-11 03:02:45115 }
116 arg++;
117 }
Jonathan Tan8d5d2a32020-06-10 20:57:18118 if (argc != arg + 2 - (commits_on_stdin || packfile))
Jonathan Nieder616f86d2009-11-09 15:04:59119 usage(http_fetch_usage);
Jonathan Tan8d5d2a32020-06-10 20:57:18120
brian m. carlson439d3a12020-07-29 23:14:18121 if (nongit)
122 die(_("not a git repository"));
Jonathan Tan8d5d2a32020-06-10 20:57:18123
124 git_config(git_default_config, NULL);
125
126 if (packfile) {
127 fetch_single_packfile(&packfile_hash, argv[arg]);
128 return 0;
129 }
130
Daniel Barkalow30ae7642007-09-11 03:02:45131 if (commits_on_stdin) {
132 commits = walker_targets_stdin(&commit_id, &write_ref);
133 } else {
134 commit_id = (char **) &argv[arg++];
135 commits = 1;
136 }
Jonathan Tan8e6adb62020-06-10 20:57:17137 return fetch_using_walker(argv[arg], get_verbosely, get_recover,
138 commits, commit_id, write_ref,
139 commits_on_stdin);
Daniel Barkalow30ae7642007-09-11 03:02:45140}