Class: Snaptoken::Commands::Ref

Inherits:
BaseCommand show all
Defined in:
lib/snaptoken/commands/ref.rb

Constant Summary

Constants inherited from BaseCommand

BaseCommand::ERROR_MSG

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#current_or_latest_step, #current_step, inherited, #initialize, #latest_step, #needs!, #parseopts!, #select_step, #step_path, #steps

Constructor Details

This class inherits a constructor from Snaptoken::Commands::BaseCommand

Class Method Details

.nameObject



2
3
4
# File 'lib/snaptoken/commands/ref.rb', line 2

def self.name
  "ref"
end

.summaryObject



6
7
8
9
10
11
# File 'lib/snaptoken/commands/ref.rb', line 6

def self.summary
  "Get the commit hash in repo/ for a step\n" +
  "name or step number. `leg <step-number>`\n" +
  "can be used as a shortcut for\n" +
  "`leg ref <step-number>`."
end

.usageObject



13
14
15
# File 'lib/snaptoken/commands/ref.rb', line 13

def self.usage
  "[<step-name> | <step-number>]"
end

Instance Method Details

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/snaptoken/commands/ref.rb', line 20

def run
  needs! :config, :repo

  ref = @args.first
  is_num = (ref =~ /\A\d+\z/)

  FileUtils.cd(@config[:path]) do
    repo = Rugged::Repository.new("repo")

    walker = Rugged::Walker.new(repo)
    walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
    walker.push(repo.branches.find { |b| b.name == "master" }.target)
    walker.each.with_index do |commit, idx|
      step = Snaptoken::Step.from_commit_msg(idx + 1, commit.message.lines.first.strip)

      if (is_num && ref.to_i == step.number) || (!is_num && ref == step.name)
        puts commit.oid
        exit
      end
    end

    puts "Error: reference not found"
    exit!
  end
end

#setopts!(o) ⇒ Object



17
18
# File 'lib/snaptoken/commands/ref.rb', line 17

def setopts!(o)
end