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

Re: [PATCH 08/21] remote: let guess_remote_head() optionally return all matches

From
Jay Soffian <jaysoffian@gmail.com>
Date
Feb 26, 2009, 18:47 UTC
Message-ID
<76718490902261047p6168e875wf29bcca780105d3e@mail.gmail.com>
In-Reply-To
<20090226144052.GB9693@coredump.intra.peff.net>
On Thu, Feb 26, 2009 at 9:40 AM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
> On Thu, Feb 26, 2009 at 09:37:29AM -0500, Jeff King wrote:
>
>> Hmm. This should probably be:
>>
>>   dst->peer_ref = src->peer_ref ? copy_ref(src->peer_ref) : NULL;
>>
>> (or copy_ref should return NULL when given NULL). I also wonder if the
>> copied ref's peer_ref should be explicitly NULL'd.

Well, if you wanted to be consistent about things (and I apologize if gmail mangles the lines), I'd probably do something like:

diff --git a/remote.c b/remote.c
index 1c09cf0..9f1bf5e 100644
--- a/remote.c
+++ b/remote.c
@@ -779,10 +779,18 @@ struct ref *alloc_ref(const char *name)

 static struct ref *copy_ref(const struct ref *ref)
 {
-	struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
-	memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1);
-	ret->next = NULL;
-	return ret;
+	struct ref *cpy;
+	if (!ref)
+		return NULL;
+	cpy = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
+	memcpy(cpy, ref, sizeof(struct ref) + strlen(ref->name) + 1);
+	cpy->next = cpy->peer_ref = NULL;
+	if (ref->peer_ref) {
+		ref = ref->peer_ref;
+		cpy->peer_ref = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1);
+		memcpy(cpy->peer_ref, ref, sizeof(struct ref) + strlen(ref->name) + 1);
+	}
+	return cpy;
 }

 struct ref *copy_ref_list(const struct ref *ref)
@@ -803,6 +811,7 @@ static void free_ref(struct ref *ref)
 		return;
 	free(ref->remote_status);
 	free(ref->symref);
+	free(ref->peer_ref);
 	free(ref);
 }

@@ -811,7 +820,6 @@ void free_refs(struct ref *ref)
 	struct ref *next;
 	while (ref) {
 		next = ref->next;
-		free(ref->peer_ref);
 		free_ref(ref);
 		ref = next;
 	}
@@ -1457,13 +1465,6 @@ struct ref *get_local_heads(void)
 	return local_refs;
 }

-struct ref *copy_ref_with_peer(const struct ref *src)
-{
-	struct ref *dst = copy_ref(src);
-	dst->peer_ref = copy_ref(src->peer_ref);
-	return dst;
-}
-
 struct ref *guess_remote_head(const struct ref *head,
 			      const struct ref *refs,
 			      int all)
@@ -1480,22 +1481,20 @@ struct ref *guess_remote_head(const struct ref *head,
 	 * where HEAD points; if that is the case, then
 	 * we don't have to guess.
 	 */
-	if (head->symref) {
-		r = find_ref_by_name(refs, head->symref);
-		return r ? copy_ref_with_peer(r) : NULL;
-	}
+	if (head->symref)
+		return copy_ref(find_ref_by_name(refs, head->symref));

 	/* If refs/heads/master could be right, it is. */
 	if (!all) {
 		r = find_ref_by_name(refs, "refs/heads/master");
 		if (r && !hashcmp(r->old_sha1, head->old_sha1))
-			return copy_ref_with_peer(r);
+			return copy_ref(r);
 	}

 	/* Look for another ref that points there */
 	for (r = refs; r; r = r->next) {
 		if (r != head && !hashcmp(r->old_sha1, head->old_sha1)) {
-			*tail = copy_ref_with_peer(r);
+			*tail = copy_ref(r);
 			tail = &((*tail)->next);
 			if (!all)
 				break;


Then peer_ref is consistently a copy, so we can free it consistently, we don't
need two separate copy functions, and copy_ref returns NULL upon receiving
NULL like most of the other foo_ref functions.

> BTW, all of my "probably" and "I wonder" here are because I think the
> "peer ref" pointer is a little vague as a concept. E.g., I think in most
> cases src->peer_ref->peer_ref != src.
>
> Rather than having ref structs with "next" and "peer" pointers, I think
> a more natural data structure would be a list (or array) of "ref pairs".

Actually, we don't need most of the fields in the peer_ref, so we could
probably just embed the extra fields that we need in a peer_struct inside the
ref struct. I can add this to my git todo list.

j.
Previous: Jeff KingNext: Jeff King
Message 12 of 28 in “git remote: set-head and new show output”
  1. 00/21 git remote: set-head and new show outputJay Soffian, Feb 25, 2009
  2. 01/21 test scripts: refactor start_httpd helperJay Soffian, Feb 25, 2009
  3. 02/21 add basic http clone/fetch testsJay Soffian, Feb 25, 2009
  4. 03/21 refactor find_ref_by_name() to accept const listJay Soffian, Feb 25, 2009
  5. 04/21 move duplicated get_local_heads() to remote.cJay Soffian, Feb 25, 2009
  6. 05/21 move duplicated ref_newer() to remote.cJay Soffian, Feb 25, 2009
  7. 06/21 move locate_head() to remote.cJay Soffian, Feb 25, 2009
  8. 07/21 remote: simplify guess_remote_head()Jay Soffian, Feb 25, 2009
  9. 08/21 remote: let guess_remote_head() optionally return all matchesJay Soffian, Feb 25, 2009
  10. Jeff KingFeb 26, 2009
  11. Jeff KingFeb 26, 2009
  12. Jay SoffianFeb 26, 2009
  13. Jeff KingFeb 27, 2009
  14. 09/21 remote: make match_refs() copy src ref before assigning to peer_refJay Soffian, Feb 25, 2009
  15. 10/21 remote: make match_refs() not short-circuitJay Soffian, Feb 25, 2009
  16. 11/21 string-list: new for_each_string_list() functionJay Soffian, Feb 25, 2009
  17. 12/21 builtin-remote: refactor duplicated cleanup codeJay Soffian, Feb 25, 2009
  18. 13/21 builtin-remote: remove unused code in get_ref_statesJay Soffian, Feb 25, 2009
  19. 14/21 builtin-remote: rename variables and eliminate redundant function callJay Soffian, Feb 25, 2009
  20. 15/21 builtin-remote: make get_remote_ref_states() always populate states.trackedJay Soffian, Feb 25, 2009
  21. 16/21 builtin-remote: fix two inconsistencies in the output of "show <remote>"Jay Soffian, Feb 25, 2009
  22. 17/21 builtin-remote: teach show to display remote HEADJay Soffian, Feb 25, 2009
  23. 18/21 builtin-remote: add set-head subcommandJay Soffian, Feb 25, 2009
  24. 19/21 remote: make guess_remote_head() use exact HEAD lookup if it is availableJay Soffian, Feb 25, 2009
  25. 20/21 builtin-remote: new show output styleJay Soffian, Feb 25, 2009
  26. 21/21 builtin-remote: new show output style for push refspecsJay Soffian, Feb 25, 2009
  27. Jeff KingFeb 26, 2009
  28. Junio C HamanoFeb 26, 2009

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.