Created
April 28, 2016 19:54
-
-
Save jigpu/d416524c8e2a68fbf6f0a2b903f3db5c to your computer and use it in GitHub Desktop.
Migrate git repositories from 'git://' to 'https://'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Migrate git repositories from 'git://' to 'https://' | |
# Usage: git-migrate.sh [path] [...] | |
# Usage: find ~ -type d -exec test -d "{}/.git" \; -print0 -prune | xargs -0 git-migrate.sh > /tmp/out 2> /tmp/err | |
# | |
# Scans through the git repositories listed on the command line to | |
# produce a set of commands (on stdout) that can be used to migrate | |
# everything from using 'git://' to 'https://'. Modified URLs are | |
# validated to work first by performing a test clone -- any errors | |
# can be seen on stderr for later follow-up. | |
# | |
# If you get prompted to enter login credentials (since some sites | |
# require login for HTTPS git access), just hit enter to bypass them. | |
# They will be logged to stderr as inaccessible. | |
shopt -s nocasematch | |
export GIT_DISCOVERY_ACROSS_FILESYSTEM=1 | |
TEMPROOT=$(mktemp -d) | |
echo "Looking for URLs to fix... Please wait." >&2 | |
for D in "$@"; do | |
sh -c "git -C \"${D}\" remote -v || echo ERROR using ${D} >&2" | while read REMOTE; do | |
FETCHURL=$(echo $REMOTE | awk '{print $2}') | |
REMOTE=$(echo $REMOTE | awk '{print $1}') | |
MODURL="" | |
#echo -e "${D}\t${REMOTE}\t${FETCHURL}" | |
if [[ -z "${FETCHURL}" ]]; then continue; fi | |
if [[ "${FETCHURL:0:6}" != "git://" ]]; then continue; fi | |
# Tweak the FETCHURL to work over HTTPS | |
FETCHURL="${FETCHURL:6}" | |
case ${FETCHURL} in | |
*git.code.sf.net*|*git.sourceforge.net*) | |
MODURL="http://${FETCHURL}" | |
;; | |
*anongit.freedesktop.org*) | |
MODURL="https://${FETCHURL/anongit.freedesktop.org/anongit.freedesktop.org\/git}" | |
;; | |
git.freedesktop.org*) | |
MODURL="https://anon${FETCHURL}" | |
;; | |
git.gnome.org*) | |
MODURL="https://${FETCHURL/\//\/browse/}" | |
;; | |
*) | |
MODURL="https://${FETCHURL}" | |
;; | |
esac | |
# Attempt to clone MODURL to see if it actually works... | |
# If it doesn't, print out an error. If it does, print | |
# out the command which will change the remote's URL to | |
# MODURL. | |
TEMPDIR=$(mktemp -d --tmpdir="${TEMPROOT}") | |
git clone -q --bare --depth 1 "${MODURL}" "${TEMPDIR}" | |
if [[ $? -ne 0 ]]; then | |
echo "ERROR cloning ${MODURL}" >&2 | |
else | |
echo git -C \"${D}\" remote set-url \"${REMOTE}\" \"${MODURL}\" | |
fi | |
rm -rf "${TEMPDIR}" | |
done | |
done | |
rm -rf "${TEMPROOT}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment