Merge branch 'nd/diff-parseopt-4'
authorJunio C Hamano <gitster@pobox.com>
Thu, 25 Apr 2019 07:41:12 +0000 (16:41 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Apr 2019 07:41:12 +0000 (16:41 +0900)
Fourth batch to teach the diff machinery to use the parse-options
API.

* nd/diff-parseopt-4:
  am: avoid diff_opt_parse()
  diff --no-index: use parse_options() instead of diff_opt_parse()
  range-diff: use parse_options() instead of diff_opt_parse()
  diff.c: allow --no-color-moved-ws
  diff-parseopt: convert --color-moved-ws
  diff-parseopt: convert --[no-]color-moved
  diff-parseopt: convert --inter-hunk-context
  diff-parseopt: convert --no-prefix
  diff-parseopt: convert --line-prefix
  diff-parseopt: convert --[src|dst]-prefix
  diff-parseopt: convert --[no-]abbrev
  diff-parseopt: convert --diff-filter
  diff-parseopt: convert --find-object
  diff-parseopt: convert -O
  diff-parseopt: convert --pickaxe-all|--pickaxe-regex
  diff-parseopt: convert -S|-G
  diff-parseopt: convert -l
  diff-parseopt: convert -z
  diff-parseopt: convert --ita-[in]visible-in-index
  diff-parseopt: convert --ws-error-highlight

1  2 
builtin/am.c
builtin/diff.c
diff-no-index.c
diff.c
diff.h
parse-options.h
t/t4053-diff-no-index.sh

diff --cc builtin/am.c
Simple merge
diff --cc builtin/diff.c
@@@ -321,40 -320,23 +321,24 @@@ int cmd_diff(int argc, const char **arg
  
        repo_init_revisions(the_repository, &rev, prefix);
  
-       if (no_index && argc != i + 2) {
-               if (no_index == DIFF_NO_INDEX_IMPLICIT) {
-                       /*
-                        * There was no --no-index and there were not two
-                        * paths. It is possible that the user intended
-                        * to do an inside-repository operation.
-                        */
-                       fprintf(stderr, "Not a git repository\n");
-                       fprintf(stderr,
-                               "To compare two paths outside a working tree:\n");
-               }
-               /* Give the usage message for non-repository usage and exit. */
-               usagef("git diff %s <path> <path>",
-                      no_index == DIFF_NO_INDEX_EXPLICIT ?
-                      "--no-index" : "[--no-index]");
 -      if (no_index)
 -              /* If this is a no-index diff, just run it and exit there. */
 -              exit(diff_no_index(the_repository, &rev,
 -                                 no_index == DIFF_NO_INDEX_IMPLICIT,
 -                                 argc, argv));
--
-       }
 -      /* Otherwise, we are doing the usual "git" diff */
 -      rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
--
 -      /* Scale to real terminal size and respect statGraphWidth config */
 +      /* Set up defaults that will apply to both no-index and regular diffs. */
        rev.diffopt.stat_width = -1;
        rev.diffopt.stat_graph_width = -1;
 -
 -      /* Default to let external and textconv be used */
        rev.diffopt.flags.allow_external = 1;
        rev.diffopt.flags.allow_textconv = 1;
  
-               diff_no_index(&rev, argc, argv);
 +      /* If this is a no-index diff, just run it and exit there. */
 +      if (no_index)
++              exit(diff_no_index(&rev, no_index == DIFF_NO_INDEX_IMPLICIT,
++                                 argc, argv));
++
 +
 +      /*
 +       * Otherwise, we are doing the usual "git" diff; set up any
 +       * further defaults that apply to regular diffs.
 +       */
 +      rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 +
        /*
         * Default to intent-to-add entries invisible in the
         * index. This makes them show up as new files in diff-files
diff --cc diff-no-index.c
@@@ -233,29 -234,43 +234,37 @@@ static void fixup_paths(const char **pa
        }
  }
  
- void diff_no_index(struct rev_info *revs,
-                  int argc, const char **argv)
+ static const char * const diff_no_index_usage[] = {
+       N_("git diff --no-index [<options>] <path> <path>"),
+       NULL
+ };
 -int diff_no_index(struct repository *r,
 -                struct rev_info *revs,
++int diff_no_index(struct rev_info *revs,
+                 int implicit_no_index,
+                 int argc, const char **argv)
  {
-       int i;
+       int i, no_index;
        const char *paths[2];
        struct strbuf replacement = STRBUF_INIT;
        const char *prefix = revs->prefix;
-       for (i = 1; i < argc - 2; ) {
-               int j;
-               if (!strcmp(argv[i], "--no-index"))
-                       i++;
-               else if (!strcmp(argv[i], "--"))
-                       i++;
-               else {
-                       j = diff_opt_parse(&revs->diffopt, argv + i, argc - i,
-                                          revs->prefix);
-                       if (j <= 0)
-                               die("invalid diff option/value: %s", argv[i]);
-                       i += j;
-               }
+       struct option no_index_options[] = {
+               OPT_BOOL_F(0, "no-index", &no_index, "",
+                          PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
+               OPT_END(),
+       };
+       struct option *options;
 -      /*
 -       * FIXME: --no-index should not look at index and we should be
 -       * able to pass NULL repo. Maybe later.
 -       */
 -      repo_diff_setup(r, &revs->diffopt);
+       options = parse_options_concat(no_index_options,
+                                      revs->diffopt.parseopts);
+       argc = parse_options(argc, argv, revs->prefix, options,
+                            diff_no_index_usage, 0);
+       if (argc != 2) {
+               if (implicit_no_index)
+                       warning(_("Not a git repository. Use --no-index to "
+                                 "compare two paths outside a working tree"));
+               usage_with_options(diff_no_index_usage, options);
        }
+       FREE_AND_NULL(options);
        for (i = 0; i < 2; i++) {
                const char *p = argv[argc - 2 + i];
                if (!strcmp(p, "-"))
diff --cc diff.c
Simple merge
diff --cc diff.h
--- 1/diff.h
--- 2/diff.h
+++ b/diff.h
@@@ -438,7 -439,8 +440,8 @@@ int diff_flush_patch_id(struct diff_opt
  
  int diff_result_code(struct diff_options *, int);
  
- void diff_no_index(struct rev_info *, int, const char **);
 -int diff_no_index(struct repository *, struct rev_info *,
++int diff_no_index(struct rev_info *,
+                 int implicit_no_index, int, const char **);
  
  int index_differs_from(struct repository *r, const char *def,
                       const struct diff_flags *flags,
diff --cc parse-options.h
Simple merge
Simple merge