]> git.ipfire.org Git - thirdparty/git.git/commitdiff
graph: reduce duplication in `graph_insert_into_new_columns()`
authorJames Coglan <jcoglan@gmail.com>
Tue, 15 Oct 2019 23:47:50 +0000 (23:47 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 16 Oct 2019 02:11:24 +0000 (11:11 +0900)
I will shortly be making some changes to this function and so am trying
to simplify it. It currently contains some duplicated logic; both
branches the function can take assign the commit's column index into
the `mapping` array and increment `mapping_index`.

Here I change the function so that the only conditional behaviour is
that it appends the commit to `new_columns` if it's not present. All
manipulation of `mapping` now happens on a single code path.

Signed-off-by: James Coglan <jcoglan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
graph.c

diff --git a/graph.c b/graph.c
index c9646d9e006849d74b82fc3dc33a3cd9df3dfe16..512ae16535b6c2f54e0f34617132814ac36d61bf 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -478,23 +478,17 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
        int i = graph_find_new_column_by_commit(graph, commit);
 
        /*
-        * If the commit is already in the new_columns list, we don't need to
-        * add it.  Just update the mapping correctly.
+        * If the commit is not already in the new_columns array, then add it
+        * and record it as being in the final column.
         */
-       if (i >= 0) {
-               graph->mapping[*mapping_index] = i;
-               *mapping_index += 2;
-               return;
+       if (i < 0) {
+               i = graph->num_new_columns++;
+               graph->new_columns[i].commit = commit;
+               graph->new_columns[i].color = graph_find_commit_color(graph, commit);
        }
 
-       /*
-        * This commit isn't already in new_columns.  Add it.
-        */
-       graph->new_columns[graph->num_new_columns].commit = commit;
-       graph->new_columns[graph->num_new_columns].color = graph_find_commit_color(graph, commit);
-       graph->mapping[*mapping_index] = graph->num_new_columns;
+       graph->mapping[*mapping_index] = i;
        *mapping_index += 2;
-       graph->num_new_columns++;
 }
 
 static void graph_update_width(struct git_graph *graph,