git-completion.bash: pass $__git_subcommand_idx from __git_main()
[git] / diff-merges.c
1 #include "diff-merges.h"
2
3 #include "revision.h"
4
5 static void suppress(struct rev_info *revs)
6 {
7         revs->separate_merges = 0;
8         revs->first_parent_merges = 0;
9         revs->combine_merges = 0;
10         revs->dense_combined_merges = 0;
11         revs->combined_all_paths = 0;
12         revs->combined_imply_patch = 0;
13         revs->merges_need_diff = 0;
14 }
15
16 static void set_separate(struct rev_info *revs)
17 {
18         suppress(revs);
19         revs->separate_merges = 1;
20 }
21
22 static void set_first_parent(struct rev_info *revs)
23 {
24         set_separate(revs);
25         revs->first_parent_merges = 1;
26 }
27
28 static void set_m(struct rev_info *revs)
29 {
30         /*
31          * To "diff-index", "-m" means "match missing", and to the "log"
32          * family of commands, it means "show full diff for merges". Set
33          * both fields appropriately.
34          */
35         set_separate(revs);
36         revs->match_missing = 1;
37 }
38
39 static void set_combined(struct rev_info *revs)
40 {
41         suppress(revs);
42         revs->combine_merges = 1;
43         revs->dense_combined_merges = 0;
44 }
45
46 static void set_dense_combined(struct rev_info *revs)
47 {
48         suppress(revs);
49         revs->combine_merges = 1;
50         revs->dense_combined_merges = 1;
51 }
52
53 static void set_diff_merges(struct rev_info *revs, const char *optarg)
54 {
55         if (!strcmp(optarg, "off") || !strcmp(optarg, "none")) {
56                 suppress(revs);
57                 /* Return early to leave revs->merges_need_diff unset */
58                 return;
59         }
60
61         if (!strcmp(optarg, "1") || !strcmp(optarg, "first-parent"))
62                 set_first_parent(revs);
63         else if (!strcmp(optarg, "m") || !strcmp(optarg, "separate"))
64                 set_separate(revs);
65         else if (!strcmp(optarg, "c") || !strcmp(optarg, "combined"))
66                 set_combined(revs);
67         else if (!strcmp(optarg, "cc") || !strcmp(optarg, "dense-combined"))
68                 set_dense_combined(revs);
69         else
70                 die(_("unknown value for --diff-merges: %s"), optarg);
71
72         /* The flag is cleared by set_xxx() functions, so don't move this up */
73         revs->merges_need_diff = 1;
74 }
75
76 /*
77  * Public functions. They are in the order they are called.
78  */
79
80 int diff_merges_parse_opts(struct rev_info *revs, const char **argv)
81 {
82         int argcount = 1;
83         const char *optarg;
84         const char *arg = argv[0];
85
86         if (!strcmp(arg, "-m")) {
87                 set_m(revs);
88         } else if (!strcmp(arg, "-c")) {
89                 set_combined(revs);
90                 revs->combined_imply_patch = 1;
91         } else if (!strcmp(arg, "--cc")) {
92                 set_dense_combined(revs);
93                 revs->combined_imply_patch = 1;
94         } else if (!strcmp(arg, "--no-diff-merges")) {
95                 suppress(revs);
96         } else if (!strcmp(arg, "--combined-all-paths")) {
97                 revs->combined_all_paths = 1;
98         } else if ((argcount = parse_long_opt("diff-merges", argv, &optarg))) {
99                 set_diff_merges(revs, optarg);
100         } else
101                 return 0;
102
103         revs->explicit_diff_merges = 1;
104         return argcount;
105 }
106
107 void diff_merges_suppress(struct rev_info *revs)
108 {
109         suppress(revs);
110 }
111
112 void diff_merges_default_to_first_parent(struct rev_info *revs)
113 {
114         if (!revs->explicit_diff_merges)
115                 revs->separate_merges = 1;
116         if (revs->separate_merges)
117                 revs->first_parent_merges = 1;
118 }
119
120 void diff_merges_default_to_dense_combined(struct rev_info *revs)
121 {
122         if (!revs->explicit_diff_merges)
123                 set_dense_combined(revs);
124 }
125
126 void diff_merges_set_dense_combined_if_unset(struct rev_info *revs)
127 {
128         if (!revs->combine_merges)
129                 set_dense_combined(revs);
130 }
131
132 void diff_merges_setup_revs(struct rev_info *revs)
133 {
134         if (revs->combine_merges == 0)
135                 revs->dense_combined_merges = 0;
136         if (revs->separate_merges == 0)
137                 revs->first_parent_merges = 0;
138         if (revs->combined_all_paths && !revs->combine_merges)
139                 die("--combined-all-paths makes no sense without -c or --cc");
140         if (revs->combined_imply_patch)
141                 revs->diff = 1;
142         if (revs->combined_imply_patch || revs->merges_need_diff) {
143                 if (!revs->diffopt.output_format)
144                         revs->diffopt.output_format = DIFF_FORMAT_PATCH;
145         }
146 }