Merge branch 'bp/fsmonitor-prime-index'
[git.git] / t / helper / test-submodule-config.c
blob5c6e4b010d61d2076f08e7077aa5ccbe6db665fc
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "submodule-config.h"
5 #include "submodule.h"
7 static void die_usage(int argc, const char **argv, const char *msg)
9 fprintf(stderr, "%s\n", msg);
10 fprintf(stderr, "Usage: %s [<commit> <submodulepath>] ...\n", argv[0]);
11 exit(1);
14 int cmd__submodule_config(int argc, const char **argv)
16 const char **arg = argv;
17 int my_argc = argc;
18 int output_url = 0;
19 int lookup_name = 0;
21 arg++;
22 my_argc--;
23 while (arg[0] && starts_with(arg[0], "--")) {
24 if (!strcmp(arg[0], "--url"))
25 output_url = 1;
26 if (!strcmp(arg[0], "--name"))
27 lookup_name = 1;
28 arg++;
29 my_argc--;
32 if (my_argc % 2 != 0)
33 die_usage(argc, argv, "Wrong number of arguments.");
35 setup_git_directory();
37 while (*arg) {
38 struct object_id commit_oid;
39 const struct submodule *submodule;
40 const char *commit;
41 const char *path_or_name;
43 commit = arg[0];
44 path_or_name = arg[1];
46 if (commit[0] == '\0')
47 oidclr(&commit_oid);
48 else if (get_oid(commit, &commit_oid) < 0)
49 die_usage(argc, argv, "Commit not found.");
51 if (lookup_name) {
52 submodule = submodule_from_name(&commit_oid, path_or_name);
53 } else
54 submodule = submodule_from_path(&commit_oid, path_or_name);
55 if (!submodule)
56 die_usage(argc, argv, "Submodule not found.");
58 if (output_url)
59 printf("Submodule url: '%s' for path '%s'\n",
60 submodule->url, submodule->path);
61 else
62 printf("Submodule name: '%s' for path '%s'\n",
63 submodule->name, submodule->path);
65 arg += 2;
68 submodule_free();
70 return 0;