]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-bundle-uri.c
The 20th batch
[thirdparty/git.git] / t / helper / test-bundle-uri.c
1 #include "test-tool.h"
2 #include "parse-options.h"
3 #include "bundle-uri.h"
4 #include "gettext.h"
5 #include "strbuf.h"
6 #include "string-list.h"
7 #include "transport.h"
8 #include "remote.h"
9
10 enum input_mode {
11 KEY_VALUE_PAIRS,
12 CONFIG_FILE,
13 };
14
15 static int cmd__bundle_uri_parse(int argc, const char **argv, enum input_mode mode)
16 {
17 const char *key_value_usage[] = {
18 "test-tool bundle-uri parse-key-values <input>",
19 NULL
20 };
21 const char *config_usage[] = {
22 "test-tool bundle-uri parse-config <input>",
23 NULL
24 };
25 const char **usage = key_value_usage;
26 struct option options[] = {
27 OPT_END(),
28 };
29 struct strbuf sb = STRBUF_INIT;
30 struct bundle_list list;
31 int err = 0;
32 FILE *fp;
33
34 if (mode == CONFIG_FILE)
35 usage = config_usage;
36
37 argc = parse_options(argc, argv, NULL, options, usage,
38 PARSE_OPT_STOP_AT_NON_OPTION);
39
40 init_bundle_list(&list);
41
42 list.baseURI = xstrdup("<uri>");
43
44 switch (mode) {
45 case KEY_VALUE_PAIRS:
46 if (argc != 1)
47 goto usage;
48 fp = fopen(argv[0], "r");
49 if (!fp)
50 die("failed to open '%s'", argv[0]);
51 while (strbuf_getline(&sb, fp) != EOF) {
52 if (bundle_uri_parse_line(&list, sb.buf))
53 err = error("bad line: '%s'", sb.buf);
54 }
55 fclose(fp);
56 break;
57
58 case CONFIG_FILE:
59 if (argc != 1)
60 goto usage;
61 err = bundle_uri_parse_config_format("<uri>", argv[0], &list);
62 break;
63 }
64 strbuf_release(&sb);
65
66 print_bundle_list(stdout, &list);
67
68 clear_bundle_list(&list);
69
70 return !!err;
71
72 usage:
73 usage_with_options(usage, options);
74 }
75
76 static int cmd_ls_remote(int argc, const char **argv)
77 {
78 const char *dest;
79 struct remote *remote;
80 struct transport *transport;
81 int status = 0;
82
83 dest = argc > 1 ? argv[1] : NULL;
84
85 remote = remote_get(dest);
86 if (!remote) {
87 if (dest)
88 die(_("bad repository '%s'"), dest);
89 die(_("no remote configured to get bundle URIs from"));
90 }
91
92 transport = transport_get(remote, NULL);
93 if (transport_get_remote_bundle_uri(transport) < 0) {
94 error(_("could not get the bundle-uri list"));
95 status = 1;
96 goto cleanup;
97 }
98
99 print_bundle_list(stdout, transport->bundles);
100
101 cleanup:
102 if (transport_disconnect(transport))
103 return 1;
104 return status;
105 }
106
107 int cmd__bundle_uri(int argc, const char **argv)
108 {
109 const char *usage[] = {
110 "test-tool bundle-uri <subcommand> [<options>]",
111 NULL
112 };
113 struct option options[] = {
114 OPT_END(),
115 };
116
117 argc = parse_options(argc, argv, NULL, options, usage,
118 PARSE_OPT_STOP_AT_NON_OPTION |
119 PARSE_OPT_KEEP_ARGV0);
120 if (argc == 1)
121 goto usage;
122
123 if (!strcmp(argv[1], "parse-key-values"))
124 return cmd__bundle_uri_parse(argc - 1, argv + 1, KEY_VALUE_PAIRS);
125 if (!strcmp(argv[1], "parse-config"))
126 return cmd__bundle_uri_parse(argc - 1, argv + 1, CONFIG_FILE);
127 if (!strcmp(argv[1], "ls-remote"))
128 return cmd_ls_remote(argc - 1, argv + 1);
129 error("there is no test-tool bundle-uri tool '%s'", argv[1]);
130
131 usage:
132 usage_with_options(usage, options);
133 }