Skip to content

Instantly share code, notes, and snippets.

@paul-english
Created May 14, 2015 15:31
Show Gist options
  • Save paul-english/9e5cab7cee55b8b89647 to your computer and use it in GitHub Desktop.
Save paul-english/9e5cab7cee55b8b89647 to your computer and use it in GitHub Desktop.
Migrate GHE repos to https (based on eric's gist)
#!/bin/bash
# Finds and converts git repositories in or under the working directory to
# reference GitHub Enterprise (code.redbrainlabs.com).
set -e
trap "echo ERRORS DETECTED" ERR
IFS="`printf '\n\t'`"
ROOT="$PWD"
BAK_EXT="bak.$(date +%s)"
function find_git_config_files () {
find .git -name "config"
find . -maxdepth 1 -name ".gitmodules"
}
function remote_origin () {
local repo="$1"
grep \
-A 1 \
'remote \"origin\"' \
"${repo}/.git/config" \
| sed 1d \
| cut -f 2 -d = \
| sed 's/^ *//'
}
function convert_git_repo () {
local repo="$1"
pushd "$repo" > /dev/null
echo
echo "$(basename $PWD)/"
for file in $(find_git_config_files); do
echo " ${file} => ${file}.${BAK_EXT}"
/usr/bin/sed \
-i ".${BAK_EXT}" \
-e 's/[email protected]:/https:\/\/code.redbrainlabs.com\//g' \
"$file"
done
echo
popd > /dev/null
}
function prompt () {
local rel="${repo##$ROOT/}"
local remote=$(remote_origin "$repo")
echo -n "REPO: $rel
REMOTE: ${remote}
Convert to HTTPS? [y/N] "
read confirm
if [ "y" = "${confirm:0:1}" -o "Y" = "${confirm:0:1}" ]; then
return 0
else
return 1
fi
}
function main () {
local repos=$(find "$PWD" -type d -name ".git" | xargs -I'{}' dirname {})
for repo in $repos; do
prompt && convert_git_repo "$repo"
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment