graph: rename `new_mapping` to `old_mapping`

The change I'm about to make requires being able to inspect the mapping array that was used to render the last GRAPH_COLLAPSING line while rendering a GRAPH_COMMIT line. The `new_mapping` array currently exists as a pre-allocated space for computing the next `mapping` array during `graph_output_collapsing_line()`, but we can repurpose it to let us see the previous `mapping` state. To support this use it will make more sense if this array is named `old_mapping`, as it will contain the mapping data for the previous line we rendered, at the point we're rendering a commit line. Signed-off-by: James Coglan <jcoglan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

James Coglan committed Oct 15, 2019 at 23:47 UTC 0195285b956e1b52defa6c259253a7b888fc25df
1 file changed +27 -27
graph.c
+27 -27
@@ -259,7 +259,7 @@ struct git_graph {
259 /*
260 * The maximum number of columns that can be stored in the columns
261 * and new_columns arrays. This is also half the number of entries
262 - * that can be stored in the mapping and new_mapping arrays.
262 + * that can be stored in the mapping and old_mapping arrays.
263 */
264 int column_capacity;
265 /*
@@ -302,7 +302,7 @@ struct git_graph {
302 * of the git_graph simply so we don't have to allocate a new
303 * temporary array each time we have to output a collapsing line.
304 */
305 - int *new_mapping;
305 + int *old_mapping;
306 /*
307 * The current default column color being used. This is
308 * stored as an index into the array column_colors.
@@ -388,7 +388,7 @@ struct git_graph *graph_init(struct rev_info *opt)
388 ALLOC_ARRAY(graph->columns, graph->column_capacity);
389 ALLOC_ARRAY(graph->new_columns, graph->column_capacity);
390 ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
391 - ALLOC_ARRAY(graph->new_mapping, 2 * graph->column_capacity);
391 + ALLOC_ARRAY(graph->old_mapping, 2 * graph->column_capacity);
392
393 /*
394 * The diff output prefix callback, with this we can make
@@ -418,7 +418,7 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
418 REALLOC_ARRAY(graph->columns, graph->column_capacity);
419 REALLOC_ARRAY(graph->new_columns, graph->column_capacity);
420 REALLOC_ARRAY(graph->mapping, graph->column_capacity * 2);
421 - REALLOC_ARRAY(graph->new_mapping, graph->column_capacity * 2);
421 + REALLOC_ARRAY(graph->old_mapping, graph->column_capacity * 2);
422 }
423
424 /*
@@ -1116,13 +1116,18 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1116 int horizontal_edge_target = -1;
1117
1118 /*
1119 - * Clear out the new_mapping array
1119 + * Swap the mapping and old_mapping arrays
1120 + */
1121 + SWAP(graph->mapping, graph->old_mapping);
1122 +
1123 + /*
1124 + * Clear out the mapping array
1125 */
1126 for (i = 0; i < graph->mapping_size; i++)
1122 - graph->new_mapping[i] = -1;
1127 + graph->mapping[i] = -1;
1128
1129 for (i = 0; i < graph->mapping_size; i++) {
1125 - int target = graph->mapping[i];
1130 + int target = graph->old_mapping[i];
1131 if (target < 0)
1132 continue;
1133
@@ -1143,14 +1148,14 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1148 * This column is already in the
1149 * correct place
1150 */
1146 - assert(graph->new_mapping[i] == -1);
1147 - graph->new_mapping[i] = target;
1148 - } else if (graph->new_mapping[i - 1] < 0) {
1151 + assert(graph->mapping[i] == -1);
1152 + graph->mapping[i] = target;
1153 + } else if (graph->mapping[i - 1] < 0) {
1154 /*
1155 * Nothing is to the left.
1156 * Move to the left by one
1157 */
1153 - graph->new_mapping[i - 1] = target;
1158 + graph->mapping[i - 1] = target;
1159 /*
1160 * If there isn't already an edge moving horizontally
1161 * select this one.
@@ -1166,9 +1171,9 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1171 * line.
1172 */
1173 for (j = (target * 2)+3; j < (i - 2); j += 2)
1169 - graph->new_mapping[j] = target;
1174 + graph->mapping[j] = target;
1175 }
1171 - } else if (graph->new_mapping[i - 1] == target) {
1176 + } else if (graph->mapping[i - 1] == target) {
1177 /*
1178 * There is a branch line to our left
1179 * already, and it is our target. We
@@ -1176,7 +1181,7 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1181 * the same parent commit.
1182 *
1183 * We don't have to add anything to the
1179 - * output or new_mapping, since the
1184 + * output or mapping, since the
1185 * existing branch line has already taken
1186 * care of it.
1187 */
@@ -1192,10 +1197,10 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1197 * The branch to the left of that space
1198 * should be our eventual target.
1199 */
1195 - assert(graph->new_mapping[i - 1] > target);
1196 - assert(graph->new_mapping[i - 2] < 0);
1197 - assert(graph->new_mapping[i - 3] == target);
1198 - graph->new_mapping[i - 2] = target;
1200 + assert(graph->mapping[i - 1] > target);
1201 + assert(graph->mapping[i - 2] < 0);
1202 + assert(graph->mapping[i - 3] == target);
1203 + graph->mapping[i - 2] = target;
1204 /*
1205 * Mark this branch as the horizontal edge to
1206 * prevent any other edges from moving
@@ -1209,14 +1214,14 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1214 /*
1215 * The new mapping may be 1 smaller than the old mapping
1216 */
1212 - if (graph->new_mapping[graph->mapping_size - 1] < 0)
1217 + if (graph->mapping[graph->mapping_size - 1] < 0)
1218 graph->mapping_size--;
1219
1220 /*
1221 * Output out a line based on the new mapping info
1222 */
1223 for (i = 0; i < graph->mapping_size; i++) {
1219 - int target = graph->new_mapping[i];
1224 + int target = graph->mapping[i];
1225 if (target < 0)
1226 graph_line_addch(line, ' ');
1227 else if (target * 2 == i)
@@ -1229,22 +1234,17 @@ static void graph_output_collapsing_line(struct git_graph *graph, struct graph_l
1234 * won't continue into the next line.
1235 */
1236 if (i != (target * 2)+3)
1232 - graph->new_mapping[i] = -1;
1237 + graph->mapping[i] = -1;
1238 used_horizontal = 1;
1239 graph_line_write_column(line, &graph->new_columns[target], '_');
1240 } else {
1241 if (used_horizontal && i < horizontal_edge)
1237 - graph->new_mapping[i] = -1;
1242 + graph->mapping[i] = -1;
1243 graph_line_write_column(line, &graph->new_columns[target], '/');
1244
1245 }
1246 }
1247
1243 - /*
1244 - * Swap mapping and new_mapping
1245 - */
1246 - SWAP(graph->mapping, graph->new_mapping);
1247 -
1248 /*
1249 * If graph->mapping indicates that all of the branch lines
1250 * are already in the correct positions, we are done.