diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..84e28cd --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + branches: [ master, main ] + pull_request: + branches: [ master, main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: ['3.0', '3.1', '3.2', '3.3'] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Ruby ${{ matrix.ruby-version }} + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + + - name: Configure git + run: | + git config --global user.name "Test User" + git config --global user.email "test@example.com" + git config --global init.defaultBranch master + + - name: Run tests + run: bundle exec rake spec diff --git a/.gitignore b/.gitignore index 3d120c8..f659bb0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ doc/* pkg/* test/repo html/* +vendor/ +.bundle/ +Gemfile.lock diff --git a/Gemfile b/Gemfile index 886bd8b..ab3ae0a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ +source 'https://rubygems.org' + gem 'rake' gem 'rspec' diff --git a/git_store.gemspec b/git_store.gemspec index 2c10386..921df6d 100644 --- a/git_store.gemspec +++ b/git_store.gemspec @@ -13,7 +13,6 @@ repository. GitStore checks out the repository into a in-memory representation, which can be modified and finally committed. END s.require_path = 'lib' - s.has_rdoc = true s.extra_rdoc_files = ['README.md'] s.files = %w{ .gitignore @@ -34,7 +33,10 @@ test/bare_store_spec.rb test/benchmark.rb test/commit_spec.rb test/git_store_spec.rb +test/helper.rb +test/spec_helper.rb test/tree_spec.rb +test/user_spec.rb } end diff --git a/lib/git_store.rb b/lib/git_store.rb index d5d91eb..671becc 100644 --- a/lib/git_store.rb +++ b/lib/git_store.rb @@ -59,8 +59,8 @@ class GitStore # Initialize a store. def initialize(path, branch = 'master', bare = false) - if bare && !File.exists?("#{path}") or - !bare && !File.exists?("#{path}/.git") + if bare && !File.exist?("#{path}") or + !bare && !File.exist?("#{path}/.git") raise ArgumentError, "first argument must be a valid Git repository: `#{path}'" end @@ -105,7 +105,7 @@ def git_path # # Returns the object id of the last commit. def read_head_id - File.read(head_path).strip if File.exists?(head_path) + File.read(head_path).strip if File.exist?(head_path) end # Return a handler for a given path. @@ -321,7 +321,7 @@ def id_for(type, content) def get_object(id) path = object_path(id) - if File.exists?(path) + if File.exist?(path) buf = open(path, "rb") { |f| f.read } raise "not a loose object: #{id}" if not legacy_loose_object?(buf) @@ -345,7 +345,7 @@ def put_object(type, content) id = sha(data) path = object_path(id) - unless File.exists?(path) + unless File.exist?(path) FileUtils.mkpath(File.dirname(path)) open(path, 'wb') do |f| f.write Zlib::Deflate.deflate(data) diff --git a/lib/git_store/commit.rb b/lib/git_store/commit.rb index 01ebbad..dee367d 100644 --- a/lib/git_store/commit.rb +++ b/lib/git_store/commit.rb @@ -40,7 +40,9 @@ def parse(data) def diff(commit, path = nil) commit = commit.id if Commit === commit - Diff.exec(store, "git diff --full-index #{commit} #{id} -- '#{path}'") + cmd = "git diff --full-index #{commit} #{id}" + cmd += " -- '#{path}'" if path + Diff.exec(store, cmd) end def diffs(path = nil) diff --git a/lib/git_store/handlers.rb b/lib/git_store/handlers.rb index e3167c4..ef7acda 100644 --- a/lib/git_store/handlers.rb +++ b/lib/git_store/handlers.rb @@ -1,16 +1,4 @@ - -# This fix ensures sorted yaml maps. -class Hash - def to_yaml( opts = {} ) - YAML::quick_emit( object_id, opts ) do |out| - out.map( taguri, to_yaml_style ) do |map| - sort_by { |k, v| k.to_s }.each do |k, v| - map.add( k, v ) - end - end - end - end -end +require 'date' class GitStore @@ -26,7 +14,7 @@ def write(data) class YAMLHandler def read(data) - YAML.load(data) + YAML.safe_load(data, permitted_classes: [Symbol, Date, Time]) end def write(data) diff --git a/lib/git_store/pack.rb b/lib/git_store/pack.rb index fb5c43d..451c547 100644 --- a/lib/git_store/pack.rb +++ b/lib/git_store/pack.rb @@ -44,7 +44,7 @@ def [](*idx) when Range offset = idx.first len = idx.last - idx.first + idx.exclude_end? ? 0 : 1 - when Fixnum + when Integer offset = idx len = nil when Array diff --git a/lib/git_store/user.rb b/lib/git_store/user.rb index a17fc67..37b6dbc 100644 --- a/lib/git_store/user.rb +++ b/lib/git_store/user.rb @@ -19,9 +19,9 @@ def self.config_get(key) if $?.exitstatus == 0 return value unless value.empty? - raise RuntimError, "#{key} is empty" + raise RuntimeError, "#{key} is empty" else - raise RuntimError, "No #{key} found in git config" + raise RuntimeError, "No #{key} found in git config" end end diff --git a/test/bare_store_spec.rb b/test/bare_store_spec.rb index c33360e..2be2af3 100644 --- a/test/bare_store_spec.rb +++ b/test/bare_store_spec.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../lib/git_store" +require "#{File.dirname(__FILE__)}/spec_helper" require "#{File.dirname(__FILE__)}/helper" require 'pp' @@ -14,6 +15,8 @@ Dir.chdir REPO `git init --bare` + `git config user.name 'User Name'` + `git config user.email 'user.name@email.com'` @store = GitStore.new(REPO, 'master', true) end diff --git a/test/commit_spec.rb b/test/commit_spec.rb index c3a2211..231a2db 100644 --- a/test/commit_spec.rb +++ b/test/commit_spec.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../lib/git_store" +require "#{File.dirname(__FILE__)}/spec_helper" require 'pp' describe GitStore::Commit do @@ -12,6 +13,8 @@ Dir.mkdir REPO Dir.chdir REPO `git init` + `git config user.name 'User Name'` + `git config user.email 'user.name@email.com'` @store = GitStore.new(REPO) end @@ -69,13 +72,13 @@ diff = b.diff(a) diff[0].a_path.should == 'x' - diff[0].deleted_file.should be_true + diff[0].deleted_file.should be true diff[1].a_path.should == 'y' diff[1].diff.should == "--- a/y\n+++ b/y\n@@ -1,4 +1,4 @@\n \n First Line.\n-Second Line.\n Last Line.\n+Another Line." diff[2].a_path.should == 'z' - diff[2].new_file.should be_true + diff[2].new_file.should be true end end diff --git a/test/git_store_spec.rb b/test/git_store_spec.rb index 3d09940..5ba2563 100644 --- a/test/git_store_spec.rb +++ b/test/git_store_spec.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../lib/git_store" +require "#{File.dirname(__FILE__)}/spec_helper" require "#{File.dirname(__FILE__)}/helper" require 'pp' diff --git a/test/spec_helper.rb b/test/spec_helper.rb new file mode 100644 index 0000000..e057e98 --- /dev/null +++ b/test/spec_helper.rb @@ -0,0 +1,5 @@ +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.syntax = [:should, :expect] + end +end diff --git a/test/tree_spec.rb b/test/tree_spec.rb index 9e8c93d..2732d8d 100644 --- a/test/tree_spec.rb +++ b/test/tree_spec.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../lib/git_store" +require "#{File.dirname(__FILE__)}/spec_helper" require "#{File.dirname(__FILE__)}/helper" require 'pp' diff --git a/test/user_spec.rb b/test/user_spec.rb index 6c9a43a..2f04042 100644 --- a/test/user_spec.rb +++ b/test/user_spec.rb @@ -1,4 +1,5 @@ require "#{File.dirname(__FILE__)}/../lib/git_store" +require "#{File.dirname(__FILE__)}/spec_helper" require 'pp' describe GitStore::User do