Class: GitLocal::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_local/repository.rb

Defined Under Namespace

Classes: NotFound

Instance Method Summary collapse

Constructor Details

#initialize(org:, repo:, branch:, local_directory:) ⇒ Repository

Returns a new instance of Repository.



6
7
8
9
10
11
# File 'lib/git_local/repository.rb', line 6

def initialize(org:, repo:, branch:, local_directory:)
  @org = org
  @repo = repo
  @branch = branch
  @local_directory = local_directory
end

Instance Method Details

#all_file_objects(file_path = nil, include_dirs = false) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/git_local/repository.rb', line 32

def all_file_objects(file_path = nil, include_dirs = false)
  repo_path = file_path.nil? ? path : File.join(path, file_path)
  searchable_repo_path = repo_path.end_with?("/") ? repo_path : "#{repo_path}/"
  Dir.glob("#{searchable_repo_path}**/*").each_with_object([]) do |filename, git_files|
    if !File.directory?(filename) || include_dirs
      git_files << GitLocal::Object.new(filename)
    end
  end
end

#file_object(file_path) ⇒ Object



17
18
19
# File 'lib/git_local/repository.rb', line 17

def file_object(file_path)
  GitLocal::Object.new(File.join(path, file_path))
end

#file_objects(file_path = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/git_local/repository.rb', line 21

def file_objects(file_path = nil)
  repo_path = file_path.nil? ? path : File.join(path, file_path)
  searchable_repo_path = repo_path.end_with?("/") ? repo_path : "#{repo_path}/"

  Dir.glob("#{searchable_repo_path}*").each_with_object([]) do |filename, git_files|
    next if %w(. ..).include?(filename) || File.extname(filename) == ".zip" || File.directory?(filename)

    git_files << GitLocal::Object.new(filename)
  end
end

#getObject



13
14
15
# File 'lib/git_local/repository.rb', line 13

def get
  Dir.exist?(path) && new_commit_on_remote? ? pull : clone_and_checkout
end

#local_path(file_path) ⇒ Object



42
43
44
# File 'lib/git_local/repository.rb', line 42

def local_path(file_path)
  file_path.gsub("#{path}/", "")
end

#new_commit_on_remote?Boolean

Returns:

  • (Boolean)

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/git_local/repository.rb', line 46

def new_commit_on_remote?
  popened_io = IO.popen("(cd #{path} && git rev-parse HEAD)")
  head = popened_io.read.chomp.split("\n").last
  Process.wait(popened_io.pid)

  popened_io = IO.popen("(cd #{path} && git remote update && git rev-parse origin/#{branch})")
  remote = popened_io.read.chomp
  Process.wait(popened_io.pid)
  raise NotFound unless $?.to_i == 0

  remote != head
end

#pathObject



59
60
61
# File 'lib/git_local/repository.rb', line 59

def path
  @path ||= "#{local_directory}/#{org_repo_branch}"
end