Module: GitMedia::Status

Defined in:
lib/git-media/status.rb

Class Method Summary collapse

Class Method Details

.find_referencesObject

find tree entries that are likely media references



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/git-media/status.rb', line 15

def self.find_references
  references = {:to_expand => [], :expanded => [], :deleted => []}
  files = `git ls-tree -l -r HEAD`.split("\n")
  files = files.map { |f| s = f.split("\t"); [s[0].split(' ').last, s[1]] }
  files = files.select { |f| f[0] == '41' } # it's the right size
  files.each do |tree_size, fname|
    if size = File.size?(fname)
      if size == tree_size.to_i
        # TODO: read in the data and verify that it's a sha + newline
        sha = File.read(fname).strip
        if sha.length == 40 && sha =~ /^[0-9a-f]+$/
          references[:to_expand] << [fname, sha]
        end
      else
        references[:expanded] << fname
      end
    else
      # file was deleted
      references[:deleted] << fname
    end
  end
  references
end

.local_cache_statusObject



85
86
87
88
89
90
91
92
93
# File 'lib/git-media/status.rb', line 85

def self.local_cache_status
  # find files in media buffer and upload them
  references = {:unpushed => [], :pushed => []}
  all_cache = Dir.chdir(GitMedia.get_media_buffer) { Dir.glob('*') }
  unpushed_files = @push.get_unpushed(all_cache) || []
  references[:unpushed] = unpushed_files
  references[:pushed] = all_cache - unpushed_files rescue []
  references
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/git-media/status.rb', line 64

def self.print_cache_status(refs)
  if refs[:unpushed].size > 0
    puts "== Unpushed Media =="
    refs[:unpushed].each do |sha|
      cache_file = GitMedia.media_path(sha)
      size = File.size(cache_file)
      puts "   " + "(#{self.to_human(size)})".ljust(8) + ' ' + sha[0, 8]
    end
    puts
  end
  if refs[:pushed].size > 0
    puts "== Already Pushed Media =="
    refs[:pushed].each do |sha|
      cache_file = GitMedia.media_path(sha)
      size = File.size(cache_file)
      puts "   " + "(#{self.to_human(size)})".ljust(8) + ' ' + sha[0, 8]
    end
    puts
  end
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git-media/status.rb', line 39

def self.print_references(refs)
  if refs[:to_expand].size > 0
    puts "== Unexpanded Media =="
    refs[:to_expand].each do |file, sha|
      puts "   " + sha[0, 8] + " " + file
    end
    puts
  end
  if refs[:expanded].size > 0
    puts "== Expanded Media =="
    refs[:expanded].each do |file|
      size = File.size(file)
      puts "   " + "(#{self.to_human(size)})".ljust(8) + " #{file}"
    end
    puts
  end
  if refs[:deleted].size > 0
    puts "== Deleted Media =="
    refs[:deleted].each do |file|
      puts "           " + " #{file}"
    end
    puts
  end
end

.run!Object



6
7
8
9
10
11
12
# File 'lib/git-media/status.rb', line 6

def self.run!
  @push = GitMedia.get_push_transport
  r = self.find_references
  self.print_references(r)
  r = self.local_cache_status
  self.print_cache_status(r)
end

.to_human(size) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/git-media/status.rb', line 96

def self.to_human(size)
  if size < 1024
    return size.to_s + 'b'
  elsif size < 1048576
    return (size / 1024).to_s + 'k'
  else
    return (size / 1048576).to_s + 'm'
  end
end