Class: Gitw::Repository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gitw/repository.rb

Overview

git repository service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = '.', **options) ⇒ Repository

Returns a new instance of Repository.



14
15
16
17
18
# File 'lib/gitw/repository.rb', line 14

def initialize(base_dir = '.', **options)
  @base_dir = File.expand_path(base_dir)
  @git_dir = options[:git_dir] || options['git_dir']
  @worktree_dir = options[:worktree_dir] || options['worktree_dir']
end

Instance Attribute Details

#base_dirObject (readonly)

Returns the value of attribute base_dir.



12
13
14
# File 'lib/gitw/repository.rb', line 12

def base_dir
  @base_dir
end

Class Method Details

.at(directory = '.', **options) ⇒ Object

self end



184
185
186
187
188
189
# File 'lib/gitw/repository.rb', line 184

def self.at(directory = '.', **options)
  repository = new(directory, **options)
  return nil unless repository.in_repository?

  repository
end

.clone(from, to = nil, **options) ⇒ Object



195
196
197
198
199
200
201
# File 'lib/gitw/repository.rb', line 195

def self.clone(from, to = nil, **options)
  return unless from

  from_url = Gitw::Git::URL.new(from)
  to ||= from_url.basename
  new(to, **options).clone_from(from_url, **options)
end

.init(directory = '.', **options) ⇒ Object



191
192
193
# File 'lib/gitw/repository.rb', line 191

def self.init(directory = '.', **options)
  new(directory, **options).init(**options)
end

Instance Method Details

#absolute_git_dirObject



53
54
55
56
57
# File 'lib/gitw/repository.rb', line 53

def absolute_git_dir
  git.absolute_git_dir
rescue Gitw::GitError
  nil
end

#bare?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
# File 'lib/gitw/repository.rb', line 59

def bare?
  git.is_bare_repository
rescue Gitw::GitError
  false
end

#clone_from(url, **options) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/gitw/repository.rb', line 82

def clone_from(url, **options)
  FileUtils.mkdir_p base_dir
  git(git_base_options).clone(url.to_s, base_dir, **options)

  self
rescue Errno::EACCES, Gitw::GitError
  nil
end

#git(options = git_options) ⇒ Object



91
92
93
94
95
# File 'lib/gitw/repository.rb', line 91

def git(options = git_options)
  Dir.chdir(base_dir) do
    Gitw::GitExe.new(options: options)
  end
end

#git_base_optionsObject



105
106
107
108
109
110
# File 'lib/gitw/repository.rb', line 105

def git_base_options
  {
    git_dir: git_dir,
    work_tree: worktree_dir
  }.compact
end

#git_dirObject



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

def git_dir
  return @git_dir if @git_dir
end

#git_optionsObject



97
98
99
100
101
102
103
# File 'lib/gitw/repository.rb', line 97

def git_options
  git_base_options.merge(
    {
      dir: base_dir
    }
  ).compact
end

#in_repository?Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
# File 'lib/gitw/repository.rb', line 28

def in_repository?
  return false unless File.directory?(base_dir)
  return true if inside_work_tree? || inside_git_dir?

  false
end

#init(**options) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/gitw/repository.rb', line 73

def init(**options)
  FileUtils.mkdir_p base_dir
  git.init(base_dir, **options)

  self
rescue Errno::EACCES, Gitw::GitError
  nil
end

#inside_git_dir?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/gitw/repository.rb', line 41

def inside_git_dir?
  git.is_inside_git_dir
rescue Gitw::GitError
  false
end

#inside_work_tree?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/gitw/repository.rb', line 35

def inside_work_tree?
  git.is_inside_work_tree
rescue Gitw::GitError
  false
end

#root_dirObject



65
66
67
68
69
70
71
# File 'lib/gitw/repository.rb', line 65

def root_dir
  return absolute_git_dir if bare?

  git.toplevel
rescue Gitw::GitError
  nil
end

#toplevelObject



47
48
49
50
51
# File 'lib/gitw/repository.rb', line 47

def toplevel
  git.toplevel
rescue Gitw::GitError
  nil
end

#worktree_dirObject



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

def worktree_dir
  return @worktree_dir if @worktree_dir
end