Initializing a new repository using GitHub API, curl, and a tiny bit of bash script

Why not use the GitHub web interface?

We could easily setup our new repository using GitHub’s nice web interface, but the point of this blog is writing code and gaining experience with all the tools at our disposal.  Also we can create a quick bash script that automatically setups up a new repo and clones it into our current directory which should help make the process more seamless.

Installing curl

curl is command line tool for making http request (and many other protocols) which we will be using to make RESTful http calls to access GitHub’s API.

In your terminal run


sudo apt-get curl

Creating a GitHub Repository

Following the current GitHub API for creating a new repo at http://developer.github.com/v3/repos/#create we can produce a single command that will create our new repo and set some basic configuration options.


curl -u USERNAME https://api.github.com/user/repos -d "{\"name\":\"REPONAME\",\"description\":\"REPODESCRIPTION\",\"gitignore_template\":\"LANG\", \"auto_init\":true}"

Enter your GitHub password and curl will print the json message if the request was succesful.

The flags for used here for curl are pretty simple.  -u specifies the username for authentication and -d takes the data, json in this case, to send to the server.  In this example -d also defaults the request method to POST.  If you need to use a different request method you can use the -X flag to specific (ie -X DELETE).

Cloning the Respository

We need to get the newly created repository copied down to our local machine.  Git provides us with the clone command to pull remote repositories into a local directory.


git clone githubrepourl

Just run the above command replacing the githubrepourl with the url of your repository and it will be copied down into a new directory named the same as your repo.  You can again use the GitHub API to view  a list of all your repositories and get the clone url.  Run


curl -u username https://api.github.com/user/repos

and look at the clone_url field for the repository you want to clone.

Bringing it together with bash script

Finally we can combine the above into a single simple to use script.

#!/bin/bash

DESCRIPTION=
NAME=
LANG=
USER=

howto()
{
cat << EOF
$0 usage
This script creates and initializes a remote github repo and clones it into the CURRENTDIR/NAME
-n name of repo and folder to create in current directory
-d repo description
-u github username
-l programming language for .gitignore
EOF
}

while getopts "n:d:l:u:" OPTION
do
 case $OPTION in
 d) DESCRIPTION=$OPTARG ;;
 l) LANG=$OPTARG ;;
 u) USER=$OPTARG ;;
 n) NAME=$OPTARG ;;
 ?) howto; exit ;;
 esac
done
if [[ -z $DESCRIPTION ]] || [[ -z $LANG ]] || [[ -z $USER ]] || [[ -z $NAME ]]
then
 howto
 exit
fi

curl -u $USER https://api.github.com/user/repos -d "{\"name\":\"$NAME\", \"description\":\"$DESCRIPTION\",\"gitignore_template\":\"$LANG\", \"auto_init\":true}"
git clone https://github.com/$USER/$NAME.git

I am new to bash scripting, so lets take a look at a few important lines in the following script to understand what they do.

All linux scripts, including others than bash, require the first line the script to be a #! followed by the path to the binary the will accept the script as input and execute it.  The path to bash is /bin/bash.

Similar to C, we can define functions in bash scripts.  Their syntax goes functionname (params) { code }. Unlike C, we don’t need to specify any return or parameter types.

To parse command line parameters bash provides us with getopts which takes string where a single character maps to the a flag on the command line (‘n’  maps to -n) and if followed by an optional : then we are provided the string passed directly after the flag.  We then store each of the defined flag into a variable for later use and verify that they had values passed in by using the if statement and test [[ ]] with the -z flag.

Finally we take the curl and git clone commands we created and do some simple replacements with our variables.

Let’s copy our new script into /usr/local/bin/ and we can use it from the command line to create a new repository any time we need.

You can access the preceding code and any other bash scripts I write in the future at https://github.com/sgarlick987/bash-scripts/ which I created using the above script.

Source Control with git

Why Source Control?

This may seem like a silly question, but there’s plenty of organizations out there that are still using no source control and take some convincing.  A few of the larger benefits include

  1. Being able to see differences between old and new versions of code
  2. Able to roll back code to an earlier state
  3. Track and audit who made specific changes
  4. Develop code in isolated branches
  5. Easily merge code from multiple users and branches

There are many more benefits to using source control but even in a solo environment the ability to track changes is undeniably the most important aspect when developing applications.

Why git?

There are many different source control systems out there but git has a few important advantages over the competition.  We will be going with git mainly due to the fact that it has very good linux support, it’s free, it’s fast and it can be easily hosted on sites such as github and bitbucket.

git is also a distributed model so we don’t always need to have access to a central server and can commit and manage branches locally.  I’ve run into the issue at work where I don’t have access to our SVN repo when remote and have started using git-svn to handle local commits.

Check out http://git-scm.com/ to learn more about git and http://stackoverflow.com/questions/871/why-is-git-better-than-subversion if you want a much more in depth look at the differences between git and other systems (don’t mind the tone of the initial question, the responses are very good read if you’d like to know more).

Installing git

Open up your terminal and run


sudo apt-get install git

Continue when prompted and let it install.

We can set your user name and email that will be used to identify your commits by running


git config --global user.name "Stephen Garlick"

git config --global user.email "sgarlick987@gmail.com"

make sure the email matches the email you plan to use for your GitHub account and finally


git config --global credential.helper cache

which will let git cache your GitHub password in memory for 15 minutes.

Next time we’ll talk about initializing our git repo and getting it up on github along with a few basic commands to get us started.