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