class Honcho::AppStatus

Constants

FETCH_ORIGIN_TIMEOUT
REMOTE_REF_STALE_TIME

Public Class Methods

new(name, path:) click to toggle source
# File lib/honcho/app_status.rb, line 6
def initialize(name, path:)
  @name = name
  @path = path
end

Public Instance Methods

data() click to toggle source
# File lib/honcho/app_status.rb, line 11
def data
  return {} unless path_exists?
  threads = [
    Thread.new { @sha1          = fetch_sha1          },
    Thread.new { @branch        = fetch_branch        },
    Thread.new { @commits_ahead = fetch_commits_ahead }
  ]
  threads.each(&:join)
  {
    sha1:          @sha1,
    branch:        @branch,
    commits_ahead: @commits_ahead
  }
end

Private Instance Methods

fetch_branch() click to toggle source
# File lib/honcho/app_status.rb, line 32
def fetch_branch
  `cd #{@path} && git symbolic-ref --short HEAD 2>/dev/null`.strip
end
fetch_commits_ahead() click to toggle source
# File lib/honcho/app_status.rb, line 39
def fetch_commits_ahead
  remote_branch = `cd #{@path} && git rev-parse --symbolic-full-name --abbrev-ref @{u} 2>/dev/null`.strip
  remote_ref_path = File.join(@path, ".git/refs/remotes/#{remote_branch}")
  return 0 unless File.exist?(remote_ref_path)
  if File.stat(remote_ref_path).mtime < Time.now - REMOTE_REF_STALE_TIME
    begin
      status = Timeout.timeout(FETCH_ORIGIN_TIMEOUT) do
        `cd #{@path} && git fetch origin && git status && touch #{remote_ref_path}`
      end
    rescue Timeout::Error
      status = `cd #{@path} && git status`
    end
  else
    status = `cd #{@path} && git status`
  end
  return 0 unless status =~ /Your branch is behind.*by (\d+) commits?/
  $1.to_i
end
fetch_sha1() click to toggle source
# File lib/honcho/app_status.rb, line 28
def fetch_sha1
  `cd #{@path} && git rev-parse HEAD 2>/dev/null`.strip
end
path_exists?() click to toggle source
# File lib/honcho/app_status.rb, line 58
def path_exists?
  File.exist?(@path)
end