git-p4: add gitConfigInt reader
[gitweb.git] / builtin / rev-parse.c
index e75ce75bedafd0fb38d716ab724f87319a8d8df7..02d747dcb1a318af98ec06a7ab2f4e489e7c2625 100644 (file)
@@ -190,17 +190,17 @@ static int show_default(void)
        return 0;
 }
 
-static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
 {
        if (ref_excluded(ref_excludes, refname))
                return 0;
-       show_rev(NORMAL, sha1, refname);
+       show_rev(NORMAL, oid->hash, refname);
        return 0;
 }
 
-static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
 {
-       show_rev(REVERSED, sha1, refname);
+       show_rev(REVERSED, oid->hash, refname);
        return 0;
 }
 
@@ -371,6 +371,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                                        N_("output in stuck long form")),
                OPT_END(),
        };
+       static const char * const flag_chars = "*=?!";
 
        struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
        const char **usage = NULL;
@@ -400,7 +401,7 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
        /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
        while (strbuf_getline(&sb, stdin, '\n') != EOF) {
                const char *s;
-               const char *end;
+               const char *help;
                struct option *o;
 
                if (!sb.len)
@@ -410,54 +411,56 @@ static int cmd_parseopt(int argc, const char **argv, const char *prefix)
                memset(opts + onb, 0, sizeof(opts[onb]));
 
                o = &opts[onb++];
-               s = strchr(sb.buf, ' ');
-               if (!s || *sb.buf == ' ') {
+               help = strchr(sb.buf, ' ');
+               if (!help || *sb.buf == ' ') {
                        o->type = OPTION_GROUP;
                        o->help = xstrdup(skipspaces(sb.buf));
                        continue;
                }
 
                o->type = OPTION_CALLBACK;
-               o->help = xstrdup(skipspaces(s));
+               o->help = xstrdup(skipspaces(help));
                o->value = &parsed;
                o->flags = PARSE_OPT_NOARG;
                o->callback = &parseopt_dump;
 
-               /* Possible argument name hint */
-               end = s;
-               while (s > sb.buf && strchr("*=?!", s[-1]) == NULL)
-                       --s;
-               if (s != sb.buf && s != end)
-                       o->argh = xmemdupz(s, end - s);
-               if (s == sb.buf)
-                       s = end;
-
-               while (s > sb.buf && strchr("*=?!", s[-1])) {
-                       switch (*--s) {
+               /* name(s) */
+               s = strpbrk(sb.buf, flag_chars);
+               if (s == NULL)
+                       s = help;
+
+               if (s - sb.buf == 1) /* short option only */
+                       o->short_name = *sb.buf;
+               else if (sb.buf[1] != ',') /* long option only */
+                       o->long_name = xmemdupz(sb.buf, s - sb.buf);
+               else {
+                       o->short_name = *sb.buf;
+                       o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
+               }
+
+               /* flags */
+               while (s < help) {
+                       switch (*s++) {
                        case '=':
                                o->flags &= ~PARSE_OPT_NOARG;
-                               break;
+                               continue;
                        case '?':
                                o->flags &= ~PARSE_OPT_NOARG;
                                o->flags |= PARSE_OPT_OPTARG;
-                               break;
+                               continue;
                        case '!':
                                o->flags |= PARSE_OPT_NONEG;
-                               break;
+                               continue;
                        case '*':
                                o->flags |= PARSE_OPT_HIDDEN;
-                               break;
+                               continue;
                        }
+                       s--;
+                       break;
                }
 
-               if (s - sb.buf == 1) /* short option only */
-                       o->short_name = *sb.buf;
-               else if (sb.buf[1] != ',') /* long option only */
-                       o->long_name = xmemdupz(sb.buf, s - sb.buf);
-               else {
-                       o->short_name = *sb.buf;
-                       o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
-               }
+               if (s < help)
+                       o->argh = xmemdupz(s, help - s);
        }
        strbuf_release(&sb);
 
@@ -511,10 +514,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
        unsigned int flags = 0;
        const char *name = NULL;
        struct object_context unused;
-       struct each_ref_fn_sha1_adapter wrapped_show_reference =
-               {show_reference, NULL};
-       struct each_ref_fn_sha1_adapter wrapped_anti_reference =
-               {anti_reference, NULL};
 
        if (argc > 1 && !strcmp("--parseopt", argv[1]))
                return cmd_parseopt(argc - 1, argv + 1, prefix);
@@ -656,7 +655,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--all")) {
-                               for_each_ref(each_ref_fn_adapter, &wrapped_show_reference);
+                               for_each_ref(show_reference, NULL);
                                continue;
                        }
                        if (starts_with(arg, "--disambiguate=")) {
@@ -664,48 +663,45 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                continue;
                        }
                        if (!strcmp(arg, "--bisect")) {
-                               for_each_ref_in("refs/bisect/bad",
-                                               each_ref_fn_adapter, &wrapped_show_reference);
-                               for_each_ref_in("refs/bisect/good",
-                                               each_ref_fn_adapter, &wrapped_anti_reference);
+                               for_each_ref_in("refs/bisect/bad", show_reference, NULL);
+                               for_each_ref_in("refs/bisect/good", anti_reference, NULL);
                                continue;
                        }
                        if (starts_with(arg, "--branches=")) {
-                               for_each_glob_ref_in(each_ref_fn_adapter, arg + 11,
-                                       "refs/heads/", &wrapped_show_reference);
+                               for_each_glob_ref_in(show_reference, arg + 11,
+                                       "refs/heads/", NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--branches")) {
-                               for_each_branch_ref(each_ref_fn_adapter, &wrapped_show_reference);
+                               for_each_branch_ref(show_reference, NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (starts_with(arg, "--tags=")) {
-                               for_each_glob_ref_in(each_ref_fn_adapter, arg + 7,
-                                       "refs/tags/", &wrapped_show_reference);
+                               for_each_glob_ref_in(show_reference, arg + 7,
+                                       "refs/tags/", NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--tags")) {
-                               for_each_tag_ref(each_ref_fn_adapter, &wrapped_show_reference);
+                               for_each_tag_ref(show_reference, NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (starts_with(arg, "--glob=")) {
-                               for_each_glob_ref(each_ref_fn_adapter, arg + 7,
-                                                 &wrapped_show_reference);
+                               for_each_glob_ref(show_reference, arg + 7, NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (starts_with(arg, "--remotes=")) {
-                               for_each_glob_ref_in(each_ref_fn_adapter, arg + 10,
-                                       "refs/remotes/", &wrapped_show_reference);
+                               for_each_glob_ref_in(show_reference, arg + 10,
+                                       "refs/remotes/", NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--remotes")) {
-                               for_each_remote_ref(each_ref_fn_adapter, &wrapped_show_reference);
+                               for_each_remote_ref(show_reference, NULL);
                                clear_ref_exclusion(&ref_excludes);
                                continue;
                        }