convert trivial cases to ALLOC_ARRAY
[git] / test-submodule-config.c
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
4
5 static void die_usage(int argc, char **argv, const char *msg)
6 {
7         fprintf(stderr, "%s\n", msg);
8         fprintf(stderr, "Usage: %s [<commit> <submodulepath>] ...\n", argv[0]);
9         exit(1);
10 }
11
12 static int git_test_config(const char *var, const char *value, void *cb)
13 {
14         return parse_submodule_config_option(var, value);
15 }
16
17 int main(int argc, char **argv)
18 {
19         char **arg = argv;
20         int my_argc = argc;
21         int output_url = 0;
22         int lookup_name = 0;
23
24         arg++;
25         my_argc--;
26         while (starts_with(arg[0], "--")) {
27                 if (!strcmp(arg[0], "--url"))
28                         output_url = 1;
29                 if (!strcmp(arg[0], "--name"))
30                         lookup_name = 1;
31                 arg++;
32                 my_argc--;
33         }
34
35         if (my_argc % 2 != 0)
36                 die_usage(argc, argv, "Wrong number of arguments.");
37
38         setup_git_directory();
39         gitmodules_config();
40         git_config(git_test_config, NULL);
41
42         while (*arg) {
43                 unsigned char commit_sha1[20];
44                 const struct submodule *submodule;
45                 const char *commit;
46                 const char *path_or_name;
47
48                 commit = arg[0];
49                 path_or_name = arg[1];
50
51                 if (commit[0] == '\0')
52                         hashcpy(commit_sha1, null_sha1);
53                 else if (get_sha1(commit, commit_sha1) < 0)
54                         die_usage(argc, argv, "Commit not found.");
55
56                 if (lookup_name) {
57                         submodule = submodule_from_name(commit_sha1, path_or_name);
58                 } else
59                         submodule = submodule_from_path(commit_sha1, path_or_name);
60                 if (!submodule)
61                         die_usage(argc, argv, "Submodule not found.");
62
63                 if (output_url)
64                         printf("Submodule url: '%s' for path '%s'\n",
65                                         submodule->url, submodule->path);
66                 else
67                         printf("Submodule name: '%s' for path '%s'\n",
68                                         submodule->name, submodule->path);
69
70                 arg += 2;
71         }
72
73         submodule_free();
74
75         return 0;
76 }