Class: Git::Trac::Runner::Base

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

Overview

:nodoc:

Direct Known Subclasses

Apply, Checkout, Cleanup, Fetch, Help, Push, Show

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, repo) ⇒ Base

Returns a new instance of Base.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git/trac/runner.rb', line 99

def initialize(argv, repo)
  @argv, @repository_option = argv, repo
  @opts = OptionParser.new
  @opts.banner = "Usage: git-trac #{self.class.command} #{banner_arguments}"
  @opts.version = Git::Trac::VERSION::STRING
  @opts.base.long["help"] = OptionParser::Switch::NoArgument.new do
    help = @opts.help.chomp.chomp + "\n"
    help += "\n#{description}" if description
    Pager.new.page(help)
    exit
  end
  @opts.separator("")
  add_options(@opts)
  begin
    @opts.parse!(@argv)
  rescue OptionParser::InvalidOption
    abort $!.message
  end
  run
end

Class Method Details

.commandObject



209
210
211
# File 'lib/git/trac/runner.rb', line 209

def self.command
  name[/[^:]*$/].gsub(/(.)([A-Z])/) { $1+"-"+$2 }.downcase
end

Instance Method Details

#abort(message) ⇒ Object



179
180
181
182
# File 'lib/git/trac/runner.rb', line 179

def abort(message)
  $stderr.puts "Error: #{message}"
  exit(1)
end

#add_local_option(opts) ⇒ Object



217
218
219
220
221
# File 'lib/git/trac/runner.rb', line 217

def add_local_option(opts)
  opts.on("--[no-]local","don't run git-svn fetch") do |bool|
    options[:local] = bool
  end
end

#add_options(opts) ⇒ Object



202
203
# File 'lib/git/trac/runner.rb', line 202

def add_options(opts)
end


205
206
207
# File 'lib/git/trac/runner.rb', line 205

def banner_arguments
  "[options]"
end

#commandObject



213
214
215
# File 'lib/git/trac/runner.rb', line 213

def command
  self.class.command
end

#descriptionObject



132
133
# File 'lib/git/trac/runner.rb', line 132

def description
end

#each_attachment(abort_when_missing = true) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/git/trac/runner.rb', line 169

def each_attachment(abort_when_missing = true)
  each_ticket_or_attachment(abort_when_missing) do |object|
    if object.respond_to?(:attachments)
      object.attachments.each {|a| yield a}
    else
      yield attachment
    end
  end
end

#each_ticket_or_attachment(abort_when_missing = true) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/git/trac/runner.rb', line 156

def each_ticket_or_attachment(abort_when_missing = true)
  missing_argument if abort_when_missing && @argv.empty?
  until @argv.empty?
    number, filename = parse_attachment(@argv.shift)
    ticket = repository.ticket(number)
    if filename
      yield ticket.attachment(filename)
    else
      yield ticket
    end
  end
end

#fetch_or_abort(attachment) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/git/trac/runner.rb', line 192

def fetch_or_abort(attachment)
  branch = options[:upstream] || repository.guess_upstream || "refs/remotes/trunk"
  attachment.fetch(options) do |a, dir, commit|
    unless dir
      abort "failed to apply #{attachment.tag_name} to #{branch.sub(/^refs\/(?:remotes\/)?/,'')}"
    end
  end
  attachment
end

#fetch_unless_localObject



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/git/trac/runner.rb', line 223

def fetch_unless_local
  unless options[:local]
    if options[:fetch_command]
      system(options[:fetch_command])
    elsif repository.config("svn-remote.svn")
      system("git svn fetch")
    elsif repository.config("remote.origin")
      system("git fetch origin")
    end
  end
end

#missing_argument(label = "attachment") ⇒ Object



188
189
190
# File 'lib/git/trac/runner.rb', line 188

def missing_argument(label = "attachment")
  abort "#{label} expected but none given"
end

#one_attachment(abort_when_extra = true) ⇒ Object



149
150
151
152
153
154
# File 'lib/git/trac/runner.rb', line 149

def one_attachment(abort_when_extra = true)
  missing_argument if @argv.empty?
  number, filename = parse_attachment(@argv.shift)
  too_many_arguments if abort_when_extra && @argv.any?
  return repository.ticket(number).attachment(filename)
end

#optionsObject



128
129
130
# File 'lib/git/trac/runner.rb', line 128

def options
  @options ||= repository.config("trac") || {}
end

#parse_attachment(arg) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/git/trac/runner.rb', line 141

def parse_attachment(arg)
  if match_data = arg.to_s.match(/\b(\d+)\b(?:\/([^?\/]+))?/)
    return Integer(match_data[1]), match_data[2]
  else
    abort "invalid argument '#{arg}'"
  end
end

#repositoryObject



120
121
122
123
124
125
126
# File 'lib/git/trac/runner.rb', line 120

def repository
  @repository ||= Git::Trac::Repository.new(@repository_option)
  unless @repository.url
    abort "no URL.  Try `git config trac-remote.trac.url http://trac-url`"
  end
  @repository
end

#system(*args) ⇒ Object



135
136
137
138
139
# File 'lib/git/trac/runner.rb', line 135

def system(*args)
  repository.in_work_tree do
    Kernel.system(*args) or exit $?.exitstatus || 1
  end
end

#too_many_argumentsObject



184
185
186
# File 'lib/git/trac/runner.rb', line 184

def too_many_arguments
  abort "too many arguments"
end