diff mbox series

[v4,02/11] Add initial support for many promisor remotes

Message ID 20190401164045.17328-3-chriscool@tuxfamily.org (mailing list archive)
State New, archived
Headers show
Series Many promisor remotes | expand

Commit Message

Christian Couder April 1, 2019, 4:40 p.m. UTC
From: Christian Couder <christian.couder@gmail.com>

The promisor-remote.{c,h} files will contain functions to
manage many promisor remotes.

We expect that there will not be a lot of promisor remotes,
so it is ok to use a simple linked list to manage them.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Makefile          |  1 +
 promisor-remote.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++
 promisor-remote.h | 16 +++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 promisor-remote.c
 create mode 100644 promisor-remote.h

Comments

SZEDER Gábor April 3, 2019, 10:08 p.m. UTC | #1
On Mon, Apr 01, 2019 at 06:40:36PM +0200, Christian Couder wrote:
> diff --git a/promisor-remote.c b/promisor-remote.c
> new file mode 100644
> index 0000000000..0c768210ee
> --- /dev/null
> +++ b/promisor-remote.c
> @@ -0,0 +1,92 @@
> +#include "cache.h"
> +#include "promisor-remote.h"
> +#include "config.h"
> +
> +static struct promisor_remote *promisors;
> +static struct promisor_remote **promisors_tail = &promisors;
> +
> +static struct promisor_remote *promisor_remote_new(const char *remote_name)
> +{
> +	struct promisor_remote *r;
> +
> +	if (*remote_name == '/') {
> +		warning(_("promisor remote name cannot begin with '/': %s"),
> +			remote_name);
> +		return NULL;
> +	}
> +
> +	FLEX_ALLOC_STR(r, name, remote_name);
> +
> +	*promisors_tail = r;
> +	promisors_tail = &r->next;
> +
> +	return r;
> +}
> +
> +static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
> +						      struct promisor_remote **previous)
> +{
> +	struct promisor_remote *r, *p;
> +
> +	for (p = NULL, r = promisors; r; p = r, r = r->next)
> +		if (r->name && !strcmp(r->name, remote_name)) {

r->name is a FLEX_ARRAY, and Clang complains about this condition:

  promisor-remote.c:34:10: error: address of array 'r->name' will always evaluate to 'true' [-Werror,-Wpointer-bool-conversion]
                  if (r->name && !strcmp(r->name, remote_name)) {
                      ~~~^~~~ ~~
  1 error generated.
  make: *** [promisor-remote.o] Error 1
Christian Couder April 4, 2019, 8:02 a.m. UTC | #2
On Thu, Apr 4, 2019 at 12:08 AM SZEDER Gábor <szeder.dev@gmail.com> wrote:
>
> On Mon, Apr 01, 2019 at 06:40:36PM +0200, Christian Couder wrote:
> > diff --git a/promisor-remote.c b/promisor-remote.c
> > new file mode 100644
> > index 0000000000..0c768210ee
> > --- /dev/null
> > +++ b/promisor-remote.c
> > @@ -0,0 +1,92 @@
> > +#include "cache.h"
> > +#include "promisor-remote.h"
> > +#include "config.h"
> > +
> > +static struct promisor_remote *promisors;
> > +static struct promisor_remote **promisors_tail = &promisors;
> > +
> > +static struct promisor_remote *promisor_remote_new(const char *remote_name)
> > +{
> > +     struct promisor_remote *r;
> > +
> > +     if (*remote_name == '/') {
> > +             warning(_("promisor remote name cannot begin with '/': %s"),
> > +                     remote_name);
> > +             return NULL;
> > +     }
> > +
> > +     FLEX_ALLOC_STR(r, name, remote_name);
> > +
> > +     *promisors_tail = r;
> > +     promisors_tail = &r->next;
> > +
> > +     return r;
> > +}
> > +
> > +static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
> > +                                                   struct promisor_remote **previous)
> > +{
> > +     struct promisor_remote *r, *p;
> > +
> > +     for (p = NULL, r = promisors; r; p = r, r = r->next)
> > +             if (r->name && !strcmp(r->name, remote_name)) {
>
> r->name is a FLEX_ARRAY, and Clang complains about this condition:
>
>   promisor-remote.c:34:10: error: address of array 'r->name' will always evaluate to 'true' [-Werror,-Wpointer-bool-conversion]
>                   if (r->name && !strcmp(r->name, remote_name)) {
>                       ~~~^~~~ ~~
>   1 error generated.
>   make: *** [promisor-remote.o] Error 1

Yeah, it's a left over from the previous version where it wasn't a FLEX_ARRAY.

Thanks!
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index 3e03290d8f..79b45060a1 100644
--- a/Makefile
+++ b/Makefile
@@ -953,6 +953,7 @@  LIB_OBJS += preload-index.o
 LIB_OBJS += pretty.o
 LIB_OBJS += prio-queue.o
 LIB_OBJS += progress.o
+LIB_OBJS += promisor-remote.o
 LIB_OBJS += prompt.o
 LIB_OBJS += protocol.o
 LIB_OBJS += quote.o
diff --git a/promisor-remote.c b/promisor-remote.c
new file mode 100644
index 0000000000..0c768210ee
--- /dev/null
+++ b/promisor-remote.c
@@ -0,0 +1,92 @@ 
+#include "cache.h"
+#include "promisor-remote.h"
+#include "config.h"
+
+static struct promisor_remote *promisors;
+static struct promisor_remote **promisors_tail = &promisors;
+
+static struct promisor_remote *promisor_remote_new(const char *remote_name)
+{
+	struct promisor_remote *r;
+
+	if (*remote_name == '/') {
+		warning(_("promisor remote name cannot begin with '/': %s"),
+			remote_name);
+		return NULL;
+	}
+
+	FLEX_ALLOC_STR(r, name, remote_name);
+
+	*promisors_tail = r;
+	promisors_tail = &r->next;
+
+	return r;
+}
+
+static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
+						      struct promisor_remote **previous)
+{
+	struct promisor_remote *r, *p;
+
+	for (p = NULL, r = promisors; r; p = r, r = r->next)
+		if (r->name && !strcmp(r->name, remote_name)) {
+			if (previous)
+				*previous = p;
+			return r;
+		}
+
+	return NULL;
+}
+
+static int promisor_remote_config(const char *var, const char *value, void *data)
+{
+	const char *name;
+	int namelen;
+	const char *subkey;
+
+	if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
+		return 0;
+
+	if (!strcmp(subkey, "promisor")) {
+		char *remote_name;
+
+		if (!git_config_bool(var, value))
+			return 0;
+
+		remote_name = xmemdupz(name, namelen);
+
+		if (!promisor_remote_lookup(remote_name, NULL))
+			promisor_remote_new(remote_name);
+
+		free(remote_name);
+		return 0;
+	}
+
+	return 0;
+}
+
+static void promisor_remote_init(void)
+{
+	static int initialized;
+
+	if (initialized)
+		return;
+	initialized = 1;
+
+	git_config(promisor_remote_config, NULL);
+}
+
+struct promisor_remote *promisor_remote_find(const char *remote_name)
+{
+	promisor_remote_init();
+
+	if (!remote_name)
+		return promisors;
+
+	return promisor_remote_lookup(remote_name, NULL);
+}
+
+int has_promisor_remote(void)
+{
+	return !!promisor_remote_find(NULL);
+}
diff --git a/promisor-remote.h b/promisor-remote.h
new file mode 100644
index 0000000000..01dcdf4dc7
--- /dev/null
+++ b/promisor-remote.h
@@ -0,0 +1,16 @@ 
+#ifndef PROMISOR_REMOTE_H
+#define PROMISOR_REMOTE_H
+
+/*
+ * Promisor remote linked list
+ * Its information come from remote.XXX config entries.
+ */
+struct promisor_remote {
+	struct promisor_remote *next;
+	const char name[FLEX_ARRAY];
+};
+
+extern struct promisor_remote *promisor_remote_find(const char *remote_name);
+extern int has_promisor_remote(void);
+
+#endif /* PROMISOR_REMOTE_H */