Skip to content

Instantly share code, notes, and snippets.

@tylerszabo
Last active October 22, 2019 14:56
Show Gist options
  • Save tylerszabo/a38b55f524ad9c135bf6cb82d86db993 to your computer and use it in GitHub Desktop.
Save tylerszabo/a38b55f524ad9c135bf6cb82d86db993 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Copyright (C) 2019 Tyler Szabo
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# See <http://www.gnu.org/licenses/>.
# Copy to a directory in your PATH and rename to git-mirror (without an extensions)
USAGE='[-f] [--from=<remote-name>] <repo> [<dir>]'
LONG_USAGE=''
NONGIT_OK=1
. "$(git --exec-path)/git-sh-setup"
[ -n "$GIT_DIR" ] && die "fatal: GIT_DIR is set"
git rev-parse --git-dir &>/dev/null && die "fatal: inside a git repo"
if [ "$1" == "help" ] ; then
git mirror -h
exit
fi
copied_from="copied-from"
while test $# != 0 ; do
case "$1" in
-f)
force=1
;;
--from=*)
copied_from=${1#--from=}
;;
--)
shift
if [ -z "$source_repo" ] ; then
source_repo=$1
shift
fi
if [ -z "$target" ] ; then
target=$1
fi
break
;;
-*)
usage
;;
*)
if [ -z "$source_repo" ] ; then
source_repo=$1
elif [ -z "$target" ] ; then
target=$1
else
usage
fi
;;
esac
shift
done
[ -z "$source_repo" ] && usage
[ -z "$copied_from" ] && usage
if [ -z "$target" ] ; then
target=`basename $source_repo`
case "$target" in
*.git)
;;
*)
target="$target.git" ;;
esac
fi
if [ -e "$target" ] ; then
die "fatal: $target already exists"
fi
[ -z $force ] && git -C "$source_repo" config --get-regexp "^remote\.$copied_from\." 2>/dev/null && die "fatal: already copied, pass -f to overwrite \"remote.$copied_from\" configs or set --from="
say "Cloning into '$target' (with custom mirror)..." >&2
mkdir -p "$target"
git -C "$target" init --bare >/dev/null
git -C "$target" remote add --mirror=fetch "$copied_from" "$source_repo"
git -C "$source_repo" config --get-regexp "^(remote|branch)\." | while read name value ; do
case $name in
remote.$copied_from.*)
;;
*)
git -C "$target" config --local --add "$name" "$value"
;;
esac
done
git -C "$target" fetch --quiet "$copied_from"
git -C "$target" fetch --quiet --progress --all
say "done." >&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment