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

[PATCH resend] make "git push" update origin and mirrors, "git push --mirror" update mirrors

From
Paolo Bonzini <bonzini@gnu.org>
Date
Apr 26, 2008, 07:31 UTC
Message-ID
<4812DA50.3000702@gnu.org>

This patch builds on the infrastructure for remote.<nick>.mirror and tweaks the behavior of "git push". The idea is that "git push", "git push --all", "git push --mirror", instead of defaulting to origin, become DWIM commands:

- "git push" pushes to origin *and* to all mirrors
- "git push --all" pushes to origin with --all.  I didn't make it push
  to mirrors because --all and --mirror are incompatible.
- "git push --mirror" only updates mirror repositories, without touching
  origin.

This is useful when a project has a public repository managed by the integrator, but the integrator also wants to publish his own mirror somewhere. In this case, the integrator will just do "git push". Similarly, if a developer uses the integrator's repository but wishes to publish his own mirror somewhere, he can just do "git push --mirror".

The code explicitly disables pushing to mirrors when a refspec is present. This is just defensive, because right now refspecs cannot be specified without giving a repository too.

Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
---
 Documentation/git-push.txt |   17 ++++++---
 builtin-push.c             |   43 ++++++++++++++++------
 t/t5517-push-mirror.sh     |   86 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 130 insertions(+), 16 deletions(-)
	Sent a week ago, no comments received.
diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt
index dc1d4b0..8195517 100644
--- a/Documentation/git-push.txt
+++ b/Documentation/git-push.txt
@@ -29,6 +29,11 @@ OPTIONS
 	The "remote" repository that is destination of a push
 	operation.  See the section <<URLS,GIT URLS>> below.

+	The default behavior of the command when no repository is specified
+	is to push to "origin" if \--all is given, to push to all mirror
+	remotes if \--mirror is given, and to do both actions if neither
+	\--all nor \--mirror is given.
+
 <refspec>::
 	The canonical format of a <refspec> parameter is
 	`+?<src>:<dst>`; that is, an optional plus `+`, followed
@@ -69,9 +74,11 @@ the remote repository.
 	be mirrored to the remote repository.  Newly created local
 	refs will be pushed to the remote end, locally updated refs
 	will be force updated on the remote end, and deleted refs
-	will be removed from the remote end.  This is the default
-	if the configuration option `remote.<remote>.mirror` is
-	set.
+	will be removed from the remote end.
+
+	When a <repository> is given on the command line, git looks up the
+	corresponding configuration option `remote.<repository>.mirror`
+	and enables mirror mode if it is set.

 \--dry-run::
 	Do everything except actually send the updates.
@@ -97,8 +104,8 @@ the remote repository.
 	remote repository to lose commits; use it with care.

 \--repo=<repo>::
-	When no repository is specified the command defaults to
-	"origin"; this overrides it.
+	Overrides the default behavior of the command when no repository
+	is specified, so that <repo> is used as the destination repository.

 \--thin, \--no-thin::
 	These options are passed to `git-send-pack`.  Thin
diff --git a/builtin-push.c b/builtin-push.c
index e992c37..3756d5e 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -48,14 +48,9 @@ static void set_refspecs(const char **refs, int nr)
 	}
 }

-static int do_push(const char *repo, int flags)
+static int do_push(struct remote *remote, int flags)
 {
 	int i, errs;
-	struct remote *remote = remote_get(repo);
-
-	if (!remote)
-		die("bad repository '%s'", repo);
-
 	if (remote->mirror)
 		flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE);

@@ -99,6 +94,18 @@ static int do_push(const char *repo, int flags)
 	return !!errs;
 }

+static int push_to_mirrors(struct remote *remote, void *priv)
+{
+	int *p_flags = priv;
+	int rc = 0;
+	if (remote->mirror)
+		rc = do_push (remote, *p_flags);
+	
+	/* No command-line errors should occur.  */
+	assert (rc != -1);
+	return rc;
+}
+
 int cmd_push(int argc, const char **argv, const char *prefix)
 {
 	int flags = 0;
@@ -109,6 +116,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 	int tags = 0;
 	int rc;
 	const char *repo = NULL;	/* default repository */
+	struct remote *remote;

 	struct option options[] = {
 		OPT__VERBOSE(&verbose),
@@ -144,9 +152,22 @@ int cmd_push(int argc, const char **argv, const char *prefix)
 		set_refspecs(argv + 1, argc - 1);
 	}

-	rc = do_push(repo, flags);
-	if (rc == -1)
-		usage_with_options(push_usage, options);
-	else
-		return rc;
+	/* "git push --mirror" will only push to mirrors.  */
+	if (repo || !mirror) {
+		remote = remote_get(repo);
+		if (!remote)
+			die("bad repository '%s'", repo);
+	
+		rc = do_push(remote, flags);
+		if (rc == -1)
+			usage_with_options(push_usage, options);
+	}
+
+	/* "git push" with only --force, --dry-run, --verbose, --mirror options
+	   will also (or, in the case of "git push --mirror", only) push to
+	   all mirrors.  */
+	if (!rc && !repo && !refspec && !all)
+		rc = for_each_remote (push_to_mirrors, &flags);
+	
+	return rc;
 }
diff --git a/t/t5517-push-mirror.sh b/t/t5517-push-mirror.sh
index ea49ded..beb71f4 100755
--- a/t/t5517-push-mirror.sh
+++ b/t/t5517-push-mirror.sh
@@ -29,6 +29,26 @@ mk_repo_pair () {
 	)
 }

+# a more complicated setup where we have also an origin repo
+mk_repo_threesome () {
+	rm -rf master mirror origin &&
+	mkdir mirror &&
+	(
+		cd mirror &&
+		git init
+	) &&
+	mkdir origin &&
+	(
+		cd origin &&
+		git init
+		echo one >foo && git add foo && git commit -m one
+	) &&
+	git clone origin master &&
+	(
+		cd master &&
+		git remote add --mirror up ../mirror
+	)
+}

 # BRANCH tests
 test_expect_success 'push mirror creates new branches' '
@@ -264,4 +284,70 @@ test_expect_success 'remote.foo.mirror=no has no effect' '

 '

+test_expect_success 'mirrors are updated by "git push"' '
+
+	mk_repo_threesome &&
+	(
+		cd master &&
+		git branch keep master &&
+		git branch remove master &&
+		git push &&
+		git branch -D remove
+		git push
+	) &&
+	(
+		cd mirror &&
+		git show-ref -s --verify refs/heads/keep &&
+		invert git show-ref -s --verify refs/heads/remove
+	) &&
+	(
+		cd origin &&
+		invert git show-ref -s --verify refs/heads/keep &&
+		invert git show-ref -s --verify refs/heads/remove
+	)
+
+'
+
+test_expect_success 'mirrors are not updated by "git push --all"' '
+
+	mk_repo_threesome &&
+	(
+		cd master &&
+		git branch b1 master &&
+		git push --all
+	) &&
+	(
+		cd mirror &&
+		invert git show-ref -s --verify refs/heads/b1
+	) &&
+	(
+		cd origin &&
+		git show-ref -s --verify refs/heads/b1
+	)
+
+'
+
+test_expect_success 'mirrors are not updated by "git push --repo" or "git push <repo>"' '
+
+	mk_repo_threesome &&
+	(
+		cd master &&
+		git branch b1 master &&
+		git push origin
+		git branch b2 master &&
+		git push --repo=origin
+	) &&
+	(
+		cd mirror &&
+		invert git show-ref -s --verify refs/heads/b1 &&
+		invert git show-ref -s --verify refs/heads/b2
+	) &&
+	(
+		cd origin &&
+		invert git show-ref -s --verify refs/heads/b1 &&
+		invert git show-ref -s --verify refs/heads/b2
+	)
+
+'
+
 test_done
-- 
1.5.5
Next: Shawn O. Pearce
Message 1 of 74 in “make "git push" update origin and mirrors, "git push --mirror" update mirrors”
  1. make "git push" update origin and mirrors, "git push --mirror" update mirrorsPaolo Bonzini, Apr 26, 2008
  2. Shawn O. PearceApr 26, 2008
  3. Junio C HamanoApr 26, 2008
  4. Shawn O. PearceApr 27, 2008
  5. Shawn O. PearceApr 27, 2008
  6. Junio C HamanoApr 27, 2008
  7. Shawn O. PearceApr 27, 2008
  8. Junio C HamanoApr 27, 2008
  9. Paolo BonziniApr 27, 2008
  10. Jeff KingApr 28, 2008
  11. Paolo BonziniApr 28, 2008
  12. Jeff KingApr 28, 2008
  13. Jeff KingApr 28, 2008
  14. Paolo BonziniApr 28, 2008
  15. Johannes SchindelinApr 28, 2008
  16. Paolo BonziniApr 28, 2008
  17. Johannes SchindelinApr 28, 2008
  18. Shawn O. PearceApr 28, 2008
  19. Paolo BonziniApr 28, 2008
  20. Stephen R. van den BergApr 28, 2008
  21. Jeff KingApr 28, 2008
  22. Paolo BonziniApr 27, 2008
  23. Paolo BonziniApr 27, 2008
  24. 0/7 limit the usage of the default remote "origin" to the minimumPaolo Bonzini, Apr 28, 2008
  25. 1/7 add special "matching refs" refspecPaolo Bonzini, Apr 28, 2008
  26. 2/7 add push line in git-clonePaolo Bonzini, Apr 28, 2008
  27. 3/7 Add a --push option to "git-remote add"Paolo Bonzini, Apr 28, 2008
  28. 4/7 make "git push" update all push repositories.Paolo Bonzini, Apr 28, 2008
  29. 5/7 don't rely on zero-argument "git fetch" from within git pullPaolo Bonzini, Apr 28, 2008
  30. 6/7 warn on "git pull" without a given branch.<name>.remote valuePaolo Bonzini, Apr 28, 2008
  31. 7/7 make "git fetch" update all fetch repositoriesPaolo Bonzini, Apr 28, 2008
  32. Alex RiesenApr 28, 2008
  33. Paolo BonziniApr 28, 2008
  34. Alex RiesenApr 28, 2008
  35. Paolo BonziniApr 29, 2008
  36. Alex RiesenApr 29, 2008
  37. Andreas EricssonApr 29, 2008
  38. Paolo BonziniApr 29, 2008
  39. Johannes SchindelinApr 29, 2008
  40. Paolo BonziniApr 29, 2008
  41. Alex RiesenApr 29, 2008
  42. Paolo BonziniApr 29, 2008
  43. Andreas EricssonApr 29, 2008
  44. Paolo BonziniApr 29, 2008
  45. Andreas EricssonApr 29, 2008
  46. Paolo BonziniApr 29, 2008
  47. しらいしななこApr 29, 2008
  48. Alex RiesenApr 29, 2008
  49. Paolo BonziniApr 29, 2008
  50. Alex RiesenApr 29, 2008
  51. Paolo BonziniApr 29, 2008
  52. Alex RiesenApr 29, 2008
  53. Johannes SchindelinApr 29, 2008
  54. Jeff KingApr 29, 2008
  55. Junio C HamanoApr 29, 2008
  56. Paolo BonziniApr 30, 2008
  57. Johannes SchindelinApr 29, 2008
  58. Alex RiesenApr 29, 2008
  59. Junio C HamanoMay 1, 2008
  60. Paolo BonziniMay 6, 2008
  61. Paolo BonziniMay 14, 2008
  62. Junio C HamanoMay 14, 2008
  63. Paolo BonziniMay 14, 2008
  64. Junio C HamanoMay 14, 2008
  65. Paolo BonziniMay 14, 2008
  66. Junio C HamanoApr 30, 2008
  67. Paolo BonziniApr 30, 2008
  68. Jeff KingApr 29, 2008
  69. Alex RiesenApr 29, 2008
  70. Junio C HamanoApr 29, 2008
  71. Jeff KingApr 29, 2008
  72. Paolo BonziniApr 30, 2008
  73. Paolo BonziniApr 29, 2008
  74. Johannes SchindelinApr 29, 2008

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.