diff: factor out src/dst prefix setup

We directly manipulate diffopt's a_prefix and b_prefix to set up either the default "a/foo" prefix or the "--no-prefix" variant. Although this is only a few lines, it's worth pulling these into their own functions. That lets us avoid one repetition already in this patch, but will also give us a cleaner interface for callers which want to tweak this setting. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 9, 2023 at 01:07 UTC 6799aadfdf484135476aaf74f5d2eb825d9f00e8
2 files changed +16 -5
diff.c
+14 -5
@@ -3374,6 +3374,17 @@ void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const
3374 options->b_prefix = b;
3375 }
3376
3377 +void diff_set_noprefix(struct diff_options *options)
3378 +{
3379 + options->a_prefix = options->b_prefix = "";
3380 +}
3381 +
3382 +void diff_set_default_prefix(struct diff_options *options)
3383 +{
3384 + options->a_prefix = "a/";
3385 + options->b_prefix = "b/";
3386 +}
3387 +
3388 struct userdiff_driver *get_textconv(struct repository *r,
3389 struct diff_filespec *one)
3390 {
@@ -4674,10 +4685,9 @@ void repo_diff_setup(struct repository *r, struct diff_options *options)
4685 options->flags.ignore_untracked_in_submodules = 1;
4686
4687 if (diff_no_prefix) {
4677 - options->a_prefix = options->b_prefix = "";
4688 + diff_set_noprefix(options);
4689 } else if (!diff_mnemonic_prefix) {
4679 - options->a_prefix = "a/";
4680 - options->b_prefix = "b/";
4690 + diff_set_default_prefix(options);
4691 }
4692
4693 options->color_moved = diff_color_moved_default;
@@ -5261,8 +5271,7 @@ static int diff_opt_no_prefix(const struct option *opt,
5271
5272 BUG_ON_OPT_NEG(unset);
5273 BUG_ON_OPT_ARG(optarg);
5264 - options->a_prefix = "";
5265 - options->b_prefix = "";
5274 + diff_set_noprefix(options);
5275 return 0;
5276 }
5277
diff.h
+2
@@ -497,6 +497,8 @@ void diff_tree_combined(const struct object_id *oid, const struct oid_array *par
497 void diff_tree_combined_merge(const struct commit *commit, struct rev_info *rev);
498
499 void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b);
500 +void diff_set_noprefix(struct diff_options *options);
501 +void diff_set_default_prefix(struct diff_options *options);
502
503 int diff_can_quit_early(struct diff_options *);
504