git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Subject: [PATCH] Push to create

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 3, 2009, 06:50 UTC
Message-ID
<7vwsb7gkvt.fsf_-_@gitster.siamese.dyndns.org>
In-Reply-To
<20090301170436.GA14365@spearce.org>

This teaches receive-pack to create a new directory as a bare repository when a user tries to push into a directory that does not exist.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 "Shawn O. Pearce" <spearce@spearce.org> writes:
 > But I think that's only true for situations where you are likely the
 > owner of the repository in your own home directory, such as ~you
 > on kernel.org.  For "hosted repositories" like any of the systems
 > you described above, there is a lot more to the creation than just
 > executing "git init" somewhere.
 > .
 > For the usage of
 > creating a new repository in your own home directory on some remote
 > server, why isn't this just an option to git push?
 I am not at all convinced that the use case people would for whatever
 reason do not want to "ssh-in, create and then push from here" is limited
 to "your own playpen in your $HOME", but it certainly limits the
 complexity and the scope of the damage.
 builtin-receive-pack.c |   29 ++++++++++++++++++++++++++++-
 t/t5541-push-create.sh |   20 ++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletions(-)
 create mode 100755 t/t5541-push-create.sh
diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c
index 849f1fe..2f2831c 100644
--- a/builtin-receive-pack.c
+++ b/builtin-receive-pack.c
@@ -652,6 +652,33 @@ static void add_alternate_refs(void)
 	foreach_alt_odb(add_refs_from_alternate, NULL);
 }
 
+static char *create_new_repo(char *dir)
+{
+	struct child_process child;
+	const char *argv[20];
+	int argc;
+
+	if (mkdir(dir, 0777)) {
+		error("cannot mkdir '%s': %s", dir, strerror(errno));
+		return NULL;
+	}
+	argc = 0;
+	argv[argc++] = "init";
+	argv[argc++] = "--bare";
+	argv[argc++] = NULL;
+	child.argv = argv;
+	if (run_command_v_opt_cd_env(argv,
+				     RUN_COMMAND_NO_STDIN |
+				     RUN_COMMAND_STDOUT_TO_STDERR |
+				     RUN_GIT_CMD,
+				     dir,
+				     NULL)) {
+		error("cannot run git init");
+		return NULL;
+	}
+	return enter_repo(dir, 0);
+}
+
 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 {
 	int i;
@@ -674,7 +701,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
 
 	setup_path();
 
-	if (!enter_repo(dir, 0))
+	if (!enter_repo(dir, 0) && !create_new_repo(dir))
 		die("'%s': unable to chdir or not a git archive", dir);
 
 	if (is_repository_shallow())
diff --git a/t/t5541-push-create.sh b/t/t5541-push-create.sh
new file mode 100755
index 0000000..52558a5
--- /dev/null
+++ b/t/t5541-push-create.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='push into nonexisting repository'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_commit A &&
+	test_commit B &&
+	test_commit C
+'
+
+test_expect_success 'push into nonexisting repository' '
+	this=$(git rev-parse B) &&
+	git push "file://$(pwd)/not-here.git" B:refs/heads/master &&
+	that=$(GIT_DIR=not-here.git git rev-parse HEAD) &&
+	test "$this" = "$that"
+'
+
+test_done
-- 
1.6.2.rc2.123.gab4478
Previous: Shawn O. PearceNext: Jay Soffian
Message 9 of 43 in “Refactor list of environment variables to be sanitized”
  1. 1/4 Refactor list of environment variables to be sanitizedJunio C Hamano, Mar 1, 2009
  2. 2/4 git-init: inject some sanity to the option parserJunio C Hamano, Mar 1, 2009
  3. 3/4 Add init-serve, the remote side of "git init --remote=host:path"Junio C Hamano, Mar 1, 2009
  4. 4/4 "git init --remote=host:path"Junio C Hamano, Mar 1, 2009
  5. Jeff KingMar 1, 2009
  6. Junio C HamanoMar 1, 2009
  7. Jeff KingMar 1, 2009
  8. Shawn O. PearceMar 1, 2009
  9. Subject: [PATCH] Push to createJunio C Hamano, Mar 3, 2009
  10. Jay SoffianMar 3, 2009
  11. Jeff KingMar 3, 2009
  12. Jay SoffianMar 3, 2009
  13. Jay SoffianMar 3, 2009
  14. Junio C HamanoMar 3, 2009
  15. Jay SoffianMar 3, 2009
  16. Junio C HamanoMar 3, 2009
  17. Junio C HamanoMar 3, 2009
  18. Jay SoffianMar 3, 2009
  19. Jeff KingMar 3, 2009
  20. Jay SoffianMar 3, 2009
  21. Jeff KingMar 4, 2009
  22. Junio C HamanoMar 4, 2009
  23. Jay SoffianMar 4, 2009
  24. Junio C HamanoMar 3, 2009
  25. Jeff KingMar 3, 2009
  26. Junio C HamanoMar 3, 2009
  27. Jeff KingMar 3, 2009
  28. Junio C HamanoMar 3, 2009
  29. Jay SoffianMar 3, 2009
  30. Theodore TsoMar 3, 2009
  31. Johannes SchindelinMar 3, 2009
  32. Theodore TsoMar 4, 2009
  33. Miles BaderMar 6, 2009
  34. Shawn O. PearceMar 3, 2009
  35. 1/2 improve missing repository error messageJeff King, Mar 4, 2009
  36. Matthieu MoyMar 4, 2009
  37. Jeff KingMar 4, 2009
  38. Shawn O. PearceMar 4, 2009
  39. Jeff KingMar 5, 2009
  40. 2/2 make remote hangup warnings more friendlyJeff King, Mar 4, 2009
  41. Shawn O. PearceMar 4, 2009
  42. Jeff KingMar 5, 2009
  43. Daniel BarkalowMar 3, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.