GitLab "Push to Create New Project" and a shortcut for Bash and Windows
Recently GitLab announced a really fantastic feature: Push to Create New Project. I was excited for this feature because it’s always a buzzkill when I’m about to commit+push a fresh project, and suddenly realize “This push is about to fail because I didn’t let GitLab know about this yet.” And so, tonight I upgraded our GitLab server from 10.3 to 10.5.
The command is a bit verbose, and very samey because it references your full GitLab path, and you weren’t able to just copy it from the root page. But your shells have you covered.
General Usage
Starting with Gitlab 10.5, assuming you have write access to the specified namespace, you can now push projects without going to GitLab first like this:
$ git commit -m "Creates quite a lovely project"
$ git push -u git@gitlab.com:ttilberg/pretty-good-project.git master
You can also use the HTTPS url instead of the SSH url:
$ git push -u https://gitlab.com/ttilberg/pretty-good-project.git master
This is an awful lot of typing when the only part of interest is the project name.
Bash
In Bash, you can create a function. In your .bashrc
or .bash_profile
add:
function pushnew() {
git push -u git@gitlab.com:ttilberg/$1.git master
}
The function is very straightforward, and easy to edit to your preference.
Now creating new projects is as simple as:
$ git commit -m "Creates quite a lovely project"
$ pushnew pretty-good-project
Windows
The trick in Windows is what led me to actually write about this tonight. I went through a few loops trying to sort it out, and in the end came up with something quite reasonable.
Save this file somewhere available in your PATH. I personally keep a %USERPROFILE%/bin
directory on my path specifically for these types of scripts.
> pushnew.bat
@ECHO OFF
@"%PROGRAMFILES%\Git\cmd\git.exe" push -u https://gitlab.com/ttilberg/%1.git master
Simple enough. Now you can fire off pushnew project
from anywhere in your Windows command line.
Windows, long way: Creating easy-to-execute bins with Ruby
In Linux and Mac, you have #!/usr/bin/env ruby
shebangs that you can attach to the top of a file, and name it rubything
. If you chmod +x
this file and put it in a PATH
dir, you can execute it from anywhere, and it feels like executing a native binary. While working through this shortcut on Windows, I strayed down a path that showed me how Ruby gem bins work in Windows, and thus, the same experience I mention above.
If you go to the directory C:\Ruby24-x64\bin
you’ll see a bunch of pairs of files. Each of them is a gemcommand.bat
and gemcommand
ruby file with a shebang. Each of the .bat
files acts as a wrapper to execute the Ruby interpreter, passing in the equivalent gemcommand
ruby file and arguments. Very clever. Just copy the template of one of the .bat
files and reference your own Ruby scripts. You could obviously use this pattern with any language interpreter such as Node or Python. As long as that .bat
file is on your PATH
, you can hit it from any shell instance.
Here’s what I came up with first while exploring this in Windows. The power to riff on this is real!
> pushnew.bat
@ECHO OFF
@"C:\Ruby24-x64\bin\ruby.exe" "%USERPROFILE%/bin/pushnew" %1
> pushnew
#!C:/Ruby24-x64/bin/ruby.exe
project = ARGV.first
raise "Specify a reasonable project name" unless project && project.length > 2
`git push -u git@gitlab.com:ttilberg/#{project}.git master`