Class: RightScraper::Retrievers::Git

Inherits:
CheckoutBase show all
Defined in:
lib/right_scraper/retrievers/git.rb

Overview

Retriever for resources stored in a git repository.

Constant Summary collapse

@@available =
false

Instance Attribute Summary

Attributes inherited from Base

#logger, #max_bytes, #max_seconds, #repo_dir, #repository

Instance Method Summary collapse

Methods inherited from CheckoutBase

#size_limit_exceeded?

Methods inherited from Base

#initialize, repo_dir

Constructor Details

This class inherits a constructor from RightScraper::Retrievers::Base

Instance Method Details

#available?Boolean

Determines if downloader is available.

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/right_scraper/retrievers/git.rb', line 41

def available?
  unless @@available
    begin
      cmd = "git --version"
      `#{cmd}`
      if $?.success?
        @@available = true
      else
        raise RetrieverError, "\"#{cmd}\" exited with #{$?.exitstatus}"
      end
    rescue
      @logger.note_error($!, :available, "git retriever is unavailable")
    end
  end
  @@available
end

#do_checkoutObject

Implements CheckoutBase#do_checkout



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/right_scraper/retrievers/git.rb', line 116

def do_checkout
  git_repo = @logger.operation(:cloning, "to #{@repo_dir}") do
    without_host_key_checking do
      ::RightGit::Git::Repository.clone_to(
        @repository.url,
        @repo_dir,
        :logger => git_repo_logger,
        :shell  => git_repo_shell)
    end
  end
  do_fetch(git_repo)
  do_checkout_revision(git_repo)
  internal_update_tag(git_repo)
  true
end

#do_updateObject

Implements CheckoutBase#do_update



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/right_scraper/retrievers/git.rb', line 133

def do_update
  # note that a recent fetch was performed by remote_differs? and even if
  # remotes have changed again in the brief interim it would invalidate
  # the decisions already made if we refetched now.
  git_repo = git_repo_for(@repo_dir)
  @logger.operation(:cleanup, "ensure no untracked files in #{@repo_dir}") do
    git_repo.hard_reset_to(nil)
    do_clean_all(git_repo)
  end
  do_checkout_revision(git_repo)
  do_clean_all(git_repo)  # clean again once we are on requested revision
  internal_update_tag(git_repo)
  true
end

#do_update_tagObject

Implements CheckoutBase#do_update_tag



149
150
151
152
153
154
155
# File 'lib/right_scraper/retrievers/git.rb', line 149

def do_update_tag
  git_repo = git_repo_for(@repo_dir)
  without_size_limit(git_repo) do
    internal_update_tag(git_repo)
  end
  true
end

#exists?Boolean

Return true if a checkout exists. Currently tests for .git in the checkout.

Returns ===

Boolean

true if the checkout already exists (and thus incremental updating can occur).

Returns:

  • (Boolean)


93
94
95
# File 'lib/right_scraper/retrievers/git.rb', line 93

def exists?
  File.exists?(File.join(@repo_dir, '.git'))
end

#ignorable_pathsObject

Ignore .git directories.



59
60
61
# File 'lib/right_scraper/retrievers/git.rb', line 59

def ignorable_paths
  ['.git']
end

#remote_differs?TrueClass|FalseClass

Determines if the remote SHA/tag/branch referenced by the repostory differs from what appears on disk.

Returns:

  • (TrueClass|FalseClass)

    true if changed



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/right_scraper/retrievers/git.rb', line 101

def remote_differs?
  remote_sha = nil
  current_sha = nil
  git_repo = git_repo_for(@repo_dir)
  without_size_limit(git_repo) do
    do_fetch(git_repo)
    revision = resolve_revision
    remote_name = validate_revision(git_repo, revision)
    remote_sha = git_repo.sha_for(remote_name ? remote_name : revision)
    current_sha = git_repo.sha_for(nil)
  end
  current_sha != remote_sha
end

#retrieveObject

In addition to normal retriever initialization, if the underlying repository has a credential we need to initialize a fresh SSHAgent and add the credential to it.

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/right_scraper/retrievers/git.rb', line 66

def retrieve
  raise RetrieverError.new("git retriever is unavailable") unless available?
  private_key = @repository.first_credential
  private_key = nil if private_key && private_key.empty?
  if is_windows?
    if private_key
      with_private_key_windows(private_key) { super }
    else
      super
    end
  else
    # always start the ssh agent in Linux so we can disable strict host name
    # checking, regardless of credentials.
    ::RightScraper::Processes::SSHAgent.with do |agent|
      agent.add_key(private_key) if private_key
      super
    end
  end
  true
end