Class: GitLocal::Repository

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

Defined Under Namespace

Classes: InvalidArgument, NotFound

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Repository.



9
10
11
12
13
14
15
# File 'lib/git_local/repository.rb', line 9

def initialize(org:, repo:, branch:, local_directory:)
  check_for_special_characters(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



36
37
38
39
40
41
42
43
44
# File 'lib/git_local/repository.rb', line 36

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

#check_for_special_characters(*args) ⇒ Object



68
69
70
71
72
73
# File 'lib/git_local/repository.rb', line 68

def check_for_special_characters(*args)
  regexp = Regexp.new(/([A-Za-z0-9\-\_\.\/#]+)/)
  args.each do |arg|
    raise InvalidArgument unless arg.gsub(regexp, "").empty?
  end
end

#file_object(file_path) ⇒ Object



21
22
23
# File 'lib/git_local/repository.rb', line 21

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

#file_objects(file_path = nil) ⇒ Object



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

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



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

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

#local_path(file_path) ⇒ Object



46
47
48
# File 'lib/git_local/repository.rb', line 46

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

#new_commit_on_remote?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/git_local/repository.rb', line 50

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}) 2>&1")
  out = popened_io.map(&:chomp) || []
  remote = popened_io.read.chomp
  Process.wait(popened_io.pid)
  raise NotFound.new.exception(out.join(" ")) unless $?.to_i == 0

  remote != head
end

#pathObject



64
65
66
# File 'lib/git_local/repository.rb', line 64

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