使用简单的git命令或shell脚本计算GIT存储库中每个子目录内部的分区数量

发布于 2025-02-10 05:04:39 字数 436 浏览 2 评论 0原文

我有一个带有以下结构的存储库,我会像tier1,tier2和tier3内的子目录伯爵。我不想在子目录中计算子目录。例如,我的文件夹在tier1内部名为1、2、3,我想把计数视为3。我不想要什么1,2,3文件夹。

应该避免GIT克隆动作,因为我们不需要整个仓库中的本地克隆以及所有历史记录信息。一个简单的获取就足够了,是否有任何更精简的方法来检索目录信息?

AM目前通过输入每个文件夹和Folloing命令来计数子目录数量:

ls | wc -l

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱着落日 2025-02-17 05:04:39

git克隆动作应避免,因为我们不需要整个存储库的本地克隆以及所有历史记录信息。一个简单的获取就足够了,是否有任何更精简的方法来检索目录信息?

您可以过滤克隆以跳过实际内容,只需获得结构即可。对于Linux存储库,这是一个约25m的下载,节省了〜99%:

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

列出高级目录,然后只是一个格式的问题,

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

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. For the linux repo this is a ~2.5M download, a ~99% savings:

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 '%7d %s\n' $(git -C checkit ls-tree -d @:$d|wc -l) $d
done
坐在坟头思考人生 2025-02-17 05:04:39

ls充其量只能为您提供直接在目录内的非隐藏条目的数量。如果您有一个普通文件,或一个包含空格的条目,或一个名称带有周期的名称的条目,或者是目录的目录条目,但本身具有子目录,则您的计数将是错误的。

相反,我会执行

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

设置nullglob的目的仅是为了避免出现错误消息,如果存在tier目录。

更新:如果您对子目录不感兴趣,则可以执行查找 a

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

dotglob 确保您不会丢失隐藏的子目录,以及最终的斜线全球模式可确保扩展仅用于表示目录的条目。

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.

无力看清 2025-02-17 05:04:39
git ls-tree -d HEAD:<dir>

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

您可以将head替换为任何commit-ish参考:

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

如果您想要 all (递归)子目录的列表,请添加-r选项。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文