Jump to content

Tiny git repositories automation to save 5 minutes daily

Posted on:November 6, 2016 at 01:00 AM

I’m a big fan of automation. I would even say ‘what can be automated, can be forgotten’ and you can focus in important things. This post happens to be next in a new series called “Automation”. The series came out somehow, I didn’t plan it. It just seems that there are things I would like to share :)

The problem

During the day I make changes in multiple git repositories. Sometimes 2, sometimes 5 or 10. Switch focus for 2 mins and back. Add some code and back. Amend some more code 20 minutes later or so. Basically it’s worth-less to push (maybe unfinished changes) over and over to remote server.

I can add an extra layer like branch. Yes, everything is clean, branches are cheap. Great… but this is another thing I have to think about! I don’t want to spend time thinking “am I on the correct branch?“. I want to automate as much as possible.

So I came up with a simple script that checks whether a particular folder has changes.

The Script

Script is written in PowerShell.

param (
[Parameter(Mandatory=$true)]
[string]$rootPath
)

$directories = Get-ChildItem $rootPath -Directory
$baseLocation = Get-Location

foreach($dir in $directories)
{
  Set-Location $dir.FullName
  $isGitRepo = Get-ChildItem . -Force -Directory | where {$_.Name -eq ".git"} | Measure-Object

  if($isGitRepo.Count -gt 0)
  {
    $changesLocal = git status -s | Measure-Object

    if($changesLocal.Count -gt 0)
    {
      Write-Host "$($dir.FullName) $($changesLocal.Count) (local changes)"
      continue;
    }
    
    $changesRemote = git diff --stat origin/master | Measure-Object

    if($changesRemote.Count -gt 0)
    {
      Write-Host "$($dir.FullName) $($changesRemote.Count-1) (push remote)"
    }
  }
}

Set-Location $baseLocation

Current version is always available here: gitDirectoryStatus.ps1

Script can be executed like following

D:\klm\projects\Scripts\gitDirectoryStatus.ps1 D:\KLM\projects

With results in:

I have a fast feedback on what should (or should not) be pushed to remote server.

Summary

Such a tiny script saves me a lot of time. I don’t need to remember to push changes after I’m done programming. Usually at the end of the day I execute the script and it gives me status of all projects. No remembering, just pure automation.