Class: Gel::GitDepot

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

Constant Summary collapse

Logger =
::Logger.new($stderr)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store, mirror_root = (ENV["GEL_CACHE"] || "~/.cache/gel") + "/git") ⇒ GitDepot

Returns a new instance of GitDepot.



10
11
12
13
# File 'lib/gel/git_depot.rb', line 10

def initialize(store, mirror_root = (ENV["GEL_CACHE"] || "~/.cache/gel") + "/git")
  @store = store
  @mirror_root = File.expand_path(mirror_root)
end

Instance Attribute Details

#mirror_rootObject (readonly)

Returns the value of attribute mirror_root.



4
5
6
# File 'lib/gel/git_depot.rb', line 4

def mirror_root
  @mirror_root
end

Instance Method Details

#checkout(remote, revision) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/gel/git_depot.rb', line 59

def checkout(remote, revision)
  destination = git_path(remote, revision)
  return destination if Dir.exist?(destination)

  mirror = remote(remote) do |cache_dir|
    # Check whether the revision is already in our mirror
    status = git(remote, "rev-list", "--quiet", revision, chdir: cache_dir)
    status.success?
  end

  status = git(remote, "clone", mirror, destination)
  raise "git clone --local failed" unless status.success?

  status = git(remote, "checkout", "--detach", "--force", revision, chdir: destination)
  raise "git checkout failed" unless status.success?

  destination
end

#git_path(remote, revision) ⇒ Object



15
16
17
18
# File 'lib/gel/git_depot.rb', line 15

def git_path(remote, revision)
  short = File.basename(remote, ".git")
  File.join(@store.root, "git", "#{short}-#{revision[0..12]}")
end

#remote(remote) ⇒ Object

Returns a path containing a local mirror of the given remote.

If the mirror already exists, yields the path (same as the return value); if the block returns false the mirror will be updated.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gel/git_depot.rb', line 24

def remote(remote)
  cache_dir = "#{@mirror_root}/#{ident(remote)}"

  if Dir.exist?(cache_dir)
    if block_given? && !yield(cache_dir)
      # The block didn't like what it saw; try updating the mirror
      # from upstream
      status = git(remote, "remote", "update", chdir: cache_dir)
      raise "git remote update failed" unless status.success?
    end
  else
    status = git(remote, "clone", "--mirror", remote, cache_dir)
    raise "git clone --mirror failed" unless status.success?
  end

  cache_dir
end

#resolve(remote, ref) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gel/git_depot.rb', line 42

def resolve(remote, ref)
  mirror = remote(remote) { false } # always update mirror

  r, w = IO.pipe
  status = git(remote, "rev-parse", ref || "HEAD", chdir: mirror, out: w)
  raise "git rev-parse failed" unless status.success?

  w.close

  r.read.chomp
end

#resolve_and_checkout(remote, ref) ⇒ Object



54
55
56
57
# File 'lib/gel/git_depot.rb', line 54

def resolve_and_checkout(remote, ref)
  revision = resolve(remote, ref)
  [revision, checkout(remote, revision)]
end