Home > other >  Count number of subdirectiories inside each subdirectory of git repo using simple git commands or sh
Count number of subdirectiories inside each subdirectory of git repo using simple git commands or sh

Time:06-23

I have a repo with the following structure and I wouldlike to countnumber of subdirectories inside tier1, tier2 and tier3. I dont want to count subdirectories within the subdirectory. For examplee i have folders named a 1, 2, 3 inside tier1 and i wanted to see the count as 3. I dont want whats isnide those 1,2,3 folders. Repo Structure

Git clone actions should be avoided, as we do not need a local clone of the whole repo plus all history information. A simple fetch will be enough, is there are any leaner ways to retrieve the directory information ??

Am presently counting number of subdirectories by, entering each folder and the folloing command:

ls | wc -l

CodePudding user response:

Git clone actions should be avoided, as we do not need a local clone of the whole repo plus all history information. A simple fetch will be enough, is there are any leaner ways to retrieve the directory information ??

You can filter your clones to skip the actual content, just get the structure.

git clone --bare --depth 1 --filter=blob:none u://r/l checkit
git -C checkit ls-tree --name-only -d @:

lists the toplevel directories, then it's just a formatting question,

for d in $(git -C checkit ls-tree --name-only -d @:)
do printf '} %s\n' $(git -C checkit ls-tree -d @:$d|wc -l) $d
done

CodePudding user response:

ls at best gives you the number of non-hidden entries directly inside the directory. If you have among them a plain file, or an entry containing spaces, or an entry where the name strats with a period, or a directory entry which is a directory, but has itself subdirectories, your count will be wrong.

I would instead do a

shopt -s nullglob
for topdir in tier[1-3]
do
  if [[ -d $topdir ]]
  then
    a=($(find "$topdir" -type d))  
    echo "Number of subdirectories below $topdir is ${#a[@]}"
  fi
done

The purpose of setting nullglob is only to avoid an error message if no tier directory exists.

UPDATE: If you are not interested in subdirectories further down, you could do instead of the find a

shopt -s dotglob
a=("$topdir"/*/)

The dotglob ensures that you are not missing hidden subdirectories, and the final slash in the glob-pattern ensures that the expansion happens only for entries which denote a directory.

CodePudding user response:

git ls-tree -d HEAD:<dir>

git ls-tree -d HEAD:<dir> | wc -l

you can replace HEAD with any commit-ish reference :

git ls-tree -d origin/master:<dir>
git ls-tree -d v2.0.3:<dir>
git ls-tree -d <sha>:<dir>
...

If you want the list of all (recursive) subdirectories, add the -r option.

  • Related