git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] graph API: don't print branch lines for uninteresting merge parents

From
Adam Simpkins <adam@adamsimpkins.net>
Date
May 24, 2008, 02:24 UTC
Message-ID
<1211595851-11992-3-git-send-email-adam@adamsimpkins.net>
In-Reply-To
<1211595851-11992-2-git-send-email-adam@adamsimpkins.net>

Previously, the graphing code printed lines coming out of a merge commit for all of its parents, even if some of them were uninteresting. Now it only prints lines for interesting commits.

For example, for a merge commit where only the first parent is interesting, the code now prints:

  *  merge commit
  *  interesting child
instead of:
  M  merge commit
  |\
  *  interesting child
---
 graph.c |   57 ++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 44 insertions(+), 13 deletions(-)
diff --git a/graph.c b/graph.c
index 400f014..add7e44 100644
--- a/graph.c
+++ b/graph.c
@@ -55,9 +55,11 @@ struct git_graph {
 	 */
 	struct commit *commit;
 	/*
-	 * The number of parents this commit has.
-	 * (Stored so we don't have to walk over them each time we need
-	 * this number)
+	 * The number of interesting parents that this commit has.
+	 *
+	 * Note that this is not the same as the actual number of parents.
+	 * This count excludes parents that won't be printed in the graph
+	 * output, as determined by graph_is_interesting().
 	 */
 	int num_parents;
 	/*
@@ -180,6 +182,18 @@ static void graph_ensure_capacity(struct git_graph *graph, int num_columns)
 				      sizeof(int) * 2 * graph->column_capacity);
 }
 
+/*
+ * Returns 1 if the commit will be printed in the graph output,
+ * and 0 otherwise.
+ */
+static int graph_is_interesting(struct commit *commit)
+{
+	/*
+	 * Uninteresting and pruned commits won't be printed
+	 */
+	return (commit->object.flags & (UNINTERESTING | TREESAME)) ? 0 : 1;
+}
+
 static void graph_insert_into_new_columns(struct git_graph *graph,
 					  struct commit *commit,
 					  int *mapping_index)
@@ -187,13 +201,10 @@ static void graph_insert_into_new_columns(struct git_graph *graph,
 	int i;
 
 	/*
-	 * Ignore uinteresting and pruned commits
+	 * Ignore uinteresting commits
 	 */
-	if (commit->object.flags & (UNINTERESTING | TREESAME))
-	{
-		*mapping_index += 2;
+	if (!graph_is_interesting(commit))
 		return;
-	}
 
 	/*
 	 * If the commit is already in the new_columns list, we don't need to
@@ -231,8 +242,8 @@ static void graph_update_width(struct git_graph *graph,
 	int max_cols = graph->num_columns + graph->num_parents;
 
 	/*
-	 * Even if the current commit has no parents, it still takes up a
-	 * column for itself.
+	 * Even if the current commit has no parents to be printed, it
+	 * still takes up a column for itself.
 	 */
 	if (graph->num_parents < 1)
 		max_cols++;
@@ -316,6 +327,7 @@ static void graph_update_columns(struct git_graph *graph)
 		}
 
 		if (col_commit == graph->commit) {
+			int old_mapping_idx = mapping_idx;
 			seen_this = 1;
 			for (parent = graph->commit->parents;
 			     parent;
@@ -324,6 +336,14 @@ static void graph_update_columns(struct git_graph *graph)
 							      parent->item,
 							      &mapping_idx);
 			}
+			/*
+			 * We always need to increment mapping_idx by at
+			 * least 2, even if it has no interesting parents.
+			 * The current commit always takes up at least 2
+			 * spaces.
+			 */
+			if (mapping_idx == old_mapping_idx)
+				mapping_idx += 2;
 		} else {
 			graph_insert_into_new_columns(graph, col_commit,
 						      &mapping_idx);
@@ -353,11 +373,13 @@ void graph_update(struct git_graph *graph, struct commit *commit)
 	graph->commit = commit;
 
 	/*
-	 * Count how many parents this commit has
+	 * Count how many interesting parents this commit has
 	 */
 	graph->num_parents = 0;
-	for (parent = commit->parents; parent; parent = parent->next)
-		graph->num_parents++;
+	for (parent = commit->parents; parent; parent = parent->next) {
+		if (graph_is_interesting(parent->item))
+			graph->num_parents++;
+	}
 
 	/*
 	 * Call graph_update_columns() to update
@@ -543,6 +565,15 @@ void graph_output_commit_line(struct git_graph *graph, struct strbuf *sb)
 
 		if (col_commit == graph->commit) {
 			seen_this = 1;
+			/*
+			 * If the commit has more than 1 interesting
+			 * parent, print 'M' to indicate that it is a
+			 * merge.  Otherwise, print '*'.
+			 *
+			 * Note that even if this is actually a merge
+			 * commit, we still print '*' if less than 2 of its
+			 * parents are interesting.
+			 */
 			if (graph->num_parents > 1)
 				strbuf_addch(sb, 'M');
 			else
-- 
1.5.5.1.359.gebc23.dirty
Previous: Adam SimpkinsNext: Teemu Likonen
Message 7 of 15 in “bug in "git log --graph" at the tail end?”
  1. Junio C HamanoMay 23, 2008
  2. Reece DunnMay 23, 2008
  3. Adam SimpkinsMay 24, 2008
  4. Junio C HamanoMay 24, 2008
  5. 0/2 fix "log --graph" issues with uninteresting parentsAdam Simpkins, May 24, 2008
  6. 1/2 graph API: fix graph mis-alignment after uninteresting commitsAdam Simpkins, May 24, 2008
  7. 2/2 graph API: don't print branch lines for uninteresting merge parentsAdam Simpkins, May 24, 2008
  8. Teemu LikonenMay 24, 2008
  9. 0/1 graph API: always print 'M' for merge commitsAdam Simpkins, May 24, 2008
  10. 1/1 graph API: always print 'M' for merge commitsAdam Simpkins, May 24, 2008
  11. Adam SimpkinsMay 24, 2008
  12. Junio C HamanoMay 25, 2008
  13. Teemu LikonenMay 25, 2008
  14. Adam SimpkinsMay 25, 2008
  15. Adam SimpkinsMay 24, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.