Class: Git::Trac::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/git/trac/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/git/trac/attachment.rb', line 7

def description
  @description
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/git/trac/attachment.rb', line 6

def filename
  @filename
end

#ticketObject (readonly)

Returns the value of attribute ticket.



6
7
8
# File 'lib/git/trac/attachment.rb', line 6

def ticket
  @ticket
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/git/trac/attachment.rb', line 6

def time
  @time
end

#userObject (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

#bodyObject



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(options = {})
  repository.each_ref("refs/remotes/#{tag_name}") do |object, ref|
    repository.exec("git","update-ref","-d",ref,object) unless options[:only_branches]
    repository.cleanup_branches(options, object)
    return object
  end
end

#emailObject



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

#extensionObject



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(options = {})
  cleanup(options)
  FileUtils.mkdir_p(ticket.trac_dir)
  repository.with_index("tracindex#{$$}") do
    parent = repository.exec("git","rev-list","--max-count=1","--before=#{timestamp}",options[:upstream] || 'refs/remotes/trunk').chomp
    repository.exec("git","read-tree",parent)
    applied = patch.apply(options.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"] = timestamp
    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

#nameObject



29
30
31
# File 'lib/git/trac/attachment.rb', line 29

def name
  @filename[/^[^.]*/]
end

#numberObject



33
34
35
# File 'lib/git/trac/attachment.rb', line 33

def number
  @filename[/\.(\d+)\./,1]
end

#patchObject



76
77
78
# File 'lib/git/trac/attachment.rb', line 76

def patch
  @patch ||= Git::Trac::Patch.new(repository, lambda { body })
end

#repositoryObject



72
73
74
# File 'lib/git/trac/attachment.rb', line 72

def repository
  ticket.repository
end

#tag_nameObject



60
61
62
# File 'lib/git/trac/attachment.rb', line 60

def tag_name
  "trac/#{ticket.number}/#{filename}"
end

#tag_pathObject



64
65
66
# File 'lib/git/trac/attachment.rb', line 64

def tag_path
  "#{ticket.repository.git_dir}/refs/remotes/#{tag_name}"
end

#timestampObject



51
52
53
# File 'lib/git/trac/attachment.rb', line 51

def timestamp
  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

#usernameObject



37
38
39
# File 'lib/git/trac/attachment.rb', line 37

def username
  user.to_s[/[^\s@]+/]
end