Modified the outer loop so that it would iterate over 'git rev-list subrepo/$subdir --format="format:%T"' instead of following the first-parent-lineage from subrepo/$subdir.  Not convinced this is perfect, but is a good start at least.  Might need to use the output of the above rev-list command, then somehow follow each ancestor-branch with matching $outer_loop_term_tree entries.
Also added an optimization on the inner-loop-element query, by only listing what commits modified the $subdir.