diff: factor out --follow pathspec check

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>

Jeff King committed Jun 1, 2023 at 13:41 UTC 9eac5954e81037f282ace2efff7110af3440dc19
3 files changed +20 -3
builtin/log.c
+1 -1
@@ -856,7 +856,7 @@ static void log_setup_revisions_tweak(struct rev_info *rev,
856 struct setup_revision_opt *opt)
857 {
858 if (rev->diffopt.flags.default_follow_renames &&
859 - rev->prune_data.nr == 1)
859 + diff_check_follow_pathspec(&rev->prune_data, 0))
860 rev->diffopt.flags.follow_renames = 1;
861
862 if (rev->first_parent_only)
diff.c
+12 -2
@@ -4721,6 +4721,16 @@ unsigned diff_filter_bit(char status)
4721 return filter_bit[(int) status];
4722 }
4723
4724 +int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error)
4725 +{
4726 + if (ps->nr != 1) {
4727 + if (die_on_error)
4728 + die(_("--follow requires exactly one pathspec"));
4729 + return 0;
4730 + }
4731 + return 1;
4732 +}
4733 +
4734 void diff_setup_done(struct diff_options *options)
4735 {
4736 unsigned check_mask = DIFF_FORMAT_NAME |
@@ -4828,8 +4838,8 @@ void diff_setup_done(struct diff_options *options)
4838
4839 options->diff_path_counter = 0;
4840
4831 - if (options->flags.follow_renames && options->pathspec.nr != 1)
4832 - die(_("--follow requires exactly one pathspec"));
4841 + if (options->flags.follow_renames)
4842 + diff_check_follow_pathspec(&options->pathspec, 1);
4843
4844 if (!options->use_color || external_diff())
4845 options->color_moved = 0;
diff.h
+7
@@ -542,6 +542,13 @@ void repo_diff_setup(struct repository *, struct diff_options *);
542 struct option *add_diff_options(const struct option *, struct diff_options *);
543 int diff_opt_parse(struct diff_options *, const char **, int, const char *);
544 void diff_setup_done(struct diff_options *);
545 +
546 +/*
547 + * Returns true if the pathspec can work with --follow mode. If die_on_error is
548 + * set, die() with a specific error message rather than returning false.
549 + */
550 +int diff_check_follow_pathspec(struct pathspec *ps, int die_on_error);
551 +
552 int git_config_rename(const char *var, const char *value);
553
554 #define DIFF_DETECT_RENAME 1