1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-02 15:28:21 +01:00
git/t/helper/test-submodule-config.c
Junio C Hamano d4c6375fd8 Merge branch 'jk/common-main'
There are certain house-keeping tasks that need to be performed at
the very beginning of any Git program, and programs that are not
built-in commands had to do them exactly the same way as "git"
potty does.  It was easy to make mistakes in one-off standalone
programs (like test helpers).  A common "main()" function that
calls cmd_main() of individual program has been introduced to
make it harder to make mistakes.

* jk/common-main:
  mingw: declare main()'s argv as const
  common-main: call git_setup_gettext()
  common-main: call restore_sigpipe_to_default()
  common-main: call sanitize_stdfds()
  common-main: call git_extract_argv0_path()
  add an extra level of indirection to main()
2016-07-19 13:22:19 -07:00

76 lines
1.6 KiB
C

#include "cache.h"
#include "submodule-config.h"
#include "submodule.h"
static void die_usage(int argc, const char **argv, const char *msg)
{
fprintf(stderr, "%s\n", msg);
fprintf(stderr, "Usage: %s [<commit> <submodulepath>] ...\n", argv[0]);
exit(1);
}
static int git_test_config(const char *var, const char *value, void *cb)
{
return parse_submodule_config_option(var, value);
}
int cmd_main(int argc, const char **argv)
{
const char **arg = argv;
int my_argc = argc;
int output_url = 0;
int lookup_name = 0;
arg++;
my_argc--;
while (starts_with(arg[0], "--")) {
if (!strcmp(arg[0], "--url"))
output_url = 1;
if (!strcmp(arg[0], "--name"))
lookup_name = 1;
arg++;
my_argc--;
}
if (my_argc % 2 != 0)
die_usage(argc, argv, "Wrong number of arguments.");
setup_git_directory();
gitmodules_config();
git_config(git_test_config, NULL);
while (*arg) {
unsigned char commit_sha1[20];
const struct submodule *submodule;
const char *commit;
const char *path_or_name;
commit = arg[0];
path_or_name = arg[1];
if (commit[0] == '\0')
hashclr(commit_sha1);
else if (get_sha1(commit, commit_sha1) < 0)
die_usage(argc, argv, "Commit not found.");
if (lookup_name) {
submodule = submodule_from_name(commit_sha1, path_or_name);
} else
submodule = submodule_from_path(commit_sha1, path_or_name);
if (!submodule)
die_usage(argc, argv, "Submodule not found.");
if (output_url)
printf("Submodule url: '%s' for path '%s'\n",
submodule->url, submodule->path);
else
printf("Submodule name: '%s' for path '%s'\n",
submodule->name, submodule->path);
arg += 2;
}
submodule_free();
return 0;
}