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
21
22
23
# File 'lib/grack/git.rb', line 18

def capture(cmd)
  # _Not_ the same as `IO.popen(...).read`
  # By using a block we tell IO.popen to close (wait for) the child process
  # after we are done reading its output.
  IO.popen(popen_env, cmd, popen_options) { |p| p.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



55
56
57
# File 'lib/grack/git.rb', line 55

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

#config_setting(service_name) ⇒ Object



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

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



25
26
27
28
29
30
31
32
33
34
# File 'lib/grack/git.rb', line 25

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



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

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

#popen_optionsObject



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

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)


59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/grack/git.rb', line 59

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