[PATCH] Add utility functions for enumerating remotes.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This creates remotes.[ch], which implement for_each_remote, a function
for enumerating all remotes.

Signed-off-by: Jeff King <peff@xxxxxxxx>
---
I wrote this to play with Carl's "look for branches under refs/remotes/"
idea. Since these functions have no users, I don't know if it's worth
applying this patch. However, I thought I would publish it, at least,
since it may eventually become useful.

 Makefile  |    3 +-
 remotes.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 remotes.h |    6 +++++
 3 files changed, 79 insertions(+), 1 deletions(-)
 create mode 100644 remotes.c
 create mode 100644 remotes.h

diff --git a/Makefile b/Makefile
index de89d8a..aca96c8 100644
--- a/Makefile
+++ b/Makefile
@@ -261,7 +261,8 @@ LIB_OBJS = \
 	revision.o pager.o tree-walk.o xdiff-interface.o \
 	write_or_die.o trace.o list-objects.o grep.o \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
-	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o
+	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
+	remotes.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
diff --git a/remotes.c b/remotes.c
new file mode 100644
index 0000000..1963753
--- /dev/null
+++ b/remotes.c
@@ -0,0 +1,71 @@
+#include "remotes.h"
+#include "cache.h"
+
+/* We can't be reentrant here, because git_config doesn't allow
+ * passing callback data. */
+static each_remote_fn *config_fn;
+static void *config_cb_data;
+static int config_ret;
+static int find_remote_in_config(const char *k, const char *v)
+{
+	const char *dot;
+	char remote[1024];
+
+	if (config_ret < 0)
+		return 0;
+
+	if (strncmp(k, "remote.", 7))
+		return 0;
+	k += 7;
+	dot = strchr(k, '.');
+	if (!dot)
+		return 0;
+	if (strcmp(dot + 1, "url"))
+		return 0;
+
+	memcpy(remote, k, dot - k);
+	remote[dot - k] = '\0';
+
+	config_ret = config_fn(remote, config_cb_data);
+	return 0;
+}
+
+static int do_for_each_remotes(each_remote_fn fn, void *cb_data)
+{
+	const char *remotes;
+        DIR *dir;
+	struct dirent *de;
+	int ret;
+
+	remotes = git_path("remotes");
+	dir = opendir(remotes);
+	if (!dir) {
+		if (errno == ENOENT)
+			return 0;
+		die("unable to open %s: %s", remotes, strerror(errno));
+	}
+
+	ret = 0;
+	while ((de = readdir(dir)) != NULL) {
+		if (de->d_name[0] == '.')
+			continue;
+		ret = fn(de->d_name, cb_data);
+		if (ret < 0)
+			break;
+	}
+
+	closedir(dir);
+	return ret;
+}
+
+int for_each_remote(each_remote_fn fn, void *cb_data)
+{
+	config_fn = fn;
+	config_cb_data = cb_data;
+	config_ret = 0;
+	git_config(find_remote_in_config);
+	if (config_ret < 0)
+		return config_ret;
+
+	return do_for_each_remotes(fn, cb_data);
+}
diff --git a/remotes.h b/remotes.h
new file mode 100644
index 0000000..632a449
--- /dev/null
+++ b/remotes.h
@@ -0,0 +1,6 @@
+#ifndef REMOTES_H
+
+typedef int each_remote_fn(const char *remote, void *cb_data);
+int for_each_remote(each_remote_fn fn, void *cb_data);
+
+#endif /* REMOTES_H */
-- 
1.5.0.rc3.554.ga40e-dirty

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]