Class: Gitlab::Git::Repository

Inherits:
Object
  • Object
show all
Includes:
Popen
Defined in:
lib/gitlab_git/repository.rb

Defined Under Namespace

Classes: InvalidBlobName, NoRepository

Constant Summary collapse

SEARCH_CONTEXT_LINES =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Popen

#popen

Constructor Details

#initialize(path) ⇒ Repository

‘path’ must be the path to a bare git repository, e.g. /path/to/my-repo.git



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

def initialize(path)
  @path = path
  @name = path.split("/").last
  @root_ref = discover_default_branch
end

Instance Attribute Details

#nameObject (readonly)

Directory name of repo



23
24
25
# File 'lib/gitlab_git/repository.rb', line 23

def name
  @name
end

#pathObject (readonly)

Full path to repo



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

def path
  @path
end

#root_refObject

Default branch in the repository



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

def root_ref
  @root_ref
end

#ruggedObject (readonly)

Rugged repo object



26
27
28
# File 'lib/gitlab_git/repository.rb', line 26

def rugged
  @rugged
end

Instance Method Details

#archive_repo(ref, storage_path, format = "tar.gz") ⇒ Object

Archive Project to .tar.gz

Already packed repo archives stored at app_root/tmp/repositories/project_name/project_name-commit-id.tag.gz



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/gitlab_git/repository.rb', line 136

def archive_repo(ref, storage_path, format = "tar.gz")
  ref ||= root_ref
  commit = Gitlab::Git::Commit.find(self, ref)
  return nil unless commit

  extension = nil
  git_archive_format = nil
  pipe_cmd = nil

  case format
  when "tar.bz2", "tbz", "tbz2", "tb2", "bz2"
    extension = ".tar.bz2"
    pipe_cmd = %W(bzip2)
  when "tar"
    extension = ".tar"
    pipe_cmd = %W(cat)
  when "zip"
    extension = ".zip"
    git_archive_format = "zip"
    pipe_cmd = %W(cat)
  else
    # everything else should fall back to tar.gz
    extension = ".tar.gz"
    git_archive_format = nil
    pipe_cmd = %W(gzip -n)
  end

  # Build file path
  file_name = self.name.gsub("\.git", "") + "-" + commit.id.to_s + extension
  file_path = File.join(storage_path, self.name, file_name)

  # Put files into a directory before archiving
  prefix = File.basename(self.name) + "/"

  # Create file if not exists
  unless File.exists?(file_path)
    FileUtils.mkdir_p File.dirname(file_path)

    # Create the archive in temp file, to avoid leaving a corrupt archive
    # to be downloaded by the next user if we get interrupted while
    # creating the archive. Note that we do not care about cleaning up
    # the temp file in that scenario, because GitLab cleans up the
    # directory holding the archive files periodically.
    temp_file_path = file_path + ".#{Process.pid}-#{Time.now.to_i}"
    archive_to_file(ref, prefix, temp_file_path, git_archive_format, pipe_cmd)

    # move temp file to persisted location
    FileUtils.move(temp_file_path, file_path)
  end

  file_path
end

#branch_namesObject

Returns an Array of branch names sorted by name ASC



49
50
51
# File 'lib/gitlab_git/repository.rb', line 49

def branch_names
  branches.map(&:name)
end

#branch_names_contains(commit) ⇒ Object

Returns branch names collection that contains the special commit(SHA1 or name)

Ex.

repo.branch_names_contains('master')


368
369
370
# File 'lib/gitlab_git/repository.rb', line 368

def branch_names_contains(commit)
  branches_contains(commit).map { |c| c.name }
end

#branchesObject

Returns an Array of Branches



54
55
56
57
58
# File 'lib/gitlab_git/repository.rb', line 54

def branches
  rugged.branches.map do |rugged_ref|
    Branch.new(rugged_ref.name, rugged_ref.target)
  end.sort_by(&:name)
end

#branches_contains(commit) ⇒ Object

Returns branch collection that contains the special commit(SHA1 or name)

Ex.

repo.branch_names_contains('master')


377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/gitlab_git/repository.rb', line 377

def branches_contains(commit)
  commit_obj = rugged.rev_parse(commit)
  parent = commit_obj.parents.first unless commit_obj.parents.empty?

  walker = Rugged::Walker.new(rugged)

  rugged.branches.select do |branch|
    walker.push(branch.target_id)
    walker.hide(parent) if parent
    result = walker.any? { |c| c.oid == commit_obj.oid }
    walker.reset

    result
  end
end

#checkout(ref, options = {}, start_point = "HEAD") ⇒ Object

Check out the specified ref. Valid options are:

:b - Create a new branch at +start_point+ and set HEAD to the new
     branch.

* These options are passed to the Rugged::Repository#checkout method:

:progress ::
  A callback that will be executed for checkout progress notifications.
  Up to 3 parameters are passed on each execution:

  - The path to the last updated file (or +nil+ on the very first
    invocation).
  - The number of completed checkout steps.
  - The number of total checkout steps to be performed.

:notify ::
  A callback that will be executed for each checkout notification
  types specified with +:notify_flags+. Up to 5 parameters are passed
  on each execution:

  - An array containing the +:notify_flags+ that caused the callback
    execution.
  - The path of the current file.
  - A hash describing the baseline blob (or +nil+ if it does not
    exist).
  - A hash describing the target blob (or +nil+ if it does not exist).
  - A hash describing the workdir blob (or +nil+ if it does not
    exist).

:strategy ::
  A single symbol or an array of symbols representing the strategies
  to use when performing the checkout. Possible values are:

  :none ::
    Perform a dry run (default).

  :safe ::
    Allow safe updates that cannot overwrite uncommitted data.

  :safe_create ::
    Allow safe updates plus creation of missing files.

  :force ::
    Allow all updates to force working directory to look like index.

  :allow_conflicts ::
    Allow checkout to make safe updates even if conflicts are found.

  :remove_untracked ::
    Remove untracked files not in index (that are not ignored).

  :remove_ignored ::
    Remove ignored files not in index.

  :update_only ::
    Only update existing files, don't create new ones.

  :dont_update_index ::
    Normally checkout updates index entries as it goes; this stops
    that.

  :no_refresh ::
    Don't refresh index/config/etc before doing checkout.

  :disable_pathspec_match ::
    Treat pathspec as simple list of exact match file paths.

  :skip_locked_directories ::
    Ignore directories in use, they will be left empty.

  :skip_unmerged ::
    Allow checkout to skip unmerged files (NOT IMPLEMENTED).

  :use_ours ::
    For unmerged files, checkout stage 2 from index (NOT IMPLEMENTED).

  :use_theirs ::
    For unmerged files, checkout stage 3 from index (NOT IMPLEMENTED).

  :update_submodules ::
    Recursively checkout submodules with same options (NOT
    IMPLEMENTED).

  :update_submodules_if_changed ::
    Recursively checkout submodules if HEAD moved in super repo (NOT
    IMPLEMENTED).

:disable_filters ::
  If +true+, filters like CRLF line conversion will be disabled.

:dir_mode ::
  Mode for newly created directories. Default: +0755+.

:file_mode ::
  Mode for newly created files. Default: +0755+ or +0644+.

:file_open_flags ::
  Mode for opening files. Default:
  <code>IO::CREAT | IO::TRUNC | IO::WRONLY</code>.

:notify_flags ::
  A single symbol or an array of symbols representing the cases in
  which the +:notify+ callback should be invoked. Possible values are:

  :none ::
    Do not invoke the +:notify+ callback (default).

  :conflict ::
    Invoke the callback for conflicting paths.

  :dirty ::
    Invoke the callback for "dirty" files, i.e. those that do not need
    an update but no longer match the baseline.

  :updated ::
    Invoke the callback for any file that was changed.

  :untracked ::
    Invoke the callback for untracked files.

  :ignored ::
    Invoke the callback for ignored files.

  :all ::
    Invoke the callback for all these cases.

:paths ::
  A glob string or an array of glob strings specifying which paths
  should be taken into account for the checkout operation. +nil+ will
  match all files.  Default: +nil+.

:baseline ::
  A Rugged::Tree that represents the current, expected contents of the
  workdir.  Default: +HEAD+.

:target_directory ::
  A path to an alternative workdir directory in which the checkout
  should be performed.


627
628
629
630
631
632
633
634
# File 'lib/gitlab_git/repository.rb', line 627

def checkout(ref, options = {}, start_point = "HEAD")
  if options[:b]
    rugged.branches.create(ref, start_point)
    options.delete(:b)
  end
  default_options = { strategy: :safe_create }
  rugged.checkout(ref, default_options.merge(options))
end

#clean(options = {}) ⇒ Object

Mimic the ‘git clean` command and recursively delete untracked files. Valid keys that can be passed in the options hash are:

:d - Remove untracked directories :f - Remove untracked directories that are managed by a different

repository

:x - Remove ignored files

The value in options must evaluate to true for an option to take effect.

Examples:

repo.clean(d: true, f: true) # Enable the -d and -f options

repo.clean(d: false, x: true) # -x is enabled, -d is not


480
481
482
483
484
485
486
# File 'lib/gitlab_git/repository.rb', line 480

def clean(options = {})
  strategies = [:remove_untracked]
  strategies.push(:force) if options[:f]
  strategies.push(:remove_ignored) if options[:x]

  # TODO: implement this method
end

#commit_count(ref) ⇒ Object

Return total commits count accessible from passed ref



441
442
443
444
445
446
# File 'lib/gitlab_git/repository.rb', line 441

def commit_count(ref)
  walker = Rugged::Walker.new(rugged)
  walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
  walker.push(ref)
  walker.count
end

#commits_between(from, to) ⇒ Object

Return a collection of Rugged::Commits between the two SHA arguments.



261
262
263
264
265
266
267
268
269
# File 'lib/gitlab_git/repository.rb', line 261

def commits_between(from, to)
  walker = Rugged::Walker.new(rugged)
  walker.push(to)
  walker.hide(from)
  commits = walker.to_a
  walker.reset

  commits.reverse
end

#commits_since(from_date) ⇒ Object



712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'lib/gitlab_git/repository.rb', line 712

def commits_since(from_date)
  walker = Rugged::Walker.new(rugged)
  walker.sorting(Rugged::SORT_DATE | Rugged::SORT_REVERSE)

  rugged.references.each("refs/heads/*") do |ref|
    walker.push(ref.target_id)
  end

  commits = []
  walker.each do |commit|
    break if commit.author[:time].to_date < from_date
    commits.push(commit)
  end

  commits
end

#delete_branch(branch_name) ⇒ Object

Delete the specified branch from the repository



637
638
639
# File 'lib/gitlab_git/repository.rb', line 637

def delete_branch(branch_name)
  rugged.branches.delete(branch_name)
end

#diff(from, to, options = {}, *paths) ⇒ Object

Return an array of Diff objects that represent the diff between from and to. See Diff::filter_diff_options for the allowed diff options. The options hash can also include :break_rewrites to split larger rewrites into delete/add pairs.



280
281
282
283
284
# File 'lib/gitlab_git/repository.rb', line 280

def diff(from, to, options = {}, *paths)
  diff_patches(from, to, options, *paths).map do |p|
    Gitlab::Git::Diff.new(p)
  end
end

#diff_text(from, to, options = {}, *paths) ⇒ Object

Return the diff between from and to in a single patch string. The options hash has the same allowed keys as #diff.



288
289
290
291
292
293
294
295
# File 'lib/gitlab_git/repository.rb', line 288

def diff_text(from, to, options = {}, *paths)
  # NOTE: It would be simpler to use the Rugged::Diff#patch method, but
  # that formats the diff text differently than Rugged::Patch#to_s for
  # changes to binary files.
  diff_patches(from, to, options, *paths).map do |p|
    p.to_s
  end.join("\n")
end

#discover_default_branchObject

Discovers the default branch based on the repository’s available branches

  • If no branches are present, returns nil

  • If one branch is present, returns its name

  • If two or more branches are present, returns current HEAD or master or first branch



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/gitlab_git/repository.rb', line 111

def discover_default_branch
  if branch_names.length == 0
    nil
  elsif branch_names.length == 1
    branch_names.first
  elsif rugged_head && branch_names.include?(Ref.extract_branch_name(rugged_head.name))
    Ref.extract_branch_name(rugged_head.name)
  elsif branch_names.include?("master")
    "master"
  else
    branch_names.first
  end
end

#empty?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/gitlab_git/repository.rb', line 98

def empty?
  rugged.empty?
end

#fetch(remote_name) ⇒ Object

Fetch the specified remote



668
669
670
# File 'lib/gitlab_git/repository.rb', line 668

def fetch(remote_name)
  rugged.remotes[remote_name].fetch
end

#find_commits(options = {}) ⇒ Object

Returns commits collection

Ex.

repo.find_commits(
  ref: 'master',
  max_count: 10,
  skip: 5,
  order: :date
)

+options+ is a Hash of optional arguments to git
  :ref is the ref from which to begin (SHA1 or name)
  :contains is the commit contained by the refs from which to begin (SHA1 or name)
  :max_count is the maximum number of commits to fetch
  :skip is the number of commits to skip
  :order is the commits order and allowed value is :date(default) or :topo


314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/gitlab_git/repository.rb', line 314

def find_commits(options = {})
  actual_options = options.dup

  allowed_options = [:ref, :max_count, :skip, :contains, :order]

  actual_options.keep_if do |key|
    allowed_options.include?(key)
  end

  default_options = { skip: 0 }
  actual_options = default_options.merge(actual_options)

  walker = Rugged::Walker.new(rugged)

  if actual_options[:ref]
    walker.push(rugged.rev_parse_oid(actual_options[:ref]))
  elsif actual_options[:contains]
    branches_contains(actual_options[:contains]).each do |branch|
      walker.push(branch.target_id)
    end
  else
    rugged.references.each("refs/heads/*") do |ref|
      walker.push(ref.target_id)
    end
  end

  if actual_options[:order] == :topo
    walker.sorting(Rugged::SORT_TOPO)
  else
    walker.sorting(Rugged::SORT_DATE)
  end


  commits = []
  offset = actual_options[:skip]
  limit = actual_options[:max_count]
  walker.each(offset: offset, limit: limit) do |commit|
    gitlab_commit = Gitlab::Git::Commit.decorate(commit)
    commits.push(gitlab_commit)
  end

  walker.reset

  commits
rescue Rugged::OdbError
  []
end

#format_patch(from, to, options = {}) ⇒ Object

Return a String containing the mbox-formatted diff between from and to. See #diff for the allowed keys in the options hash.



679
680
681
682
683
684
685
686
687
688
689
# File 'lib/gitlab_git/repository.rb', line 679

def format_patch(from, to, options = {})
  options ||= {}
  break_rewrites = options[:break_rewrites]
  actual_options = Diff.filter_diff_options(options)

  from_sha = rugged.rev_parse_oid(from)
  to_sha = rugged.rev_parse_oid(to)
  commits_between(from_sha, to_sha).map do |commit|
    commit.to_mbox(actual_options)
  end.join("\n")
end

#has_commits?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/gitlab_git/repository.rb', line 94

def has_commits?
  !empty?
end

#headsObject

Deprecated. Will be removed in 5.2



88
89
90
91
92
# File 'lib/gitlab_git/repository.rb', line 88

def heads
  rugged.references.each("refs/heads/*").map do |head|
    Gitlab::Git::Ref.new(head.name, head.target)
  end.sort_by(&:name)
end

#log(options) ⇒ Object

Use the Rugged Walker API to build an array of commits.

Usage.

repo.log(
  ref: 'master',
  path: 'app/models',
  limit: 10,
  offset: 5,
)


226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/gitlab_git/repository.rb', line 226

def log(options)
  default_options = {
    limit: 10,
    offset: 0,
    path: nil,
    ref: root_ref,
    follow: false,
    skip_merges: false
  }

  options = default_options.merge(options)
  options[:limit] ||= 0
  options[:offset] ||= 0
  actual_ref = options[:ref] || root_ref
  sha = sha_from_ref(actual_ref)
  build_log(sha, options)
rescue Rugged::OdbError, Rugged::InvalidError, Rugged::ReferenceError
  # Return an empty array if the ref wasn't found
  []
end

#lookup(oid_or_ref_name) ⇒ Object

Lookup for rugged object by oid or ref name



410
411
412
# File 'lib/gitlab_git/repository.rb', line 410

def lookup(oid_or_ref_name)
  rugged.rev_parse(oid_or_ref_name)
end

#merge(source_name, target_name, options = {}) ⇒ Object

Merge the source_name branch into the target_name branch. This is equivalent to ‘git merge –no_ff source_name`, since a merge commit is always created.



694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/gitlab_git/repository.rb', line 694

def merge(source_name, target_name, options = {})
  our_commit = rugged.branches[target_name].target
  their_commit = rugged.branches[source_name].target

  raise "Invalid merge target" if our_commit.nil?
  raise "Invalid merge source" if their_commit.nil?

  merge_index = rugged.merge_commits(our_commit, their_commit)
  return false if merge_index.conflicts?

  actual_options = options.merge(
    parents: [our_commit, their_commit],
    tree: merge_index.write_tree(rugged),
    update_ref: "refs/heads/#{target_name}"
  )
  Rugged::Commit.create(rugged, actual_options)
end

#merge_base_commit(from, to) ⇒ Object

Returns the SHA of the most recent common ancestor of from and to



272
273
274
# File 'lib/gitlab_git/repository.rb', line 272

def merge_base_commit(from, to)
  rugged.merge_base(from, to)
end

#push(remote_name, *refspecs) ⇒ Object

Push *refspecs to the remote identified by remote_name.



673
674
675
# File 'lib/gitlab_git/repository.rb', line 673

def push(remote_name, *refspecs)
  rugged.remotes[remote_name].push(refspecs)
end

#rawObject

Alias to old method for compatibility



37
38
39
# File 'lib/gitlab_git/repository.rb', line 37

def raw
  rugged
end

#ref_namesObject

Returns an Array of branch and tag names



83
84
85
# File 'lib/gitlab_git/repository.rb', line 83

def ref_names
  branch_names + tag_names
end

#refs_hashObject

Get refs hash which key is SHA1 and value is a Rugged::Reference



395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/gitlab_git/repository.rb', line 395

def refs_hash
  # Initialize only when first call
  if @refs_hash.nil?
    @refs_hash = Hash.new { |h, k| h[k] = [] }

    rugged.references.each do |r|
      sha = rev_parse_target(r.target.oid).oid

      @refs_hash[sha] << r
    end
  end
  @refs_hash
end

#remote_add(remote_name, url) ⇒ Object

Add a new remote to this repository. Returns a Rugged::Remote object



652
653
654
# File 'lib/gitlab_git/repository.rb', line 652

def remote_add(remote_name, url)
  rugged.remotes.create(remote_name, url)
end

#remote_delete(remote_name) ⇒ Object

Delete the specified remote from this repository.



647
648
649
# File 'lib/gitlab_git/repository.rb', line 647

def remote_delete(remote_name)
  rugged.remotes.delete(remote_name)
end

#remote_namesObject

Return an array of this repository’s remote names



642
643
644
# File 'lib/gitlab_git/repository.rb', line 642

def remote_names
  rugged.remotes.each_name.to_a
end

#remote_update(remote_name, options = {}) ⇒ Object

Update the specified remote using the values in the options hash

Example repo.update_remote(“origin”, url: “path/to/repo”)



660
661
662
663
664
665
# File 'lib/gitlab_git/repository.rb', line 660

def remote_update(remote_name, options = {})
  # TODO: Implement other remote options
  remote = rugged.remotes[remote_name]
  remote.url = options[:url] if options[:url]
  remote.save
end

#repo_exists?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/gitlab_git/repository.rb', line 102

def repo_exists?
  !!rugged
end

#reset(ref, reset_type) ⇒ Object

Sets HEAD to the commit specified by ref; ref can be a branch or tag name or a commit SHA. Valid reset_type values are:

[:soft]
  the head will be moved to the commit.
[:mixed]
  will trigger a +:soft+ reset, plus the index will be replaced
  with the content of the commit tree.
[:hard]
  will trigger a +:mixed+ reset and the working directory will be
  replaced with the content of the index. (Untracked and ignored files
  will be left alone)


460
461
462
# File 'lib/gitlab_git/repository.rb', line 460

def reset(ref, reset_type)
  rugged.reset(ref, reset_type)
end

#rev_parse_target(revspec) ⇒ Object

Return the object that revspec points to. If revspec is an annotated tag, then return the tag’s target instead.



253
254
255
256
257
# File 'lib/gitlab_git/repository.rb', line 253

def rev_parse_target(revspec)
  obj = rugged.rev_parse(revspec)
  obj = obj.target while obj.is_a?(Rugged::Tag::Annotation)
  obj
end

#rugged_headObject



125
126
127
128
129
# File 'lib/gitlab_git/repository.rb', line 125

def rugged_head
  rugged.head
rescue Rugged::ReferenceError
  nil
end

#search_files(query, ref = nil) ⇒ Object

Returns an array of BlobSnippets for files at the specified ref that contain the query string.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/gitlab_git/repository.rb', line 197

def search_files(query, ref = nil)
  greps = []
  ref ||= root_ref

  populated_index(ref).each do |entry|
    # Discard submodules
    next if submodule?(entry)

    content = Blob.raw(self, entry[:oid]).data

    # Skip binary files
    next if content.encoding == Encoding::ASCII_8BIT

    greps += build_greps(content, query, ref, entry[:path])
  end

  greps
end

#sha_from_ref(ref) ⇒ Object



247
248
249
# File 'lib/gitlab_git/repository.rb', line 247

def sha_from_ref(ref)
  rev_parse_target(ref).oid
end

#sizeObject

Return repo size in megabytes



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

def size
  size = popen(%W(du -s), path).first.strip.to_i
  (size.to_f / 1024).round(2)
end

#submodules(ref) ⇒ Object

Return hash with submodules info for this repository

Ex.

{
  "rack"  => {
    "id" => "c67be4624545b4263184c4a0e8f887efd0a66320",
    "path" => "rack",
    "url" => "git://github.com/chneukirchen/rack.git"
  },
  "encoding" => {
    "id" => ....
  }
}


428
429
430
431
432
433
434
435
436
437
438
# File 'lib/gitlab_git/repository.rb', line 428

def submodules(ref)
  commit = rugged.rev_parse(ref)

  begin
    content = blob_content(commit, ".gitmodules")
  rescue InvalidBlobName
    return {}
  end

  parse_gitmodules(commit, content)
end

#tag_namesObject

Returns an Array of tag names



61
62
63
# File 'lib/gitlab_git/repository.rb', line 61

def tag_names
  rugged.tags.map { |t| t.name }
end

#tagsObject

Returns an Array of Tags



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gitlab_git/repository.rb', line 66

def tags
  rugged.references.each("refs/tags/*").map do |ref|
    message = nil

    if ref.target.is_a?(Rugged::Tag::Annotation)
      tag_message = ref.target.message

      if tag_message.respond_to?(:chomp)
        message = tag_message.chomp
      end
    end

    Tag.new(ref.name, ref.target, message)
  end.sort_by(&:name)
end