Class: Gitoc::Repository

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, url = nil) ⇒ Repository

Returns a new instance of Repository.



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

def initialize path, url = nil
  @path = Pathname.new(path).expand_path
  @url = url
end

Class Attribute Details

.baseObject

Returns the value of attribute base.



6
7
8
# File 'lib/gitoc/repository.rb', line 6

def base
  @base
end

Instance Attribute Details

#pathObject (readonly)

Path to the git repository



14
15
16
# File 'lib/gitoc/repository.rb', line 14

def path
  @path
end

Class Method Details

.load(attribute) ⇒ Object



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

def load attribute
  new base.join(attribute[:path]), attribute[:url]
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  self.to_hash == other.to_hash
end

#cloneObject



51
52
53
54
55
# File 'lib/gitoc/repository.rb', line 51

def clone
  return unless url?
  path.parent.mkpath
  run_in path.parent, "git clone #{url}"
end

#exist?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/gitoc/repository.rb', line 36

def exist?
  path.exist?
end

#pullObject



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

def pull
  return unless exist?
  run_in path, "git pull"
end

#rel_pathObject



25
26
27
# File 'lib/gitoc/repository.rb', line 25

def rel_path
  @rel_path ||= path.relative_path_from(self.class.base)
end

#run_in(path, cmd) ⇒ Object



62
63
64
65
66
# File 'lib/gitoc/repository.rb', line 62

def run_in path, cmd
  Dir.chdir path do
    Open3.capture2 cmd
  end
end

#to_hashObject



29
30
31
32
33
34
# File 'lib/gitoc/repository.rb', line 29

def to_hash
  {
    path: rel_path.to_s,
    url: url
  }
end

#urlObject



44
45
46
47
48
49
# File 'lib/gitoc/repository.rb', line 44

def url
  @url ||= begin
    out, _status = run_in path, "git config remote.origin.url"
    out.chomp
  end
end

#url?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/gitoc/repository.rb', line 40

def url?
  !(url.nil? || url.empty?)
end