Class: Gitcredential

Inherits:
Object
  • Object
show all
Defined in:
lib/gitcredential.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Gitcredential

Returns a new instance of Gitcredential.



15
16
17
18
19
20
21
# 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)
end

Instance Attribute Details

#backendObject

Returns the value of attribute backend.



14
15
16
# File 'lib/gitcredential.rb', line 14

def backend
  @backend
end

Class Method Details

.default_backendObject



3
4
5
6
7
8
9
10
11
12
# 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

Instance Method Details

#cmdObject



23
24
25
# File 'lib/gitcredential.rb', line 23

def cmd
  "git-credential-#{@backend}"
end

#get(u) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/gitcredential.rb', line 41

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 = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/gitcredential.rb', line 27

def get_payload search_data={}
  u = @search_dft.merge search_data
  "protocol=\#{u[:proto]}\nhost=\#{u[:host]}\npath=\#{u[:path]}\nusername=\#{u[:user]}\n"
end

#set(u) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/gitcredential.rb', line 52

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 = {}) ⇒ Object



37
38
39
# File 'lib/gitcredential.rb', line 37

def set_payload set_data={}
  get_payload(set_data) + "password=#{set_data[:password]}\n"
end

#unset(u) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gitcredential.rb', line 62

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