Class: Git::Trac::Runner::Checkout

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fetchable

#after_fetch

Methods inherited from Base

#abort, #add_local_option, 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



9
10
11
# File 'lib/git/trac/runner/checkout.rb', line 9

def self.summary
  "Fetch an attachment and check it out into a branch"
end

Instance Method Details

#add_options(opts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/git/trac/runner/checkout.rb', line 29

def add_options(opts)
  @checkout_options = []
  super
  opts.on("--rebase","rebase after checkout") do
    options[:rebase] = true
  end
  opts.on("-q","git checkout -q") do
    @checkout_options << "-f"
  end
  opts.on("-f","git checkout -f") do
    @checkout_options << "-f"
  end
  opts.on("-m","git checkout -m") do
    @checkout_options << "-m"
  end
end


13
14
15
# File 'lib/git/trac/runner/checkout.rb', line 13

def banner_arguments
  "[options] <attachment> [<branchname>]"
end

#descriptionObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git/trac/runner/checkout.rb', line 17

def description
  <<-EOF
Fetch the specified attachment and check it out into a new branch.  If no
branch name is given, one is generated automatically from the attachment
filename.

If the branch already exists, it will be deleted if it contains either the same
exact changes as the attachment, or no changes at all, with respect to
upstream.
  EOF
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/git/trac/runner/checkout.rb', line 46

def run
  options[:upstream] ||= repository.guess_upstream || "refs/remotes/trunk"
  fetch_unless_local
  attachment = one_attachment(false)
  branch = @argv.shift || attachment.name
  too_many_arguments if @argv.any?
  fetch_or_abort(attachment)

  to_be_deleted = nil
  repository.each_ref("refs/heads/#{branch}") do |object,ref|
    hash = repository.diff_hash(object)
    if hash == Digest::SHA1.digest("") || hash == repository.diff_hash(attachment.tag_name)
      to_be_deleted = "#{branch}_tmp_git-trac_#{$$}"
      repository.exec("git","branch","-m",branch,to_be_deleted)
    end
  end

  repository.in_work_tree do
    unless Kernel.system("git","checkout",*@checkout_options + ["-b",branch,attachment.tag_name])
      exitstatus = $?.exitstatus
      if to_be_deleted
        repository.exec("git","branch","-m",to_be_deleted,branch)
      end
      exit exitstatus
    end
  end

  repository.exec("git","branch","-D",to_be_deleted) if to_be_deleted
  system("git","rebase",options[:upstream]) if options[:rebase]
end