class Gitcredential

Attributes

backend[RW]

Public Class Methods

default_backend() click to toggle source
# File lib/gitcredential.rb, line 3
def self.default_backend
  case RUBY_PLATFORM
  when /-darwin[\d\.]+\z/
    "osxkeychain"
  when /win32/
    "winstore"
  else
    "cache"
  end
end
new(args = {}) click to toggle source
# File lib/gitcredential.rb, line 15
def initialize args = {}
  @valid_backends = ["osxkeychain", "winstore", "cache", "store", "default"]
  @search_dft = {:proto => "https", :path => "/"}
  @backend = args[:backend] || Gitcredential.default_backend

  raise Exception "no such backend" unless @valid_backends.include?(@backend)

  begin
    `#{cmd} 2>/dev/null`
  rescue Errno::ENOENT
    ["/usr/local/lib/git-core", "/usr/local/libexec/git-core",
     "/usr/lib/git-core", "/usr/libexec/git-core",
     "/Library/Developer/CommandLineTools/usr/libexec/git-core"].each do |dir|
      found=false
      if File.exists? "#{dir}/#{cmd}"
        ENV["PATH"] += ":#{dir}"
        found=true
        break
      end
      raise "can not find backend helper" unless found
    end
  end

end

Public Instance Methods

cmd() click to toggle source
# File lib/gitcredential.rb, line 40
def cmd
  "git-credential-#{@backend}"
end
get(u) click to toggle source
# File lib/gitcredential.rb, line 58
def get u
  out = nil
  IO.popen("#{cmd} get", mode='r+') { |fd|
    fd.write(get_payload u)
    fd.close_write
    out = fd.read
  }
  return nil if (out.nil? or out.empty?)
  out.sub!(/^password=/, '').chomp
end
get_payload(search_data={}) click to toggle source
# File lib/gitcredential.rb, line 44
  def get_payload search_data={}
    u = @search_dft.merge search_data
    <<-__EOS
protocol=#{u[:proto]}
host=#{u[:host]}
path=#{u[:path]}
username=#{u[:user]}
__EOS
  end
set(u) click to toggle source
# File lib/gitcredential.rb, line 69
def set u
  unset(u) unless get(u).nil?
  IO.popen("#{cmd} store", mode='r+') { |fd|
    fd.write(set_payload u)
    fd.close_write
  }
  raise "can't save pw for #{u[:user]}" if get(u).nil?
  true
end
set_payload(set_data={}) click to toggle source
# File lib/gitcredential.rb, line 54
def set_payload set_data={}
  get_payload(set_data) + "password=#{set_data[:password]}\n"
end
unset(u) click to toggle source
# File lib/gitcredential.rb, line 79
def unset u
  return true if get(u).nil?

  IO.popen("#{cmd} erase", mode='r+') { |fd|
    fd.write(get_payload u)
    fd.close_write
  }

  raise "deletion error on #{u[:user]}" unless get(u).nil?
  true
end