git-p4: fix typos
[git/gitster.git] / t / helper / test-bundle-uri.c
blob0c5fa723d8dc0364776589e7af834dbf936c6666
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"
10 enum input_mode {
11 KEY_VALUE_PAIRS,
12 CONFIG_FILE,
15 static int cmd__bundle_uri_parse(int argc, const char **argv, enum input_mode mode)
17 const char *key_value_usage[] = {
18 "test-tool bundle-uri parse-key-values <input>",
19 NULL
21 const char *config_usage[] = {
22 "test-tool bundle-uri parse-config <input>",
23 NULL
25 const char **usage = key_value_usage;
26 struct option options[] = {
27 OPT_END(),
29 struct strbuf sb = STRBUF_INIT;
30 struct bundle_list list;
31 int err = 0;
32 FILE *fp;
34 if (mode == CONFIG_FILE)
35 usage = config_usage;
37 argc = parse_options(argc, argv, NULL, options, usage,
38 PARSE_OPT_STOP_AT_NON_OPTION);
40 init_bundle_list(&list);
42 list.baseURI = xstrdup("<uri>");
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);
55 fclose(fp);
56 break;
58 case CONFIG_FILE:
59 if (argc != 1)
60 goto usage;
61 err = bundle_uri_parse_config_format("<uri>", argv[0], &list);
62 break;
64 strbuf_release(&sb);
66 print_bundle_list(stdout, &list);
68 clear_bundle_list(&list);
70 return !!err;
72 usage:
73 usage_with_options(usage, options);
76 static int cmd_ls_remote(int argc, const char **argv)
78 const char *dest;
79 struct remote *remote;
80 struct transport *transport;
81 int status = 0;
83 dest = argc > 1 ? argv[1] : NULL;
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"));
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;
99 print_bundle_list(stdout, transport->bundles);
101 cleanup:
102 if (transport_disconnect(transport))
103 return 1;
104 return status;
107 int cmd__bundle_uri(int argc, const char **argv)
109 const char *usage[] = {
110 "test-tool bundle-uri <subcommand> [<options>]",
111 NULL
113 struct option options[] = {
114 OPT_END(),
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;
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]);
131 usage:
132 usage_with_options(usage, options);