{ elliotec }

Greenery - A Bash script to get all the green squares on Github

March 20, 2019

Get that green

green squares Heh, 4,303 contributions? Yeah…

I remember when first starting out in development, people would tell me that hiring managers and recruiters really cared about those little green contribution squares on Github. It shows how active you are, they said. The more squares you have the better, they told me.

At first I believed them and genuinely tried to commit code near-daily to make sure my public image at least seemed prolific enough to be noticed. Pretty quickly I realized how easily this could be automated. I didn’t think much about how to do that yet, and it felt a bit dirty, especially when I was looking for my first dev job.

With a bit more experience and a few fewer shits to give, I figured I’d give it a shot for a little scripting practice and fun.

Greenery

The first thing I needed was some content to actually commit. The obvious choice was Unix’s fortune, which generates a psuedorandom quote based on parameters you give it. I used shorter fortunes for commit messages and longer ones for the file contents.

Then I needed to write the script to automatically generate the files based on the fortunes, commit a random number of changes daily (for the different shades of green on the squares), and push them to a Github repo. Nothing a tiny bash script and cron job can’t handle.

There are 6 files involved in this:

  1. README.md - totally unnecessary. I just use it as a disclaimer to make it clear I’m bullshitting.

  2. .gitignore - absolutely essential to ignore the file which contains your Github credentials, in my case push.sh

  3. file.txt - the only file that ever changes, contents are replaced by the fortune command on each commit.

  4. committed.sh - the file that generates the fortunes, replaces the contents of file.txt, and commits up to 23 (but no fewer than 1) changes.

#!/bin/bash
NUMBER=$(($RANDOM % 23))

for ((run=1; run <= NUMBER + 1; run++))
do
  echo `/usr/games/fortune` > ~/greenery/file.txt
  /usr/bin/git -C ~/greenery/ add . -A
  /usr/bin/git -C ~/greenery/ commit -m "`/usr/games/fortune -sn 32`"
done
  1. push.sh - the file that runs the commited script and pushes to Github with your credentials. Do not commit this file.
#!/bin/bash
bash ~/greenery/committed.sh
/usr/bin/git -C ~/greenery push -u https://{MY_GITHUB_USERNAME}:{MY_GITHUB_PASSWORD}@github.com/{MY_GITHUB_USERNAME}/greenery.git master
  1. cron.txt - this is a reference file for the actual cron job run by my server which you can edit in a Unix environment with crontab -e. Look up how to write cron jobs, here you see I have mine running on the 20th minute of the 16th hour of every day 🔥
20 16 * * * bash ~/greenery/push.sh

That’s it. Super tiny, totally works, totally tricked a recruiter or two maybe.

Here’s the repo for reference.

RSS