Class: Zio::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/zio/branch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Branch

Returns a new instance of Branch.

Raises:



6
7
8
9
# File 'lib/zio/branch.rb', line 6

def initialize(options = {})
  @options = options
  raise MISSING_ARGUMENTS unless options[:dir] || options[:set]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/zio/branch.rb', line 5

def options
  @options
end

Instance Method Details

#_pathsObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/zio/branch.rb', line 34

def _paths
  return options[:set] unless options[:set].nil?
  @_paths ||= begin
    Dir.foreach(options[:dir]).map do |path|
      next if path == '.' || path == '..' || !File.exist?(File.expand_path(path, options[:dir]) + '/.git')
      ab_path = File.expand_path(path, options[:dir])
      File.directory?(ab_path) ? ab_path : nil
    end.compact
  end
end

#checkoutObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/zio/branch.rb', line 11

def checkout
  _paths.each do |path|
    puts sprintf("\nProcessing - #{path}").color(:green)
    puts sprintf('---------------------------').color(:green)
    stdout, stderr, status = Open3.capture3("cd #{path} && git stash && git rev-parse --verify #{options[:branch]}")

    # Branch does not exists
    if status.exitstatus != 0
      puts "Branch #{options[:branch]} does not exists yet!".color(:blue)
      if options[:force]
        puts "Creating new branch - #{options[:branch]}".color(:blue)
        stdout, stderr, status = Open3.capture3(
          "cd #{path} && git checkout #{options[:base_branch]} && git checkout -b #{options[:branch]}"
        )
        puts stdout.color(:blue)
      end
    else
      stdout, stderr, status = Open3.capture3("cd #{path} && git checkout #{options[:branch]}")
      puts sprintf("%s%s", stderr.color(:red), stdout.color(:blue))
    end
  end
end