Skip to content

Commit df27b5e

Browse files
author
scott Chacon
committed
added 'archive' and tests
1 parent cbf72e3 commit df27b5e

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

lib/git/base.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,11 @@ def add_tag(tag_name)
311311
tag(tag_name)
312312
end
313313

314+
# creates an archive file of the given tree-ish
315+
def archive(treeish, file = nil, opts = {})
316+
self.object(treeish).archive(file, opts)
317+
end
318+
314319
# repacks the repository
315320
def repack
316321
self.lib.repack

lib/git/branch.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ def initialize(base, name)
2121
end
2222

2323
def gcommit
24-
@gcommit = @base.object(name) if !@gcommit
24+
@gcommit = @base.object(@full) if !@gcommit
2525
@gcommit
2626
end
2727

2828
def checkout
2929
check_if_create
30-
@base.checkout(@name)
30+
@base.checkout(@full)
3131
end
3232

33+
def archive(file, opts = {})
34+
@base.lib.archive(@full, file, opts)
35+
end
3336

3437
# g.branch('new_branch').in_branch do
3538
# # create new file

lib/git/lib.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'tempfile'
2+
13
module Git
24

35
class GitExecuteError < StandardError
@@ -344,6 +346,37 @@ def repack
344346
command('repack', ['-a', '-d'])
345347
end
346348

349+
# creates an archive file
350+
#
351+
# options
352+
# :format (zip, tar)
353+
# :prefix
354+
# :remote
355+
# :path
356+
def archive(sha, file = nil, opts = {})
357+
opts[:format] = 'zip' if !opts[:format]
358+
359+
if opts[:format] == 'tgz'
360+
opts[:format] = 'tar'
361+
opts[:add_gzip] = true
362+
end
363+
364+
if !file
365+
file = Tempfile.new('archive').path
366+
end
367+
368+
arr_opts = []
369+
arr_opts << "--format=#{opts[:format]}" if opts[:format]
370+
arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
371+
arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
372+
arr_opts << sha
373+
arr_opts << opts[:path] if opts[:path]
374+
arr_opts << '| gzip' if opts[:add_gzip]
375+
arr_opts << "> #{file.to_s}"
376+
command('archive', arr_opts)
377+
return file
378+
end
379+
347380
private
348381

349382
def command_lines(cmd, opts = {})
@@ -367,10 +400,11 @@ def command(cmd, opts = {})
367400
#puts out
368401
#puts
369402
if $?.exitstatus > 0
403+
puts $?.exitstatus
370404
if $?.exitstatus == 1 && out == ''
371405
return ''
372406
end
373-
raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)
407+
raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)
374408
end
375409
out
376410
end

lib/git/object.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def log(count = 30)
4848
Git::Log.new(@base, count).object(@sha)
4949
end
5050

51+
# creates an archive of this object (tree)
52+
def archive(file = nil, opts = {})
53+
@base.lib.archive(@sha, file, opts)
54+
end
55+
5156
end
5257

5358

tests/units/test_archive.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env ruby
2+
3+
require File.dirname(__FILE__) + '/../test_helper'
4+
5+
class TestArchive < Test::Unit::TestCase
6+
7+
def setup
8+
set_file_paths
9+
@git = Git.open(@wdir)
10+
end
11+
12+
def tempfile
13+
Tempfile.new('archive-test').path
14+
end
15+
16+
def test_archive
17+
f = @git.archive('v2.6', tempfile)
18+
assert(File.exists?(f))
19+
20+
f = @git.object('v2.6').archive(tempfile) # writes to given file
21+
assert(File.exists?(f))
22+
23+
f = @git.object('v2.6').archive # returns path to temp file
24+
assert(File.exists?(f))
25+
26+
f = @git.object('v2.6').archive(tempfile, :format => 'zip')
27+
assert(File.file?(f))
28+
29+
f = @git.object('v2.6').archive(tempfile, :format => 'tgz', :prefix => 'test/')
30+
assert(File.exists?(f))
31+
32+
f = @git.object('v2.6').archive(tempfile, :format => 'tar', :prefix => 'test/', :path => 'ex')
33+
assert(File.exists?(f))
34+
35+
in_temp_dir do
36+
c = Git.clone(@wbare, 'new')
37+
c.chdir do
38+
f = @git.remote('origin').branch('master').archive(tempfile, :format => 'tgz')
39+
assert(File.exists?(f))
40+
end
41+
end
42+
end
43+
44+
end

0 commit comments

Comments
 (0)