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

[PATCH v2 2/6] name_rev: clarify the logic to assign a new tip-name to a commit

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 30, 2012, 03:50 UTC
Message-ID
<1346298629-13730-3-git-send-email-gitster@pobox.com>
In-Reply-To
<1346298629-13730-1-git-send-email-gitster@pobox.com>

In preparation for later changes, restructure the logic a little bit to separate how the code decides to use the new "tip" for naming a particular commit, and what happens based on the decision.

Also re-indent and correct style of this function while we are at it.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/name-rev.c | 45 +++++++++++++++++++++++++--------------------
 1 file changed, 25 insertions(+), 20 deletions(-)
diff --git a/builtin/name-rev.c b/builtin/name-rev.c
index 8af2cfa..ebbf541 100644
--- a/builtin/name-rev.c
+++ b/builtin/name-rev.c
@@ -19,12 +19,13 @@ static long cutoff = LONG_MAX;
 #define MERGE_TRAVERSAL_WEIGHT 65535
 
 static void name_rev(struct commit *commit,
-		const char *tip_name, int generation, int distance,
-		int deref)
+		     const char *tip_name, int generation, int distance,
+		     int deref)
 {
 	struct rev_name *name = (struct rev_name *)commit->util;
 	struct commit_list *parents;
-	int parent_number = 1;
+	int parent_number;
+	int use_this_tip = 0;
 
 	if (!commit->object.parsed)
 		parse_commit(commit);
@@ -42,21 +43,26 @@ static void name_rev(struct commit *commit,
 			die("generation: %d, but deref?", generation);
 	}
 
-	if (name == NULL) {
-		name = xmalloc(sizeof(struct rev_name));
+	if (!name) {
+		name = xcalloc(1, sizeof(struct rev_name));
 		commit->util = name;
-		goto copy_data;
-	} else if (name->distance > distance) {
-copy_data:
-		name->tip_name = tip_name;
-		name->generation = generation;
-		name->distance = distance;
-	} else
+		use_this_tip = 1;
+	}
+
+	if (distance < name->distance)
+		use_this_tip = 1;
+
+	if (!use_this_tip)
 		return;
 
-	for (parents = commit->parents;
-			parents;
-			parents = parents->next, parent_number++) {
+	name->tip_name = tip_name;
+	name->generation = generation;
+	name->distance = distance;
+
+	/* Propagate our name to our parents */
+	for (parents = commit->parents, parent_number = 1;
+	     parents;
+	     parents = parents->next, parent_number++) {
 		if (parent_number > 1) {
 			int len = strlen(tip_name);
 			char *new_name = xmalloc(len +
@@ -68,16 +74,15 @@ copy_data:
 				len -= 2;
 			if (generation > 0)
 				sprintf(new_name, "%.*s~%d^%d", len, tip_name,
-						generation, parent_number);
+					generation, parent_number);
 			else
 				sprintf(new_name, "%.*s^%d", len, tip_name,
-						parent_number);
-
+					parent_number);
 			name_rev(parents->item, new_name, 0,
-				distance + MERGE_TRAVERSAL_WEIGHT, 0);
+				 distance + MERGE_TRAVERSAL_WEIGHT, 0);
 		} else {
 			name_rev(parents->item, tip_name, generation + 1,
-				distance + 1, 0);
+				 distance + 1, 0);
 		}
 	}
 }
-- 
1.7.12.286.g9df01f7
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 8 in “describe --contains / name-rev --weight”
  1. 0/6 describe --contains / name-rev --weightJunio C Hamano, Aug 30, 2012
  2. 1/6 name-rev: lose unnecessary typedefJunio C Hamano, Aug 30, 2012
  3. 2/6 name_rev: clarify the logic to assign a new tip-name to a commitJunio C Hamano, Aug 30, 2012
  4. 3/6 name-rev: --weight optionJunio C Hamano, Aug 30, 2012
  5. 5/6 name-rev --weight: trivial optimizationJunio C Hamano, Sep 4, 2012
  6. 4/6 name-rev --weight: cache the computed weight in notesJunio C Hamano, Aug 30, 2012
  7. 5/6 name-rev --weight: tests and documentationJunio C Hamano, Aug 30, 2012
  8. 6/6 describe --contains: use "name-rev --weight"Junio C Hamano, Aug 30, 2012

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.