From df27b5e7d6cb210b5a0870eb671a983336fb95c7 Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Wed, 14 Nov 2007 10:24:22 -0800 Subject: added 'archive' and tests --- lib/git/base.rb | 5 +++++ lib/git/branch.rb | 7 +++++-- lib/git/lib.rb | 36 +++++++++++++++++++++++++++++++++++- lib/git/object.rb | 5 +++++ 4 files changed, 50 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/git/base.rb b/lib/git/base.rb index e7eaf22..6fb8adb 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -311,6 +311,11 @@ module Git tag(tag_name) end + # creates an archive file of the given tree-ish + def archive(treeish, file = nil, opts = {}) + self.object(treeish).archive(file, opts) + end + # repacks the repository def repack self.lib.repack diff --git a/lib/git/branch.rb b/lib/git/branch.rb index 7cef94e..f9106ab 100644 --- a/lib/git/branch.rb +++ b/lib/git/branch.rb @@ -21,15 +21,18 @@ module Git end def gcommit - @gcommit = @base.object(name) if !@gcommit + @gcommit = @base.object(@full) if !@gcommit @gcommit end def checkout check_if_create - @base.checkout(@name) + @base.checkout(@full) end + def archive(file, opts = {}) + @base.lib.archive(@full, file, opts) + end # g.branch('new_branch').in_branch do # # create new file diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 051025c..6cebc95 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1,3 +1,5 @@ +require 'tempfile' + module Git class GitExecuteError < StandardError @@ -344,6 +346,37 @@ module Git command('repack', ['-a', '-d']) end + # creates an archive file + # + # options + # :format (zip, tar) + # :prefix + # :remote + # :path + def archive(sha, file = nil, opts = {}) + opts[:format] = 'zip' if !opts[:format] + + if opts[:format] == 'tgz' + opts[:format] = 'tar' + opts[:add_gzip] = true + end + + if !file + file = Tempfile.new('archive').path + end + + arr_opts = [] + arr_opts << "--format=#{opts[:format]}" if opts[:format] + arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix] + arr_opts << "--remote=#{opts[:remote]}" if opts[:remote] + arr_opts << sha + arr_opts << opts[:path] if opts[:path] + arr_opts << '| gzip' if opts[:add_gzip] + arr_opts << "> #{file.to_s}" + command('archive', arr_opts) + return file + end + private def command_lines(cmd, opts = {}) @@ -367,10 +400,11 @@ module Git #puts out #puts if $?.exitstatus > 0 + puts $?.exitstatus if $?.exitstatus == 1 && out == '' return '' end - raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s) + raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s) end out end diff --git a/lib/git/object.rb b/lib/git/object.rb index 7702811..c811810 100644 --- a/lib/git/object.rb +++ b/lib/git/object.rb @@ -48,6 +48,11 @@ module Git Git::Log.new(@base, count).object(@sha) end + # creates an archive of this object (tree) + def archive(file = nil, opts = {}) + @base.lib.archive(@sha, file, opts) + end + end -- cgit