gitlab_git 10.5.0 → 10.6.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: 5186b04c57f9ce96224e1ce5b567a935d3f87937
4
- data.tar.gz: edd381e264f533ebfd893e3be1c1b53f4f09bf91
3
+ metadata.gz: 55838a68588d3d13baf3b25006303104097c90f0
4
+ data.tar.gz: 7851cc80bdabd78e1883d4db0fecec5e1cba5e8d
5
5
  SHA512:
6
- metadata.gz: a07ce78888f76148a23822e49dd79707d47d0cb9f7ce1ae4f14b3053ad124865a7ddcd6089419ecab7524ae24e76e39a95a620cfa3ec75f2098ffb19bceafc0d
7
- data.tar.gz: b8721276ada106d0da73fce92d27f67e4004cf994f32d691d10e239ad898c52b4cd5a2f367948ca7da6ca4f41c1a839829879f67b1452964a6d65abca6f0368f
6
+ metadata.gz: be8c4fe7d0dcdfe967ffdeed219b2b5237580ce0bdf0ad863757783233984e748ecf3b83ad7ef87102076b7fb5a076446532225d58e6342809a3152ba6061f5c
7
+ data.tar.gz: 6b12bdfb2628285e9f3430fa1d7594fcae55160e700abc7a805f9f28a3e1592eb112527b85e1606a1eb94c296857e4f6073efaefdf6210d3f30e17851f8dfce6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 10.5.0
1
+ 10.6.0
data/lib/gitlab_git.rb CHANGED
@@ -25,3 +25,4 @@ require_relative "gitlab_git/ref"
25
25
  require_relative "gitlab_git/branch"
26
26
  require_relative "gitlab_git/tag"
27
27
  require_relative "gitlab_git/util"
28
+ require_relative "gitlab_git/attributes"
@@ -0,0 +1,116 @@
1
+ module Gitlab
2
+ module Git
3
+ # Class for parsing Git attribute files and extracting the attributes for
4
+ # file patterns.
5
+ #
6
+ # Unlike Rugged this parser only needs a single IO call (a call to `open`),
7
+ # vastly reducing the time spent in extracting attributes.
8
+ #
9
+ # This class _only_ supports parsing the attributes file located at
10
+ # `$GIT_DIR/info/attributes` as GitLab doesn't use any other files
11
+ # (`.gitattributes` is copied to this particular path).
12
+ #
13
+ # Basic usage:
14
+ #
15
+ # attributes = Gitlab::Git::Attributes.new(some_repo.path)
16
+ #
17
+ # attributes.attributes('README.md') # => { "eol" => "lf }
18
+ class Attributes
19
+ # path - The path to the Git repository.
20
+ def initialize(path)
21
+ @path = path
22
+ @patterns = nil
23
+ end
24
+
25
+ # Returns all the Git attributes for the given path.
26
+ #
27
+ # path - A path to a file for which to get the attributes.
28
+ #
29
+ # Returns a Hash.
30
+ def attributes(path)
31
+ patterns.each do |pattern, attrs|
32
+ return attrs if File.fnmatch(pattern, path)
33
+ end
34
+
35
+ {}
36
+ end
37
+
38
+ # Returns a Hash containing the file patterns and their attributes.
39
+ def patterns
40
+ @patterns ||= parse_file
41
+ end
42
+
43
+ # Parses an attribute string.
44
+ #
45
+ # These strings can be in the following formats:
46
+ #
47
+ # text # => { "text" => true }
48
+ # -text # => { "text" => false }
49
+ # key=value # => { "key" => "value" }
50
+ #
51
+ # string - The string to parse.
52
+ #
53
+ # Returns a Hash containing the attributes and their values.
54
+ def parse_attributes(string)
55
+ values = {}
56
+ dash = '-'
57
+ equal = '='
58
+
59
+ string.split(/\s+/).each do |chunk|
60
+ # Data such as "foo = bar" should be treated as "foo" and "bar" being
61
+ # separate boolean attributes.
62
+ next if chunk == equal
63
+
64
+ # Input: "-foo"
65
+ if chunk.start_with?(dash)
66
+ key = chunk.byteslice(1, chunk.length - 1)
67
+
68
+ values[key] = false
69
+
70
+ # Input: "foo=bar"
71
+ elsif chunk.include?(equal)
72
+ key, value = chunk.split(equal, 2)
73
+
74
+ values[key] = value
75
+
76
+ # Input: "foo"
77
+ else
78
+ values[chunk] = true
79
+ end
80
+ end
81
+
82
+ values
83
+ end
84
+
85
+ # Iterates over every line in the attributes file.
86
+ def each_line
87
+ full_path = File.join(@path, 'info/attributes')
88
+
89
+ File.open(full_path, 'r') do |handle|
90
+ handle.each_line do |line|
91
+ yield line.strip
92
+ end
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ # Parses the Git attributes file.
99
+ def parse_file
100
+ pairs = []
101
+ comment = '#'
102
+
103
+ each_line do |line|
104
+ next if line.start_with?(comment) || line.empty?
105
+
106
+ pattern, attrs = line.split(/\s+/, 2)
107
+
108
+ pairs << [pattern, parse_attributes(attrs)]
109
+ end
110
+
111
+ # Newer entries take precedence over older entries.
112
+ pairs.reverse.to_h
113
+ end
114
+ end
115
+ end
116
+ end
@@ -27,14 +27,12 @@ module Gitlab
27
27
  # Rugged repo object
28
28
  attr_reader :rugged
29
29
 
30
- # Define a delegator for the rugged attributes
31
- def_delegator :rugged, :attributes
32
-
33
30
  # 'path' must be the path to a _bare_ git repository, e.g.
34
31
  # /path/to/my-repo.git
35
32
  def initialize(path)
36
33
  @path = path
37
34
  @name = path.split("/").last
35
+ @attributes = Attributes.new(path)
38
36
  end
39
37
 
40
38
  # Default branch in the repository
@@ -978,8 +976,14 @@ module Gitlab
978
976
 
979
977
  # Checks if the blob should be diffable according to its attributes
980
978
  def diffable?(blob)
981
- blob_attributes = attributes(blob.path).to_h
982
- blob_attributes.fetch('diff', blob.text?)
979
+ attributes(blob.path).fetch('diff') { blob.text? }
980
+ end
981
+
982
+ # Returns the Git attributes for the given file path.
983
+ #
984
+ # See `Gitlab::Git::Attributes` for more information.
985
+ def attributes(path)
986
+ @attributes.attributes(path)
983
987
  end
984
988
 
985
989
  private
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: 10.5.0
4
+ version: 10.6.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: 2016-08-29 00:00:00.000000000 Z
11
+ date: 2016-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: github-linguist
@@ -88,6 +88,7 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - VERSION
90
90
  - lib/gitlab_git.rb
91
+ - lib/gitlab_git/attributes.rb
91
92
  - lib/gitlab_git/blame.rb
92
93
  - lib/gitlab_git/blob.rb
93
94
  - lib/gitlab_git/blob_snippet.rb
@@ -125,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
126
  version: '0'
126
127
  requirements: []
127
128
  rubyforge_project:
128
- rubygems_version: 2.2.5
129
+ rubygems_version: 2.4.8
129
130
  signing_key:
130
131
  specification_version: 4
131
132
  summary: Gitlab::Git library