Sync with master
[git/jrn.git] / test-submodule-config.c
blobeda356e6d51594bdb85bdd6dfda94ee555c4b4b6
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
5 static void die_usage(int argc, char **argv, const char *msg)
7 fprintf(stderr, "%s\n", msg);
8 fprintf(stderr, "Usage: %s [<commit> <submodulepath>] ...\n", argv[0]);
9 exit(1);
12 static int git_test_config(const char *var, const char *value, void *cb)
14 return parse_submodule_config_option(var, value);
17 int main(int argc, char **argv)
19 char **arg = argv;
20 int my_argc = argc;
21 int output_url = 0;
23 arg++;
24 my_argc--;
25 while (starts_with(arg[0], "--")) {
26 if (!strcmp(arg[0], "--url"))
27 output_url = 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();
36 gitmodules_config();
37 git_config(git_test_config, NULL);
39 while (*arg) {
40 unsigned char commit_sha1[20];
41 const struct submodule *submodule;
42 const char *commit;
43 const char *path;
45 commit = arg[0];
46 path = arg[1];
48 if (commit[0] == '\0')
49 hashcpy(commit_sha1, null_sha1);
50 else if (get_sha1(commit, commit_sha1) < 0)
51 die_usage(argc, argv, "Commit not found.");
53 submodule = submodule_from_path(commit_sha1, path);
54 if (!submodule)
55 die_usage(argc, argv, "Submodule not found.");
57 if (output_url)
58 printf("Submodule url: '%s' for path '%s'\n",
59 submodule->url, path);
60 else
61 printf("Submodule name: '%s' for path '%s'\n",
62 submodule->name, path);
64 arg += 2;
67 submodule_free();
69 return 0;