Class: Git::Trac::Attachment
- Inherits:
-
Object
- Object
- Git::Trac::Attachment
- Defined in:
- lib/git/trac/attachment.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#ticket ⇒ Object
readonly
Returns the value of attribute ticket.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #body ⇒ Object
- #cleanup(options = {}) ⇒ Object
- #email ⇒ Object
- #extension ⇒ Object
- #fetch(options = {}) ⇒ Object
-
#initialize(ticket, filename, user, time, description) ⇒ Attachment
constructor
A new instance of Attachment.
- #name ⇒ Object
- #number ⇒ Object
- #patch ⇒ Object
- #repository ⇒ Object
- #tag_name ⇒ Object
- #tag_path ⇒ Object
- #timestamp ⇒ Object
- #url(format = :raw) ⇒ Object
- #username ⇒ Object
Constructor Details
#initialize(ticket, filename, user, time, description) ⇒ Attachment
Returns a new instance of Attachment.
9 10 11 12 |
# File 'lib/git/trac/attachment.rb', line 9 def initialize(ticket, filename, user, time, description) @ticket, @filename, @user, @time = ticket, filename, user, time @description = description end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/git/trac/attachment.rb', line 7 def description @description end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
6 7 8 |
# File 'lib/git/trac/attachment.rb', line 6 def filename @filename end |
#ticket ⇒ Object (readonly)
Returns the value of attribute ticket.
6 7 8 |
# File 'lib/git/trac/attachment.rb', line 6 def ticket @ticket end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
6 7 8 |
# File 'lib/git/trac/attachment.rb', line 6 def time @time end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
6 7 8 |
# File 'lib/git/trac/attachment.rb', line 6 def user @user end |
Class Method Details
.from_html(ticket, html) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/git/trac/attachment.rb', line 14 def self.from_html(ticket, html) require 'time' unesc = lambda do |string| string.gsub(/&(\w+);/) { {"lt"=>"<","rt"=>">","amp"=>"&"}[$1]||$& } end html.scan(%r{<dt><a .*?>(.*?)</a>.*? <em>(.*?)</em>\s*on\s+(.*?)\.</dt>\s*(?:<dd>(.*?)</dd>)?}).map do |(filename, user, time, desc)| time = Time.parse("#{time} +0000").utc new(ticket, unesc[filename], unesc[user], time, desc && unesc[desc]) end end |
Instance Method Details
#body ⇒ Object
68 69 70 |
# File 'lib/git/trac/attachment.rb', line 68 def body @body ||= repository.agent.get_file(url(:raw)) end |
#cleanup(options = {}) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/git/trac/attachment.rb', line 80 def cleanup( = {}) repository.each_ref("refs/remotes/#{tag_name}") do |object, ref| repository.exec("git","update-ref","-d",ref,object) unless [:only_branches] repository.cleanup_branches(, object) return object end end |
#email ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/git/trac/attachment.rb', line 41 def email if user =~ /<(\S*@\S*)>/ return $1 elsif user =~ /\S*@\S*/ return user else [user, repository.url[%r{://([^/]*)},1]].compact.join("@") end end |
#extension ⇒ Object
25 26 27 |
# File 'lib/git/trac/attachment.rb', line 25 def extension File.extname(@filename) end |
#fetch(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/git/trac/attachment.rb', line 88 def fetch( = {}) cleanup() FileUtils.mkdir_p(ticket.trac_dir) repository.with_index("tracindex#{$$}") do parent = repository.exec("git","rev-list","--max-count=1","--before=#{}",[:upstream] || 'refs/remotes/trunk').chomp repository.exec("git","read-tree",parent) applied = patch.apply(.merge(:cached => true)) unless applied yield self, nil, nil return end tree = repository.exec("git","write-tree").chomp ENV["GIT_AUTHOR_NAME"] = ENV["GIT_COMMITTER_NAME"] = username ENV["GIT_AUTHOR_EMAIL"] = ENV["GIT_COMMITTER_EMAIL"] = email ENV["GIT_AUTHOR_DATE"] = ENV["GIT_COMMITTER_DATE"] = commit = repository.popen3("git commit-tree #{tree} -p #{parent}") do |i,o,e| i.puts "trac/#{ticket.number}/#{filename}" i.puts "\n#{description}" if description i.close o.read.chomp end File.open(tag_path,"w") do |f| f.puts commit end yield self, applied, commit return commit end ensure %w(GIT_AUTHOR_NAME GIT_COMMITTER_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_EMAIL GIT_AUTHOR_DATE GIT_COMMITTER_DATE).each {|e| ENV[e] = nil} end |
#name ⇒ Object
29 30 31 |
# File 'lib/git/trac/attachment.rb', line 29 def name @filename[/^[^.]*/] end |
#number ⇒ Object
33 34 35 |
# File 'lib/git/trac/attachment.rb', line 33 def number @filename[/\.(\d+)\./,1] end |
#patch ⇒ Object
76 77 78 |
# File 'lib/git/trac/attachment.rb', line 76 def patch @patch ||= Git::Trac::Patch.new(repository, lambda { body }) end |
#repository ⇒ Object
72 73 74 |
# File 'lib/git/trac/attachment.rb', line 72 def repository ticket.repository end |
#tag_name ⇒ Object
60 61 62 |
# File 'lib/git/trac/attachment.rb', line 60 def tag_name "trac/#{ticket.number}/#{filename}" end |
#tag_path ⇒ Object
64 65 66 |
# File 'lib/git/trac/attachment.rb', line 64 def tag_path "#{ticket.repository.git_dir}/refs/remotes/#{tag_name}" end |
#timestamp ⇒ Object
51 52 53 |
# File 'lib/git/trac/attachment.rb', line 51 def time.utc.strftime("%Y-%m-%d_%H:%M:%S_+0000") end |
#url(format = :raw) ⇒ Object
55 56 57 58 |
# File 'lib/git/trac/attachment.rb', line 55 def url(format = :raw) query = "?format=#{format}" if format "#{ticket.repository.url}/attachment/ticket/#{ticket.number}/#{filename}#{query}" end |
#username ⇒ Object
37 38 39 |
# File 'lib/git/trac/attachment.rb', line 37 def username user.to_s[/[^\s@]+/] end |