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

[PATCH 07/15] ref_remove_duplicates(): simplify function

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Oct 23, 2013, 15:50 UTC
Message-ID
<1382543448-2586-8-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1382543448-2586-1-git-send-email-mhagger@alum.mit.edu>
* Use a dedicated variable, ref, for referring to the current item
  rather than using the ref_map pointer for this purpose.
* Use a (struct ref **) as iteration variable to avoid having to keep
  track of prev and next in addition to the pointer to the current
  item.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 remote.c | 32 ++++++++++++++++++--------------
 1 file changed, 18 insertions(+), 14 deletions(-)
diff --git a/remote.c b/remote.c
index a44e897..5ade07f 100644
--- a/remote.c
+++ b/remote.c
@@ -748,29 +748,33 @@ int for_each_remote(each_remote_fn fn, void *priv)
 void ref_remove_duplicates(struct ref *ref_map)
 {
 	struct string_list refs = STRING_LIST_INIT_NODUP;
-	struct string_list_item *item = NULL;
-	struct ref *prev = NULL, *next = NULL;
+	struct ref **p;
 
-	for (; ref_map; prev = ref_map, ref_map = next) {
-		next = ref_map->next;
-		if (!ref_map->peer_ref)
+	for (p = &ref_map; *p; ) {
+		struct ref *ref = *p;
+		struct string_list_item *item;
+
+		if (!ref->peer_ref) {
+			p = &ref->next;
 			continue;
+		}
 
-		item = string_list_insert(&refs, ref_map->peer_ref->name);
+		item = string_list_insert(&refs, ref->peer_ref->name);
 		if (item->util) {
 			/* Entry already existed */
 			if (strcmp(((struct ref *)item->util)->name,
-				   ref_map->name))
+				   ref->name))
 				die("%s tracks both %s and %s",
-				    ref_map->peer_ref->name,
+				    ref->peer_ref->name,
 				    ((struct ref *)item->util)->name,
-				    ref_map->name);
-			prev->next = ref_map->next;
-			free(ref_map->peer_ref);
-			free(ref_map);
-			ref_map = prev; /* skip this; we freed it */
+				    ref->name);
+			/* item is a duplicate; remove and free it */
+			*p = ref->next;
+			free(ref->peer_ref);
+			free(ref);
 		} else {
-			item->util = ref_map;
+			item->util = ref;
+			p = &ref->next;
 		}
 	}
 	string_list_clear(&refs, 0);
-- 
1.8.4
Previous: Michael HaggertyNext: Michael Haggerty
Message 15 of 34 in “Change semantics of "fetch --tags"”
  1. 00/15 Change semantics of "fetch --tags"Michael Haggerty, Oct 23, 2013
  2. 01/15 t5510: use the correct tag name in testMichael Haggerty, Oct 23, 2013
  3. 02/15 t5510: prepare test refs more straightforwardlyMichael Haggerty, Oct 23, 2013
  4. Junio C HamanoOct 23, 2013
  5. Michael HaggertyOct 24, 2013
  6. Junio C HamanoOct 24, 2013
  7. 03/15 t5510: check that "git fetch --prune --tags" does not prune branchesMichael Haggerty, Oct 23, 2013
  8. 04/15 api-remote.txt: correct section "struct refspect"Michael Haggerty, Oct 23, 2013
  9. Junio C HamanoOct 23, 2013
  10. Michael HaggertyOct 24, 2013
  11. 05/15 get_ref_map(): rename local variablesMichael Haggerty, Oct 23, 2013
  12. Junio C HamanoOct 23, 2013
  13. Michael HaggertyOct 24, 2013
  14. 06/15 ref_remove_duplicates(): avoid redundant bisectionMichael Haggerty, Oct 23, 2013
  15. 07/15 ref_remove_duplicates(): simplify functionMichael Haggerty, Oct 23, 2013
  16. 08/15 ref_remove_duplicates(): improve documentation commentMichael Haggerty, Oct 23, 2013
  17. Junio C HamanoOct 23, 2013
  18. 09/15 builtin/fetch.c: reorder function definitionsMichael Haggerty, Oct 23, 2013
  19. 10/15 fetch --tags: fetch tags *in addition to* other stuffMichael Haggerty, Oct 23, 2013
  20. Junio C HamanoOct 24, 2013
  21. Michael HaggertyOct 25, 2013
  22. Junio C HamanoOct 28, 2013
  23. Michael HaggertyOct 30, 2013
  24. Michael HaggertyOct 26, 2013
  25. 11/15 fetch --prune: prune only based on explicit refspecsMichael Haggerty, Oct 23, 2013
  26. Junio C HamanoOct 24, 2013
  27. Michael HaggertyOct 26, 2013
  28. Junio C HamanoOct 28, 2013
  29. 12/15 query_refspecs(): move some constants out of the loopMichael Haggerty, Oct 23, 2013
  30. 13/15 builtin/remote.c: reorder function definitionsMichael Haggerty, Oct 23, 2013
  31. 14/15 builtin/remote.c:update(): use struct argv_arrayMichael Haggerty, Oct 23, 2013
  32. 15/15 fetch, remote: properly convey --no-prune options to subprocessesMichael Haggerty, Oct 23, 2013
  33. Junio C HamanoOct 24, 2013
  34. Junio C HamanoOct 23, 2013

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.