Skip to contentSkip to conversation

RFC: Better handling of git init remote-spec

PATCH In review Peter Krefting 3 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover RFC: Better handling of git init remote-spec

Quoted 1 line

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(-)
Signature · 1 line
1.7.3
#1 lore raw

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

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(-)
builtin/init-db.c +6 -1
index 9d4886c..f80ff08 100644
@@ -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,
Signature · 1 line
1.7.3
#2 lore raw

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

Quoted 11 lines · Peter Krefting

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(-)
builtin/init-db.c +0 -0
index 9d4886c..f80ff08 100644

builtin/init-db.c int cmd_init_db(int argc, const char **argv, const char *prefix) · line 440

Quoted 7 lines
builtin/init-db.c +3 -1
@@ -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?

#3 lore raw

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

Junio C Hamano:

Quoted 11 lines

builtin/init-db.c int cmd_init_db(int argc, const char **argv, const char *prefix) · line 440

Quoted 10 lines
builtin/init-db.c +3 -1
@@ -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?

Signature · 1 line
\\// Peter - http://www.softwolves.pp.se/
#4 lore raw

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

In reply to Peter Krefting

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")

builtin/init-db.c +9 -0
index f80ff08..e7baf4a 100644
@@ -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;
remote.c +11 -0
index 9143ec7..fa57689 100644
@@ -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;
remote.h +1 -0
index 888d7c1..eafa578 100644
@@ -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);
Signature · 1 line
1.7.3
#5 lore raw

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

Heya,

Quoted 4 lines · Peter Krefting

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.

Signature · 3 lines
Cheers,

Sverre Rabbelier
#6 lore raw

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

Hi!

Sverre Rabbelier:

Quoted 1 line

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)?

Signature · 1 line
\\// Peter - http://www.softwolves.pp.se/
#7 lore raw

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

Quoted 2 lines · Peter Krefting

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"?

#8 lore raw

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

Junio C Hamano:

Quoted 1 line

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.

Signature · 1 line
\\// Peter - http://www.softwolves.pp.se/