Class: GitLocal::Repository

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

Defined Under Namespace

Classes: InvalidArgument, NotFound

Constant Summary collapse

GITHUB_HOST =
"github.com".freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Repository.



11
12
13
14
15
16
17
18
# File 'lib/git_local/repository.rb', line 11

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

Instance Method Details

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



39
40
41
42
43
44
45
46
47
# File 'lib/git_local/repository.rb', line 39

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



71
72
73
74
75
76
# File 'lib/git_local/repository.rb', line 71

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



24
25
26
# File 'lib/git_local/repository.rb', line 24

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

#file_objects(file_path = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/git_local/repository.rb', line 28

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



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

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

#local_path(file_path) ⇒ Object



49
50
51
# File 'lib/git_local/repository.rb', line 49

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

#new_commit_on_remote?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git_local/repository.rb', line 53

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



67
68
69
# File 'lib/git_local/repository.rb', line 67

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