I built a bash script for lazy freelancers with Python and Git

Nikolay Miroshnychenko
4 min readSep 3, 2021
Photo by Photoholgic on Unsplash

The problem

If you’re a software developer and build programs that “automate stuff” in various domains then you probably have the same problem as I do… That problem is the burning desire to write a script that would automate certain mundane tasks that we encounter in our professional lives.

Since for the last couple of years I’ve been working as a contractor for various companies, I usually have to fill out a log of the stuff that I did for the day. Considering I don’t have the sharpest memory in terms of remembering what I did for the day — I usually turn to Git to help me out with that. This usually involves me bringing up the Git log and going through each entry one by one. Then I manually “generate” a report of what I did for the day. Whenever I would fill this out daily I would always have this thought I could streamline this…

Image by https://pixabay.com/users/geralt-9301

The solution

I decided that the best way to let my clients know what I did for the day is to actually get that info from the tasks I performed — i.e. my Git commit messages.

Also, I’m well aware that I might be a victim of my own Einstellung effect here — i.e. solving everything by writing a program as a programmer. So, if you know a better way to solve this problem by just running some Git command — I would be grateful if you could share this in the comments:)

For my purposes it would suffice if the script would:

  1. Get all of my commit messages for the day
  2. Assemble and format them
  3. Copy the output into the clipboard, so that I could then paste it into my report

So, let’s build this thing…

Image by https://pixabay.com/users/pexels-2286921

Step 1 — Get the logs:

This can be done with a simple one-liner in git:

git --no-pager log --author="Nikolay Miroshnychenko" --since="0am" > log.txt

We also want to write the output into a file log.txt, which would be necessary for our next step, hence the >log.txt part.

Step 2 —Writing a Python script that would parse the log file:

Honestly, I decided to go with Python because it’s the first result that came up in my google search of “best language for command line app” and I’m glad I did. It’s very easy to work with Python, it’s super forgiving and it plays well with a bash script.

Here’s the complete Python program that I used for formatting:

Basically here we do the following:

  1. Take the text file from the previous step and extract only the commit message texts
  2. Format them — capitalize them, remove ticket number (any numbers before a commit message), add a dot at the end — we’re using proper grammar after all ;)

Step 3 — Glue it all together in a bash script and copy the output :

git --no-pager log --author="$1" --since="0am" > log.txt

python - << EOF
//python code from the previous step goes here
EOF
pbcopy < formatted.txt
rm log.txt
rm formatted.txt
echo "formatted logs were copied to the clipboard!"

So in our previous step, we wrote our output to formatted.txt. Here we copy that output to the clipboard, clean up, and let the user know that the script actually ran.

Voila! Our script that extracts the logs for today in a neat and readable format is done!

Conclusion

In case you want to use it or add something yourself, you can find the script here — https://github.com/nsmirosh/lazyGitLog

Overall, it’s been an interesting experience building this script. I didn’t expect how easy it would be to work with Python and how well it integrates into a bash script. Of course, this implementation is far from perfect and I encourage you to submit a pull request to the repo and improve the code.

Some of the potential features/improvements that I can think of:

  1. Have the script accept a specific date / range instead of only “today”
  2. Improve upon removing the ticket number in the commit message — build a more sophisticated recognition algorithm
  3. Build the script to accept options instead of positional arguments
  4. Pipe the git command output into the script directly rather than writing to files

All in all, I hope this script will help you get rid of at least one repetitive task in your daily professional life :)

Thanks for reading. If you found this post valuable, please recommend it (the little handclap) so it can reach others.

--

--

Nikolay Miroshnychenko

Android engineer. Learning daily and sharing my knowledge in the process. Into mobile, computer science, and the brain.