你知道有没有办法列出每个分支的git存储库的空间使用情况?(就像df或du那样)
分支的“空间使用量”指的是“尚未与存储库的其他分支共享的提交所使用的空间”。
发布于 2012-12-06 20:19:26
看起来像这样的东西还不存在,下面是我为它做的一个Ruby脚本。
#!/usr/bin/env ruby -w
require 'set'
display_branches = ARGV
packed_blobs = {}
class PackedBlob
attr_accessor :sha, :type, :size, :packed_size, :offset, :depth, :base_sha, :is_shared, :branch
def initialize(sha, type, size, packed_size, offset, depth, base_sha)
@sha = sha
@type = type
@size = size
@packed_size = packed_size
@offset = offset
@depth = depth
@base_sha = base_sha
@is_shared = false
@branch = nil
end
end
class Branch
attr_accessor :name, :blobs, :non_shared_size, :non_shared_packed_size, :shared_size, :shared_packed_size, :non_shared_dependable_size, :non_shared_dependable_packed_size
def initialize(name)
@name = name
@blobs = Set.new
@non_shared_size = 0
@non_shared_packed_size = 0
@shared_size = 0
@shared_packed_size = 0
@non_shared_dependable_size = 0
@non_shared_dependable_packed_size = 0
end
end
dependable_blob_shas = Set.new
# Collect every packed blobs information
for pack_idx in Dir[".git/objects/pack/pack-*.idx"]
IO.popen("git verify-pack -v #{pack_idx}", 'r') do |pack_list|
pack_list.each_line do |pack_line|
pack_line.chomp!
if not pack_line.include? "delta"
sha, type, size, packed_size, offset, depth, base_sha = pack_line.split(/\s+/, 7)
size = size.to_i
packed_size = packed_size.to_i
packed_blobs[sha] = PackedBlob.new(sha, type, size, packed_size, offset, depth, base_sha)
dependable_blob_shas.add(base_sha) if base_sha != nil
else
break
end
end
end
end
branches = {}
# Now check all blobs for every branches in order to determine whether it's shared between branches or not
IO.popen("git branch --list", 'r') do |branch_list|
branch_list.each_line do |branch_line|
# For each branch
branch_name = branch_line[2..-1].chomp
branch = Branch.new(branch_name)
branches[branch_name] = branch
IO.popen("git rev-list #{branch_name}", 'r') do |rev_list|
rev_list.each_line do |commit|
# Look into each commit in order to collect all the blobs used
for object in `git ls-tree -zrl #{commit}`.split("\0")
bits, type, sha, size, path = object.split(/\s+/, 5)
if type == 'blob'
blob = packed_blobs[sha]
branch.blobs.add(blob)
if not blob.is_shared
if blob.branch != nil and blob.branch != branch
# this blob has been used in another branch, let's set it to "shared"
blob.is_shared = true
blob.branch = nil
else
blob.branch = branch
end
end
end
end
end
end
end
end
# Now iterate on each branch to compute the space usage for each
branches.each_value do |branch|
branch.blobs.each do |blob|
if blob.is_shared
branch.shared_size += blob.size
branch.shared_packed_size += blob.packed_size
else
if dependable_blob_shas.include?(blob.sha)
branch.non_shared_dependable_size += blob.size
branch.non_shared_dependable_packed_size += blob.packed_size
else
branch.non_shared_size += blob.size
branch.non_shared_packed_size += blob.packed_size
end
end
end
# Now print it if wanted
if display_branches.empty? or display_branches.include?(branch.name)
puts "branch: %s" % branch.name
puts "\tnon shared:"
puts "\t\tpacked: %s" % branch.non_shared_packed_size
puts "\t\tnon packed: %s" % branch.non_shared_size
puts "\tnon shared but with dependencies on it:"
puts "\t\tpacked: %s" % branch.non_shared_dependable_packed_size
puts "\t\tnon packed: %s" % branch.non_shared_dependable_size
puts "\tshared:"
puts "\t\tpacked: %s" % branch.shared_packed_size
puts "\t\tnon packed: %s" % branch.shared_size, ""
end
end有了这一个,我能够看到在我的2Mo git库中,我有一个无用的分支,它占用了我1Mo的blob,而不是与任何其他分支共享。
发布于 2012-12-05 19:52:13
这个问题没有正确的答案。如果您查看仅包含在特定分支中的提交,您将获得blobs (基本上是文件版本)的列表。现在,您必须检查这些blob是否是其他分支中任何提交的一部分。这样做之后,您将拥有一个只是分支的一部分的blob列表。
现在,您可以将这些斑点的大小相加以得到结果-但这可能是非常错误的。Git将这些斑点相互压缩,因此斑点的实际大小取决于您的存储库中有哪些其他斑点。您可以删除1000个blobs,每个10MB,并且只能释放1kb的磁盘空间。
通常,较大的repo大小是由repo中的单个大文件造成的(如果不是这样,您可能做错了什么:)。有关如何查找这些内容的信息可在此处找到:Find files in git repo over x megabytes, that don't exist in HEAD
发布于 2012-12-05 19:34:23
Git维护了一个有向无环图,每次提交都会占用磁盘空间(从简单的意义上讲)。
除非你的所有分支都偏离了第一次提交,否则就会有不同分支所共有的提交,这意味着每个分支都会“共享”一定数量的磁盘空间。
这使得很难提供磁盘使用的“每个分支”的数字,因为它需要限定共享的数量,以及与哪些其他分支共享它。
https://stackoverflow.com/questions/13722076
复制相似问题