Class: Fido

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/fido.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = Logger.new("/dev/null")) ⇒ Fido

Returns a new instance of Fido.



10
11
12
# File 'lib/fido.rb', line 10

def initialize(logger=Logger.new("/dev/null"))
  @logger = logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/fido.rb', line 8

def logger
  @logger
end

Instance Method Details

#clone(repo, *to) ⇒ Object



14
15
16
17
18
19
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
45
46
# File 'lib/fido.rb', line 14

def clone(repo, *to)
  force = to.last == true && to.pop
  to << "master"

  dir = File.basename(repo, ".git")
  mkdir_p dir

  cd dir do
    return if !force && File.exists?(".git/FIDO")

    if !File.exists?(".git")
      cmd "git init"
      cmd "git remote add origin #{repo}"
    end

    cmd "git fetch origin"

    branches = cmd("git branch -a")

    to.each do |branch|
      if branches =~ /\* #{branch}\n/
        cmd "git reset --hard origin/#{branch}"
        break
      elsif branches =~ /  remotes\/origin\/#{branch}\n/
        cmd "git branch -D #{branch}" if branches =~ /  #{branch}\n/
        cmd "git checkout -b #{branch} origin/#{branch}"
        break
      end
    end

    touch ".git/FIDO"
  end
end

#cmd(c) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fido.rb', line 48

def cmd(c)
  out, err = nil
  @logger.debug c
  Open3.popen3(c) do |_i, o, e|
    out = o.read
    err = e.read
  end
  @logger.debug out if out !~ /^\s*$/
  @logger.error err if err !~ /^\s*$/
  out
end