gitlab_git 5.0.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ce597843c96f522a4eeeb7b12834eb4a1c6ef9c
4
- data.tar.gz: 9e421ac92c6bc6429dec0ebfbb13bb8e4cf2f8b4
3
+ metadata.gz: f27529cfedcde0463fd58df616dd0ccfc0f8d747
4
+ data.tar.gz: d9b76853f6e0ab13439e56fd8cc4c67c8d3b75ee
5
5
  SHA512:
6
- metadata.gz: 1f57ab708782206339b826b8197af04bf5cb29e854668d57f4e92fcbfefdd70c24659663a9ba157ba28fde6fecd17ac21981bf5951b4d917d49c606bbeea811a
7
- data.tar.gz: 849667367f90c41f2b0b92f9c55d6b10877cbd9a4a34771820c0d8fdf3cf6148c25ad26dddf7dfe8fc03e789f2c0bfc17178e1ed3a09b0ed1e6f34ec7e01e9bf
6
+ metadata.gz: bce2de00ee1416fd9fe88d3d8987df62f046ef0c4443df8e3c1a64a24d93ca194706bb4795635ff86e4d01ab5b3087954ee00ac480b0613e33099c92ab8f10b5
7
+ data.tar.gz: b0c00d55181cb8bd403322bae01bd5f73d73578a9cac7a1b110b81997ff85d81f95f5ed4ea3932519a07ad3f35510118a3b089608e559873a4337ea41e85137e
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 5.1.0
data/lib/gitlab_git.rb CHANGED
@@ -6,6 +6,7 @@ require 'linguist'
6
6
  require 'active_support/core_ext/hash/keys'
7
7
  require 'grit'
8
8
  require 'grit_ext'
9
+ require 'rugged'
9
10
 
10
11
  Grit::Blob.class_eval do
11
12
  include Linguist::BlobHelper
@@ -24,3 +25,6 @@ require_relative "gitlab_git/tree"
24
25
  require_relative "gitlab_git/blob_snippet"
25
26
  require_relative "gitlab_git/git_stats"
26
27
  require_relative "gitlab_git/log_parser"
28
+ require_relative "gitlab_git/ref"
29
+ require_relative "gitlab_git/branch"
30
+ require_relative "gitlab_git/tag"
@@ -7,16 +7,22 @@ module Gitlab
7
7
 
8
8
  class << self
9
9
  def find(repository, sha, path)
10
- commit = Commit.find(repository, sha)
11
- grit_blob = commit.tree / path
10
+ commit = repository.lookup(sha)
11
+ root_tree = commit.tree
12
12
 
13
- if grit_blob.kind_of?(Grit::Blob)
13
+ blob_entry = find_entry_by_path(repository, root_tree.oid, path)
14
+
15
+ return nil unless blob_entry
16
+
17
+ blob = repository.lookup(blob_entry[:oid])
18
+
19
+ if blob
14
20
  Blob.new(
15
- id: grit_blob.id,
16
- name: grit_blob.name,
17
- size: grit_blob.size,
18
- data: grit_blob.data,
19
- mode: grit_blob.mode,
21
+ id: blob.oid,
22
+ name: blob_entry[:name],
23
+ size: blob.size,
24
+ data: blob.content,
25
+ mode: blob_entry[:mode],
20
26
  path: path,
21
27
  commit_id: sha,
22
28
  )
@@ -24,13 +30,49 @@ module Gitlab
24
30
  end
25
31
 
26
32
  def raw(repository, sha)
27
- grit_blob = repository.grit.blob(sha)
33
+ blob = repository.lookup(sha)
34
+
28
35
  Blob.new(
29
- id: grit_blob.id,
30
- size: grit_blob.size,
31
- data: grit_blob.data,
36
+ id: blob.oid,
37
+ size: blob.size,
38
+ data: blob.content,
32
39
  )
33
40
  end
41
+
42
+ # Recursive search of blob id by path
43
+ #
44
+ # Ex.
45
+ # blog/ # oid: 1a
46
+ # app/ # oid: 2a
47
+ # models/ # oid: 3a
48
+ # file.rb # oid: 4a
49
+ #
50
+ #
51
+ # Blob.find_entry_by_path(repo, '1a', 'app/file.rb') # => '4a'
52
+ #
53
+ def find_entry_by_path(repository, root_id, path)
54
+ root_tree = repository.lookup(root_id)
55
+ path_arr = path.split('/')
56
+
57
+ entry = root_tree.find do |entry|
58
+ entry[:name] == path_arr[0]
59
+ end
60
+
61
+ return nil unless entry
62
+
63
+ if path_arr.size > 1
64
+ return nil unless entry[:type] == :tree
65
+ else
66
+ return nil unless entry[:type] == :blob
67
+ end
68
+
69
+ if path_arr.size > 1
70
+ path_arr.shift
71
+ find_entry_by_path(repository, entry[:oid], path_arr.join('/'))
72
+ else
73
+ entry
74
+ end
75
+ end
34
76
  end
35
77
 
36
78
  def initialize(options)
@@ -0,0 +1,6 @@
1
+ module Gitlab
2
+ module Git
3
+ class Branch < Ref
4
+ end
5
+ end
6
+ end
@@ -47,7 +47,7 @@ module Gitlab
47
47
  tag = repo.tags.find {|tag| tag.name == commit_id} if commit_id
48
48
 
49
49
  commit = if tag
50
- tag.commit
50
+ repo.log(ref: tag.target).first
51
51
  else
52
52
  repo.log(ref: commit_id).first
53
53
  end
@@ -0,0 +1,26 @@
1
+ module Gitlab
2
+ module Git
3
+ class Ref
4
+ # Branch or tag name
5
+ # without "refs/tags|heads" prefix
6
+ attr_reader :name
7
+
8
+ # Target sha.
9
+ # Usually it is commit sha but in case
10
+ # when tag reference on other tag it can be tag sha
11
+ attr_reader :target
12
+
13
+ # Extract branch name from full ref path
14
+ #
15
+ # Ex.
16
+ # Ref.extract_branch_name('refs/heads/master') #=> 'master'
17
+ def self.extract_branch_name(str)
18
+ str.gsub(/\Arefs\/heads\//, '')
19
+ end
20
+
21
+ def initialize(name, target)
22
+ @name, @target = name.gsub(/\Arefs\/(tags|heads)\//, ''), target
23
+ end
24
+ end
25
+ end
26
+ end
@@ -20,8 +20,8 @@ module Gitlab
20
20
  # Grit repo object
21
21
  attr_reader :grit
22
22
 
23
- # Alias to old method for compatibility
24
- alias_method :raw, :grit
23
+ # Rugged repo object
24
+ attr_reader :rugged
25
25
 
26
26
  def initialize(path)
27
27
  @path = path
@@ -35,6 +35,17 @@ module Gitlab
35
35
  raise NoRepository.new('no repository for such path')
36
36
  end
37
37
 
38
+ # Alias to old method for compatibility
39
+ def raw
40
+ grit
41
+ end
42
+
43
+ def rugged
44
+ @rugged ||= Rugged::Repository.new(path)
45
+ rescue Rugged::RepositoryError, Rugged::OSError
46
+ raise NoRepository.new('no repository for such path')
47
+ end
48
+
38
49
  # Returns an Array of branch names
39
50
  # sorted by name ASC
40
51
  def branch_names
@@ -43,7 +54,11 @@ module Gitlab
43
54
 
44
55
  # Returns an Array of Branches
45
56
  def branches
46
- grit.branches.sort_by(&:name)
57
+ rugged.refs.select do |ref|
58
+ ref.name =~ /\Arefs\/heads/
59
+ end.map do |rugged_ref|
60
+ Branch.new(rugged_ref.name, rugged_ref.target)
61
+ end.sort_by(&:name)
47
62
  end
48
63
 
49
64
  # Returns an Array of tag names
@@ -53,7 +68,11 @@ module Gitlab
53
68
 
54
69
  # Returns an Array of Tags
55
70
  def tags
56
- grit.tags.sort_by(&:name).reverse
71
+ rugged.refs.select do |ref|
72
+ ref.name =~ /\Arefs\/tags/
73
+ end.map do |rugged_ref|
74
+ Tag.new(rugged_ref.name, rugged_ref.target)
75
+ end.sort_by(&:name)
57
76
  end
58
77
 
59
78
  # Returns an Array of branch and tag names
@@ -61,18 +80,21 @@ module Gitlab
61
80
  branch_names + tag_names
62
81
  end
63
82
 
83
+ # Deprecated. Will be removed in 5.2
64
84
  def heads
65
85
  @heads ||= grit.heads.sort_by(&:name)
66
86
  end
67
87
 
68
88
  def has_commits?
69
- !!Gitlab::Git::Commit.last(self)
70
- rescue Grit::NoSuchPathError
71
- false
89
+ !empty?
72
90
  end
73
91
 
74
92
  def empty?
75
- !has_commits?
93
+ rugged.empty?
94
+ end
95
+
96
+ def repo_exists?
97
+ !!rugged
76
98
  end
77
99
 
78
100
  # Discovers the default branch based on the repository's available branches
@@ -85,8 +107,8 @@ module Gitlab
85
107
  nil
86
108
  elsif branch_names.length == 1
87
109
  branch_names.first
88
- elsif grit.head
89
- grit.head.name
110
+ elsif rugged.head
111
+ Ref.extract_branch_name(rugged.head.name)
90
112
  elsif branch_names.include?("master")
91
113
  "master"
92
114
  elsif
@@ -103,11 +125,11 @@ module Gitlab
103
125
  ref = ref || self.root_ref
104
126
  commit = Gitlab::Git::Commit.find(self, ref)
105
127
  return nil unless commit
106
-
128
+
107
129
  extension = nil
108
130
  git_archive_format = nil
109
131
  pipe_cmd = nil
110
-
132
+
111
133
  case format
112
134
  when "tar.bz2", "tbz", "tbz2", "tb2", "bz2"
113
135
  extension = ".tar.bz2"
@@ -293,6 +315,11 @@ module Gitlab
293
315
  end
294
316
  @refs_hash
295
317
  end
318
+
319
+ # Lookup for rugged object by oid
320
+ def lookup(oid)
321
+ rugged.lookup(oid)
322
+ end
296
323
  end
297
324
  end
298
325
  end
@@ -0,0 +1,6 @@
1
+ module Gitlab
2
+ module Git
3
+ class Tag < Ref
4
+ end
5
+ end
6
+ end
@@ -1,35 +1,75 @@
1
1
  module Gitlab
2
2
  module Git
3
3
  class Tree
4
- attr_accessor :id, :name, :path, :type, :mode, :commit_id, :submodule_url
4
+ attr_accessor :id, :root_id, :name, :path, :type,
5
+ :mode, :commit_id, :submodule_url
5
6
 
6
7
  class << self
7
- def where(repository, sha, path = '/')
8
- commit = Commit.find(repository, sha)
9
- grit_tree = commit.tree / path
10
-
11
- if grit_tree && grit_tree.respond_to?(:contents)
12
- grit_tree.contents.map do |entry|
13
- type = entry.class.to_s.split("::").last.downcase.to_sym
14
-
15
- Tree.new(
16
- id: entry.id,
17
- name: entry.name,
18
- type: type,
19
- mode: entry.mode,
20
- path: path == '/' ? entry.name : File.join(path, entry.name),
21
- commit_id: sha,
22
- submodule_url: (type == :submodule) ? entry.url(sha) : nil
23
- )
24
- end
8
+ # Get list of tree objects
9
+ # for repository based on commit sha and path
10
+ # Uses rugged for raw objects
11
+ def where(repository, sha, path = nil)
12
+ path = nil if path == '' || path == '/'
13
+
14
+ commit = repository.lookup(sha)
15
+ root_tree = commit.tree
16
+
17
+ tree = if path
18
+ id = Tree.find_id_by_path(repository, root_tree.oid, path)
19
+ if id
20
+ repository.lookup(id)
21
+ else
22
+ []
23
+ end
24
+ else
25
+ root_tree
26
+ end
27
+
28
+ tree.map do |entry|
29
+ Tree.new(
30
+ id: entry[:oid],
31
+ root_id: root_tree.oid,
32
+ name: entry[:name],
33
+ type: entry[:type] || :submodule,
34
+ mode: entry[:filemode],
35
+ path: path ? File.join(path, entry[:name]) : entry[:name],
36
+ commit_id: sha,
37
+ )
38
+ end
39
+ end
40
+
41
+ # Recursive search of tree id for path
42
+ #
43
+ # Ex.
44
+ # blog/ # oid: 1a
45
+ # app/ # oid: 2a
46
+ # models/ # oid: 3a
47
+ # views/ # oid: 4a
48
+ #
49
+ #
50
+ # Tree.find_id_by_path(repo, '1a', 'app/models') # => '3a'
51
+ #
52
+ def find_id_by_path(repository, root_id, path)
53
+ root_tree = repository.lookup(root_id)
54
+ path_arr = path.split('/')
55
+
56
+ entry = root_tree.find do |entry|
57
+ entry[:name] == path_arr[0] && entry[:type] == :tree
58
+ end
59
+
60
+ return nil unless entry
61
+
62
+ if path_arr.size > 1
63
+ path_arr.shift
64
+ find_id_by_path(repository, entry[:oid], path_arr.join('/'))
25
65
  else
26
- []
66
+ entry[:oid]
27
67
  end
28
68
  end
29
69
  end
30
70
 
31
71
  def initialize(options)
32
- %w(id name path type mode commit_id submodule_url).each do |key|
72
+ %w(id root_id name path type mode commit_id).each do |key|
33
73
  self.send("#{key}=", options[key.to_sym])
34
74
  end
35
75
  end
@@ -52,4 +92,3 @@ module Gitlab
52
92
  end
53
93
  end
54
94
  end
55
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitlab_git
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitriy Zaporozhets
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-30 00:00:00.000000000 Z
11
+ date: 2014-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gitlab-linguist
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: 4.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rugged
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.19.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.19.0
55
69
  description: GitLab wrapper around git objects
56
70
  email: dmitriy.zaporozhets@gmail.com
57
71
  executables: []
@@ -62,15 +76,19 @@ files:
62
76
  - lib/gitlab_git/blame.rb
63
77
  - lib/gitlab_git/blob.rb
64
78
  - lib/gitlab_git/blob_snippet.rb
79
+ - lib/gitlab_git/branch.rb
65
80
  - lib/gitlab_git/commit.rb
66
81
  - lib/gitlab_git/compare.rb
67
82
  - lib/gitlab_git/diff.rb
68
83
  - lib/gitlab_git/git_stats.rb
69
84
  - lib/gitlab_git/log_parser.rb
70
85
  - lib/gitlab_git/popen.rb
86
+ - lib/gitlab_git/ref.rb
71
87
  - lib/gitlab_git/repository.rb
72
88
  - lib/gitlab_git/stats.rb
89
+ - lib/gitlab_git/tag.rb
73
90
  - lib/gitlab_git/tree.rb
91
+ - VERSION
74
92
  homepage: http://rubygems.org/gems/gitlab_git
75
93
  licenses:
76
94
  - MIT