Migrating GitHub gists to gitlab

Gitlab, as far as I know, does not have a quick-and-dirty area for snippets in the fashion of Gists on GitHub. My choice was to stick all my gists in a single repository. Here is how I got it done.

This script saves to the current directory all your gists, each to its named folder with an additional file “description” with the gist description since I couldn’t find it inside the git repo for a gist.

https://gitlab.com/bgstack15/former-gists/blob/master/get-my-gists.py

#!/usr/bin/env python
# Filename: get-my-gists.py
# Location: gitlab, probably
# Author: Chris Arndt (stackoverflow uid 39275), bgstack15
# Startdate: 2018-06-05 21:49
# Title: Script That Downloads GitHub Gists in a Nice Format
# Purpose: To facilitate my departure from GitHub
# History:
# Usage: ./get-my-gists.py bgstack15
# Reference:
#    copied from https://stackoverflow.com/questions/6724490/pull-all-gists-from-github/34052242#34052242
# Improve:
# -*- coding: utf-8 -*-
"""Clone all gists of GitHub username given on the command line."""

import subprocess
import sys
import requests

if len(sys.argv) > 1:
   gh_user = sys.argv[1]
else:
   print("Usage: get-my-gists.py ")
   sys.exit(1)

req = requests.get('https://api.github.com/users/%s/gists' % gh_user)

for gist in req.json():

   # get attributes
   name = gist['files'].keys()[0]
   descrip = gist['description']

   # debugging
   print name + ": " + descrip

   # clone the repo
   ret = subprocess.call(['git', 'clone', gist['git_pull_url'], name])
   if ret != 0:
      print("ERROR cloning gist %s. Please check output." % gist['id'])

   # save description
   with open(name + "/" + "description", "w") as text_file:
      text_file.write(descrip)

Once I had all my gist directories there, I cleaned up their .git directories, and made a new git repo and uploaded it to gitlab.

rm -rf */.git/
git init
git add . -m 'initial retrieval from github gists'
git remote add origin https://gitlab.com/bgstack15/former-gists.git
git push --set-upstream origin master
Advertisements

Migrating github projects to gitlab

For the projects that have issues, wikis, and all those nice addons, use the GitLab GitHub importer tool.

For simple projects, you can just call them from the command line. See https://gitlab.com/bgstack15/former-gists/blob/master/migrate.sh.

#!/bin/sh
# https://stackoverflow.com/questions/20359936/import-an-existing-git-project-into-gitlab/30483494#30483494

username="bgstack15"
repo="${1}"

func() {
   git clone --mirror "https://github.com/${username}/${repo}" "./${repo}"
   pushd "${repo}"
   git remote add gitlab "https://gitlab.com/${username}/${repo}.git"
   git push gitlab --mirror
   popd
}

time func ;

#Now if you have a locally cloned repository that you want to keep using with the new remote, just run the following commands* there:
#
#git remote remove origin
#git remote add origin "http://gitlab.example.com/${username}/${repo}.git"
#git fetch --all

#movingtogitlab

#movingtogitlab

Meme about github and ms

I do not use social media, unless github, gitlab, or WordPress.com count. But, here is my hashtag for the week: #movingtogitlab.

With the disheartening news about a week ago of a malevolent force taking over the community darling github.com, I have decided to move my projects to gitlab. Plus, I was awakened to the fact that github’s value-added bits are closed source. Well, what an interesting situation! And I was just getting a few stars on some of my repositories too.

Check out the traffic of the gitlab github importer: https://monitor.gitlab.net/dashboard/db/github-importer?orgId=1.

I will be sharing over the next few posts some tools I used to facilitate this process. I had some help with the gist one, and I used a simple tool for the git repos.

Someday I may even run my own instance of gitlab.