hex.h: move some hex-related declarations from cache.h
[git.git] / diff-merges.c
blobfaa7bc73a343a1eca1e28d920ab32980e0a2f730
1 #include "git-compat-util.h"
2 #include "diff-merges.h"
4 #include "revision.h"
6 typedef void (*diff_merges_setup_func_t)(struct rev_info *);
7 static void set_separate(struct rev_info *revs);
9 static diff_merges_setup_func_t set_to_default = set_separate;
10 static int suppress_m_parsing;
12 static void suppress(struct rev_info *revs)
14 revs->separate_merges = 0;
15 revs->first_parent_merges = 0;
16 revs->combine_merges = 0;
17 revs->dense_combined_merges = 0;
18 revs->combined_all_paths = 0;
19 revs->merges_imply_patch = 0;
20 revs->merges_need_diff = 0;
21 revs->remerge_diff = 0;
24 static void common_setup(struct rev_info *revs)
26 suppress(revs);
27 revs->merges_need_diff = 1;
30 static void set_none(struct rev_info *revs)
32 suppress(revs);
35 static void set_separate(struct rev_info *revs)
37 common_setup(revs);
38 revs->separate_merges = 1;
39 revs->simplify_history = 0;
42 static void set_first_parent(struct rev_info *revs)
44 set_separate(revs);
45 revs->first_parent_merges = 1;
48 static void set_combined(struct rev_info *revs)
50 common_setup(revs);
51 revs->combine_merges = 1;
52 revs->dense_combined_merges = 0;
55 static void set_dense_combined(struct rev_info *revs)
57 common_setup(revs);
58 revs->combine_merges = 1;
59 revs->dense_combined_merges = 1;
62 static void set_remerge_diff(struct rev_info *revs)
64 common_setup(revs);
65 revs->remerge_diff = 1;
66 revs->simplify_history = 0;
69 static diff_merges_setup_func_t func_by_opt(const char *optarg)
71 if (!strcmp(optarg, "off") || !strcmp(optarg, "none"))
72 return set_none;
73 if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
74 return set_first_parent;
75 if (!strcmp(optarg, "separate"))
76 return set_separate;
77 if (!strcmp(optarg, "c") || !strcmp(optarg, "combined"))
78 return set_combined;
79 if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
80 return set_dense_combined;
81 if (!strcmp(optarg, "r") || !strcmp(optarg, "remerge"))
82 return set_remerge_diff;
83 if (!strcmp(optarg, "m") || !strcmp(optarg, "on"))
84 return set_to_default;
85 return NULL;
88 static void set_diff_merges(struct rev_info *revs, const char *optarg)
90 diff_merges_setup_func_t func = func_by_opt(optarg);
92 if (!func)
93 die(_("invalid value for '%s': '%s'"), "--diff-merges", optarg);
95 func(revs);
99 * Public functions. They are in the order they are called.
102 int diff_merges_config(const char *value)
104 diff_merges_setup_func_t func = func_by_opt(value);
106 if (!func)
107 return -1;
109 set_to_default = func;
110 return 0;
113 void diff_merges_suppress_m_parsing(void)
115 suppress_m_parsing = 1;
118 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
120 int argcount = 1;
121 const char *optarg;
122 const char *arg = argv[0];
124 if (!suppress_m_parsing && !strcmp(arg, "-m")) {
125 set_to_default(revs);
126 revs->merges_need_diff = 0;
127 } else if (!strcmp(arg, "-c")) {
128 set_combined(revs);
129 revs->merges_imply_patch = 1;
130 } else if (!strcmp(arg, "--cc")) {
131 set_dense_combined(revs);
132 revs->merges_imply_patch = 1;
133 } else if (!strcmp(arg, "--remerge-diff")) {
134 set_remerge_diff(revs);
135 revs->merges_imply_patch = 1;
136 } else if (!strcmp(arg, "--no-diff-merges")) {
137 set_none(revs);
138 } else if (!strcmp(arg, "--combined-all-paths")) {
139 revs->combined_all_paths = 1;
140 } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
141 set_diff_merges(revs, optarg);
142 } else
143 return 0;
145 revs->explicit_diff_merges = 1;
146 return argcount;
149 void diff_merges_suppress(struct rev_info *revs)
151 set_none(revs);
154 void diff_merges_default_to_first_parent(struct rev_info *revs)
156 if (!revs->explicit_diff_merges)
157 revs->separate_merges = 1;
158 if (revs->separate_merges)
159 revs->first_parent_merges = 1;
162 void diff_merges_default_to_dense_combined(struct rev_info *revs)
164 if (!revs->explicit_diff_merges)
165 set_dense_combined(revs);
168 void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
170 if (!revs->combine_merges)
171 set_dense_combined(revs);
174 void diff_merges_setup_revs(struct rev_info *revs)
176 if (revs->combine_merges == 0)
177 revs->dense_combined_merges = 0;
178 if (revs->separate_merges == 0)
179 revs->first_parent_merges = 0;
180 if (revs->combined_all_paths && !revs->combine_merges)
181 die("--combined-all-paths makes no sense without -c or --cc");
182 if (revs->merges_imply_patch)
183 revs->diff = 1;
184 if (revs->merges_imply_patch || revs->merges_need_diff) {
185 if (!revs->diffopt.output_format)
186 revs->diffopt.output_format = DIFF_FORMAT_PATCH;