如何获取Git Bundle中项目文件的元数据(不含Git内部文件)
实现Git Bundle涉及文件的元数据列表生成
需求说明
创建Git Bundle(例如包含master分支最后2次提交的mybundle)后,需要提取该Bundle所涉及的所有项目文件的元数据(文件名、文件类型、文件大小等),且排除Git内部生成的文件。
已用Git命令
- 创建Bundle命令:
git bundle create mybundle -2 master - 从Bundle拉取到本地分支命令:
git fetch mybundle master:localRef
生成文件元数据列表的便捷方法
方法一:基于拉取后的本地分支统计
拉取Bundle到本地分支后,直接针对目标分支的最新状态生成文件元数据:
git ls-tree -r --name-only localRef | while read file; do repo_root=$(git rev-parse --show-toplevel) full_path="$repo_root/$file" if [ -f "$full_path" ]; then file_size=$(du -h "$full_path" | cut -f1) file_type=$(file -b "$full_path") echo "文件名: $file | 大小: $file_size | 类型: $file_type" fi done
说明:
git ls-tree -r --name-only localRef:递归列出localRef分支下所有非Git内部的文件路径- 循环遍历每个文件,通过
du获取可读格式的文件大小,file命令获取文件类型,最后格式化输出元数据
方法二:无需拉取,直接解析Bundle
如果不想拉取到本地仓库,可直接通过Git命令解析Bundle内的提交内容:
git bundle list-heads mybundle | awk '{print $2}' | while read ref; do git cat-file -p $ref^{tree} | grep '^100644 blob' | while read line; do blob_hash=$(echo "$line" | awk '{print $3}') file=$(echo "$line" | awk '{print $4}') file_size=$(git cat-file -s $blob_hash) file_type=$(git cat-file -t $blob_hash) echo "文件名: $file | 大小: $file_size 字节 | 类型: $file_type" done done
说明:
git bundle list-heads mybundle:列出Bundle内包含的所有引用git cat-file -p $ref^{tree}:查看引用对应的树对象内容- 提取文件对应的Blob哈希,用
git cat-file -s获取字节数大小,git cat-file -t确认文件类型,输出元数据
内容的提问来源于stack exchange,提问作者Tuhin
相关产品推荐
相关产品推荐

