find: Finding the Shallowest Instance

1 
1 10.4 Finding the Shallowest Instance
1 ====================================
1 
1 Suppose you maintain local copies of sources from various projects, each
1 with their own choice of directory organisation and source code
1 management (SCM) tool.  You need to periodically synchronize each
1 project with its upstream tree.  As the number local repositories grows,
1 so does the work involved in maintaining synchronization.  SCM utilities
1 typically create some sort of administrative directory: .svn for
1 Subversion, CVS for CVS, and so on.  These directories can be used as a
1 key to search for the bases of the project source trees.  Suppose we
1 have the following directory structure:
1 
1      repo/project1/CVS
1      repo/gnu/project2/.svn
1      repo/gnu/project3/.svn
1      repo/gnu/project3/src/.svn
1      repo/gnu/project3/doc/.svn
1      repo/project4/.git
1 
1    One would expect to update each of the 'projectX' directories, but
1 not their subdirectories (src, doc, etc.).  To locate the project roots,
1 we would need to find the least deeply nested directories containing an
1 SCM-related subdirectory.  The following command discovers those roots
1 efficiently.  It is efficient because it avoids searching subdirectories
1 inside projects whose SCM directory we already found.
1 
1      find repo/ \
1      -exec test -d {}/.svn \; -or \
1      -exec test -d {}/.git \; -or \
1      -exec test -d {}/CVS \; -print -prune
1 
1    In this example, 'test' is used to tell if we are currently examining
1 a directory which appears to the a project's root directory (because it
1 has an SCM subdirectory).  When we find a project root, there is no need
1 to search inside it, and '-prune' makes sure that we descend no further.
1 
1    For large, complex trees like the Linux kernel, this will prevent
1 searching a large portion of the structure, saving a good deal of time.
1