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