Skip to content

Integrate Rubocop for code quality #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Gemfile.lock
node_modules
package-lock.json
ai-prompt.erb
rubocop-report.json
9 changes: 9 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
inherit_from: .rubocop_todo.yml

inherit_gem:
main_branch_shared_rubocop_config: config/rubocop.yml

AllCops:
# Pin this project to Ruby 3.1 in case the shared config above is upgraded to 3.2
# or later.
TargetRubyVersion: 3.2
163 changes: 163 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2025-07-03 00:33:40 UTC using RuboCop version 1.77.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
Lint/DuplicateMethods:
Exclude:
- 'lib/git/base.rb'
- 'lib/git/worktree.rb'

# Offense count: 1
# Configuration parameters: AllowComments, AllowEmptyLambdas.
Lint/EmptyBlock:
Exclude:
- 'tests/units/test_archive.rb'

# Offense count: 3
# Configuration parameters: AllowedParentClasses.
Lint/MissingSuper:
Exclude:
- 'lib/git/branch.rb'
- 'lib/git/remote.rb'
- 'lib/git/worktree.rb'

# Offense count: 8
Lint/StructNewOverride:
Exclude:
- 'tests/units/test_command_line_error.rb'
- 'tests/units/test_failed_error.rb'
- 'tests/units/test_signaled_error.rb'
- 'tests/units/test_timeout_error.rb'

# Offense count: 2
# Configuration parameters: AllowComments, AllowNil.
Lint/SuppressedException:
Exclude:
- 'lib/git/lib.rb'
- 'tests/units/test_each_conflict.rb'

# Offense count: 1
Lint/UselessConstantScoping:
Exclude:
- 'lib/git/branch.rb'

# Offense count: 68
# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 109

# Offense count: 8
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
# AllowedMethods: refine
Metrics/BlockLength:
Max: 49

# Offense count: 21
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 898

# Offense count: 14
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/CyclomaticComplexity:
Max: 21

# Offense count: 111
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
Metrics/MethodLength:
Max: 51

# Offense count: 2
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Metrics/ParameterLists:
Max: 8

# Offense count: 12
# Configuration parameters: AllowedMethods, AllowedPatterns.
Metrics/PerceivedComplexity:
Max: 22

# Offense count: 1
Naming/AccessorMethodName:
Exclude:
- 'lib/git/object.rb'

# Offense count: 1
# Configuration parameters: ForbiddenDelimiters.
# ForbiddenDelimiters: (?i-mx:(^|\s)(EO[A-Z]{1}|END)(\s|$))
Naming/HeredocDelimiterNaming:
Exclude:
- 'tests/units/test_signed_commits.rb'

# Offense count: 5
# Configuration parameters: Mode, AllowedMethods, AllowedPatterns, AllowBangMethods.
# AllowedMethods: call
Naming/PredicateMethod:
Exclude:
- 'lib/git/branch.rb'
- 'lib/git/lib.rb'
- 'tests/units/test_command_line.rb'

# Offense count: 3
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros, UseSorbetSigs.
# NamePrefix: is_, has_, have_, does_
# ForbiddenPrefixes: is_, has_, have_, does_
# AllowedMethods: is_a?
# MethodDefinitionMacros: define_method, define_singleton_method
Naming/PredicatePrefix:
Exclude:
- 'spec/**/*'
- 'lib/git/base.rb'

# Offense count: 2
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
# SupportedStyles: snake_case, normalcase, non_integer
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
Naming/VariableNumber:
Exclude:
- 'tests/units/test_log.rb'
- 'tests/units/test_log_execute.rb'

# Offense count: 1
Style/ClassVars:
Exclude:
- 'lib/git/base.rb'

# Offense count: 66
# Configuration parameters: AllowedConstants.
Style/Documentation:
Enabled: false

# Offense count: 4
# This cop supports safe autocorrection (--autocorrect).
Style/IfUnlessModifier:
Exclude:
- 'lib/git/base.rb'
- 'lib/git/lib.rb'

# Offense count: 2
Style/MultilineBlockChain:
Exclude:
- 'lib/git/base.rb'

# Offense count: 5
# Configuration parameters: AllowedMethods.
# AllowedMethods: respond_to_missing?
Style/OptionalBooleanParameter:
Exclude:
- 'lib/git/base.rb'
- 'lib/git/object.rb'
- 'lib/git/path.rb'
- 'lib/git/stash.rb'

# Offense count: 64
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
# URISchemes: http, https
Layout/LineLength:
Max: 346
14 changes: 13 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'English'

Expand All @@ -18,6 +20,16 @@ task :test do
end
default_tasks << :test

# Rubocop

require 'rubocop/rake_task'

RuboCop::RakeTask.new

default_tasks << :rubocop

# YARD

unless RUBY_PLATFORM == 'java' || RUBY_ENGINE == 'truffleruby'
#
# YARD documentation for this project can NOT be built with JRuby.
Expand Down Expand Up @@ -51,7 +63,7 @@ default_tasks << :build
task default: default_tasks

desc 'Build and install the git gem and run a sanity check'
task :'test:gem' => :install do
task 'test:gem': :install do
output = `ruby -e "require 'git'; g = Git.open('.'); puts g.log.size"`.chomp
raise 'Gem test failed' unless $CHILD_STATUS.success?
raise 'Expected gem test to return an integer' unless output =~ /^\d+$/
Expand Down
6 changes: 3 additions & 3 deletions bin/command_line_test
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class CommandLineParser
def define_options
option_parser.banner = "Usage:\n#{command_template}"
option_parser.separator ''
option_parser.separator "Both --stdout and --stderr can be given."
option_parser.separator 'Both --stdout and --stderr can be given.'
option_parser.separator 'If --signal is given, --exitstatus is ignored.'
option_parser.separator 'If nothing is given, the script will exit with exitstatus 0.'
option_parser.separator ''
Expand Down Expand Up @@ -210,8 +210,8 @@ end

options = CommandLineParser.new.parse(*ARGV)

STDOUT.puts options.stdout if options.stdout
STDERR.puts options.stderr if options.stderr
$stdout.puts options.stdout if options.stdout
warn options.stderr if options.stderr
sleep options.duration unless options.duration.zero?
Process.kill(options.signal, Process.pid) if options.signal
exit(options.exitstatus) if options.exitstatus
65 changes: 35 additions & 30 deletions git.gemspec
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path('lib', __dir__)
require 'git/version'

Gem::Specification.new do |s|
s.author = 'Scott Chacon and others'
s.email = 'schacon@gmail.com'
s.homepage = 'http://github.com/ruby-git/ruby-git'
s.license = 'MIT'
s.name = 'git'
s.summary = 'An API to create, read, and manipulate Git repositories'
s.description = <<~DESCRIPTION
Gem::Specification.new do |spec|
spec.author = 'Scott Chacon and others'
spec.email = 'schacon@gmail.com'
spec.homepage = 'http://github.com/ruby-git/ruby-git'
spec.license = 'MIT'
spec.name = 'git'
spec.summary = 'An API to create, read, and manipulate Git repositories'
spec.description = <<~DESCRIPTION
The git gem provides an API that can be used to
create, read, and manipulate Git repositories by wrapping system calls to the git
command line. The API can be used for working with Git in complex interactions
including branching and merging, object inspection and manipulation, history, patch
generation and more.
DESCRIPTION
s.version = Git::VERSION
spec.version = Git::VERSION

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}/file/CHANGELOG.md"
spec.metadata['documentation_uri'] = "https://rubydoc.info/gems/#{spec.name}/#{spec.version}"
spec.metadata['rubygems_mfa_required'] = 'true'

s.metadata['homepage_uri'] = s.homepage
s.metadata['source_code_uri'] = s.homepage
s.metadata['changelog_uri'] = "https://rubydoc.info/gems/#{s.name}/#{s.version}/file/CHANGELOG.md"
s.metadata['documentation_uri'] = "https://rubydoc.info/gems/#{s.name}/#{s.version}"
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.2.0'
spec.requirements = ['git 2.28.0 or greater']

s.require_paths = ['lib']
s.required_ruby_version = '>= 3.2.0'
s.requirements = ['git 2.28.0 or greater']
spec.add_dependency 'activesupport', '>= 5.0'
spec.add_dependency 'addressable', '~> 2.8'
spec.add_dependency 'process_executer', '~> 4.0'
spec.add_dependency 'rchardet', '~> 1.9'

s.add_runtime_dependency 'activesupport', '>= 5.0'
s.add_runtime_dependency 'addressable', '~> 2.8'
s.add_runtime_dependency 'process_executer', '~> 4.0'
s.add_runtime_dependency 'rchardet', '~> 1.9'
spec.add_development_dependency 'create_github_release', '~> 2.1'
spec.add_development_dependency 'main_branch_shared_rubocop_config', '~> 0.1'
spec.add_development_dependency 'minitar', '~> 1.0'
spec.add_development_dependency 'mocha', '~> 2.7'
spec.add_development_dependency 'rake', '~> 13.2'
spec.add_development_dependency 'rubocop', '~> 1.77'

s.add_development_dependency 'create_github_release', '~> 2.1'
s.add_development_dependency 'minitar', '~> 1.0'
s.add_development_dependency 'mocha', '~> 2.7'
s.add_development_dependency 'rake', '~> 13.2'
s.add_development_dependency 'test-unit', '~> 3.6'
spec.add_development_dependency 'test-unit', '~> 3.6'

unless RUBY_PLATFORM == 'java'
s.add_development_dependency 'redcarpet', '~> 3.6'
s.add_development_dependency 'yard', '~> 0.9', '>= 0.9.28'
s.add_development_dependency 'yardstick', '~> 0.9'
spec.add_development_dependency 'redcarpet', '~> 3.6'
spec.add_development_dependency 'yard', '~> 0.9', '>= 0.9.28'
spec.add_development_dependency 'yardstick', '~> 0.9'
end

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
s.files = Dir.chdir(File.expand_path(__dir__)) do
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(tests|spec|features|bin)/}) }
end
end
28 changes: 14 additions & 14 deletions lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ module Git
# @author Scott Chacon (mailto:schacon@gmail.com)
#
module Git
#g.config('user.name', 'Scott Chacon') # sets value
#g.config('user.email', 'email@email.com') # sets value
#g.config('user.name') # returns 'Scott Chacon'
#g.config # returns whole config hash
# g.config('user.name', 'Scott Chacon') # sets value
# g.config('user.email', 'email@email.com') # sets value
# g.config('user.name') # returns 'Scott Chacon'
# g.config # returns whole config hash
def config(name = nil, value = nil)
lib = Git::Lib.new
if(name && value)
if name && value
# set value
lib.config_set(name, value)
elsif (name)
elsif name
# return value
lib.config_get(name)
else
Expand Down Expand Up @@ -191,7 +191,7 @@ def self.bare(git_dir, options = {})
# of the cloned local working copy or cloned repository.
#
def self.clone(repository_url, directory = nil, options = {})
clone_to_options = options.select { |key, _value| %i[bare mirror].include?(key) }
clone_to_options = options.slice(:bare, :mirror)
directory ||= Git::URL.clone_to(repository_url, **clone_to_options)
Base.clone(repository_url, directory, options)
end
Expand Down Expand Up @@ -245,23 +245,23 @@ def self.default_branch(repository, options = {})
# remote, 'origin.'
def self.export(repository, name, options = {})
options.delete(:remote)
repo = clone(repository, name, {:depth => 1}.merge(options))
repo = clone(repository, name, { depth: 1 }.merge(options))
repo.checkout("origin/#{options[:branch]}") if options[:branch]
FileUtils.rm_r File.join(repo.dir.to_s, '.git')
end

# Same as g.config, but forces it to be at the global level
#
#g.config('user.name', 'Scott Chacon') # sets value
#g.config('user.email', 'email@email.com') # sets value
#g.config('user.name') # returns 'Scott Chacon'
#g.config # returns whole config hash
# g.config('user.name', 'Scott Chacon') # sets value
# g.config('user.email', 'email@email.com') # sets value
# g.config('user.name') # returns 'Scott Chacon'
# g.config # returns whole config hash
def self.global_config(name = nil, value = nil)
lib = Git::Lib.new(nil, nil)
if(name && value)
if name && value
# set value
lib.global_config_set(name, value)
elsif (name)
elsif name
# return value
lib.global_config_get(name)
else
Expand Down
10 changes: 5 additions & 5 deletions lib/git/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class Author
attr_accessor :name, :email, :date

def initialize(author_string)
if m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
@name = m[1]
@email = m[2]
@date = Time.at(m[3].to_i)
end
return unless (m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string))

@name = m[1]
@email = m[2]
@date = Time.at(m[3].to_i)
end
end
end
Loading