Git 2.32.3
[git.git] / diff-merges.c
blobf3a9daed7e0519895e4fc4380d05c78b02cb06fa
1 #include "diff-merges.h"
3 #include "revision.h"
5 typedef void (*diff_merges_setup_func_t)(struct rev_info *);
6 static void set_separate(struct rev_info *revs);
8 static diff_merges_setup_func_t set_to_default = set_separate;
10 static void suppress(struct rev_info *revs)
12 revs->separate_merges = 0;
13 revs->first_parent_merges = 0;
14 revs->combine_merges = 0;
15 revs->dense_combined_merges = 0;
16 revs->combined_all_paths = 0;
17 revs->combined_imply_patch = 0;
18 revs->merges_need_diff = 0;
21 static void set_separate(struct rev_info *revs)
23 suppress(revs);
24 revs->separate_merges = 1;
27 static void set_first_parent(struct rev_info *revs)
29 set_separate(revs);
30 revs->first_parent_merges = 1;
33 static void set_m(struct rev_info *revs)
36 * To "diff-index", "-m" means "match missing", and to the "log"
37 * family of commands, it means "show default diff for merges". Set
38 * both fields appropriately.
40 set_to_default(revs);
41 revs->match_missing = 1;
44 static void set_combined(struct rev_info *revs)
46 suppress(revs);
47 revs->combine_merges = 1;
48 revs->dense_combined_merges = 0;
51 static void set_dense_combined(struct rev_info *revs)
53 suppress(revs);
54 revs->combine_merges = 1;
55 revs->dense_combined_merges = 1;
58 static diff_merges_setup_func_t func_by_opt(const char *optarg)
60 if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
61 return suppress;
62 if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
63 return set_first_parent;
64 else if (!strcmp(optarg, "separate"))
65 return set_separate;
66 else if (!strcmp(optarg, "c") || !strcmp(optarg, "combined"))
67 return set_combined;
68 else if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
69 return set_dense_combined;
70 else if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
71 return set_to_default;
72 return NULL;
75 static void set_diff_merges(struct rev_info *revs, const char *optarg)
77 diff_merges_setup_func_t func = func_by_opt(optarg);
79 if (!func)
80 die(_("unknown value for --diff-merges: %s"), optarg);
82 func(revs);
84 /* NOTE: the merges_need_diff flag is cleared by func() call */
85 if (func != suppress)
86 revs->merges_need_diff = 1;
90 * Public functions. They are in the order they are called.
93 int diff_merges_config(const char *value)
95 diff_merges_setup_func_t func = func_by_opt(value);
97 if (!func)
98 return -1;
100 set_to_default = func;
101 return 0;
104 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
106 int argcount = 1;
107 const char *optarg;
108 const char *arg = argv[0];
110 if (!strcmp(arg, "-m")) {
111 set_m(revs);
112 } else if (!strcmp(arg, "-c")) {
113 set_combined(revs);
114 revs->combined_imply_patch = 1;
115 } else if (!strcmp(arg, "--cc")) {
116 set_dense_combined(revs);
117 revs->combined_imply_patch = 1;
118 } else if (!strcmp(arg, "--no-diff-merges")) {
119 suppress(revs);
120 } else if (!strcmp(arg, "--combined-all-paths")) {
121 revs->combined_all_paths = 1;
122 } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
123 set_diff_merges(revs, optarg);
124 } else
125 return 0;
127 revs->explicit_diff_merges = 1;
128 return argcount;
131 void diff_merges_suppress(struct rev_info *revs)
133 suppress(revs);
136 void diff_merges_default_to_first_parent(struct rev_info *revs)
138 if (!revs->explicit_diff_merges)
139 revs->separate_merges = 1;
140 if (revs->separate_merges)
141 revs->first_parent_merges = 1;
144 void diff_merges_default_to_dense_combined(struct rev_info *revs)
146 if (!revs->explicit_diff_merges)
147 set_dense_combined(revs);
150 void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
152 if (!revs->combine_merges)
153 set_dense_combined(revs);
156 void diff_merges_setup_revs(struct rev_info *revs)
158 if (revs->combine_merges == 0)
159 revs->dense_combined_merges = 0;
160 if (revs->separate_merges == 0)
161 revs->first_parent_merges = 0;
162 if (revs->combined_all_paths && !revs->combine_merges)
163 die("--combined-all-paths makes no sense without -c or --cc");
164 if (revs->combined_imply_patch)
165 revs->diff = 1;
166 if (revs->combined_imply_patch || revs->merges_need_diff) {
167 if (!revs->diffopt.output_format)
168 revs->diffopt.output_format = DIFF_FORMAT_PATCH;