Skip to content

Commit f5caf77

Browse files
Adding delete_tag
Adding support for some `tag` options -f -d -s -a -m Supporting `tag` object reference closes ruby-git#109 closes ruby-git#104 closes ruby-git#24 refs ruby-git#27
1 parent 8ed8ba4 commit f5caf77

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

lib/git/base.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,26 @@ def tag(tag_name)
408408
Git::Object.new(self, tag_name, 'tag', true)
409409
end
410410

411-
# creates a new git tag (Git::Tag)
412-
def add_tag(tag_name)
413-
self.lib.tag(tag_name)
414-
tag(tag_name)
411+
# Creates a new git tag (Git::Tag)
412+
# Usage:
413+
# repo.add_tag('tag_name', object_reference)
414+
# repo.add_tag('tag_name', object_reference, {:options => 'here'})
415+
# repo.add_tag('tag_name', {:options => 'here'})
416+
#
417+
# Options:
418+
# :a | :annotate -> true
419+
# :d -> true
420+
# :m | :message -> String
421+
# :s -> true
422+
#
423+
def add_tag(name, *opts)
424+
self.lib.tag(name, *opts)
425+
tag(name)
426+
end
427+
428+
# deletes a tag
429+
def delete_tag(name)
430+
self.lib.tag(name, {:d => true})
415431
end
416432

417433
# creates an archive file of the given tree-ish

lib/git/lib.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,26 @@ def tags
562562
command_lines('tag')
563563
end
564564

565-
def tag(tag)
566-
command('tag', tag)
565+
def tag(name, *opts)
566+
target = opts[0].instance_of?(String) ? opts[0] : nil
567+
568+
opts = opts.last.instance_of?(Hash) ? opts.last : {}
569+
570+
if (opts[:a] || opts[:annotate]) && !(opts[:m] || opts[:message])
571+
raise "Can not create an [:a|:annotate] tag without the precense of [:m|:message]."
572+
end
573+
574+
arr_opts = []
575+
576+
arr_opts << '-f' if opts[:force] || opts[:f]
577+
arr_opts << '-a' if opts[:a] || opts[:annotate]
578+
arr_opts << '-s' if opts[:s] || opts[:sign]
579+
arr_opts << '-d' if opts[:d] || opts[:delete]
580+
arr_opts << name
581+
arr_opts << target if target
582+
arr_opts << "-m #{opts[:m] || opts[:message]}" if opts[:m] || opts[:message]
583+
584+
command('tag', arr_opts)
567585
end
568586

569587

tests/units/test_tags.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,36 @@ def test_tags
2424
r1.commit('my commit')
2525
r1.add_tag('second')
2626

27-
assert(r1.tags.map{|t| t.name}.include?('first'))
27+
assert(r1.tags.any?{|t| t.name == 'first'})
2828

2929
r2.add_tag('third')
3030

31-
assert(r2.tags.map{|t| t.name}.include?('third'))
32-
assert(!r2.tags.map{|t| t.name}.include?('second'))
31+
assert(r2.tags.any?{|t| t.name == 'third'})
32+
assert(r2.tags.none?{|t| t.name == 'second'})
33+
34+
assert_raise RuntimeError do
35+
r2.add_tag('fourth', {:a => true})
36+
end
37+
38+
r2.add_tag('fourth', {:a => true, :m => 'test message'})
39+
40+
assert(r2.tags.any?{|t| t.name == 'fourth'})
41+
42+
r2.add_tag('fifth', r2.tags.detect{|t| t.name == 'third'}.objectish)
43+
44+
assert(r2.tags.detect{|t| t.name == 'third'}.objectish == r2.tags.detect{|t| t.name == 'fifth'}.objectish)
45+
46+
assert_raise Git::GitExecuteError do
47+
r2.add_tag('third')
48+
end
49+
50+
r2.add_tag('third', {:f => true})
51+
52+
r2.delete_tag('third')
53+
54+
assert_raise Git::GitTagNameDoesNotExist do
55+
r2.tag('third')
56+
end
3357
end
3458
end
35-
end
59+
end

0 commit comments

Comments
 (0)