Class: ThemeStore::GitImporter

Inherits:
BaseImporter show all
Defined in:
lib/theme_store/git_importer.rb

Constant Summary collapse

COMMAND_TIMEOUT_SECONDS =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseImporter

#[], #all_files, #cleanup!, #file_size, #real_path, #temp_folder

Constructor Details

#initialize(url, private_key: nil, branch: nil) ⇒ GitImporter

Returns a new instance of GitImporter.



8
9
10
11
12
# File 'lib/theme_store/git_importer.rb', line 8

def initialize(url, private_key: nil, branch: nil)
  @url = GitUrl.normalize(url)
  @private_key = private_key
  @branch = branch
end

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/theme_store/git_importer.rb', line 6

def url
  @url
end

Instance Method Details

#commits_since(hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/theme_store/git_importer.rb', line 37

def commits_since(hash)
  commit_hash, commits_behind = nil

  commit_hash = execute("git", "rev-parse", "HEAD").strip
  commits_behind =
    begin
      execute("git", "rev-list", "#{hash}..HEAD", "--count").strip
    rescue StandardError
      -1
    end

  [commit_hash, commits_behind]
end

#import!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/theme_store/git_importer.rb', line 14

def import!
  clone!

  if version = Discourse.find_compatible_git_resource(temp_folder)
    begin
      execute "git", "cat-file", "-e", version
    rescue RuntimeError => e
      tracking_ref =
        execute "git", "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"
      remote_name = tracking_ref.split("/", 2)[0]
      execute "git", "fetch", remote_name, "#{version}:#{version}"
    end

    begin
      execute "git", "reset", "--hard", version
    rescue RuntimeError
      raise RemoteTheme::ImportError.new(
              I18n.t("themes.import_error.git_ref_not_found", ref: version),
            )
    end
  end
end

#versionObject



51
52
53
# File 'lib/theme_store/git_importer.rb', line 51

def version
  execute("git", "rev-parse", "HEAD").strip
end