Class: Git::Trac::Runner::Show

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#abort, #add_local_option, #add_options, #command, command, #each_attachment, #each_ticket_or_attachment, #fetch_or_abort, #fetch_unless_local, #initialize, #missing_argument, #one_attachment, #options, #parse_attachment, #repository, #system, #too_many_arguments

Constructor Details

This class inherits a constructor from Git::Trac::Runner::Base

Class Method Details

.summaryObject



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

def self.summary
  "Show an attachment or a list of attachments"
end

Instance Method Details



11
12
13
# File 'lib/git/trac/runner/show.rb', line 11

def banner_arguments
  "[<ticket> | <attachment> | branches | rebased | today]"
end

#branches(rebased = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/git/trac/runner/show.rb', line 55

def branches(rebased = false)
  output = ""
  current_checkout = repository.current_checkout
  branches = []
  repository.each_ref("refs/heads") do |object, ref|
    branches << [ref.sub(/^refs\/heads\//,''), object]
    branches.last << (rebased ? repository.diff_hash(object) : nil)
  end
  repository.each_ref("refs/remotes/trac") do |object, ref|
    current = (object == current_checkout)
    line = ref.sub(/^refs\/remotes\//,'')
    diff_hash = rebased && repository.diff_hash(object)
    branches.each do |(branch,branch_object,branch_hash)|
      if object == branch_object
        line << " <#{branch}>"
      elsif diff_hash == branch_hash
        line << " [#{branch}]"
        current ||= (branch == current_checkout)
      end
    end
    output << (current ? "* " : "  ") + line + "\n"
  end
  output
end

#descriptionObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git/trac/runner/show.rb', line 15

def description
  <<-EOF
Show the given ticket attachment.  The patch will be highlighted in a manner
If the filename is omitted, show a list of attachments for the ticket.

If the literal word "branches" is given, a list of all remote heads is output,
followed by the branches that refer to them.  A literal "rebased" also shows
branches that appear to be rebased attachments.  These rebased branches will be
shown in brackets.

Attachments can be downloaded by redirecting to a file.  The following example
shows how one might download all patches for a given ticket:

  for patch in $(git-trac show 123); do
    git-trac show $patch > $(basename $patch)
  done
  EOF
end

#runObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git/trac/runner/show.rb', line 34

def run
  if [[], %w(branches), %w(rebased)].include?(@argv)
    puts branches(@argv == %w(rebased))
    return
  elsif %w(today) == @argv
    Pager.new.page(today)
    return
  end
  each_ticket_or_attachment do |ticket|
    if ticket.respond_to?(:body)
      repository.pager(ticket.body, %w(.diff .patch).include?(ticket.extension))
    else
      body = ticket.attachments.map do |attachment|
        "#{attachment.ticket.number}/#{attachment.filename}\n"
      end.join
      exit(1) if body.empty?
      repository.pager(body)
    end
  end
end

#today(days_back = 2) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/git/trac/runner/show.rb', line 80

def today(days_back = 2)
  require 'hpricot'
  response = repository.get_response("#{repository.url}/timeline?ticket_details=on&max=#{50*days_back}&daysback=#{days_back}&format=rss")
  response.error! unless response.kind_of?(Net::HTTPSuccess)
  h = Hpricot(response.body)
  (h/:title).map {|y| y.inner_text.match(/^(.*) attached to ticket #(\d+)$/) && "#$2/#$1\n"}.compact.join
end