threads / rfc / 25725

patch, 2 parts[RFC] Detect attempts at calling git init specifying a remote repository.

Subject: [PATCH 1/2] [RFC] Detect attempts at calling git init specifying a remote repository.

## tl;dr

9 messages between Nov 12, 2010 and Nov 16, 2010. Diffs are folded; open one to read it.

replies: 8people: 3as markdown or json

Peter Krefting· Nov 12, 2010, 11:31 UTC · lore

[PATCH 0/2] RFC: Better handling of git init remote-spec

>From time to time I forget myself and try to initialize a repository on a remote
machine by writing something like
  git init --bare remote.machine:myrepo.git
just to get a local directory called "remote.machine:myrepo.git" set up.
Or, even worse, a
  git init --bare ssh://remote.machine/myrepo.git

These patches try to fix this. The first patch tries to detect if a remote repository specification was supplied, and just stop with an error message. That one is fairly safe.

The second one I haven't got working yet, but I have the beginnings of a patch that actually tries to run "git-init" on the remote repository. Any ideas on how to get this actually working (it should be possible for the "ssh" transport at least) are welcome.

Peter Krefting (2):
  Detect attempts at calling git init specifying a remote repository.
  WIP: Allow running git init on a remote repository specification.
 builtin/init-db.c |   16 +++++++++++++++-
 remote.c          |   11 +++++++++++
 remote.h          |    1 +
 3 files changed, 27 insertions(+), 1 deletions(-)
-- 
1.7.3
Peter Krefting· Nov 12, 2010, 11:27 UTC · re: Peter Krefting · lore
We can only initialize local repositories, so bail out immediately if the
user tries to specify a remote repository.
---
 builtin/init-db.c |    7 ++++++-
 1 files changed, 6 insertions(+), 1 deletions(-)
Show changes to builtin/init-db.c +6 −1
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 9d4886c..f80ff08 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -435,7 +435,12 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 		int mkdir_tried = 0;
 	retry:
 		if (chdir(argv[0]) < 0) {
-			if (!mkdir_tried) {
+			if (is_url(argv[0]) || strchr(argv[0], '@') != NULL || (strchr(argv[0], ':') != NULL && !has_dos_drive_prefix(argv[0]))) {
+				/*
+				 * We were passed a remote repository specification.
+				 */
+				die("Cannot initialize remote repository '%s'", argv[0]);
+			} else if (!mkdir_tried) {
 				int saved;
 				/*
 				 * At this point we haven't read any configuration,
-- 
1.7.3
Junio C Hamano· Nov 16, 2010, 17:57 UTC · re: Peter Krefting · lore

Re: [PATCH 1/2] [RFC] Detect attempts at calling git init specifying a remote repository.

Peter Krefting <peter@softwolves.pp.se> writes:
Show 18 quoted lines
> We can only initialize local repositories, so bail out immediately if the
> user tries to specify a remote repository.
> ---
>  builtin/init-db.c |    7 ++++++-
>  1 files changed, 6 insertions(+), 1 deletions(-)
>
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index 9d4886c..f80ff08 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -435,7 +435,12 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
>  		int mkdir_tried = 0;
>  	retry:
>  		if (chdir(argv[0]) < 0) {
> -			if (!mkdir_tried) {
> +			if (is_url(argv[0]) || strchr(argv[0], '@') != NULL || (strchr(argv[0], ':') != NULL && !has_dos_drive_prefix(argv[0]))) {
> +				/*
> +				 * We were passed a remote repository specification.
Or a directory with ':' in its pathname, no?
Peter Krefting· Nov 16, 2010, 20:10 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] [RFC] Detect attempts at calling git init specifying a remote repository.

Junio C Hamano:
Show 11 quoted lines
>> --- a/builtin/init-db.c
>> +++ b/builtin/init-db.c
>> @@ -435,7 +435,12 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
>>  		int mkdir_tried = 0;
>>  	retry:
>>  		if (chdir(argv[0]) < 0) {
>> -			if (!mkdir_tried) {
>> +			if (is_url(argv[0]) || strchr(argv[0], '@') != NULL || (strchr(argv[0], ':') != NULL && !has_dos_drive_prefix(argv[0]))) {
>> +				/*
>> +				 * We were passed a remote repository specification.
> Or a directory with ':' in its pathname, no?
Indeed. Don't do that, then :-)

Seriously, though, I tried finding an API that checked if the pathspec is local or remote, but I was unable to. Is there one I should use instead?

-- 
\\// Peter - http://www.softwolves.pp.se/
Peter Krefting· Nov 12, 2010, 11:30 UTC · re: Peter Krefting · lore

[PATCH 2/2] [WIP] Allow running git init on a remote repository specification.

If we pass a remote repository specification to git init, we should try
connecting to the remote and run git init there. Not all transports
can support this, so fall back to reporting an error message for those
that cannot.
---
 builtin/init-db.c |    9 +++++++++
 remote.c          |   11 +++++++++++
 remote.h          |    1 +
 3 files changed, 21 insertions(+), 0 deletions(-)
This one doesn't work. I am not sure getting this to work is even possible.
  $ ~/proj/git/git-init remote.server:/home/peter/foobartest.git
  bash: git-init: command not found
  error: cannot run ssh: No such file or directory
(it is the remote side that complains about not finding "git-init")
Show changes to 3 files +21 −0

builtin/init-db.c, remote.c, remote.h

diff --git a/builtin/init-db.c b/builtin/init-db.c
index f80ff08..e7baf4a 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -7,6 +7,8 @@
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
+#include "remote.h"
+#include "transport.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -439,6 +441,13 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 				/*
 				 * We were passed a remote repository specification.
 				 */
+				struct remote *remote = remote_get_unconfigured(argv[0]);
+				struct transport *transport = transport_get(remote, argv[0]);
+				int fd[2];
+				fd[0] = 0;
+				fd[1] = 1;
+				if (0 == transport_connect(transport, argv[0], "git-init", fd))
+					return transport_disconnect(transport);
 				die("Cannot initialize remote repository '%s'", argv[0]);
 			} else if (!mkdir_tried) {
 				int saved;
diff --git a/remote.c b/remote.c
index 9143ec7..fa57689 100644
--- a/remote.c
+++ b/remote.c
@@ -722,6 +722,17 @@ struct remote *remote_get(const char *name)
 	return ret;
 }
 
+struct remote *remote_get_unconfigured(const char *remotespec)
+{
+	struct remote *ret = xcalloc(1, sizeof(struct remote));
+	if (!ret)
+		return NULL;
+	add_url_alias(ret, remotespec);
+	if (!valid_remote(ret))
+		return NULL;
+	return ret;
+}
+
 int remote_is_configured(const char *name)
 {
 	int i;
diff --git a/remote.h b/remote.h
index 888d7c1..eafa578 100644
--- a/remote.h
+++ b/remote.h
@@ -51,6 +51,7 @@ struct remote {
 };
 
 struct remote *remote_get(const char *name);
+struct remote *remote_get_unconfigured(const char *remotespec);
 int remote_is_configured(const char *name);
 
 typedef int each_remote_fn(struct remote *remote, void *priv);
-- 
1.7.3
Sverre Rabbelier· Nov 13, 2010, 21:29 UTC · re: Peter Krefting · lore

Re: [PATCH 2/2] [WIP] Allow running git init on a remote repository specification.

Heya,
On Fri, Nov 12, 2010 at 12:30, Peter Krefting <peter@softwolves.pp.se> wrote:
> If we pass a remote repository specification to git init, we should try
> connecting to the remote and run git init there. Not all transports
> can support this, so fall back to reporting an error message for those
> that cannot.

I remember some discussion about this before, I think Scott and Jeff (cc-ed) were involved at least, maybe they remember? I remember the idea of a new remote service (e.g., in addition to 'git-upload-pack' and 'git-receive-pack') to create a new repository. The reason for making this a new service was so that (among others) the github guys could make that do something sensible for their service.

-- 
Cheers,

Sverre Rabbelier
Peter Krefting· Nov 16, 2010, 05:33 UTC · re: Sverre Rabbelier · lore

Re: [PATCH 2/2] [WIP] Allow running git init on a remote repository specification.

Hi!
Sverre Rabbelier:
> I remember the idea of a new remote service (e.g., in addition to 
> 'git-upload-pack' and 'git-receive-pack') to create a new repository. The 
> reason for making this a new service was so that (among others) the github 
> guys could make that do something sensible for their service.

Right, that does indeed make sense. I guess that means I need to read up a bit more on how to hook things together. It wasn't quite as obvious on a cursory glance :-)

Any objections to the first part of the patch (that detects attempts at remote init and rejects them)?

-- 
\\// Peter - http://www.softwolves.pp.se/
Junio C Hamano· Nov 16, 2010, 17:58 UTC · re: Peter Krefting · lore

Re: [PATCH 2/2] [WIP] Allow running git init on a remote repository specification.

Peter Krefting <peter@softwolves.pp.se> writes:
> Any objections to the first part of the patch (that detects attempts
> at remote init and rejects them)?
Yes.

Didn't we discuss this long time ago and decided, if anything is to be done, it would be something like "the first push creates"?

Peter Krefting· Nov 16, 2010, 20:12 UTC · re: Junio C Hamano · lore

Re: [PATCH 2/2] [WIP] Allow running git init on a remote repository specification.

Junio C Hamano:
> Didn't we discuss this long time ago and decided, if anything is to be 
> done, it would be something like "the first push creates"?

I have probably missed that reference. But you mean to instead set up an empty repository, with a configured remote? That is an interesting alternative, but would still fail as there still is no way to initialize a new repository on the remote.

But I am definitely open to fix it properly, I just haven't been able to figure out how to do that yet.

-- 
\\// Peter - http://www.softwolves.pp.se/

← back to recent threads