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!, #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
# File 'lib/snaptoken/commands/ref.rb', line 6

def self.summary
  "Convert a step number or name to a git commit reference"
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/snaptoken/commands/ref.rb', line 10

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