Class: Snaptoken::Commands::Unrepo

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

Constant Summary

Constants inherited from BaseCommand

BaseCommand::ERROR_MSG

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

inherited, #initialize, #latest_step, #needs!, #parseopts!, #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/unrepo.rb', line 2

def self.name
  "unrepo"
end

.summaryObject



6
7
8
9
# File 'lib/snaptoken/commands/unrepo.rb', line 6

def self.summary
  "Convert repo/ to steps/. Doesn't overwrite\n" +
  "steps/ unless forced."
end

.usageObject



11
12
13
# File 'lib/snaptoken/commands/unrepo.rb', line 11

def self.usage
  "[-f] [-q]"
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/snaptoken/commands/unrepo.rb', line 25

def run
  needs! :config, :repo

  FileUtils.cd(@config[:path]) do
    if @opts[:force]
      FileUtils.rm_rf("steps")
    else
      needs! not: :steps_folder
    end

    FileUtils.mkdir("steps")

    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|
      break if commit.message.lines.first.strip == "-"

      step = Snaptoken::Step.from_commit_msg(idx + 1, commit.message.lines.first.strip)
      print "\r\e[K[repo/ -> steps/] #{step.folder_name}" unless @opts[:quiet]

      repo.checkout(commit.oid, strategy: :force,
                                target_directory: step_path(step))
    end
    print "\n" unless @opts[:quiet]
  end
end

#setopts!(o) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/snaptoken/commands/unrepo.rb', line 15

def setopts!(o)
  o.on("-f", "--force", "Overwrite steps/ folder") do |f|
    @opts[:force] = f
  end

  o.on("-q", "--quiet", "Don't output progress") do |q|
    @opts[:quiet] = q
  end
end