docs:git:gitr

gitr

gitr is a script I've developed for my project needs. It runs git commands recursively with special consideration for what I want to see or how my projects are typically structured.

  • it helps me identify projects where I have commits that haven't been pushed to remotes
  • it helps me update all my repos at once, either by fetching to review or by pulling to merge

You can have this as a command available wherever you are by placing it in /usr/bin/gitr (without the .sh), and making it executable.

gitr.sh

#!/bin/sh

gitrmode=""
if [ "$1" = "fetch" ]; then
	gitrmode="fetch"
elif [ "$1" = "status" ]; then
	gitrmode="status"
elif [ "$1" = "pull" ]; then
	gitrmode="pull"
elif [ "$1" = "showconfig" ]; then
	gitrmode="showconfig"
fi

if [ "$gitrmode" = "" ]; then
	echo -e "Usage: `basename $0` <fetch|status|pull|showconfig>\n"
	exit
fi

function runGitCmd()
{
	`which ls` -1d * | while read item;do
		# make sure we have a directory
		if [ -d "${item}" ]; then
			cd "${item}"

			# make sure the directory is a .git project
			if [ -d ".git" ]; then

				if [ "$gitrmode" = "status" ]; then
					echo "statusing ${item}..."
					git status | grep -i -E '^[^#]|^# On branch|^# Your branch'
					echo -e "\n";

				elif [ "$gitrmode" = "fetch" ]; then
					echo "fetching all remotes for ${item}..."
					git fetch --all
					echo -e "\n";

				elif [ "$gitrmode" = "pull" ]; then
					echo "pulling all remotes for ${item}..."
					git pull --all
					echo -e "\n";

				elif [ "$gitrmode" = "showconfig" ]; then
					echo "showing .git/config for ${item}..."
					cat .git/config
					echo -e "\n";
				fi

			fi
			cd ..

		fi
	done
}

runGitCmd

if [ -d "js" ]; then
	cd js
	pwd
	runGitCmd
	cd ..
fi

if [ -d "lib" ]; then
	cd lib
	pwd
	runGitCmd
	cd ..
fi

unset -f runGitCmd
  • docs/git/gitr.txt
  • Last modified: 2010/10/18 15:22
  • by billh