gazelle_styleguide 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjQxOGYzZTNlYTAzNWFiNjJlZmMwNjhhNDE1YzVmZTNiYTYzZTlkNA==
4
+ ZWRkZWZlY2I0OThiZmE2ZDBjZWYxN2JiMTE2MDcyNjk1MGIxYjY0OQ==
5
5
  data.tar.gz: !binary |-
6
- MmExNTRkODM4YWFmMjE5NjJkNjk2ODFkYzhjYTZlMWI4YzA5NGFiNg==
6
+ OTQxOTM4YjI4YjJmNDZkNWM0NWRhZWViYWM0ZTRiMDkzM2JkZjg4Mg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NjBmNDI0MWEyOTc5MWU1MTMyMmE4MmJiY2NmZTc3NjFmNmU4MDQwZjJlZjA2
10
- MjRiNGYwOGI0OTlhOWNlNTdlYTVmOGJiODYxMWNiZDc4Y2IxOWMyMTRkNTIz
11
- YTE4MmI1OTgyOWFhYmFiYzliZTUzMjgxNWExNjI1NTdmNjVhZjg=
9
+ MjQ0OGJiYjllNDY3MmQwZjQxMTk0MjFjZDZjYTc3NjJkY2NmMzZmZGExNWIy
10
+ OWJhODU4YTlhYjVkMzkxNTE2YmE0ZmZjOWVjYzQ1OWVhNDE1MGNlZTNlYmY3
11
+ ZjNmMDc0MTM0YWMyYmExMjVmOGNmYzYxNDhhY2IyMzk4ZTFkNjI=
12
12
  data.tar.gz: !binary |-
13
- OTk4NGNmZGViNWYyNmIzOWY4NWVkYzk0OGIzZDhiZmJiY2EyNjNlYTNmZDY4
14
- MTZkMTlkMmUwNzk4MDU5MzQ0ZGVjYjBmZjQyZjIyOTEyMjRmYzBjOTcxODI5
15
- NjcyMDhhYTRlOWE5MmY2OTg5OTQwMjlmM2RmZGU3MjMxMWY0NDU=
13
+ ODE0NjIyNTQ0NTg5YzJlNTM3ZWU5NjE3NzNiNDg1ZDhkZTQ2NzZiNTA4Mzdl
14
+ ZTYxZTgzNDgyMTMxMjE0NjM2NjY1YTI2Yzg4NjFjZmY5MmQ1Zjk2NjI5MWMx
15
+ MjYxNTM4YjUwMDg2MWVhY2NlOTEyYTU2YjA1NTJlMzgzMTQ0Yzk=
data/config/rubocop.yml CHANGED
@@ -386,12 +386,6 @@ SpaceAroundEqualsInParameterDefault:
386
386
  - space
387
387
  - no_space
388
388
 
389
- TrailingBlankLines:
390
- EnforcedStyle: final_newline
391
- SupportedStyles:
392
- - final_newline
393
- - final_blank_line
394
-
395
389
  TrailingComma:
396
390
  EnforcedStyleForMultiline: no_comma
397
391
  SupportedStyles:
@@ -1,5 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'grit'
3
+ require 'grit_ext/status' # add ignored support
3
4
  require 'thor'
4
5
  require 'fileutils'
5
6
 
@@ -20,6 +21,7 @@ module GazelleStyleguide
20
21
  desc 'lint [flags|FILES]', 'Runs linters'
21
22
  option :all, aliases: :a
22
23
  option :changed, aliases: :c
24
+ option :staged, aliases: :s
23
25
  def lint(*files)
24
26
  require 'pre-commit'
25
27
  files = lint_files(files, options)
@@ -27,7 +29,19 @@ module GazelleStyleguide
27
29
  PreCommit::Runner.new($stderr, files).run
28
30
  end
29
31
 
30
- private
32
+ desc 'fix [flags|FILES]', 'Runs rubocop fix against ruby files'
33
+ option :all, aliases: :a
34
+ option :changed, aliases: :c
35
+ option :staged, aliases: :s
36
+ def fix(*files)
37
+ require 'rubocop'
38
+ files = lint_files(files, options).select{|file| file =~ /\.rb|Gemfile|Rakefile|\.task|\.rake/}
39
+ args = ['-a', '-c', GazelleStyleguide.config_for('rubocop.yml')] + files
40
+
41
+ Rubocop::CLI.new.run(args)
42
+ end
43
+
44
+ private
31
45
 
32
46
  def install_pre_commit
33
47
  require 'pre-commit/installer'
@@ -64,14 +78,20 @@ module GazelleStyleguide
64
78
  if files.any?
65
79
  files
66
80
  elsif options[:all]
67
- Dir['./**/*'].select { |f| File.file?(f) }
81
+ Dir['./**/*'].select{|f| File.file?(f)}
82
+ elsif options[:staged]
83
+ staged_files
68
84
  else
69
85
  changed_files
70
86
  end
71
87
  end
72
88
 
89
+ def staged_files
90
+ repo.status.changed.keys + repo.status.added.keys
91
+ end
92
+
73
93
  def changed_files
74
- repo.status.files.select { |file, status| status.type == 'M' || status.untracked }.keys
94
+ repo.status.untracked.keys + staged_files
75
95
  end
76
96
 
77
97
  def repo
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Styleguide version
4
4
  module GazelleStyleguide
5
- VERSION = '0.0.4'
5
+ VERSION = '0.0.5'
6
6
  end
@@ -0,0 +1,191 @@
1
+ module Grit
2
+ class Status
3
+ include Enumerable
4
+
5
+ attr_reader :files
6
+
7
+ @base = nil
8
+ @files = nil
9
+
10
+ def initialize(base)
11
+ @base = base
12
+ construct_status
13
+ end
14
+
15
+ def changed
16
+ @files.select{|k, f| f.type == 'M'}
17
+ end
18
+
19
+ def added
20
+ @files.select{|k, f| f.type == 'A'}
21
+ end
22
+
23
+ def deleted
24
+ @files.select{|k, f| f.type == 'D'}
25
+ end
26
+
27
+ def untracked
28
+ @files.select{|k, f| f.untracked && !f.ignored}
29
+ end
30
+
31
+ def pretty
32
+ out = ''
33
+ each do |file|
34
+ out << file.path
35
+ out << "\n\tsha(r) " + file.sha_repo.to_s + ' ' + file.mode_repo.to_s
36
+ out << "\n\tsha(i) " + file.sha_index.to_s + ' ' + file.mode_index.to_s
37
+ out << "\n\ttype " + file.type.to_s
38
+ out << "\n\tstage " + file.stage.to_s
39
+ out << "\n\tuntrac " + file.untracked.to_s
40
+ out << "\n"
41
+ end
42
+ out << "\n"
43
+ out
44
+ end
45
+
46
+ # enumerable method
47
+
48
+ def [](file)
49
+ @files[file]
50
+ end
51
+
52
+ def each
53
+ @files.each do |k, file|
54
+ yield file
55
+ end
56
+ end
57
+
58
+ class StatusFile
59
+ attr_accessor :path, :type, :stage, :untracked, :ignored
60
+ attr_accessor :mode_index, :mode_repo
61
+ attr_accessor :sha_index, :sha_repo
62
+
63
+ @base = nil
64
+
65
+ def initialize(base, hash)
66
+ @base = base
67
+ @path = hash[:path]
68
+ @type = hash[:type]
69
+ @stage = hash[:stage]
70
+ @mode_index = hash[:mode_index]
71
+ @mode_repo = hash[:mode_repo]
72
+ @sha_index = hash[:sha_index]
73
+ @sha_repo = hash[:sha_repo]
74
+ @untracked = hash[:untracked]
75
+ @ignored = hash[:ignored]
76
+ end
77
+
78
+ def blob(type = :index)
79
+ if type == :repo
80
+ @base.object(@sha_repo)
81
+ else
82
+ @base.object(@sha_index) rescue @base.object(@sha_repo)
83
+ end
84
+ end
85
+ end
86
+
87
+ private
88
+
89
+ def construct_status
90
+ @files = ls_files
91
+
92
+ Dir.chdir(@base.working_dir) do
93
+ # find untracked in working dir
94
+ ls_untracked.each do |path, data|
95
+ @files[path] = data unless @files[path]
96
+ end
97
+
98
+ # find modified in tree
99
+ diff_files.each do |path, data|
100
+ @files[path] ? @files[path].merge!(data) : @files[path] = data
101
+ end
102
+
103
+ # find added but not committed - new files
104
+ diff_index('HEAD').each do |path, data|
105
+ @files[path] ? @files[path].merge!(data) : @files[path] = data
106
+ end
107
+
108
+ @files.each do |k, file_hash|
109
+ @files[k] = StatusFile.new(@base, file_hash)
110
+ end
111
+ end
112
+ end
113
+
114
+ # compares the index and the working directory
115
+ def diff_files
116
+ hsh = {}
117
+ @base.git.diff_files.split("\n").each do |line|
118
+ (info, file) = line.split("\t")
119
+ (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
120
+ hsh[file] = {path: file, mode_file: mode_src.to_s[1, 7], mode_index: mode_dest,
121
+ sha_file: sha_src, sha_index: sha_dest, type: type}
122
+ end
123
+ hsh
124
+ end
125
+
126
+ # compares the index and the repository
127
+ def diff_index(treeish)
128
+ hsh = {}
129
+ @base.git.diff_index({}, treeish).split("\n").each do |line|
130
+ (info, file) = line.split("\t")
131
+ (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
132
+ hsh[file] = {path: file, mode_repo: mode_src.to_s[1, 7], mode_index: mode_dest,
133
+ sha_repo: sha_src, sha_index: sha_dest, type: type}
134
+ end
135
+ hsh
136
+ end
137
+
138
+ def ls_files
139
+ hsh = {}
140
+ lines = @base.git.ls_files(stage: true)
141
+ lines.split("\n").each do |line|
142
+ (info, file) = line.split("\t")
143
+ (mode, sha, stage) = info.split
144
+ hsh[file] = {path: file, mode_index: mode, sha_index: sha, stage: stage}
145
+ end
146
+ hsh
147
+ end
148
+
149
+ def ls_untracked
150
+ hsh, wdir = {}, @base.working_dir
151
+
152
+ # directories and hidden files are skiped so as to preserve
153
+ # backward compatibility with 2.4.1
154
+ skip = lambda{|f|
155
+ File.directory?(File.join(wdir, f)) || f =~ /^\./
156
+ }
157
+
158
+ # `git ls-files --others --ignore --exclude-standard --directory`
159
+ # returns untracked and ignored files as well as ignored
160
+ # directories
161
+ ignored_dirs = []
162
+ @base.git.ls_files(:others => true,
163
+ :ignored => true, :directory => true,
164
+ :"exclude-standard" => true).
165
+ split("\n").each do |file|
166
+ if File.directory?(File.join(wdir, file))
167
+ ignored_dirs << file
168
+ elsif !skip[file]
169
+ hsh[file] = {path: file,
170
+ untracked: true,
171
+ ignored: true}
172
+ end
173
+ end
174
+
175
+ # `git ls-files --others` is used for remaining files
176
+ @base.git.ls_files(others: true).
177
+ split("\n").each do |file|
178
+ if ignored_dirs.any?{|d| file.index(d) == 0}
179
+ hsh[file] = {path: file,
180
+ untracked: true,
181
+ ignored: true}
182
+ elsif !(skip[file] || hsh[file])
183
+ hsh[file] = {path: file,
184
+ untracked: true}
185
+ end
186
+ end
187
+
188
+ hsh
189
+ end
190
+ end
191
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gazelle_styleguide
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Allen Madsen
@@ -162,6 +162,7 @@ files:
162
162
  - lib/gazelle_styleguide.rb
163
163
  - lib/gazelle_styleguide/cli.rb
164
164
  - lib/gazelle_styleguide/version.rb
165
+ - lib/grit_ext/status.rb
165
166
  - spec/gazelle_styleguide_spec.rb
166
167
  - spec/spec_helper.rb
167
168
  homepage: https://github.com/secondrotation/gazelle_styleguide