Adding OSX Automated Tasks for Git Commits

Suppose you work on a project where you are spending a lot of time focused on the code and fixing the bugs that keep popping up. It’s been a stressful day and you know it, the worst of it is when you forget to commit your changes you have been working on the whole day. Then keep on reading..

Now I’m proposing having an automated script which will run git status on your repo every night and checking if things need to be committed or not.

Here’s how you can do it:

Requirements:

  • OSX
  • Being comfortable writing shell scripts

1. Create an Automator

Now to create an automator for certain time, this is a bit different:

  1. Open Automator
  2. Choose Calendar
  3. Apple Script

For clearer way to create this automator, here’s a good video tutorial to create calendar automators for that.

on run {input, parameters}
	tell application "Terminal"
		if not (exists window 1) then reopen
		activate
		do script "gitcommit" in window 1
		delay 2
		close window 1
	end tell

	return input
end run

Now this automator is basically calling a gitcommit shell script which you on your machine.

2. Create Shell Script

Here’s what you do to create shell script:

  1. Open your favorite editor
  2. Write some code in it
  3. Close it, run chmod 700
  4. Copy and paste this very file in your usr/local/bin directory

Here’s the code for that shell script:

 

#!/bin/bash

open -a Terminal

cd path/to/osx_actions
source ready.cfg

if [[ $var1 == 'commit' ]]; then
	cd path/to/your_repo
	if git status | grep -q 'nothing to commit'; then
	  echo "already up to date"
	else
		echo "a commit needs to be done."
		git add -A
	 	git commit -m "$var2"
	fi
fi

Now, this bash script goes and read from a cfg (configuration file) you have. In this case, I have my configuration file under osx_actions directory.

3. Create Configuration file

The cfg file basically looks like this:

var1=commit
var2="your default commit message"
  • var1 is a flag whether to perform commit operations or not. It takes values [commit/nocommit]
  • var2 takes the default commit message you have, off course if commits happened automatically without you changing this message you may need to do git commit –amend to edit that message for that commit be more meaningful.

Now you are almost done.

Just go to your calendar, and using the same mentioned video above:

4. Add Event to Calendar

Add an event to your calendar, choose whether you want it to repeat and how frequent and also add the time on which you want to this script to run.

Hope this tutorial helps.

Happy Coding 🙂

References Used

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s