Merge branch 'dw/diff-highlight-makefile-fix'
[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 int cmd_main(int argc, const char **argv)
14 {
15         const char **arg = argv;
16         int my_argc = argc;
17         int output_url = 0;
18         int lookup_name = 0;
19
20         arg++;
21         my_argc--;
22         while (arg[0] && starts_with(arg[0], "--")) {
23                 if (!strcmp(arg[0], "--url"))
24                         output_url = 1;
25                 if (!strcmp(arg[0], "--name"))
26                         lookup_name = 1;
27                 arg++;
28                 my_argc--;
29         }
30
31         if (my_argc % 2 != 0)
32                 die_usage(argc, argv, "Wrong number of arguments.");
33
34         setup_git_directory();
35
36         while (*arg) {
37                 struct object_id commit_oid;
38                 const struct submodule *submodule;
39                 const char *commit;
40                 const char *path_or_name;
41
42                 commit = arg[0];
43                 path_or_name = arg[1];
44
45                 if (commit[0] == '\0')
46                         oidclr(&commit_oid);
47                 else if (get_oid(commit, &commit_oid) < 0)
48                         die_usage(argc, argv, "Commit not found.");
49
50                 if (lookup_name) {
51                         submodule = submodule_from_name(&commit_oid, path_or_name);
52                 } else
53                         submodule = submodule_from_path(&commit_oid, path_or_name);
54                 if (!submodule)
55                         die_usage(argc, argv, "Submodule not found.");
56
57                 if (output_url)
58                         printf("Submodule url: '%s' for path '%s'\n",
59                                         submodule->url, submodule->path);
60                 else
61                         printf("Submodule name: '%s' for path '%s'\n",
62                                         submodule->name, submodule->path);
63
64                 arg += 2;
65         }
66
67         submodule_free();
68
69         return 0;
70 }