From: Junio C Hamano Date: Tue, 19 Jul 2016 20:22:19 +0000 (-0700) Subject: Merge branch 'jk/common-main' X-Git-Tag: v2.10.0-rc0~107 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d4c6375fd8996d7d809fb34b9743339d7192c58b;p=git 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() --- d4c6375fd8996d7d809fb34b9743339d7192c58b diff --cc compat/mingw.h index 9a8803b876,1ac9086a82..233933ee86 --- a/compat/mingw.h +++ b/compat/mingw.h @@@ -532,10 -532,10 +532,10 @@@ extern CRITICAL_SECTION pinfo_cs * A replacement of main() that adds win32 specific initialization. */ -void mingw_startup(); -#define main(c,v) dummy_decl_mingw_main(); \ +void mingw_startup(void); +#define main(c,v) dummy_decl_mingw_main(void); \ static int mingw_main(c,v); \ - int main(int argc, char **argv) \ + int main(int argc, const char **argv) \ { \ mingw_startup(); \ return mingw_main(__argc, (void *)__argv); \ diff --cc fast-import.c index 59630cee14,84a13756cc..bf53ac95da --- a/fast-import.c +++ b/fast-import.c @@@ -164,9 -164,7 +164,8 @@@ Format of STDIN stream #include "refs.h" #include "csum-file.h" #include "quote.h" - #include "exec_cmd.h" #include "dir.h" +#include "run-command.h" #define PACK_ID_BITS 16 #define MAX_PACK_ID ((1< %s\n", *argv, show_date(t, tz, &mode)); + } +} + - static void parse_dates(char **argv, struct timeval *now) + static void parse_dates(const char **argv, struct timeval *now) { struct strbuf result = STRBUF_INIT; diff --cc t/helper/test-regex.c index eff26f534f,37b7f06e55..b5ea8a97c5 --- a/t/helper/test-regex.c +++ b/t/helper/test-regex.c @@@ -33,43 -16,5 +33,43 @@@ static int test_regex_bug(void if (m[0].rm_so == 3) /* matches '\n' when it should not */ die("regex bug confirmed: re-build git with NO_REGEX=1"); - exit(0); + return 0; +} + - int main(int argc, char **argv) ++int cmd_main(int argc, const char **argv) +{ + const char *pat; + const char *str; + int flags = 0; + regex_t r; + regmatch_t m[1]; + + if (argc == 2 && !strcmp(argv[1], "--bug")) + return test_regex_bug(); + else if (argc < 3) + usage("test-regex --bug\n" + "test-regex []"); + + argv++; + pat = *argv++; + str = *argv++; + while (*argv) { + struct reg_flag *rf; + for (rf = reg_flags; rf->name; rf++) + if (!strcmp(*argv, rf->name)) { + flags |= rf->flag; + break; + } + if (!rf->name) + die("do not recognize %s", *argv); + argv++; + } + git_setup_gettext(); + + if (regcomp(&r, pat, flags)) + die("failed regcomp() for pattern '%s'", pat); + if (regexec(&r, str, 1, m, 0)) + return 1; + + return 0; } diff --cc upload-pack.c index f93787003a,b97a7659d1..d4cc414bc2 --- a/upload-pack.c +++ b/upload-pack.c @@@ -828,35 -816,45 +828,32 @@@ static int upload_pack_config(const cha return parse_hide_refs_config(var, value, "uploadpack"); } - int main(int argc, const char **argv) + int cmd_main(int argc, const char **argv) { const char *dir; - int i; int strict = 0; + struct option options[] = { + OPT_BOOL(0, "stateless-rpc", &stateless_rpc, + N_("quit after a single request/response exchange")), + OPT_BOOL(0, "advertise-refs", &advertise_refs, + N_("exit immediately after intial ref advertisement")), + OPT_BOOL(0, "strict", &strict, + N_("do not try /.git/ if is no Git directory")), + OPT_INTEGER(0, "timeout", &timeout, + N_("interrupt transfer after seconds of inactivity")), + OPT_END() + }; - git_setup_gettext(); - packet_trace_identity("upload-pack"); - git_extract_argv0_path(argv[0]); check_replace_refs = 0; - for (i = 1; i < argc; i++) { - const char *arg = argv[i]; + argc = parse_options(argc, argv, NULL, options, upload_pack_usage, 0); - if (arg[0] != '-') - break; - if (!strcmp(arg, "--advertise-refs")) { - advertise_refs = 1; - continue; - } - if (!strcmp(arg, "--stateless-rpc")) { - stateless_rpc = 1; - continue; - } - if (!strcmp(arg, "--strict")) { - strict = 1; - continue; - } - if (starts_with(arg, "--timeout=")) { - timeout = atoi(arg+10); - daemon_mode = 1; - continue; - } - if (!strcmp(arg, "--")) { - i++; - break; - } - } + if (argc != 1) + usage_with_options(upload_pack_usage, options); - if (i != argc-1) - usage(upload_pack_usage); + if (timeout) + daemon_mode = 1; setup_path();