Pages

Sunday, July 20, 2014

Script to find repositories with unstaged/uncommited changes

I found this nice script that I often use to check if I did not forget to push the latest changes to git.

Here are some instructions on how to use it:
  1. Save the snippet below to a file and name it something like multipleStatus.sh;
  2. Place the file on the same folder of your repositories;
  3. To run the script, browse to that folder and type:
    source multipleStatus.sh; find-dirty

And here is the snippet you need to save:
#!/bin/bash

function unstaged_changes() {
    worktree=${1%/*};
    git --git-dir="$1" --work-tree="$worktree" diff-files --quiet --ignore-submodules --
}

function uncommited_changes() {
    worktree=${1%/*};
    git --git-dir="$1" --work-tree="$worktree" diff-index --cached --quiet HEAD --ignore-submodules --
}

function find-dirty () {
    for gitdir in `find . -name .git`;
    do
        worktree=${gitdir%/*};
        if ! unstaged_changes $gitdir
        then
            echo "unstaged     $gitdir"
        fi

        if ! uncommited_changes $gitdir
        then
            echo "uncommitted  $gitdir"
        fi
    done
}

No comments:

Post a Comment