Merge branch 'ah/plugleaks'
[git/debian.git] / diff-merges.c
blob0dfcaa1b11b06c456b9b2349859255602df647e8
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;
9 static int suppress_parsing;
11 static void suppress(struct rev_info *revs)
13 revs->separate_merges = 0;
14 revs->first_parent_merges = 0;
15 revs->combine_merges = 0;
16 revs->dense_combined_merges = 0;
17 revs->combined_all_paths = 0;
18 revs->merges_imply_patch = 0;
19 revs->merges_need_diff = 0;
22 static void set_separate(struct rev_info *revs)
24 suppress(revs);
25 revs->separate_merges = 1;
28 static void set_first_parent(struct rev_info *revs)
30 set_separate(revs);
31 revs->first_parent_merges = 1;
34 static void set_combined(struct rev_info *revs)
36 suppress(revs);
37 revs->combine_merges = 1;
38 revs->dense_combined_merges = 0;
41 static void set_dense_combined(struct rev_info *revs)
43 suppress(revs);
44 revs->combine_merges = 1;
45 revs->dense_combined_merges = 1;
48 static diff_merges_setup_func_t func_by_opt(const char *optarg)
50 if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
51 return suppress;
52 if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
53 return set_first_parent;
54 else if (!strcmp(optarg, "separate"))
55 return set_separate;
56 else if (!strcmp(optarg, "c") || !strcmp(optarg, "combined"))
57 return set_combined;
58 else if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
59 return set_dense_combined;
60 else if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
61 return set_to_default;
62 return NULL;
65 static void set_diff_merges(struct rev_info *revs, const char *optarg)
67 diff_merges_setup_func_t func = func_by_opt(optarg);
69 if (!func)
70 die(_("unknown value for --diff-merges: %s"), optarg);
72 func(revs);
74 /* NOTE: the merges_need_diff flag is cleared by func() call */
75 if (func != suppress)
76 revs->merges_need_diff = 1;
80 * Public functions. They are in the order they are called.
83 int diff_merges_config(const char *value)
85 diff_merges_setup_func_t func = func_by_opt(value);
87 if (!func)
88 return -1;
90 set_to_default = func;
91 return 0;
94 void diff_merges_suppress_options_parsing(void)
96 suppress_parsing = 1;
99 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
101 int argcount = 1;
102 const char *optarg;
103 const char *arg = argv[0];
105 if (suppress_parsing)
106 return 0;
108 if (!strcmp(arg, "-m")) {
109 set_to_default(revs);
110 revs->merges_imply_patch = 1;
111 } else if (!strcmp(arg, "-c")) {
112 set_combined(revs);
113 revs->merges_imply_patch = 1;
114 } else if (!strcmp(arg, "--cc")) {
115 set_dense_combined(revs);
116 revs->merges_imply_patch = 1;
117 } else if (!strcmp(arg, "--no-diff-merges")) {
118 suppress(revs);
119 } else if (!strcmp(arg, "--combined-all-paths")) {
120 revs->combined_all_paths = 1;
121 } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
122 set_diff_merges(revs, optarg);
123 } else
124 return 0;
126 revs->explicit_diff_merges = 1;
127 return argcount;
130 void diff_merges_suppress(struct rev_info *revs)
132 suppress(revs);
135 void diff_merges_default_to_first_parent(struct rev_info *revs)
137 if (!revs->explicit_diff_merges)
138 revs->separate_merges = 1;
139 if (revs->separate_merges)
140 revs->first_parent_merges = 1;
143 void diff_merges_default_to_dense_combined(struct rev_info *revs)
145 if (!revs->explicit_diff_merges)
146 set_dense_combined(revs);
149 void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
151 if (!revs->combine_merges)
152 set_dense_combined(revs);
155 void diff_merges_setup_revs(struct rev_info *revs)
157 if (suppress_parsing)
158 return;
160 if (revs->combine_merges == 0)
161 revs->dense_combined_merges = 0;
162 if (revs->separate_merges == 0)
163 revs->first_parent_merges = 0;
164 if (revs->combined_all_paths && !revs->combine_merges)
165 die("--combined-all-paths makes no sense without -c or --cc");
166 if (revs->merges_imply_patch)
167 revs->diff = 1;
168 if (revs->merges_imply_patch || revs->merges_need_diff) {
169 if (!revs->diffopt.output_format)
170 revs->diffopt.output_format = DIFF_FORMAT_PATCH;