packfile_reader 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b8e93c293d659c69561f903785550ebdcf9cbb4eff620d5f711a2eb55e2452f
4
- data.tar.gz: 504bec783784ff1bd3233d7e39b1e53e3621239c79e54d6c54375358d69d504c
3
+ metadata.gz: ea469cdf74d5f5c3d95fc87de089c9ffe622f159200003d423e5c8d00e273ad5
4
+ data.tar.gz: aeea839368939c2b328c737c67acf987a94f67c1e3bfb5a6bd5a84c8afcf2d97
5
5
  SHA512:
6
- metadata.gz: 06f757f92ed4ea72c888784ce94ede9d4502da4ee840f986ee74fb4fc984387387eba316ed7fc54dbb604d7df295f38bf5df589c0fee5bb962b49eac9fb5ebf7
7
- data.tar.gz: 23dbeb6424004becebd49eb8823d14230a95fcd14e842054d3be46b54acfbee887e1d47d97f4f6f66e01d63b435d97331fe21daa27b6267f142230e5bdd14fad
6
+ metadata.gz: e5b3980bd08e06c19e8ca9ab9d46104b1a8282cf2e54129c3f63ee204eb0d477ea8b43316c361a70bba3a788c4d2fbfc7c66ea1812aa32581255f181ac0e7492
7
+ data.tar.gz: 95e3c21fa8c71e6bae77018fb18472055e0ceab34490103a683787e3f189615499eda5dadb850d28ad0c242400ca853f6acb1dfdaffb3445b3c705c17f395ade
data/README.md CHANGED
@@ -39,7 +39,8 @@ where [options] are:
39
39
  -i, --filter-by-ids=<s> Comma separated list of object ids to look for (default: any)
40
40
  -e, --expand-objects Whether to expand objects data
41
41
  -o, --output-dir=<s> Directory to store the expanded objects (default: .)
42
- -v, --version Print version and exit
42
+ -v, --verbose Log some debugging informaiton to stderr
43
+ -r, --version Print version and exit
43
44
  -l, --help Show this message
44
45
 
45
46
  ```
@@ -114,5 +115,18 @@ $ cat /tmp/5297f8f21ad868d9eb6a9c01ad09a9d186177047.txt
114
115
  # test-git%
115
116
  ```
116
117
 
118
+ ### Debugging information
119
+
120
+ Passing `--verbose` to the command will add some debugging information to the output as well as the timestamp when the entry got processed at the beginning of the entry line.
121
+
122
+ ```
123
+ packfile_reader --no-headers -verbose pack.sample
124
+ ```
125
+
126
+ ```
127
+ [2020-12-01 21:24:43 -0800] 96438dd1e26e6963fa65be0012e8f6e84209bc5d OBJ_COMMIT 653
128
+ [2020-12-01 21:24:43 -0800] 5297f8f21ad868d9eb6a9c01ad09a9d186177047 OBJ_BLOB 10
129
+ [2020-12-01 21:24:44 -0800] bf195faf9d23ce0615cdefd2b746a077ef82f03f OBJ_TREE 37
130
+ ```
117
131
  # References
118
132
  - http://shafiul.github.io/gitbook/7_the_packfile.html
@@ -10,21 +10,22 @@ opts = Optimist::options do
10
10
  By default, the script will only report the object ids, their type and their deflated sizes.
11
11
  You can also make the script expand the content of the objects in the local directory or a directory
12
12
  of your choice.
13
-
13
+
14
14
  Usage:
15
15
  packfile_reader [options] <packfile>
16
16
  where [options] are:
17
17
  EOS
18
-
18
+
19
19
  opt :headers_only, 'Display only the headers of the packfile'
20
20
  opt :no_headers, 'Skip displaying the headers of the packfile'
21
21
  opt :filter_by_ids, 'Comma separated list of object ids to look for', :default => 'any', :short => '-i', :type => String
22
22
  opt :expand_objects, 'Whether to expand objects data', :default => false, :short => '-e'
23
23
  opt :output_dir, 'Directory to store the expanded objects', :default => '.', :short => '-o', :type => String
24
+ opt :verbose, 'Log some debugging informaiton to stderr', :default => false, :short => '-v'
24
25
  end
25
-
26
+
26
27
  (puts "You must inform a single packfile, found #{ARGV.size}"; exit 1) if ARGV.size > 1 or ARGV.empty?
27
-
28
+
28
29
  packfile = ARGV.first
29
30
  (puts 'Packfile not found'; exit 2) unless File.exist?(packfile)
30
31
 
@@ -50,13 +51,14 @@ File.open(packfile, 'rb') do |f|
50
51
  entries_processed = 0
51
52
  limit = objects_to_find == :any ? header.n_entries : objects_to_find.size
52
53
  (0...limit).each do
53
- entry = PackfileReader::PackfileEntry.next_entry(f, objects_to_find) do |c,u,id|
54
+ entry = PackfileReader::PackfileEntry.next_entry(f, objects_to_find, opts[:verbose]) do |c,u,id|
54
55
  if opts[:expand_objects]
55
56
  dir = opts[:output_dir]
56
57
  File.open(File.join(dir, "#{id}.txt"), 'w') {|o| o.write u}
57
58
  end
58
59
  end
59
60
 
60
- puts "#{entry.id}\t#{entry.type}\t#{entry.size}"
61
+ timestamp = opts[:verbose] ? "[#{Time.now}] " : ''
62
+ puts "\u001b[0K#{timestamp}#{entry.id}\t#{entry.type}\t#{entry.size}"
61
63
  end
62
- end
64
+ end
@@ -17,7 +17,9 @@ module PackfileReader
17
17
 
18
18
  # Accepts a block that will receive the compressed data, uncompressed data and
19
19
  # the computed object id
20
- def self.next_entry(packfile_io, objects_to_find=:any)
20
+ def self.next_entry(packfile_io, objects_to_find=:any, log_verbose=false)
21
+ raise 'Object id must be a valid sha1' unless objects_to_find == :any || objects_to_find.all? {|id| /^[0-9a-f]{40}$/.match? id }
22
+
21
23
  loop do
22
24
  return nil if packfile_io.eof?
23
25
 
@@ -27,15 +29,19 @@ module PackfileReader
27
29
  size = hunk.size
28
30
  offset = hunk.offset_size
29
31
 
32
+ # Clean the current line before printing the message
33
+ $stderr.puts "\u001b[0K>>>> Processing new entry [#{type}]" if log_verbose
30
34
  while hunk.continuation?
31
35
  hunk = PackfileReader::Hunk.new_without_type(packfile_io)
32
36
  size = (hunk.size << offset) | size # Data size is a combination of all hunk sizes
33
37
  offset += hunk.offset_size
34
38
  end
35
39
 
36
- compressed_data, uncompressed_data = find_data(packfile_io)
40
+ compressed_data, uncompressed_data = find_data(packfile_io, log_verbose)
37
41
  object_id = compute_id(type, size, uncompressed_data)
38
42
 
43
+ type = "#{type} [CORRUPTED] " if uncompressed_data.nil?
44
+
39
45
  if objects_to_find == :any || objects_to_find.member?(object_id)
40
46
  yield compressed_data, uncompressed_data, object_id if block_given?
41
47
  return PackfileEntry.new(type, size, object_id)
@@ -44,7 +50,7 @@ module PackfileReader
44
50
  end
45
51
 
46
52
  private
47
- def self.find_data(packfile_io)
53
+ def self.find_data(packfile_io, log_verbose)
48
54
  data_header = find_zlib_data_header(packfile_io)
49
55
 
50
56
  # since we don't have the index file that accompanies pack files
@@ -54,11 +60,16 @@ module PackfileReader
54
60
  compressed_data = data_header
55
61
  compressed_data += packfile_io.read(1)
56
62
 
63
+ bytes_read = 1
57
64
  begin
58
65
  uncompressed_data = Zlib.inflate(compressed_data)
59
66
  rescue Zlib::BufError
60
67
  compressed_data += packfile_io.read(1)
68
+ bytes_read += 1
69
+ $stderr.print " .... retrying on data gathering [#{bytes_read}] bytes read\r" if log_verbose
61
70
  retry
71
+ rescue Zlib::DataError
72
+ uncompressed_data = nil
62
73
  end
63
74
 
64
75
  [compressed_data, uncompressed_data]
@@ -92,7 +103,7 @@ module PackfileReader
92
103
  end
93
104
 
94
105
  return '000' if header_type.empty?
95
-
106
+
96
107
  header = "#{header_type} #{size}\0"
97
108
  store = "#{header}#{uncompressed_data}"
98
109
  Digest::SHA1.hexdigest(store)
@@ -104,4 +115,4 @@ module PackfileReader
104
115
  @id = id
105
116
  end
106
117
  end
107
- end
118
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: packfile_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robison WR Santos
@@ -56,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubygems_version: 3.1.2
59
+ rubygems_version: 3.0.3
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Parses git packfiles without the help of idx companion