Skip to content
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
52 changes: 40 additions & 12 deletions lib/ruby_git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,53 @@
require 'ruby_git/version'
require 'ruby_git/worktree'

require 'null_logger'

# RubyGit is an object-oriented wrapper for the `git` command line tool for
# working with Worktrees and Repositories. It tries to make more sense out
# of the Git command line.
#
# @api public
#
module RubyGit
# Return information about the git binary used by this library
#
# Use this object to set the path to the git binary to use or to see the
# path being used.
#
# @example Setting the git binary path
# RubyGit.git.path = '/usr/local/bin/git'
#
# @return [RubyGit::GitBinary]
#
def self.git
(@git ||= RubyGit::GitBinary.new)
@git = RubyGit::GitBinary.new

class << self
# Information about the git binary used by the RubyGit gem
#
# Use this object to set the path to the git binary to use or to see the
# path being used.
#
# @example Setting the git binary path
# RubyGit.git.path = '/usr/local/bin/git'
#
# @return [RubyGit::GitBinary] the git binary object
#
attr_reader :git
end

@logger = NullLogger.new

class << self
# The logger used by the RubyGit gem
#
# The default value is a NullLogger
#
# @example Using the logger
# RubyGit.logger.debug('Debug message')
#
# @example Setting the logger
# require 'logger'
# require 'stringio'
# log_device = StringIO.new
# RubyGit.logger = Logger.new(log_device, level: Logger::DEBUG)
# RubyGit.logger.debug('Debug message')
# log_device.string.include?('Debug message')
# => true
#
# @return [Logger] the logger used by the RubyGit gem
#
attr_accessor :logger
end

# Create an empty Git repository under the root worktree `path`
Expand Down
1 change: 1 addition & 0 deletions lib/ruby_git/worktree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def initialize(worktree_path)
raise RubyGit::Error, "Path '#{worktree_path}' not valid." unless File.directory?(worktree_path)

@path = root_path(worktree_path)
RubyGit.logger.debug("Created #{inspect}")
end

# Find the root path of a worktree containing `path`
Expand Down
2 changes: 2 additions & 0 deletions ruby_git.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_runtime_dependency 'null-logger', '~> 0.1'

spec.add_development_dependency 'bump', '~> 0.9'
spec.add_development_dependency 'bundler-audit', '~> 0.7'
spec.add_development_dependency 'github_changelog_generator', '~> 1.15'
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/ruby_git/worktree_initialize_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'logger'
require 'stringio'
require 'tmpdir'

RSpec.describe RubyGit::Worktree do
describe '.initialize' do
subject { described_class.open(worktree_path) }
let(:tmpdir) { Dir.mktmpdir }
after { FileUtils.rm_rf(tmpdir) if File.exist?(tmpdir) }

context 'with a valid worktree path' do
let(:worktree_path) { tmpdir }
before do
raise RuntimeError unless system('git init', chdir: worktree_path, %i[out err] => IO::NULL)
end
it 'should log that a Worktree object was created at debug level' do
log_device = StringIO.new
saved_logger = RubyGit.logger
RubyGit.logger = Logger.new(log_device, level: Logger::DEBUG)
RubyGit::Worktree.new(worktree_path)
RubyGit.logger = saved_logger
expect(log_device.string).to include(' : Created #<RubyGit::Worktree:')
end
end
end
end
15 changes: 15 additions & 0 deletions spec/lib/ruby_git_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
# frozen_string_literal: true

require 'tmpdir'
require 'logger'

RSpec.describe RubyGit do
it 'should have a version number' do
expect(RubyGit::VERSION).not_to be nil
end

describe '.logger' do
subject { described_class.logger }

context 'when a logger has not be set' do
it { is_expected.to be_kind_of(NullLogger) }
end

context 'when a logger has been set' do
let(:logger) { Logger.new($stdout, level: Logger::DEBUG) }
before { RubyGit.logger = logger }
it { is_expected.to eq(logger) }
end
end

describe '.git_binary' do
subject { described_class.git }
it { is_expected.to be_kind_of(RubyGit::GitBinary) }
Expand Down