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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

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



40
41
42
# File 'lib/gitcredential.rb', line 40

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

#get(u) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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 = {}) ⇒ Object



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

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



69
70
71
72
73
74
75
76
77
# 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 = {}) ⇒ Object



54
55
56
# File 'lib/gitcredential.rb', line 54

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

#unset(u) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# 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