Class: Gitlab::Git::Repository

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

Defined Under Namespace

Classes: NoRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Popen

#popen

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



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

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

Instance Attribute Details

#gritObject (readonly)

Grit repo object



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

def grit
  @grit
end

#nameObject (readonly)

Directory name of repo



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

def name
  @name
end

#pathObject (readonly)

Full path to repo



15
16
17
# File 'lib/gitlab_git/repository.rb', line 15

def path
  @path
end

#root_refObject

Default branch in the repository



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

def root_ref
  @root_ref
end

Instance Method Details

#archive_repo(ref, storage_path) ⇒ Object

Archive Project to .tar.gz

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gitlab_git/repository.rb', line 102

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

  # Build file path
  file_name = self.name.gsub("\.git", "") + "-" + commit.id.to_s + ".tar.gz"
  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)
    file = self.grit.archive_to_file(ref, prefix,  file_path)
  end

  file_path
end

#branch_namesObject

Returns an Array of branch names sorted by name ASC



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

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')


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

def branch_names_contains(commit)
  output = grit.git.native(:branch, {contains: true}, commit)
  # The output is expected as follow
  #   fix-aaa
  #   fix-bbb
  # * master
  output.scan(/[^* \n]+/)
end

#branchesObject

Returns an Array of Branches



45
46
47
# File 'lib/gitlab_git/repository.rb', line 45

def branches
  grit.branches.sort_by(&:name)
end

#commits_between(from, to) ⇒ Object

Delegate commits_between to Grit method



173
174
175
# File 'lib/gitlab_git/repository.rb', line 173

def commits_between(from, to)
  grit.commits_between(from, to)
end

#diff(from, to) ⇒ Object



181
182
183
# File 'lib/gitlab_git/repository.rb', line 181

def diff(from, to)
  grit.diff(from, to)
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



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gitlab_git/repository.rb', line 83

def discover_default_branch
  if branch_names.length == 0
    nil
  elsif branch_names.length == 1
    branch_names.first
  elsif grit.head
    grit.head.name
  elsif branch_names.include?("master")
    "master"
  elsif
    branch_names.first
  end
end

#empty?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/gitlab_git/repository.rb', line 74

def empty?
  !has_commits?
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


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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 202

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

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

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

  default_options = {pretty: 'raw', order: :date}

  actual_options = default_options.merge(actual_options)

  order = actual_options.delete(:order)

  case order
  when :date
    actual_options[:date_order] = true
  when :topo
    actual_options[:topo_order] = true
  end

  ref = actual_options.delete(:ref)

  containing_commit = actual_options.delete(:contains)

  args = []

  if ref
    args.push(ref)
  elsif containing_commit
    args.push(*branch_names_contains(containing_commit))
  else
    actual_options[:all] = true
  end

  output = grit.git.native(:rev_list, actual_options, *args)

  Grit::Commit.list_from_string(grit, output).map do |commit|
    Gitlab::Git::Commit.decorate(commit)
  end
rescue Grit::GitRuby::Repository::NoSuchShaFound
  []
end

#has_commits?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'lib/gitlab_git/repository.rb', line 68

def has_commits?
  !!Gitlab::Git::Commit.last(self)
rescue Grit::NoSuchPathError
  false
end

#headsObject



64
65
66
# File 'lib/gitlab_git/repository.rb', line 64

def heads
  @heads ||= grit.heads.sort_by(&:name)
end

#log(options) ⇒ Object

Delegate log to Grit method

Usage.

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


151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gitlab_git/repository.rb', line 151

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

  options = default_options.merge(options)

  grit.log(
    options[:ref] || root_ref,
    options[:path],
    max_count: options[:limit].to_i,
    skip: options[:offset].to_i,
    follow: options[:follow]
  )
end

#merge_base_commit(from, to) ⇒ Object



177
178
179
# File 'lib/gitlab_git/repository.rb', line 177

def merge_base_commit(from, to)
  grit.git.native(:merge_base, {}, [to, from]).strip
end

#rawObject

Grit repo object Alias to old method for compatibility



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

def grit
  @grit
end

#ref_namesObject

Returns an Array of branch and tag names



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

def ref_names
  branch_names + tag_names
end

#refs_hashObject

Get refs hash which key is SHA1 and value is ref object(Grit::Head or Grit::Remote or Grit::Tag)



263
264
265
266
267
268
269
270
271
272
273
# File 'lib/gitlab_git/repository.rb', line 263

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

    grit.refs.each do |r|
      @refs_hash[r.commit.id] << r
    end
  end
  @refs_hash
end

#search_files(query, ref = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gitlab_git/repository.rb', line 129

def search_files(query, ref = nil)
  if ref.nil? || ref == ""
    ref = root_ref
  end

  greps = grit.grep(query, 3, ref)

  greps.map do |grep|
    Gitlab::Git::BlobSnippet.new(ref, grep.content, grep.startline, grep.filename)
  end
end

#sizeObject

Return repo size in megabytes



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

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

#tag_namesObject

Returns an Array of tag names



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

def tag_names
  tags.map(&:name)
end

#tagsObject

Returns an Array of Tags



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

def tags
  grit.tags.sort_by(&:name).reverse
end