version: convert to parse-options
[gitweb.git] / help.c
diff --git a/help.c b/help.c
index 53e2a67e0052b7abb9f01e075f76c4eb5f35cbfc..1064363cd5f70aa69c97192e13fd2ca15d8e3b6f 100644 (file)
--- a/help.c
+++ b/help.c
@@ -8,6 +8,7 @@
 #include "column.h"
 #include "version.h"
 #include "refs.h"
+#include "parse-options.h"
 
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
@@ -105,7 +106,22 @@ static int is_executable(const char *name)
                return 0;
 
 #if defined(GIT_WINDOWS_NATIVE)
-{      /* cannot trust the executable bit, peek into the file instead */
+       /*
+        * On Windows there is no executable bit. The file extension
+        * indicates whether it can be run as an executable, and Git
+        * has special-handling to detect scripts and launch them
+        * through the indicated script interpreter. We test for the
+        * file extension first because virus scanners may make
+        * it quite expensive to open many files.
+        */
+       if (ends_with(name, ".exe"))
+               return S_IXUSR;
+
+{
+       /*
+        * Now that we know it does not have an executable extension,
+        * peek into the file instead.
+        */
        char buf[3] = { 0 };
        int n;
        int fd = open(name, O_RDONLY);
@@ -113,8 +129,8 @@ static int is_executable(const char *name)
        if (fd >= 0) {
                n = read(fd, buf, 2);
                if (n == 2)
-                       /* DOS executables start with "MZ" */
-                       if (!strcmp(buf, "#!") || !strcmp(buf, "MZ"))
+                       /* look for a she-bang */
+                       if (!strcmp(buf, "#!"))
                                st.st_mode |= S_IXUSR;
                close(fd);
        }
@@ -409,16 +425,30 @@ const char *help_unknown_cmd(const char *cmd)
 
 int cmd_version(int argc, const char **argv, const char *prefix)
 {
+       int build_options = 0;
+       const char * const usage[] = {
+               N_("git version [<options>]"),
+               NULL
+       };
+       struct option options[] = {
+               OPT_BOOL(0, "build-options", &build_options,
+                        "also print build options"),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, options, usage, 0);
+
        /*
         * The format of this string should be kept stable for compatibility
         * with external projects that rely on the output of "git version".
+        *
+        * Always show the version, even if other options are given.
         */
        printf("git version %s\n", git_version_string);
-       while (*++argv) {
-               if (!strcmp(*argv, "--build-options")) {
-                       printf("sizeof-long: %d\n", (int)sizeof(long));
-                       /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
-               }
+
+       if (build_options) {
+               printf("sizeof-long: %d\n", (int)sizeof(long));
+               /* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
        }
        return 0;
 }