Class: Grack::Git

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_path, repo_path) ⇒ Git

Returns a new instance of Git.



5
6
7
8
# File 'lib/grack/git.rb', line 5

def initialize(git_path, repo_path)
  @git_path = git_path
  @repo = repo_path
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



3
4
5
# File 'lib/grack/git.rb', line 3

def repo
  @repo
end

Instance Method Details

#capture(cmd) ⇒ Object



18
19
20
# File 'lib/grack/git.rb', line 18

def capture(cmd)
  IO.popen(popen_env, cmd, popen_options).read
end

#command(cmd) ⇒ Object



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

def command(cmd)
  [@git_path || 'git'] + cmd
end

#config(config_name) ⇒ Object



52
53
54
# File 'lib/grack/git.rb', line 52

def config(config_name)
  execute(%W(config #{config_name}))
end

#config_setting(service_name) ⇒ Object



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

def config_setting(service_name)
  service_name = service_name.gsub('-', '')
  setting = config("http.#{service_name}")

  if service_name == 'uploadpack'
    setting != 'false'
  else
    setting == 'true'
  end
end

#execute(cmd) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/grack/git.rb', line 22

def execute(cmd)
  cmd = command(cmd)
  if block_given?
    IO.popen(popen_env, cmd, File::RDWR, popen_options) do |pipe|
      yield(pipe)
    end
  else
    capture(cmd).chomp
  end
end

#popen_envObject



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

def popen_env
  { 'PATH' => ENV['PATH'], 'GL_ID' => ENV['GL_ID'] }
end

#popen_optionsObject



33
34
35
# File 'lib/grack/git.rb', line 33

def popen_options
  { chdir: repo, unsetenv_others: true }
end

#update_server_infoObject



10
11
12
# File 'lib/grack/git.rb', line 10

def update_server_info
  execute(%W(update-server-info))
end

#valid_repo?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/grack/git.rb', line 56

def valid_repo?
  return false unless File.exists?(repo) && File.realpath(repo) == repo

  match = execute(%W(rev-parse --git-dir)).match(/\.$|\.git$/)
  
  if match.to_s == '.git'
    # Since the parent could be a git repo, we want to make sure the actual repo contains a git dir.
    return false unless Dir.entries(repo).include?('.git')
  end

  match
end