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

Re: [PATCH 12/13] remote.c: refactor get_remote_ref_states()

From
Jay Soffian <jaysoffian@gmail.com>
Date
Feb 23, 2009, 08:31 UTC
Message-ID
<1235377892-96089-1-git-send-email-jaysoffian@gmail.com>
In-Reply-To
<76718490902222355v7510d2f9p51ade61d1f257146@mail.gmail.com>

--- So here's the inter-diff doing it the way you suggest. I'm surprised it didn't add any LOC. If you really think this is better I'll re-do the end of the series to do it this way.

 builtin-remote.c |   81 ++++++++++++++++++++++++++---------------------------
 1 files changed, 40 insertions(+), 41 deletions(-)
diff --git a/builtin-remote.c b/builtin-remote.c
index ac49219..5651b41 100644
--- a/builtin-remote.c
+++ b/builtin-remote.c
@@ -19,10 +19,6 @@ static const char * const builtin_remote_usage[] = {
 	NULL
 };
 
-#define GET_REF_STATES (1<<0)
-#define GET_HEAD_NAMES (1<<1)
-#define GET_PUSH_REF_STATES (1<<2)
-
 static int verbose;
 
 static int show_all(void);
@@ -383,7 +379,7 @@ static int get_push_ref_states_noquery(struct ref_states *states)
 }
 
 static int get_head_names(const struct ref *remote_refs,
-	const char *remote_name, struct ref_states *states)
+			  struct ref_states *states)
 {
 	struct ref *ref, *matches;
 	struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
@@ -785,37 +781,15 @@ static int append_ref_to_tracked_list(const char *refname,
 	return 0;
 }
 
-static int get_remote_ref_states(const char *name,
-				 struct ref_states *states,
-				 int query)
+static const struct ref *get_remote_refs(struct ref_states *states)
 {
 	struct transport *transport;
 	const struct ref *remote_refs;
-
-	states->remote = remote_get(name);
-	if (!states->remote)
-		return error("No such remote: %s", name);
-
-	read_branches();
-
-	if (query) {
-		transport = transport_get(NULL, states->remote->url_nr > 0 ?
-			states->remote->url[0] : NULL);
-		remote_refs = transport_get_remote_refs(transport);
-		transport_disconnect(transport);
-		states->queried = 1;
-		if (query & GET_REF_STATES)
-			get_ref_states(remote_refs, states);
-		if (query & GET_HEAD_NAMES)
-			get_head_names(remote_refs, name, states);
-		if (query & GET_PUSH_REF_STATES)
-			get_push_ref_states(remote_refs, states);
-	} else {
-		for_each_ref(append_ref_to_tracked_list, states);
-		sort_string_list(&states->tracked);
-		get_push_ref_states_noquery(states);
-	}
-	return 0;
+	transport = transport_get(NULL, states->remote->url_nr > 0 ?
+				  states->remote->url[0] : NULL);
+	remote_refs = transport_get_remote_refs(transport);
+	transport_disconnect(transport);
+	return remote_refs;
 }
 
 struct show_info {
@@ -970,7 +944,7 @@ int show_push_info_item(struct string_list_item *item, void *cb_data)
 
 static int show(int argc, const char **argv)
 {
-	int no_query = 0, result = 0, query_flag = 0;
+	int no_query = 0, result = 0;
 	struct option options[] = {
 		OPT_GROUP("show specific options"),
 		OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
@@ -985,17 +959,31 @@ static int show(int argc, const char **argv)
 	if (argc < 1)
 		return show_all();
 
-	if (!no_query)
-		query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
-
+	read_branches();
 	memset(&states, 0, sizeof(states));
 	memset(&info, 0, sizeof(info));
+	states.queried = !no_query;
 	info.states = &states;
 	info.list = &info_list;
+
 	for (; argc; argc--, argv++) {
 		int i;
 
-		get_remote_ref_states(*argv, &states, query_flag);
+		if (!(states.remote = remote_get(*argv))) {
+			error("No such remote: %s", *argv);
+			continue;
+		}
+		if (no_query) {
+			for_each_ref(append_ref_to_tracked_list, &states);
+			sort_string_list(&states.tracked);
+			get_push_ref_states_noquery(&states);
+		} else {
+			const struct ref *remote_refs;
+			remote_refs = get_remote_refs(&states);
+			get_ref_states(remote_refs, &states);
+			get_head_names(remote_refs, &states);
+			get_push_ref_states(remote_refs, &states);
+		}
 
 		printf("* remote %s\n  URL: %s\n", *argv,
 			states.remote->url_nr > 0 ?
@@ -1077,8 +1065,13 @@ static int set_head(int argc, const char **argv)
 		head_name = xstrdup(argv[1]);
 	} else if (opt_a && !opt_d && argc == 1) {
 		struct ref_states states;
+		const struct ref *remote_refs;
 		memset(&states, 0, sizeof(states));
-		get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
+		read_branches();
+		if (!(states.remote = remote_get(argv[0])))
+			return error("No such remote: %s", argv[0]);
+		remote_refs = get_remote_refs(&states);
+		get_head_names(remote_refs, &states);
 		if (!states.heads.nr)
 			result |= error("Cannot determine remote HEAD");
 		else if (states.heads.nr > 1) {
@@ -1134,11 +1127,17 @@ static int prune(int argc, const char **argv)
 			? " %s will become dangling!\n"
 			: " %s has become dangling!\n");
 
+	read_branches();
 	memset(&states, 0, sizeof(states));
 	for (; argc; argc--, argv++) {
 		int i;
-
-		get_remote_ref_states(*argv, &states, GET_REF_STATES);
+		const struct ref *remote_refs;
+		if (!(states.remote = remote_get(*argv))) {
+			error("No such remote: %s", *argv);
+			continue;
+		}
+		remote_refs = get_remote_refs(&states);
+		get_ref_states(remote_refs, &states);
 
 		if (states.stale.nr) {
 			printf("Pruning %s\n", *argv);
-- 
1.6.2.rc1.223.gfed32
Previous: Jay SoffianNext: Jeff King
Message 34 of 38 in “New output style for git remote show”
  1. 00/13 New output style for git remote showJay Soffian, Feb 23, 2009
  2. 01/13 remote: rename variable and eliminate redundant function callJay Soffian, Feb 23, 2009
  3. 02/13 remote: remove unused code in get_ref_statesJay Soffian, Feb 23, 2009
  4. 03/13 remote: fix two inconsistencies in the output of "show <remote>"Jay Soffian, Feb 23, 2009
  5. 04/13 remote: make get_remote_ref_states() always populate states.trackedJay Soffian, Feb 23, 2009
  6. Junio C HamanoFeb 24, 2009
  7. Jay SoffianFeb 24, 2009
  8. Jay SoffianFeb 24, 2009
  9. 05/13 remote: name remote_refs consistentlyJay Soffian, Feb 23, 2009
  10. 06/13 string-list: new for_each_string_list() functionJay Soffian, Feb 23, 2009
  11. 07/13 remote: new show output styleJay Soffian, Feb 23, 2009
  12. Jeff KingFeb 23, 2009
  13. Marc BranchaudFeb 23, 2009
  14. 08/13 refactor duplicated get_local_heads() to remote.cJay Soffian, Feb 23, 2009
  15. 09/13 refactor duplicated ref_newer() to remote.cJay Soffian, Feb 23, 2009
  16. Jeff KingFeb 23, 2009
  17. 09/13 refactor duplicated ref_newer() to remote.cJay Soffian, Feb 23, 2009
  18. Johannes SixtFeb 23, 2009
  19. Jay SoffianFeb 23, 2009
  20. 10/13 remote.c: make match_refs() copy src ref before assigning to peer_refJay Soffian, Feb 23, 2009
  21. Junio C HamanoFeb 24, 2009
  22. Jay SoffianFeb 24, 2009
  23. Junio C HamanoFeb 24, 2009
  24. remote.c: make match_refs() copy src ref before assigning to peer_refJay Soffian, Feb 24, 2009
  25. Junio C HamanoFeb 24, 2009
  26. Jay SoffianFeb 24, 2009
  27. Junio C HamanoFeb 24, 2009
  28. 11/13 remote.c: don't short-circuit match_refs() when error in match_explicit_refs()Jay Soffian, Feb 23, 2009
  29. Junio C HamanoFeb 24, 2009
  30. Jay SoffianFeb 24, 2009
  31. 12/13 remote.c: refactor get_remote_ref_states()Jay Soffian, Feb 23, 2009
  32. Jeff KingFeb 23, 2009
  33. Jay SoffianFeb 23, 2009
  34. Jay SoffianFeb 23, 2009
  35. Jeff KingFeb 24, 2009
  36. 13/13 remote: new show output style for push refspecsJay Soffian, Feb 23, 2009
  37. Jeff KingFeb 23, 2009
  38. Jay SoffianFeb 23, 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.