Subject: [PATCH] Added --mirror-all to git-fetch.

From: Shawn Pearce <[email-masked]>
Date: Tue, 19 Sep 2006 19:28:51 -0400

The --mirror-all option to git-fetch can be used to obtain a copy of
every available remote ref into the current repository.  This can be
a rather destructive update as the local repository will have its
HEAD ref overwritten, as well as any ref which it shares in common
with the remote repository.  On the other hand it can be useful if
all you want to do is have this repository track another repository,
such as if you are providing Git repository hosting and mirroring
source repositories on other systems.

Currently local refs are not deleted even if they do not exist in the
remote repository.  This may be taken as either a feature or a bug.

Signed-off-by: Shawn O. Pearce <[email-masked]>
---
 pasky was asking about this on #git.  So here it is.

 Documentation/fetch-options.txt |    7 ++++++
 git-fetch.sh                    |   43 ++++++++++++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/Documentation/fetch-options.txt b/Documentation/fetch-options.txt
index 13f34d3..5ed9b4f 100644
--- a/Documentation/fetch-options.txt
+++ b/Documentation/fetch-options.txt
@@ -30,6 +30,13 @@
 	flag lets all tags and their associated objects be
 	downloaded.
 
+\--mirror-all::
+	All refs and tags on the remote side are downloaded to
+	the local side.  This option is not intendend for user
+	repositories as it will overwrite every local ref,
+	including HEAD.  You probably don't mean to use this
+	option.
+
 -k, \--keep::
 	Keep downloaded pack.
 
diff --git a/git-fetch.sh b/git-fetch.sh
index 09a5d6c..d22c560 100755
--- a/git-fetch.sh
+++ b/git-fetch.sh
@@ -14,6 +14,7 @@ IFS="$LF"
 rloga=fetch
 no_tags=
 tags=
+mirror_all=
 append=
 force=
 verbose=
@@ -36,6 +37,9 @@ do
 	-f|--f|--fo|--for|--forc|--force)
 		force=t
 		;;
+	--mirror-all)
+		mirror_all=t
+		;;
 	-t|--t|--ta|--tag|--tags)
 		tags=t
 		;;
@@ -216,12 +220,45 @@ case "$update_head_ok" in
 	;;
 esac
 
-# If --tags (and later --heads or --all) is specified, then we are
-# not talking about defaults stored in Pull: line of remotes or
+# If --tags or -mirror_all (and later --heads) is specified, then we
+# are not talking about defaults stored in Pull: line of remotes or
 # branches file, and just fetch those and refspecs explicitly given.
 # Otherwise we do what we always did.
 
-reflist=$(get_remote_refs_for_fetch "$@")
+if test "$mirror_all"
+then
+	tags=0;  # tags are implied in all 
+	reflist=`IFS="	" &&
+		  (
+			git-ls-remote $upload_pack "$remote" ||
+			echo fail ouch
+		  ) |
+	          while read sha1 name
+		  do
+			case "$sha1" in
+			fail)
+				exit 1
+			esac
+			case "$name" in
+			*^*) continue ;;
+			esac
+		  	if git-check-ref-format "$name"
+			then
+			    echo ".+${name}:${name}"
+			else
+				if test "x$name" = xHEAD
+				then
+					echo ".+${name}:${name}"
+				else
+					echo >&2 "warning: malformed ref ${name} ignored"
+				fi
+			fi
+		  done` || exit
+else
+	# Not using all; do what we always have done.
+	reflist=$(get_remote_refs_for_fetch "$@")
+fi
+
 if test "$tags"
 then
 	taglist=`IFS="	" &&
-- 
1.4.2.1.g832e