]> git.ipfire.org Git - thirdparty/git.git/blame - http-fetch.c
http: allow custom index-pack args
[thirdparty/git.git] / http-fetch.c
CommitLineData
30ae764b 1#include "cache.h"
b2141fc1 2#include "config.h"
d807c4a0 3#include "exec-cmd.h"
888692b7 4#include "http.h"
30ae764b
DB
5#include "walker.h"
6
616f86d7 7static const char http_fetch_usage[] = "git http-fetch "
8d5d2a34 8"[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin | --packfile=hash | commit-id] url";
616f86d7 9
8e6adb69
JT
10static 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)
30ae764b 13{
8e6adb69 14 char *url = NULL;
30ae764b 15 struct walker *walker;
8e6adb69
JT
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
726b25a9
JT
46static const char *index_pack_args[] =
47 {"index-pack", "--stdin", "--keep", NULL};
48
8d5d2a34
JT
49static void fetch_single_packfile(struct object_id *packfile_hash,
50 const char *url) {
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));
58 if (preq == NULL)
59 die("couldn't create http pack request");
60 preq->slot->results = &results;
726b25a9
JT
61 preq->index_pack_args = index_pack_args;
62 preq->preserve_index_pack_stdout = 1;
8d5d2a34
JT
63
64 if (start_active_slot(preq->slot)) {
65 run_active_slot(preq->slot);
66 if (results.curl_result != CURLE_OK) {
67 die("Unable to get pack file %s\n%s", preq->url,
68 curl_errorstr);
69 }
70 } else {
71 die("Unable to start request");
72 }
73
74 if ((ret = finish_http_pack_request(preq)))
75 die("finish_http_pack_request gave result %d", ret);
76
77 release_http_pack_request(preq);
78 http_cleanup();
79}
80
8e6adb69
JT
81int cmd_main(int argc, const char **argv)
82{
30ae764b
DB
83 int commits_on_stdin = 0;
84 int commits;
85 const char **write_ref = NULL;
86 char **commit_id;
30ae764b 87 int arg = 1;
30ae764b
DB
88 int get_verbosely = 0;
89 int get_recover = 0;
8d5d2a34 90 int packfile = 0;
439d3a17 91 int nongit;
8d5d2a34 92 struct object_id packfile_hash;
30ae764b 93
439d3a17 94 setup_git_directory_gently(&nongit);
95
30ae764b 96 while (arg < argc && argv[arg][0] == '-') {
8d5d2a34
JT
97 const char *p;
98
30ae764b 99 if (argv[arg][1] == 't') {
30ae764b 100 } else if (argv[arg][1] == 'c') {
30ae764b 101 } else if (argv[arg][1] == 'a') {
30ae764b
DB
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++;
616f86d7
JN
107 } else if (argv[arg][1] == 'h') {
108 usage(http_fetch_usage);
30ae764b
DB
109 } else if (!strcmp(argv[arg], "--recover")) {
110 get_recover = 1;
111 } else if (!strcmp(argv[arg], "--stdin")) {
112 commits_on_stdin = 1;
8d5d2a34
JT
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);
30ae764b
DB
119 }
120 arg++;
121 }
8d5d2a34 122 if (argc != arg + 2 - (commits_on_stdin || packfile))
616f86d7 123 usage(http_fetch_usage);
6f5185bd 124
439d3a17 125 if (nongit)
126 die(_("not a git repository"));
616f86d7
JN
127
128 git_config(git_default_config, NULL);
129
8d5d2a34
JT
130 if (packfile) {
131 fetch_single_packfile(&packfile_hash, argv[arg]);
132 return 0;
133 }
30ae764b 134
8d5d2a34
JT
135 if (commits_on_stdin) {
136 commits = walker_targets_stdin(&commit_id, &write_ref);
137 } else {
138 commit_id = (char **) &argv[arg++];
139 commits = 1;
140 }
8e6adb69
JT
141 return fetch_using_walker(argv[arg], get_verbosely, get_recover,
142 commits, commit_id, write_ref,
143 commits_on_stdin);
30ae764b 144}