Skip to content

Commit c010a86

Browse files
committed
fix: fix Rubocop Style/OptionalBooleanParameter offense
1 parent dd4e4ec commit c010a86

File tree

5 files changed

+53
-27
lines changed

5 files changed

+53
-27
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 5
10-
# Configuration parameters: AllowedMethods.
11-
# AllowedMethods: respond_to_missing?
12-
Style/OptionalBooleanParameter:
13-
Exclude:
14-
- 'lib/git/base.rb'
15-
- 'lib/git/object.rb'
16-
- 'lib/git/path.rb'
17-
- 'lib/git/stash.rb'
18-
199
# Offense count: 64
2010
# This cop supports safe autocorrection (--autocorrect).
2111
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.

lib/git/base.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,24 @@ def repo_size
241241
.sum { |file| File.stat(file).size.to_i }
242242
end
243243

244-
def set_index(index_file, check = true)
244+
def set_index(index_file, check = nil, must_exist: nil)
245+
Git::Deprecation.warn('The "check" argument is deprecated and will be removed in a future version. Use "must_exist:" instead.') unless check.nil?
246+
247+
# default is true
248+
must_exist = must_exist.nil? && check.nil? ? true : must_exist | check
249+
245250
@lib = nil
246-
@index = Git::Index.new(index_file.to_s, check)
251+
@index = Git::Index.new(index_file.to_s, must_exist:)
247252
end
248253

249-
def set_working(work_dir, check = true)
254+
def set_working(work_dir, check = nil, must_exist: nil)
255+
Git::Deprecation.warn('The "check" argument is deprecated and will be removed in a future version. Use "must_exist:" instead.') unless check.nil?
256+
257+
# default is true
258+
must_exist = must_exist.nil? && check.nil? ? true : must_exist | check
259+
250260
@lib = nil
251-
@working_directory = Git::WorkingDirectory.new(work_dir.to_s, check)
261+
@working_directory = Git::WorkingDirectory.new(work_dir.to_s, must_exist:)
252262
end
253263

254264
# returns +true+ if the branch exists locally
@@ -258,7 +268,7 @@ def local_branch?(branch)
258268
end
259269

260270
def is_local_branch?(branch) # rubocop:disable Naming/PredicatePrefix
261-
Git.deprecated('Git::Base#is_local_branch? is deprecated. Use Git::Base#local_branch? instead.')
271+
Git.deprecation('Git::Base#is_local_branch? is deprecated. Use Git::Base#local_branch? instead.')
262272
local_branch?(branch)
263273
end
264274

@@ -759,7 +769,7 @@ def status
759769

760770
# @return [Git::Object::Tag] a tag object
761771
def tag(tag_name)
762-
Git::Object.new(self, tag_name, 'tag', true)
772+
Git::Object::Tag.new(self, tag_name)
763773
end
764774

765775
# Find as good common ancestors as possible for a merge

lib/git/object.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,36 @@ def check_commit
251251
# Annotated tags contain additional metadata such as the tagger's name, email, and
252252
# the date when the tag was created, along with a message.
253253
#
254+
# TODO: Annotated tags are not objects
255+
#
254256
class Tag < AbstractObject
255257
attr_accessor :name
256258

257-
def initialize(base, sha, name)
259+
# @overload initialize(base, name)
260+
# @param base [Git::Base] The Git base object
261+
# @param name [String] The name of the tag
262+
#
263+
# @overload initialize(base, sha, name)
264+
# @param base [Git::Base] The Git base object
265+
# @param sha [String] The SHA of the tag object
266+
# @param name [String] The name of the tag
267+
#
268+
def initialize(base, sha, name = nil)
269+
if name.nil?
270+
name = sha
271+
sha = base.lib.tag_sha(name)
272+
raise Git::UnexpectedResultError, "Tag '#{name}' does not exist." if sha == ''
273+
end
274+
258275
super(base, sha)
276+
259277
@name = name
260278
@annotated = nil
261279
@loaded = false
262280
end
263281

264282
def annotated?
265-
@annotated ||= (@base.lib.cat_file_type(name) == 'tag')
283+
@annotated = @annotated.nil? ? (@base.lib.cat_file_type(name) == 'tag') : @annotated
266284
end
267285

268286
def message
@@ -298,12 +316,10 @@ def check_tag
298316

299317
# if we're calling this, we don't know what type it is yet
300318
# so this is our little factory method
301-
def self.new(base, objectish, type = nil, is_tag = false)
319+
def self.new(base, objectish, type = nil, is_tag = false) # rubocop:disable Style/OptionalBooleanParameter
302320
if is_tag
303-
sha = base.lib.tag_sha(objectish)
304-
raise Git::UnexpectedResultError, "Tag '#{objectish}' does not exist." if sha == ''
305-
306-
return Git::Object::Tag.new(base, sha, objectish)
321+
Git::Deprecation.warn('Git::Object.new with is_tag argument is deprecated. Use Git::Object::Tag.new instead.')
322+
return Git::Object::Tag.new(base, objectish)
307323
end
308324

309325
type ||= base.lib.cat_file_type(objectish)

lib/git/path.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ module Git
99
class Path
1010
attr_accessor :path
1111

12-
def initialize(path, check_path = true)
12+
def initialize(path, check_path = nil, must_exist: nil)
13+
Git::Deprecation.warn('The "check_path" argument is deprecated and will be removed in a future version. Use "must_exist:" instead.') unless check_path.nil?
14+
15+
# default is true
16+
must_exist = must_exist.nil? && check_path.nil? ? true : must_exist || check_path
17+
1318
path = File.expand_path(path)
1419

15-
raise ArgumentError, 'path does not exist', [path] if check_path && !File.exist?(path)
20+
raise ArgumentError, 'path does not exist', [path] if must_exist && !File.exist?(path)
1621

1722
@path = path
1823
end

lib/git/stash.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
module Git
44
# A stash in a Git repository
55
class Stash
6-
def initialize(base, message, existing = false)
6+
def initialize(base, message, existing = nil, save: nil)
7+
Git::Deprecation.warn('The "existing" argument is deprecated and will be removed in a future version. Use "save:" instead.') unless existing.nil?
8+
9+
# default is false
10+
save = existing.nil? && save.nil? ? false : save | existing
11+
712
@base = base
813
@message = message
9-
save unless existing
14+
self.save unless save
1015
end
1116

1217
def save

0 commit comments

Comments
 (0)