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

[PATCH 2/4] builtin-remote: split show_or_prune() in two separate functions

From
OMOlivier Marin <dkr+ml.git@free.fr>
Date
Jun 10, 2008, 14:51 UTC
Message-ID
<1213109481-6939-1-git-send-email-dkr+ml.git@free.fr>
In-Reply-To
<1213109413-6842-1-git-send-email-dkr+ml.git@free.fr>
From: Olivier Marin <dkr@freesurf.fr>

This allow us to add different features to each of them and keep the code simple at the same time. Also create a get_remote_ref_states() to avoid duplicated code.

Signed-off-by: Olivier Marin <dkr@freesurf.fr>
---
 builtin-remote.c |  101 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 67 insertions(+), 34 deletions(-)
diff --git a/builtin-remote.c b/builtin-remote.c
index efe74c7..745a4ee 100644
--- a/builtin-remote.c
+++ b/builtin-remote.c
@@ -419,7 +419,32 @@ static void show_list(const char *title, struct path_list *list)
 	printf("\n");
 }
 
-static int show_or_prune(int argc, const char **argv, int prune)
+static int get_remote_ref_states(const char *name,
+				 struct ref_states *states,
+				 int query)
+{
+	struct transport *transport;
+	const struct ref *ref;
+
+	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);
+		ref = transport_get_remote_refs(transport);
+		transport_disconnect(transport);
+
+		get_ref_states(ref, states);
+	}
+
+	return 0;
+}
+
+static int show(int argc, const char **argv)
 {
 	int no_query = 0, result = 0;
 	struct option options[] = {
@@ -431,42 +456,15 @@ static int show_or_prune(int argc, const char **argv, int prune)
 
 	argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
 
-	if (argc < 1) {
-		if (!prune)
-			return show_all();
-		usage_with_options(builtin_remote_usage, options);
-	}
+	if (argc < 1)
+		return show_all();
 
 	memset(&states, 0, sizeof(states));
 	for (; argc; argc--, argv++) {
-		struct transport *transport;
-		const struct ref *ref;
 		struct strbuf buf;
 		int i;
 
-		states.remote = remote_get(*argv);
-		if (!states.remote)
-			return error("No such remote: %s", *argv);
-
-		read_branches();
-
-		if (!no_query) {
-			transport = transport_get(NULL,
-				states.remote->url_nr > 0 ?
-				states.remote->url[0] : NULL);
-			ref = transport_get_remote_refs(transport);
-			transport_disconnect(transport);
-
-			get_ref_states(ref, &states);
-		}
-
-		if (prune) {
-			for (i = 0; i < states.stale.nr; i++) {
-				const char *refname = states.stale.items[i].util;
-				result |= delete_ref(refname, NULL);
-			}
-			goto cleanup_states;
-		}
+		get_remote_ref_states(*argv, &states, !no_query);
 
 		printf("* remote %s\n  URL: %s\n", *argv,
 			states.remote->url_nr > 0 ?
@@ -513,7 +511,42 @@ static int show_or_prune(int argc, const char **argv, int prune)
 			}
 			printf("\n");
 		}
-cleanup_states:
+
+		/* NEEDSWORK: free remote */
+		path_list_clear(&states.new, 0);
+		path_list_clear(&states.stale, 0);
+		path_list_clear(&states.tracked, 0);
+	}
+
+	return result;
+}
+
+static int prune(int argc, const char **argv)
+{
+	int no_query = 0, result = 0;
+	struct option options[] = {
+		OPT_GROUP("prune specific options"),
+		OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
+		OPT_END()
+	};
+	struct ref_states states;
+
+	argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
+
+	if (argc < 1)
+		usage_with_options(builtin_remote_usage, options);
+
+	memset(&states, 0, sizeof(states));
+	for (; argc; argc--, argv++) {
+		int i;
+
+		get_remote_ref_states(*argv, &states, !no_query);
+
+		for (i = 0; i < states.stale.nr; i++) {
+			const char *refname = states.stale.items[i].util;
+			result |= delete_ref(refname, NULL);
+		}
+
 		/* NEEDSWORK: free remote */
 		path_list_clear(&states.new, 0);
 		path_list_clear(&states.stale, 0);
@@ -634,9 +667,9 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
 	else if (!strcmp(argv[0], "rm"))
 		result = rm(argc, argv);
 	else if (!strcmp(argv[0], "show"))
-		result = show_or_prune(argc, argv, 0);
+		result = show(argc, argv);
 	else if (!strcmp(argv[0], "prune"))
-		result = show_or_prune(argc, argv, 1);
+		result = prune(argc, argv);
 	else if (!strcmp(argv[0], "update"))
 		result = update(argc, argv);
 	else {
-- 
1.5.6.rc2.160.gd660c
Previous: Olivier MarinNext: Olivier Marin
Message 27 of 36 in “remote show/prune: strange -n(--dry-run) option.”
  1. Olivier MarinJun 8, 2008
  2. Documentation/git-remote.txt: remove description for useless -n optionOlivier Marin, Jun 8, 2008
  3. Documentation/git-remote.txt: remove description for useless -n optiondkr+ml.git@free.fr, Jun 8, 2008
  4. Junio C HamanoJun 8, 2008
  5. Olivier MarinJun 9, 2008
  6. remote show: fix the -n optionOlivier Marin, Jun 9, 2008
  7. Johannes SchindelinJun 9, 2008
  8. Olivier MarinJun 9, 2008
  9. Johannes SchindelinJun 9, 2008
  10. Olivier MarinJun 9, 2008
  11. Johannes SchindelinJun 9, 2008
  12. Olivier MarinJun 9, 2008
  13. Olivier MarinJun 9, 2008
  14. Johannes SchindelinJun 9, 2008
  15. remote show: fix the -n optionOlivier Marin, Jun 9, 2008
  16. Johannes SchindelinJun 9, 2008
  17. Olivier MarinJun 9, 2008
  18. Johannes SchindelinJun 9, 2008
  19. Olivier MarinJun 9, 2008
  20. builtin-remote: make reuse of code easier by not die()ingJohannes Schindelin, Jun 9, 2008
  21. Olivier MarinJun 9, 2008
  22. Junio C HamanoJun 10, 2008
  23. Shawn O. PearceJun 10, 2008
  24. Johannes SchindelinJun 10, 2008
  25. 0/4 remote show/prune improvementOlivier Marin, Jun 10, 2008
  26. 1/4 remote show: fix the -n optionOlivier Marin, Jun 10, 2008
  27. 2/4 builtin-remote: split show_or_prune() in two separate functionsOlivier Marin, Jun 10, 2008
  28. 3/4 remote prune: print the list of pruned branchesOlivier Marin, Jun 10, 2008
  29. Junio C HamanoJun 12, 2008
  30. Olivier MarinJun 12, 2008
  31. 4/4 remote show: list tracked remote branches with -nOlivier Marin, Jun 10, 2008
  32. Junio C HamanoJun 10, 2008
  33. Olivier MarinJun 10, 2008
  34. Jakub NarebskiJun 10, 2008
  35. Olivier MarinJun 10, 2008
  36. Jakub NarebskiJun 10, 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.