Class: Gitdocs::Repository
- Inherits:
-
Object
- Object
- Gitdocs::Repository
show all
- Defined in:
- lib/gitdocs/repository.rb,
lib/gitdocs/repository/path.rb,
lib/gitdocs/repository/committer.rb
Defined Under Namespace
Classes: Committer, FetchError, InvalidError, MergeError, Path
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#author_count(last_oid) ⇒ Hash<String, Int>
Get the count of commits by author from the head to the specified oid.
-
#available_branches ⇒ nil, Array<String>
-
#available_remotes ⇒ nil, Array<String>
-
#blob_at(relative_path, ref) ⇒ Object
-
#commit ⇒ nil
-
#commits_for(relative_path, limit) ⇒ Array<Rugged::Commit>
Excluding the initial commit (without a parent) which keeps things consistent with the original behaviour.
-
#current_oid ⇒ nil, String
-
#dirty? ⇒ Boolean
Is the working directory dirty.
-
#fetch ⇒ nil, ...
Fetch all the remote branches.
-
#grep(term) {|file, context| ... } ⇒ Object
-
#initialize(path_or_share) ⇒ Repository
constructor
Initialize the repository on the specified path.
-
#last_commit_for(relative_path) ⇒ Rugged::Commit
-
#merge ⇒ nil, ...
-
#need_sync? ⇒ Boolean
-
#push ⇒ nil, ...
-
#root ⇒ String
-
#synchronize(type) ⇒ Hash{:merge,:push => Object}
-
#valid? ⇒ Boolean
-
#write_commit_message(message) ⇒ void
Constructor Details
#initialize(path_or_share) ⇒ Repository
Initialize the repository on the specified path. If the path is not valid for some reason, the object will be initialized but it will be put into an invalid state.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/gitdocs/repository.rb', line 30
def initialize(path_or_share)
path = path_or_share
if path_or_share.respond_to?(:path)
path = path_or_share.path
@remote_name = path_or_share.remote_name
@branch_name = path_or_share.branch_name
end
@rugged = Rugged::Repository.new(path)
@grit = Grit::Repo.new(path)
Grit::Git.git_timeout = 120
@invalid_reason = nil
@commit_message_path = abs_path('.gitmessage~')
rescue Rugged::OSError
@invalid_reason = :directory_missing
rescue Rugged::RepositoryError
@invalid_reason = :no_repository
end
|
Instance Attribute Details
#invalid_reason ⇒ Object
Returns the value of attribute invalid_reason.
17
18
19
|
# File 'lib/gitdocs/repository.rb', line 17
def invalid_reason
@invalid_reason
end
|
Class Method Details
Clone a repository, and create the destination path if necessary.
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/gitdocs/repository.rb', line 57
def self.clone(path, remote)
FileUtils.mkdir_p(File.dirname(path))
Grit::Git.new(path).clone({ raise: true, quiet: true }, remote, path)
repository = new(path)
fail("Unable to clone into #{path}") unless repository.valid?
repository
rescue Grit::Git::GitTimeout
raise("Unable to clone into #{path} because it timed out")
rescue Grit::Git::CommandFailed => e
raise("Unable to clone into #{path} because of #{e.err}")
end
|
Instance Method Details
#author_count(last_oid) ⇒ Hash<String, Int>
Get the count of commits by author from the head to the specified oid.
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/gitdocs/repository.rb', line 223
def author_count(last_oid)
walker = head_walker
walker.hide(last_oid) if last_oid
walker.reduce(Hash.new(0)) do |result, commit|
result["#{commit.author[:name]} <#{commit.author[:email]}>"] += 1
result
end
rescue Rugged::ReferenceError
{}
rescue Rugged::OdbError
{}
end
|
#available_branches ⇒ nil, Array<String>
92
93
94
95
|
# File 'lib/gitdocs/repository.rb', line 92
def available_branches
return nil unless valid?
@rugged.branches.each_name(:local).sort
end
|
#available_remotes ⇒ nil, Array<String>
85
86
87
88
|
# File 'lib/gitdocs/repository.rb', line 85
def available_remotes
return nil unless valid?
@rugged.branches.each_name(:remote).sort
end
|
#blob_at(relative_path, ref) ⇒ Object
293
294
295
|
# File 'lib/gitdocs/repository.rb', line 293
def blob_at(relative_path, ref)
@rugged.blob_at(ref, relative_path)
end
|
#commit ⇒ nil
192
193
194
195
|
# File 'lib/gitdocs/repository.rb', line 192
def commit
return unless valid?
Committer.new(root).commit
end
|
#commits_for(relative_path, limit) ⇒ Array<Rugged::Commit>
Excluding the initial commit (without a parent) which keeps things consistent with the original behaviour. TODO: reconsider if this is the correct behaviour
273
274
275
276
277
278
279
280
281
282
|
# File 'lib/gitdocs/repository.rb', line 273
def commits_for(relative_path, limit)
commits = head_walker.select do |commit|
commit.parents.size == 1 && changes?(commit, relative_path)
end
commits.first(limit)
end
|
#current_oid ⇒ nil, String
99
100
101
102
103
|
# File 'lib/gitdocs/repository.rb', line 99
def current_oid
@rugged.head.target_id
rescue Rugged::ReferenceError
nil
end
|
#dirty? ⇒ Boolean
Is the working directory dirty
108
109
110
111
112
113
|
# File 'lib/gitdocs/repository.rb', line 108
def dirty?
return false unless valid?
return Dir.glob(abs_path('*')).any? unless current_oid
@rugged.diff_workdir(current_oid, include_untracked: true).deltas.any?
end
|
#fetch ⇒ nil, ...
Fetch all the remote branches
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/gitdocs/repository.rb', line 147
def fetch
return nil unless valid?
return :no_remote unless remote?
@rugged.remotes.each { |x| @grit.remote_fetch(x.name) }
:ok
rescue Grit::Git::GitTimeout
raise(FetchError, "Fetch timed out for #{root}")
rescue Grit::Git::CommandFailed => e
raise(FetchError, e.err)
end
|
#grep(term) {|file, context| ... } ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/gitdocs/repository.rb', line 126
def grep(term, &block)
@grit.git.grep(
{ raise: true, bare: false, chdir: root, ignore_case: true },
term
).scan(/(.*?):([^\n]*)/, &block)
rescue Grit::Git::GitTimeout
''
rescue Grit::Git::CommandFailed
''
end
|
#last_commit_for(relative_path) ⇒ Rugged::Commit
287
288
289
|
# File 'lib/gitdocs/repository.rb', line 287
def last_commit_for(relative_path)
head_walker.find { |commit| changes?(commit, relative_path) }
end
|
#merge ⇒ nil, ...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/gitdocs/repository.rb', line 168
def merge
return nil unless valid?
return :no_remote unless remote?
return :ok unless remote_oid
return :ok if remote_oid == current_oid
last_oid = current_oid
@grit.git.merge(
{ raise: true, chdir: root },
"#{@remote_name}/#{@branch_name}"
)
author_count(last_oid)
rescue Grit::Git::GitTimeout
raise(MergeError, "Merge timed out for #{root}")
rescue Grit::Git::CommandFailed => e
@rugged.index.reload
raise(MergeError, e.err) unless @rugged.index.conflicts?
mark_conflicts
end
|
#need_sync? ⇒ Boolean
116
117
118
119
120
|
# File 'lib/gitdocs/repository.rb', line 116
def need_sync?
return false unless valid?
return false unless remote?
remote_oid != current_oid
end
|
#push ⇒ nil, ...
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/gitdocs/repository.rb', line 204
def push
return unless valid?
return :no_remote unless remote?
return :nothing unless current_oid
return :nothing if remote_oid == current_oid
last_oid = remote_oid
@grit.git.push({ raise: true }, @remote_name, @branch_name)
author_count(last_oid)
rescue Grit::Git::CommandFailed => e
return :conflict if e.err[/\[rejected\]/]
e.err end
|
#root ⇒ String
73
74
75
76
|
# File 'lib/gitdocs/repository.rb', line 73
def root
return nil unless valid?
@rugged.path.sub(/.\.git./, '')
end
|
#synchronize(type) ⇒ Hash{:merge,:push => Object}
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/gitdocs/repository.rb', line 237
def synchronize(type)
result = { merge: nil, push: nil }
return result unless valid?
case type
when 'fetch'
fetch
when 'full'
commit
fetch
result[:merge] = merge
result[:push] = push
end
result
rescue Gitdocs::Repository::FetchError
result
rescue Gitdocs::Repository::MergeError => e
result[:merge] = e.message
result
end
|
#valid? ⇒ Boolean
79
80
81
|
# File 'lib/gitdocs/repository.rb', line 79
def valid?
!@invalid_reason
end
|
#write_commit_message(message) ⇒ void
This method returns an undefined value.