Module: Stove::Git

Included in:
Cookbook
Defined in:
lib/stove/git.rb

Instance Method Summary collapse

Instance Method Details

#git(command) ⇒ String

Run a git command.

Parameters:

  • command (String)

    the command to run

Returns:

  • (String)

    the stdout from the command



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/stove/git.rb', line 12

def git(command)
  Stove::Logger.debug "shellout 'git #{command}'"
  response = shellout("git #{command}")

  Stove::Logger.debug response.stdout

  unless response.success?
    Stove::Logger.debug response.stderr
    raise Stove::GitError, response.stderr
  end

  response.stdout.strip
end

#git_remote_uptodate?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/stove/git.rb', line 47

def git_remote_uptodate?(options = {})
  git('fetch')
  local  = git("rev-parse #{options[:branch]}").strip
  remote = git("rev-parse #{options[:remote]}/#{options[:branch]}").strip

  local == remote
end

#git_repo?Boolean

Return true if the current working directory is a valid git repot, false otherwise.

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/stove/git.rb', line 30

def git_repo?
  git('rev-parse --show-toplevel')
  true
rescue
  false
end

#git_repo_clean?Boolean

Return true if the current working directory is clean,

false otherwise

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/stove/git.rb', line 41

def git_repo_clean?
  !!git('status -s').strip.empty?
rescue
  false
end

#shellout(command) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stove/git.rb', line 55

def shellout(command)
  out, err = Tempfile.new('shellout.stdout'), Tempfile.new('shellout.stderr')

  begin
    pid = Process.spawn(command, out: out.to_i, err: err.to_i)
    pid, status = Process.waitpid2(pid)

    # Check if we're getting back a process status because win32-process 6.x was a fucking MURDERER.
    # https://github.com/djberg96/win32-process/blob/master/lib/win32/process.rb#L494-L519
    exitstatus  = status.is_a?(Process::Status) ? status.exitstatus : status
  rescue Errno::ENOENT => e
    err.write('')
    err.write('Command not found: ' + command)
  end

  out.close
  err.close

  OpenStruct.new({
    exitstatus: exitstatus,
    stdout: File.read(out).strip,
    stderr: File.read(err).strip,
    success?: exitstatus == 0,
    error?: exitstatus == 0,
  })
end