Tiny git repositories automation to save 5 minutes daily

Published on 06 November 2016

I'm a big fun of automation. I would even say 'what can be automated, can be forgotten' and you can focus in important things. This post happen to be next in new series called "Automation". The series came out somehow, I didn't plan it. It just seem 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 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 use take on 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 particular folder got 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:klmprojectsScriptsgitDirectoryStatus.ps1 D:KLMprojects

With results in:

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

Summary

Such 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.'

comments powered by Disqus