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