0

I am using Grit (Ruby gem) to load a git repo, extract a given commit to a folder, then 'do stuff' with it. Grit supports git archive with archive_tar which retuns a 'string containing tar archive'. I would like to extract this to the filesystem using Ruby libs / gems if possible (not direct system calls) and avoid things like saving the data to an archive first and then extracting it (really looking for an efficient one-liner here).

2
  • Please show the code that you're using to do this now.
    – Ryan Bigg
    Oct 22, 2012 at 3:16
  • I didn't really have code to do it before. I just used Grit to make a tar file first and then call tar as a system command. Nov 12, 2012 at 7:48

1 Answer 1

2

Under the assumption that 'string containing tar archive' is equivalent to

File.open("Downloads.tar", "rb") {|a| a.read }

I am able to accomplish this using fileutils, stringio, and the minitar gem. First you will need to install minitar, which should be as simple as

gem install minitar

Since minitar does not support it's regular unpack to use a stream, we'll create our own rudimentary unpacking method.

require 'archive/tar/minitar'
require 'stringio'
require 'fileutils'

def unpack_tar(directory, string)
  FileUtils.mkdir_p(directory) if !File.exist?(directory)
  stringio = StringIO.new(string)
  input = Archive::Tar::Minitar::Input.new(stringio)
  input.each {|entry|
    input.extract_entry(directory, entry)
  }
end

unpack_tar("./test", File.open("Downloads.tar", "rb") {|a| a.read })

Of course replacing the whole File.open part with the archive_tar function of the Grit gem. This is all assuming that first assumption of course, though i'm sure ths method can easily be adapted to suit whatever archive_tar actually returns.

1
  • This worked great using e.g. Grit::Repo.new('repo_dir').archive_tar('commit_id') as the string. Nov 12, 2012 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.