merge-ort: setup basic internal data structures

Set up some basic internal data structures. The only carry-over from merge-recursive.c is call_depth, though needed_rename_limit will be added later. The central piece of data will definitely be the strmap "paths", which will map every relevant pathname under consideration to either a merged_info or a conflict_info. ("conflicted" is a strmap that is a subset of "paths".) merged_info contains all relevant information for a non-conflicted entry. conflict_info contains a merged_info, plus any additional information about a conflict such as the higher orders stages involved and the names of the paths those came from (handy once renames get involved). If an entry remains conflicted, the merged_info portion of a conflict_info will later be filled with whatever version of the file should be placed in the working directory (e.g. an as-merged-as-possible variation that contains conflict markers). Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 13, 2020 at 08:04 UTC 5b59c3db059d85306ebeb680c4d322a69ee29fde
1 file changed +147
merge-ort.c
+147
@@ -17,6 +17,153 @@
17 #include "cache.h"
18 #include "merge-ort.h"
19
20 +#include "strmap.h"
21 +
22 +struct merge_options_internal {
23 + /*
24 + * paths: primary data structure in all of merge ort.
25 + *
26 + * The keys of paths:
27 + * * are full relative paths from the toplevel of the repository
28 + * (e.g. "drivers/firmware/raspberrypi.c").
29 + * * store all relevant paths in the repo, both directories and
30 + * files (e.g. drivers, drivers/firmware would also be included)
31 + * * these keys serve to intern all the path strings, which allows
32 + * us to do pointer comparison on directory names instead of
33 + * strcmp; we just have to be careful to use the interned strings.
34 + *
35 + * The values of paths:
36 + * * either a pointer to a merged_info, or a conflict_info struct
37 + * * merged_info contains all relevant information for a
38 + * non-conflicted entry.
39 + * * conflict_info contains a merged_info, plus any additional
40 + * information about a conflict such as the higher orders stages
41 + * involved and the names of the paths those came from (handy
42 + * once renames get involved).
43 + * * a path may start "conflicted" (i.e. point to a conflict_info)
44 + * and then a later step (e.g. three-way content merge) determines
45 + * it can be cleanly merged, at which point it'll be marked clean
46 + * and the algorithm will ignore any data outside the contained
47 + * merged_info for that entry
48 + * * If an entry remains conflicted, the merged_info portion of a
49 + * conflict_info will later be filled with whatever version of
50 + * the file should be placed in the working directory (e.g. an
51 + * as-merged-as-possible variation that contains conflict markers).
52 + */
53 + struct strmap paths;
54 +
55 + /*
56 + * conflicted: a subset of keys->values from "paths"
57 + *
58 + * conflicted is basically an optimization between process_entries()
59 + * and record_conflicted_index_entries(); the latter could loop over
60 + * ALL the entries in paths AGAIN and look for the ones that are
61 + * still conflicted, but since process_entries() has to loop over
62 + * all of them, it saves the ones it couldn't resolve in this strmap
63 + * so that record_conflicted_index_entries() can iterate just the
64 + * relevant entries.
65 + */
66 + struct strmap conflicted;
67 +
68 + /*
69 + * current_dir_name: temporary var used in collect_merge_info_callback()
70 + *
71 + * Used to set merged_info.directory_name; see documentation for that
72 + * variable and the requirements placed on that field.
73 + */
74 + const char *current_dir_name;
75 +
76 + /* call_depth: recursion level counter for merging merge bases */
77 + int call_depth;
78 +};
79 +
80 +struct version_info {
81 + struct object_id oid;
82 + unsigned short mode;
83 +};
84 +
85 +struct merged_info {
86 + /* if is_null, ignore result. otherwise result has oid & mode */
87 + struct version_info result;
88 + unsigned is_null:1;
89 +
90 + /*
91 + * clean: whether the path in question is cleanly merged.
92 + *
93 + * see conflict_info.merged for more details.
94 + */
95 + unsigned clean:1;
96 +
97 + /*
98 + * basename_offset: offset of basename of path.
99 + *
100 + * perf optimization to avoid recomputing offset of final '/'
101 + * character in pathname (0 if no '/' in pathname).
102 + */
103 + size_t basename_offset;
104 +
105 + /*
106 + * directory_name: containing directory name.
107 + *
108 + * Note that we assume directory_name is constructed such that
109 + * strcmp(dir1_name, dir2_name) == 0 iff dir1_name == dir2_name,
110 + * i.e. string equality is equivalent to pointer equality. For this
111 + * to hold, we have to be careful setting directory_name.
112 + */
113 + const char *directory_name;
114 +};
115 +
116 +struct conflict_info {
117 + /*
118 + * merged: the version of the path that will be written to working tree
119 + *
120 + * WARNING: It is critical to check merged.clean and ensure it is 0
121 + * before reading any conflict_info fields outside of merged.
122 + * Allocated merge_info structs will always have clean set to 1.
123 + * Allocated conflict_info structs will have merged.clean set to 0
124 + * initially. The merged.clean field is how we know if it is safe
125 + * to access other parts of conflict_info besides merged; if a
126 + * conflict_info's merged.clean is changed to 1, the rest of the
127 + * algorithm is not allowed to look at anything outside of the
128 + * merged member anymore.
129 + */
130 + struct merged_info merged;
131 +
132 + /* oids & modes from each of the three trees for this path */
133 + struct version_info stages[3];
134 +
135 + /* pathnames for each stage; may differ due to rename detection */
136 + const char *pathnames[3];
137 +
138 + /* Whether this path is/was involved in a directory/file conflict */
139 + unsigned df_conflict:1;
140 +
141 + /*
142 + * For filemask and dirmask, the ith bit corresponds to whether the
143 + * ith entry is a file (filemask) or a directory (dirmask). Thus,
144 + * filemask & dirmask is always zero, and filemask | dirmask is at
145 + * most 7 but can be less when a path does not appear as either a
146 + * file or a directory on at least one side of history.
147 + *
148 + * Note that these masks are related to enum merge_side, as the ith
149 + * entry corresponds to side i.
150 + *
151 + * These values come from a traverse_trees() call; more info may be
152 + * found looking at tree-walk.h's struct traverse_info,
153 + * particularly the documentation above the "fn" member (note that
154 + * filemask = mask & ~dirmask from that documentation).
155 + */
156 + unsigned filemask:3;
157 + unsigned dirmask:3;
158 +
159 + /*
160 + * Optimization to track which stages match, to avoid the need to
161 + * recompute it in multiple steps. Either 0 or at least 2 bits are
162 + * set; if at least 2 bits are set, their corresponding stages match.
163 + */
164 + unsigned match_mask:3;
165 +};
166 +
167 void merge_switch_to_result(struct merge_options *opt,
168 struct tree *head,
169 struct merge_result *result,