Script to create remote Gogs GIT Repositories

in Tech

I am a big fan of git and try to keep to a fairly religious process of using it when working on my side projects at home, despite being the only person working on the code, git provides a great backup mechanism and also allows me to do some fun stuff like automated OTA wirless node deployments via git :D

I have for many years kept a copy of all my repositories on a VPS and used git's native push to effectively store an offsite backup of my code.

My git server was nothing fancy, just git installed on an ubuntu machine that I ssh'd to and create bare repos. To simplify the process I had created a shell script that I ran whenever I was starting a new project. The script did a few things;

  1. Created a local folder on my laptop under my projects working directory.
  2. SSH'd to the remote server and created the corresponding folder and initialised the bare repo
  3. Added/configured the remote server as ORIGIN within the local repo on my laptop
  4. Created a default .gitignore file from a template
  5. Performed an inital commit
  6. Pushed the local repo from the laptop to the server

This simple script helped me get into good habits when starting our on a new project by quickly setting up a git repo and automatically setting up the remote.

For various reasons I had to recently rebuild my VPS that was hosting my remote git repositories. I took this an opportunity to try out Gogs, Gogs is a self-hosted version of Github that is written in Go and seems to be incredibly light-weight considering the features it provides. So far I am very impressed, it is nice to have a web ui to view my repositories and have the option to do things like clone git repo's via HTTPs when I am not on a computer that has my SSH key.

I was left with one drawback, however, my script that I use to automate the process of creating a local and remote repository no longer worked as the repo needs to be created via the web ui. Well I went digging and it turns out the Gogs has an API available, so a few small updates and the script is once again working.

If anyone wants to use the script it is pretty simple, you just need to update the 4 variables at the top with your relevant details and create an API key by going to your user settings within Gogs. Then just place this script in the root of your projects/code workbase and run it whenever you want to make a new repo.

Disclaimer - this was hacked together to work with my process / workflow, YMMV and you may need to update it to work with your particular workflow


[email protected]  

# Get user input
echo "New repo name (please note a dir will be created locally with this name):"  
if [ -d ${REPONAME} ]; then  
  echo "Houston we have a problem!!"
  echo "${REPONAME} already exists as a local directory, please choose a new repo name"
echo "Repo Description:"  
echo "Would you like this repo to be private (enter for ${DEFAULT_PRIVATE}):"  

echo "Ok I am going to create the remote repo first..."  
# Curl some json to the github API oh damn we so fancy
response=$(curl --write-out %{http_code} --silent --output /dev/null -f -H 'content-type: application/json' -H "Authorization: token ${ACCESS_TOKEN}" -k ${GOGS_URL}/api/v1/user/repos -d "{\"Name\": \"${REPONAME:-${CURRENTDIR}}\", \"Description\": \"${DESCRIPTION}\", \"private\": ${DEFAULT_PRIVATE:-{$PRIVATE}}}")  
echo "--- checking that we didn't have errors creating remote repo ---"

if test "$response" != "201"; then  
   echo "seemed to have failed creating remote repo with error: $res"
   echo "not sure what to do..."
   echo "bye!!"
echo "--- remote repo created sucesfully ---"  
echo "let's make the local directory"  
mkdir ${REPONAME}  
cd ${REPONAME}  

echo "The remote URL for this repo is: ${GOGS_REMOTE_SSH}"  
echo "-------"  
touch .gitignore  
git init  
git add .  
git commit -m 'initial commit by create_repo script'  
git remote add origin $GOGS_REMOTE_SSH  
git push origin master  
git config branch.master.remote origin  
git config branch.master.merge refs/heads/master  
git fetch  
git merge master  
git branch -a