Class: Gitdocs::Repository

Inherits:
Object
  • Object
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

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.

Parameters:

  • path_or_share (String, Share)

See Also:



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_reasonObject (readonly)

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(path, remote) ⇒ Gitdocs::Repository

Clone a repository, and create the destination path if necessary.

Parameters:

  • path (String)

    to clone the repository to

  • remote (String)

    URI of the git repository to clone

Returns:

Raises:

  • (RuntimeError)

    if the clone fails



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))
  # TODO: determine how to do this with rugged, and handle SSH and HTTPS
  #   credentials.
  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.

Parameters:

  • last_oid (String)

Returns:

  • (Hash<String, Int>)


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_branchesnil, Array<String>

Returns:

  • (nil)

    if the repository is invalid

  • (Array<String>)

    sorted list of local branches



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_remotesnil, Array<String>

Returns:

  • (nil)

    if the repository is invalid

  • (Array<String>)

    sorted list of remote branches



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

Parameters:

  • relative_path (String)
  • oid (String)


293
294
295
# File 'lib/gitdocs/repository.rb', line 293

def blob_at(relative_path, ref)
  @rugged.blob_at(ref, relative_path)
end

#commitnil

Returns:

  • (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

Parameters:

  • relative_path (String)
  • limit (Integer)

    the number of commits which will be returned

Returns:

  • (Array<Rugged::Commit>)


273
274
275
276
277
278
279
280
281
282
# File 'lib/gitdocs/repository.rb', line 273

def commits_for(relative_path, limit)
  # TODO: should add a filter here for checking that the commit actually has
  # an associated blob.
  commits = head_walker.select do |commit|
    commit.parents.size == 1 && changes?(commit, relative_path)
  end
  # TODO: should re-write this limit in a way that will skip walking all of
  # the commits.
  commits.first(limit)
end

#current_oidnil, String

Returns:

  • (nil)

    if there are no commits present

  • (String)

    oid of the HEAD of the working directory



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

Returns:

  • (Boolean)


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

#fetchnil, ...

Fetch all the remote branches

Returns:

  • (nil)

    if the repository is invalid

  • (:no_remote)

    if the remote is not yet set

  • (:ok)

    if the fetch worked

Raises:

  • (FetchError)

    if there is an error return message



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

Parameters:

  • term (String)

Yields:

  • (file, context)

    Gives the files and context for each of the results

Yield Parameters:

  • file (String)
  • context (String)


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
  # TODO: add logging to record the error details
  ''
rescue Grit::Git::CommandFailed
  # TODO: add logging to record the error details if they are not just
  # nothing found
  ''
end

#last_commit_for(relative_path) ⇒ Rugged::Commit

Parameters:

  • relative_path (String)

Returns:

  • (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

#mergenil, ...

Merge the repository

Returns:

  • (nil)

    if the repository is invalid

  • (:no_remote)

    if the remote is not yet set

  • (Array<String>)

    if there is a conflict return the Array of conflicted file names

  • (see #author_count)

    if merged with no errors or conflicts

Raises:

  • (MergeError)

    if there is an error, it it will include the message



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
  # HACK: The rugged in-memory index will not have been updated after the
  # Grit merge command. Reload it before checking for conflicts.
  @rugged.index.reload
  raise(MergeError, e.err) unless @rugged.index.conflicts?
  mark_conflicts
end

#need_sync?Boolean

Returns:

  • (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

#pushnil, ...

Push the repository

Returns:

  • (nil)

    if the repository is invalid

  • (:no_remote)

    if the remote is not yet set

  • (:nothing)

    if there was nothing to do

  • (String)

    if there is an error return the message

  • (see #author_count)

    if pushed without errors or conflicts



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 # return the output on error
end

#rootString

Returns:

  • (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}

Returns:

  • (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

Returns:

  • (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.



260
261
262
263
# File 'lib/gitdocs/repository.rb', line 260

def write_commit_message(message)
  return unless valid?
  Committer.new(root).write_commit_message(message)
end