]> git.ipfire.org Git - thirdparty/git.git/commitdiff
diff: factor out --follow pathspec check
authorJeff King <peff@peff.net>
Thu, 1 Jun 2023 17:41:06 +0000 (13:41 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sat, 3 Jun 2023 01:34:25 +0000 (10:34 +0900)
In --follow mode, we require exactly one pathspec. We check this
condition in two places:

  - in diff_setup_done(), we complain if --follow is used with an
    inapropriate pathspec

  - in git-log's revision "tweak" function, we enable log.follow only if
    the pathspec allows it

The duplication isn't a big deal right now, since the logic is so
simple. But in preparation for it becoming more complex, let's pull it
into a shared function.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/log.c
diff.c
diff.h

index a70fba198f9451c9005b3ecd14cfce8765e1d838..06e3a1a946f3d3328c2018fb3a76d598cd4618bc 100644 (file)
@@ -856,7 +856,7 @@ static void log_setup_revisions_tweak(struct rev_info *rev,
                                      struct setup_revision_opt *opt)
 {
        if (rev->diffopt.flags.default_follow_renames &&
-           rev->prune_data.nr == 1)
+           diff_check_follow_pathspec(&rev->prune_data, 0))
                rev->diffopt.flags.follow_renames = 1;
 
        if (rev->first_parent_only)
diff --git a/diff.c b/diff.c
index 469e18aed20ed0128bfa85774cb617d455287eaa..dc9f21a9dcd272e205883b017189269fdcf1480f 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -4721,6 +4721,16 @@ unsigned diff_filter_bit(char status)
        return filter_bit[(int) status];
 }
 
+int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error)
+{
+       if (ps->nr != 1) {
+               if (die_on_error)
+                       die(_("--follow requires exactly one pathspec"));
+               return 0;
+       }
+       return 1;
+}
+
 void diff_setup_done(struct diff_options *options)
 {
        unsigned check_mask = DIFF_FORMAT_NAME |
@@ -4828,8 +4838,8 @@ void diff_setup_done(struct diff_options *options)
 
        options->diff_path_counter = 0;
 
-       if (options->flags.follow_renames && options->pathspec.nr != 1)
-               die(_("--follow requires exactly one pathspec"));
+       if (options->flags.follow_renames)
+               diff_check_follow_pathspec(&options->pathspec, 1);
 
        if (!options->use_color || external_diff())
                options->color_moved = 0;
diff --git a/diff.h b/diff.h
index 8d770b1d579c8ec61ce52c4ac4c9df799c509901..31e7db9ab02c86cab8013804e52f5b66a10dd1ca 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -542,6 +542,13 @@ void repo_diff_setup(struct repository *, struct diff_options *);
 struct option *add_diff_options(const struct option *, struct diff_options *);
 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
 void diff_setup_done(struct diff_options *);
+
+/*
+ * Returns true if the pathspec can work with --follow mode. If die_on_error is
+ * set, die() with a specific error message rather than returning false.
+ */
+int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error);
+
 int git_config_rename(const char *var, const char *value);
 
 #define DIFF_DETECT_RENAME     1