Class: Raykit::Git::Repository

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

Overview

Functionality to manage a remote git repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Repository

Returns a new instance of Repository.



11
12
13
14
15
# File 'lib/raykit/git/repository.rb', line 11

def initialize(url)
  @url = url
  @clone_directory = Raykit::Git::Directory.new(get_dev_dir("clone"))
  @work_directory = Raykit::Git::Directory.new(get_dev_dir("work"))
end

Instance Attribute Details

#clone_directoryObject

Returns the value of attribute clone_directory.



9
10
11
# File 'lib/raykit/git/repository.rb', line 9

def clone_directory
  @clone_directory
end

#urlObject

The url of the remote repository



8
9
10
# File 'lib/raykit/git/repository.rb', line 8

def url
  @url
end

#work_directoryObject

Returns the value of attribute work_directory.



9
10
11
# File 'lib/raykit/git/repository.rb', line 9

def work_directory
  @work_directory
end

Class Method Details

.parse(json) ⇒ Object



21
22
23
24
# File 'lib/raykit/git/repository.rb', line 21

def self.parse(json)
  hash = JSON.parse(json)
  Repository.new(hash["url"])
end

Instance Method Details

#branchesObject

The branches for the git repository



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

def branches
  results = []
  update_local_clone_directory
  if Dir.exist?(local_clone_directory)
    Dir.chdir(local_clone_directory) do
      `git branch`.split('\n').each do |line|
        branch = line.gsub("*", "").strip
        results.insert(-1, branch) if branch.length.positive?
      end
    end
  end
  results
end

#clone(directory, depth = 0) ⇒ Object

Clone the repository to a specific directory



37
38
39
40
41
42
43
# File 'lib/raykit/git/repository.rb', line 37

def clone(directory, depth = 0)
  if depth.zero?
    PROJECT.run("git clone #{@url} #{directory}")
  else
    PROJECT.run("git clone #{@url} #{directory} --depth #{depth}")
  end
end

#get_dev_dir(dir) ⇒ Object



31
32
33
34
# File 'lib/raykit/git/repository.rb', line 31

def get_dev_dir(dir)
  dev_dir = Environment.get_dev_dir(dir)
  "#{dev_dir}/#{relative_path}"
end

#latest_commit(branch) ⇒ Object

The latest commit id for a branch of the repostiory



61
62
63
64
65
66
67
68
# File 'lib/raykit/git/repository.rb', line 61

def latest_commit(branch)
  if checkout_local_clone_directory_branch(branch)
    text = `git log -n 1`
    scan = text.scan(/commit (\w+)\s/)
    return scan[0][0].to_s
  end
  ""
end

#latest_tag(branch) ⇒ Object

The latest tag for a branch of the repository



71
72
73
74
75
# File 'lib/raykit/git/repository.rb', line 71

def latest_tag(branch)
  return `git describe --abbrev=0`.strip if checkout_local_clone_directory_branch(branch)

  ""
end

#relative_pathObject

The relative path is a valid local path name derived from the url



27
28
29
# File 'lib/raykit/git/repository.rb', line 27

def relative_path
  @url.gsub("https://", "").gsub(".git", "").gsub("http://", "")
end

#to_json(*_args) ⇒ Object



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

def to_json(*_args)
  JSON.generate({ "url" => @url })
end