git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Olivier Marin <dkr+ml.git@free.fr>
To: Junio C Hamano <gitster@pobox.com>
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	"Shawn O. Pearce" <spearce@spearce.org>,
	git@vger.kernel.org, Olivier Marin <dkr@freesurf.fr>
Subject: [PATCH 2/4] builtin-remote: split show_or_prune() in two separate functions
Date: Tue, 10 Jun 2008 16:51:21 +0200	[thread overview]
Message-ID: <1213109481-6939-1-git-send-email-dkr+ml.git@free.fr> (raw)
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

  parent reply	other threads:[~2008-06-10 14:52 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-08  0:54 remote show/prune: strange -n(--dry-run) option Olivier Marin
2008-06-08 11:03 ` [PATCH] Documentation/git-remote.txt: remove description for useless -n option Olivier Marin
2008-06-08 12:22 ` dkr+ml.git
2008-06-08 20:27   ` Junio C Hamano
2008-06-09  0:43     ` Olivier Marin
2008-06-09  0:48       ` [PATCH] remote show: fix the " Olivier Marin
2008-06-09  1:16         ` Johannes Schindelin
2008-06-09  2:06           ` Olivier Marin
2008-06-09  2:35             ` Johannes Schindelin
2008-06-09  4:16               ` Olivier Marin
2008-06-09  4:53                 ` Johannes Schindelin
2008-06-09 14:22                   ` Olivier Marin
2008-06-09 15:43                     ` Olivier Marin
2008-06-09 16:31                       ` Johannes Schindelin
2008-06-09 15:58         ` [PATCH v2] " Olivier Marin
2008-06-09 16:35           ` Johannes Schindelin
2008-06-09 16:58             ` Olivier Marin
2008-06-09 17:56               ` Johannes Schindelin
2008-06-09 18:37                 ` Olivier Marin
2008-06-09 20:11                   ` [PATCH] builtin-remote: make reuse of code easier by not die()ing Johannes Schindelin
2008-06-09 20:43                     ` Olivier Marin
2008-06-10  1:10             ` [PATCH v2] remote show: fix the -n option Junio C Hamano
2008-06-10  1:19               ` Shawn O. Pearce
2008-06-10  2:39                 ` Johannes Schindelin
2008-06-10 14:50               ` [PATCH 0/4] remote show/prune improvement Olivier Marin
2008-06-10 14:51                 ` [PATCH 1/4] remote show: fix the -n option Olivier Marin
2008-06-10 14:51                 ` Olivier Marin [this message]
2008-06-10 14:51                 ` [PATCH 3/4] remote prune: print the list of pruned branches Olivier Marin
2008-06-12  7:00                   ` Junio C Hamano
2008-06-12 11:07                     ` Olivier Marin
2008-06-10 14:51                 ` [PATCH 4/4] remote show: list tracked remote branches with -n Olivier Marin
2008-06-10 19:12                   ` Junio C Hamano
2008-06-10 22:54                     ` [PATCH v2 " Olivier Marin
2008-06-10 15:09                 ` [PATCH 0/4] remote show/prune improvement Jakub Narebski
2008-06-10 16:10                   ` Olivier Marin
2008-06-10 17:11                     ` Jakub Narebski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1213109481-6939-1-git-send-email-dkr+ml.git@free.fr \
    --to=dkr+ml.git@free.fr \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=dkr@freesurf.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).