【问题标题】:Get file size of a git commit without a tmp file获取没有 tmp 文件的 git 提交的文件大小
【发布时间】:2016-05-25 23:22:20
【问题描述】:

我有 answered this question,建议使用 git hooks,但坦率地说,我对第二部分并不完全满意,因为创建一个临时文件只是为了获取提交的大小看起来既多余又丑陋。有没有办法直接获取大小?假设标准 git 实现。

正如@Cyrus 所建议的,git diff --cached | wc -c 是一个很好的近似值,但它依赖于编码。

我也尝试过进程替换 ls -l <(git diff --cached) | awk '{print $5}' 但返回 0。

编辑:基于@torek's answer我想出了这两个解决方案:

git ls-files --stage | cut -d' ' -f2 | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

git diff --cached | awk -F. '/index/ {print $3}' | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}'

后者也处理新文件。

【问题讨论】:

  • git diff --cached | wc -c?
  • 我想这是一个很好的近似值,但它依赖于编码。我也尝试过处理 sub ls -l <(git diff --cached) | awk '{print $5}' 但返回 0

标签: linux git bash


【解决方案1】:

要获取 git 对象的大小(以字节为单位),请使用 git cat-file -s <em>revspec</em>。例如:

$ git cat-file -s HEAD:Makefile
950

请注意,这会显示树的对象大小:

$ git cat-file -t HEAD:
tree
$ git cat-file -s HEAD:
546

对于大量 ID,您可能希望使用 git cat-file --batch-check(从 gi​​t 1.8.4 开始,现在采用格式参数来选择要打印的信息;请参阅 the documentation)。

【讨论】:

  • 谢谢,索引的 refspec 是什么?
  • 我想出了git diff --cached | awk -F. '/index/ {print $3}' | xargs -L 1 git cat-file -s | awk '{s+=$1} END {print s}',但我想有一个更优雅的解决方案
  • 索引没有参考规范,但如果您知道路径,:n:path(其中 n 是 0、1、2、3 之一)可以工作。更一般地说,要阅读它,请使用git ls-files(在这种情况下git ls-files --stage 可能是正确的方法)。
  • n 代表什么?
  • 例如,git rev-parse :0:README 获取文件 README 的索引槽 0 的 SHA-1。如果foo.py 处于冲突合并的中间,:1:foo.py:2:foo.py:3:foo.py 获取文件三个版本的 SHA-1(尽管如果冲突是 delete-vs-modify 或 create -vs-create 可能少于 3 个版本)。
【解决方案2】:

我认为git diff --cached --numstat基本上是信息的主要来源。对我来说,还不清楚你想对提交施加什么限制,但总结一下插入和删除的数量,你可以使用这样的东西:

git diff --numstat --cached | awk '
   BEGIN { insertions=0; deletions=0; }
   { insertions+=$1; deletions+=$2; }
   END { printf("inserttions=%d, deletions=%d\n", insertions, deletions); }'

【讨论】:

  • 谢谢,但我指的是文件大小(以字节为单位),如 ls 的输出所示
猜你喜欢
  • 1970-01-01
  • 2013-08-09
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多