Class: Fido

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

Instance Method Summary collapse

Instance Method Details

#clone(repo, *to) ⇒ Object



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
35
36
37
38
39
40
41
42
# File 'lib/fido.rb', line 10

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



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fido.rb', line 44

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