As the number of projects I'm working on has grown so has my number of code repositories. And with each new repository comes a series of steps to initialize Git, my source code version manager of choice.
Until recently the process to set up a new repository started with creating a new folder in Finder, copying the location of that folder to my clipboard, navigating to that location in Terminal, and finally initializing the repository. That's not a lot of navigating, copy/pasting, and typing; but I figured some of that could be automated.
The solution? I used Automator to create an Applescript service to initialize a new Git repository from Finder. I fired up Automator and hacked out a few lines of Applescript:
on run {input, parameters}
set the_path to (the POSIX path of input)
set AppleScript's text item delimiters to "/"
if the_path does not end with "/" then
set parentPath to (items 1 thru -2 of text items of the_path) as string
set the_path to parentPath
end if
-- the variable the_path now contains the current working directory with trailing slash
set the clipboard to the_path as text
set fileTarget to the_path & ".git" as text
if FileExists(fileTarget) then
display dialog "There is already Git repository here. The path has been copied to the clipboard"
else
set cmd to "cd " & quoted form of the_path & " && git init"
do shell script cmd
display dialog "A Git repository has been created and the path has been copied to the clipboard"
end if
end run
on FileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists folder theFile then
return true
else
return false
end if
end tell
end FileExistsNow the process of creating and initializing a new Git code repository on my local machine looks like this:
- Create the project folder in Finder
- Right-click the folder -> Services -> Create Git Repo Here
Simple and quick. One click and I'm ready to start adding and editing files. And as a bonus, the path to the folder is also in the clipboard ready to be used for whatever the next step might be.
All you have to do to get this running is copy the above code into a new Automator service template, save it as "Create Git Repo Here", and you're done.