RepoMakefile

Only a few unix commands use the `-C' flag to change the working directory for execution. Of these, the famous ones are tar, make, and git. In terms of historical precedence, git was developed much later so one could say that its `-C' support was argument by analogy.

While all three commands benefit from the `-C' flag because each is file-system position dependent, only git will recurse up the directory hierarchy to find the git root. Since tar has no command files in the filesystem whereby it could determine a natural root, it cannot support this recursive search.

On the other hand, `make' could because it requires a `Makefile' for its dependency graph and commands. Adding support is straight-forward, just put the following in your $PATH before /usr/bin

#!/bin/bash
#
# recurse up the hierarchy to find the Makefile
#
make='/usr/bin/make --no-print-directory'
dir=`pwd`
path=''

mkargs() {
    for i in "$@"; do
        if [[ "$i" =~ = ]]; then
            echo "$i"
        else
            echo "$path$i"
        fi
    done
}


while ! [[ -e "${dir}/Makefile" ]]; do
    [[ "$dir" == "" || "$dir" == "/" ]] && break
    path="` basename $dir `/$path"
    dir=` dirname $dir `
done

args=` mkargs "$@" `
exec $make -C $dir $args

This all came to the fore as part of a discussion of the relative merits of monorepos, polyrepos, and metarepos,1 ie. what is a repo anyway? and how to determine what should be in a repo? The jury is still out on those questions, however....