Class: GitLocal::Repository

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

Defined Under Namespace

Classes: InvalidArgument, InvalidProtocol, NotFound

Constant Summary collapse

GITHUB_HOST =
"github.com".freeze
VALID_PROTOCOLS =
{
  ssh: "SSH",
  https: "HTTPS"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(org:, repo:, branch:, local_directory:, host: GITHUB_HOST, protocol: VALID_PROTOCOLS[:ssh]) ⇒ Repository

Returns a new instance of Repository.



18
19
20
21
22
23
24
25
26
27
# File 'lib/git_local/repository.rb', line 18

def initialize(org:, repo:, branch:, local_directory:, host: GITHUB_HOST, protocol: VALID_PROTOCOLS[:ssh])
  check_for_special_characters(org, repo, branch, local_directory)
  @branch = branch
  @host = host
  @local_directory = local_directory
  @org = org
  @repo = repo
  @protocol = protocol
  validate_protocol
end

Instance Method Details

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



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

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



80
81
82
83
84
85
# File 'lib/git_local/repository.rb', line 80

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



33
34
35
# File 'lib/git_local/repository.rb', line 33

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

#file_objects(file_path = nil) ⇒ Object



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

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



29
30
31
# File 'lib/git_local/repository.rb', line 29

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

#local_path(file_path) ⇒ Object



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

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

#new_commit_on_remote?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/git_local/repository.rb', line 62

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



76
77
78
# File 'lib/git_local/repository.rb', line 76

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