1#include "cache.h"
2#include "config.h"
3#include "submodule-config.h"
4#include "submodule.h"
5
6static 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
13int 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 gitmodules_config();
36
37 while (*arg) {
38 struct object_id commit_oid;
39 const struct submodule *submodule;
40 const char *commit;
41 const char *path_or_name;
42
43 commit = arg[0];
44 path_or_name = arg[1];
45
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.");
50
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.");
57
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);
64
65 arg += 2;
66 }
67
68 submodule_free();
69
70 return 0;
71}