Created
May 10, 2015 02:30
-
-
Save josephhainline/b37c5fab8a4bcd6e441e to your computer and use it in GitHub Desktop.
If you have repos in github that you'd like to port to gitlab, with all branches and tags, this will do it. If you want to port your wikis too, this will also work, although it's only a 90% solution for wikis. Github and gitlab use different file name and markdown conventions. Run the command port_repos_and_wikis_from_github_to_gitlab.sh to get …
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 | |
echo | |
echo "Processing all *.md files in current directory..." | |
echo | |
# Loop through all .md files in directory and rename spaces to dashes | |
for file in *" "*; do | |
echo "Renaming $file" | |
mv -- "$file" "${file// /-}" | |
done | |
for file in *"("*; do | |
echo "Renaming $file" | |
mv -- "$file" "${file//(/}" | |
done | |
for file in *")"*; do | |
echo "Renaming $file" | |
mv -- "$file" "${file//)/}" | |
done | |
# Loop through all .md files in directory and convert them | |
for file in `find . -name '*.md'` ; | |
do | |
echo "Converting ${file}"; | |
ruby ~/dotfiles/bin/github_wiki_to_gitlab_format.rb -i $file -o $file | |
done | |
echo "Complete." |
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
#!/usr/bin/ruby | |
require 'optparse' | |
options = {} | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: convert_github_wiki_links_to_gitlab_format.rb" | |
opts.on("-i", "--input filepath", "Input file path (required)") do |i| | |
options[:input] = i | |
end | |
opts.on("-o", "--output filepath", "Output file path (required)") do |i| | |
options[:output] = i | |
end | |
opts.on('-h', '--help', 'Display this screen') do | |
puts opts | |
exit | |
end | |
end | |
begin | |
optparse.parse! | |
mandatory = [:input, :output] # Enforce the presence of | |
missing = mandatory.select { |param| options[param].nil? } # the -t and -f switches | |
if not missing.empty? # | |
puts "Missing options: #{missing.join(', ')}" | |
puts optparse | |
exit | |
end | |
rescue OptionParser::InvalidOption, OptionParser::MissingArgument | |
puts $!.to_s # Friendly output when parsing fails | |
puts optparse | |
exit | |
end | |
temp_file = "#{options[:output]}.tmp" | |
outfile = File.open(temp_file, 'w') | |
File.open(options[:input], 'r').each do |input_line| | |
link_match = input_line.match(/(?<prelink>.*)\[\[(?<link>.+)\]\](?<postlink>.*)/) | |
if (link_match) | |
pretty_title = link_match[:link] | |
corrected_link = pretty_title.gsub(" ", "-") | |
corrected_link = corrected_link.gsub("(", "-") | |
corrected_link = corrected_link.gsub(")", "-") | |
output_line = "#{link_match[:prelink]}[#{pretty_title}](#{corrected_link})#{link_match[:postlink]}" | |
outfile << output_line << "\n" | |
else | |
outfile << input_line | |
end | |
end | |
outfile.close | |
File.rename(temp_file, options[:output]) |
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 | |
if [ "$#" -lt 3 ]; then | |
echo "Usage: $0 githubTeam gitlabTeam repoNameToPortA [repoNameToPortB] ... [repoNameToPortZ]" | |
exit 1 | |
fi | |
ORIG_TEAM=$1 | |
DEST_TEAM=$2 | |
shift | |
shift | |
repo_names_array=( "$@" ) | |
echo | |
echo "This script ports the following repos and their wikis from:" | |
echo | |
for repo_name in "${repo_names_array[@]}" | |
do | |
echo -e https://github.com/${ORIG_TEAM}/${repo_name}.git | |
done | |
for repo_name in "${repo_names_array[@]}" | |
do | |
echo -e https://github.com/${ORIG_TEAM}/${repo_name}.wiki.git | |
done | |
echo | |
echo to these repos: | |
echo | |
for repo_name in "${repo_names_array[@]}" | |
do | |
echo -e https://gitlab.com/${DEST_TEAM}/${repo_name}.git | |
done | |
for repo_name in "${repo_names_array[@]}" | |
do | |
echo -e https://gitlab.com/${DEST_TEAM}/${repo_name}.wiki.git | |
done | |
echo | |
echo "The destination repos must already exist, but their wikis don't need to." | |
echo "This may take some time..." | |
read -p "Press [Enter] key to continue" | |
echo | |
# and now loop through the repo names: | |
for repo_name in "${repo_names_array[@]}" | |
do | |
# Gitlab needs lowercase repo names, and dots turn into dashes | |
gitlab_repo_name=`echo "print '$repo_name'.downcase.gsub('.', '-')" | ruby` | |
git clone https://github.com/${ORIG_TEAM}/${repo_name}.git | |
if cd ${repo_name}; then | |
# git submodule update --init --recursive | |
git remote add gitlab https://gitlab.com/${DEST_TEAM}/${gitlab_repo_name}.git | |
git push gitlab refs/remotes/origin/*:refs/heads/* | |
git push gitlab --tags | |
cd .. | |
else | |
echo "Repo ${repo_name} could not be cloned. Aborting!" | |
exit 1 | |
fi | |
git clone https://github.com/${ORIG_TEAM}/${repo_name}.wiki.git | |
if cd ${repo_name}.wiki; then | |
git tag -a gitlab_wiki_conversion_before -m "Tag before conversion of github wiki links to gitlab format." | |
convert_github_md_to_gitlab_format.sh | |
git add . -A | |
git commit -m "Automatically converted github wiki links to gitlab format." | |
git tag -a gitlab_wiki_conversion_after -m "Tag after conversion of github wiki links to gitlab format." | |
git remote add gitlab https://gitlab.com/${DEST_TEAM}/${gitlab_repo_name}.wiki.git | |
git push gitlab master | |
git push gitlab refs/remotes/origin/*:refs/heads/* | |
git push gitlab --tags | |
cd .. | |
else | |
echo "No wiki could be cloned. Continuing..." | |
fi | |
done | |
echo Done! |
Does it work the other way around?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that the gitlab repo must exist for the script to work. The gitlab wiki repo doesn't need to exist though, that will get created if the original github repo had a wiki.