http-fetch: refactor into function
[git/raj.git] / http-fetch.c
blobe538174bde42e5a17e0bf6cb03b2dec9e8dfd843
1 #include "cache.h"
2 #include "config.h"
3 #include "exec-cmd.h"
4 #include "http.h"
5 #include "walker.h"
7 static const char http_fetch_usage[] = "git http-fetch "
8 "[-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url";
10 static 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)
14 char *url = NULL;
15 struct walker *walker;
16 int rc;
18 str_end_url_with_slash(raw_url, &url);
20 http_init(NULL, url, 0);
22 walker = get_http_walker(url);
23 walker->get_verbosely = get_verbosely;
24 walker->get_recover = get_recover;
25 walker->get_progress = 0;
27 rc = walker_fetch(walker, commits, commit_id, write_ref, url);
29 if (commits_on_stdin)
30 walker_targets_free(commits, commit_id, write_ref);
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");
39 walker_free(walker);
40 http_cleanup();
41 free(url);
43 return rc;
46 int cmd_main(int argc, const char **argv)
48 int commits_on_stdin = 0;
49 int commits;
50 const char **write_ref = NULL;
51 char **commit_id;
52 int arg = 1;
53 int get_verbosely = 0;
54 int get_recover = 0;
56 while (arg < argc && argv[arg][0] == '-') {
57 if (argv[arg][1] == 't') {
58 } else if (argv[arg][1] == 'c') {
59 } else if (argv[arg][1] == 'a') {
60 } else if (argv[arg][1] == 'v') {
61 get_verbosely = 1;
62 } else if (argv[arg][1] == 'w') {
63 write_ref = &argv[arg + 1];
64 arg++;
65 } else if (argv[arg][1] == 'h') {
66 usage(http_fetch_usage);
67 } else if (!strcmp(argv[arg], "--recover")) {
68 get_recover = 1;
69 } else if (!strcmp(argv[arg], "--stdin")) {
70 commits_on_stdin = 1;
72 arg++;
74 if (argc != arg + 2 - commits_on_stdin)
75 usage(http_fetch_usage);
76 if (commits_on_stdin) {
77 commits = walker_targets_stdin(&commit_id, &write_ref);
78 } else {
79 commit_id = (char **) &argv[arg++];
80 commits = 1;
83 setup_git_directory();
85 git_config(git_default_config, NULL);
87 if (!argv[arg])
88 BUG("must have one arg remaining");
90 return fetch_using_walker(argv[arg], get_verbosely, get_recover,
91 commits, commit_id, write_ref,
92 commits_on_stdin);