Skip to content

Commit 9eac595

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8e32caa commit 9eac595

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ static void log_setup_revisions_tweak(struct rev_info *rev,
856856
struct setup_revision_opt *opt)
857857
{
858858
if (rev->diffopt.flags.default_follow_renames &&
859-
rev->prune_data.nr == 1)
859+
diff_check_follow_pathspec(&rev->prune_data, 0))
860860
rev->diffopt.flags.follow_renames = 1;
861861

862862
if (rev->first_parent_only)

diff.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,6 +4721,16 @@ unsigned diff_filter_bit(char status)
47214721
return filter_bit[(int) status];
47224722
}
47234723

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+
47244734
void diff_setup_done(struct diff_options *options)
47254735
{
47264736
unsigned check_mask = DIFF_FORMAT_NAME |
@@ -4828,8 +4838,8 @@ void diff_setup_done(struct diff_options *options)
48284838

48294839
options->diff_path_counter = 0;
48304840

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);
48334843

48344844
if (!options->use_color || external_diff())
48354845
options->color_moved = 0;

diff.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ void repo_diff_setup(struct repository *, struct diff_options *);
542542
struct option *add_diff_options(const struct option *, struct diff_options *);
543543
int diff_opt_parse(struct diff_options *, const char **, int, const char *);
544544
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+
545552
int git_config_rename(const char *var, const char *value);
546553

547554
#define DIFF_DETECT_RENAME 1

0 commit comments

Comments
 (0)