DEV Community

Cover image for If repetitive, then script it.
Hafiz Jaafar
Hafiz Jaafar

Posted on

If repetitive, then script it.

Abstract

Let's assume that on Windows, you put your development projects under a main or common directory, e.g., 'development', and all of these projects are local Git repositories.

The repetitive problem: めんどくさい mendokusai - a hassle

It gets tedious, if you have to navigate into every directory and execute 'git remote update'.

you@your-pc MINGW64 /d/developments
$ ls -1
foo-bar-backend/
foo-bar-db/
foo-bar-frontend/
Enter fullscreen mode Exit fullscreen mode

Solution: a helper script

Using Git Bash on Windows, I recommend an executable bash-like helper script to navigate through all these projects and be done with it.

Write a helper script

Say, this script is named git_remote_update.bash:

#!/bin/bash.exe

echo
echo "Going into every directory here."
echo

for x in *;
do
    if [ -d "${x}" ]
    then
        if [ -e "${x}"/.git ]
        then
            cd "${x}"
            echo "${x}"
            echo "======================"
            echo "Calling git remote update ..."
            git remote update
            echo
            echo "Calling git status ..."
            git status
            echo
            cd ../
        fi
    fi
done
Enter fullscreen mode Exit fullscreen mode

Set the file permission

Place this helper script in a dedicated directory, e.g., helperScriptsWindows, and change its permission so:

you@your-pc MINGW64 /d/helperScriptsWindows
$ chmod 755 git_remote_update.bash
$ ls -la
-rwxr-xr-x 1 you 197609 434 Mar 17  2018 git_remote_v.bash*
Enter fullscreen mode Exit fullscreen mode

Include the directory into Path

  1. Open the Windows Control Panel.
  2. Navigate to System and Security => System => Advanced system settings => Environment Variables => System variables.
  3. Click Path => New. Add the absolute path to the directory helperScriptsWindows. Done.

Check the PATH

You should be able to see the absolute path to the directory of your helper scripts is included:

you@your-pc MINGW64 ~
$ echo $PATH
Enter fullscreen mode Exit fullscreen mode

Helper script usage

The script git_remote_update.bash should be already 'registered', and with few typing efforts, it's callable in the terminal:

you@your-pc MINGW64 /d/developments
$ git_remote_update.bash

Going into every directory here.

foo-bar-backend
======================
Calling git remote update ...
Fetching origin

Calling git status ...
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

foo-bar-db
======================
Calling git remote update ...
Fetching origin

Calling git status ...
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

foo-bar-frontend
======================
Calling git remote update ...
Fetching origin

Calling git status ...
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

Have fun.

Top comments (0)