]> git.ipfire.org Git - thirdparty/git.git/blob - t/helper/test-bundle-uri.c
Merge branch 'jk/diff-external-with-no-index' into maint-2.43
[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 if (!remote->url_nr)
92 die(_("remote '%s' has no configured URL"), dest);
93
94 transport = transport_get(remote, NULL);
95 if (transport_get_remote_bundle_uri(transport) < 0) {
96 error(_("could not get the bundle-uri list"));
97 status = 1;
98 goto cleanup;
99 }
100
101 print_bundle_list(stdout, transport->bundles);
102
103 cleanup:
104 if (transport_disconnect(transport))
105 return 1;
106 return status;
107 }
108
109 int cmd__bundle_uri(int argc, const char **argv)
110 {
111 const char *usage[] = {
112 "test-tool bundle-uri <subcommand> [<options>]",
113 NULL
114 };
115 struct option options[] = {
116 OPT_END(),
117 };
118
119 argc = parse_options(argc, argv, NULL, options, usage,
120 PARSE_OPT_STOP_AT_NON_OPTION |
121 PARSE_OPT_KEEP_ARGV0);
122 if (argc == 1)
123 goto usage;
124
125 if (!strcmp(argv[1], "parse-key-values"))
126 return cmd__bundle_uri_parse(argc - 1, argv + 1, KEY_VALUE_PAIRS);
127 if (!strcmp(argv[1], "parse-config"))
128 return cmd__bundle_uri_parse(argc - 1, argv + 1, CONFIG_FILE);
129 if (!strcmp(argv[1], "ls-remote"))
130 return cmd_ls_remote(argc - 1, argv + 1);
131 error("there is no test-tool bundle-uri tool '%s'", argv[1]);
132
133 usage:
134 usage_with_options(usage, options);
135 }