Merge branch 'rs/parse-options-with-keep-unknown-abbrev-fix' into maint-2.43
[alt-git.git] / diff-merges.c
blob45507588a2797b8d3618e3a19a2854d12051157b
1 #include "git-compat-util.h"
2 #include "diff-merges.h"
4 #include "gettext.h"
5 #include "revision.h"
7 typedef void (*diff_merges_setup_func_t)(struct rev_info *);
8 static void set_separate(struct rev_info *revs);
10 static diff_merges_setup_func_t set_to_default = set_separate;
11 static int suppress_m_parsing;
13 static void suppress(struct rev_info *revs)
15 revs->separate_merges = 0;
16 revs->first_parent_merges = 0;
17 revs->combine_merges = 0;
18 revs->dense_combined_merges = 0;
19 revs->combined_all_paths = 0;
20 revs->merges_imply_patch = 0;
21 revs->merges_need_diff = 0;
22 revs->remerge_diff = 0;
25 static void common_setup(struct rev_info *revs)
27 suppress(revs);
28 revs->merges_need_diff = 1;
31 static void set_none(struct rev_info *revs)
33 suppress(revs);
36 static void set_separate(struct rev_info *revs)
38 common_setup(revs);
39 revs->separate_merges = 1;
40 revs->simplify_history = 0;
43 static void set_first_parent(struct rev_info *revs)
45 set_separate(revs);
46 revs->first_parent_merges = 1;
49 static void set_combined(struct rev_info *revs)
51 common_setup(revs);
52 revs->combine_merges = 1;
53 revs->dense_combined_merges = 0;
56 static void set_dense_combined(struct rev_info *revs)
58 common_setup(revs);
59 revs->combine_merges = 1;
60 revs->dense_combined_merges = 1;
63 static void set_remerge_diff(struct rev_info *revs)
65 common_setup(revs);
66 revs->remerge_diff = 1;
67 revs->simplify_history = 0;
70 static diff_merges_setup_func_t func_by_opt(const char *optarg)
72 if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
73 return set_none;
74 if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
75 return set_first_parent;
76 if (!strcmp(optarg, "separate"))
77 return set_separate;
78 if (!strcmp(optarg, "c") || !strcmp(optarg, "combined"))
79 return set_combined;
80 if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
81 return set_dense_combined;
82 if (!strcmp(optarg, "r") || !strcmp(optarg, "remerge"))
83 return set_remerge_diff;
84 if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
85 return set_to_default;
86 return NULL;
89 static void set_diff_merges(struct rev_info *revs, const char *optarg)
91 diff_merges_setup_func_t func = func_by_opt(optarg);
93 if (!func)
94 die(_("invalid value for '%s': '%s'"), "--diff-merges", optarg);
96 func(revs);
100 * Public functions. They are in the order they are called.
103 int diff_merges_config(const char *value)
105 diff_merges_setup_func_t func = func_by_opt(value);
107 if (!func)
108 return -1;
110 set_to_default = func;
111 return 0;
114 void diff_merges_suppress_m_parsing(void)
116 suppress_m_parsing = 1;
119 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
121 int argcount = 1;
122 const char *optarg;
123 const char *arg = argv[0];
125 if (!suppress_m_parsing && !strcmp(arg, "-m")) {
126 set_to_default(revs);
127 revs->merges_need_diff = 0;
128 } else if (!strcmp(arg, "-c")) {
129 set_combined(revs);
130 revs->merges_imply_patch = 1;
131 } else if (!strcmp(arg, "--cc")) {
132 set_dense_combined(revs);
133 revs->merges_imply_patch = 1;
134 } else if (!strcmp(arg, "--dd")) {
135 set_first_parent(revs);
136 revs->merges_imply_patch = 1;
137 } else if (!strcmp(arg, "--remerge-diff")) {
138 set_remerge_diff(revs);
139 revs->merges_imply_patch = 1;
140 } else if (!strcmp(arg, "--no-diff-merges")) {
141 set_none(revs);
142 } else if (!strcmp(arg, "--combined-all-paths")) {
143 revs->combined_all_paths = 1;
144 } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
145 set_diff_merges(revs, optarg);
146 } else
147 return 0;
149 revs->explicit_diff_merges = 1;
150 return argcount;
153 void diff_merges_suppress(struct rev_info *revs)
155 set_none(revs);
158 void diff_merges_default_to_first_parent(struct rev_info *revs)
160 if (!revs->explicit_diff_merges)
161 revs->separate_merges = 1;
162 if (revs->separate_merges)
163 revs->first_parent_merges = 1;
166 void diff_merges_default_to_dense_combined(struct rev_info *revs)
168 if (!revs->explicit_diff_merges)
169 set_dense_combined(revs);
172 void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
174 if (!revs->combine_merges)
175 set_dense_combined(revs);
178 void diff_merges_setup_revs(struct rev_info *revs)
180 if (revs->combine_merges == 0)
181 revs->dense_combined_merges = 0;
182 if (revs->separate_merges == 0)
183 revs->first_parent_merges = 0;
184 if (revs->combined_all_paths && !revs->combine_merges)
185 die("--combined-all-paths makes no sense without -c or --cc");
186 if (revs->merges_imply_patch)
187 revs->diff = 1;
188 if (revs->merges_imply_patch || revs->merges_need_diff) {
189 if (!revs->diffopt.output_format)
190 revs->diffopt.output_format = DIFF_FORMAT_PATCH;