Skip to content

fnmatch patterns has preblem in diff #882

Open
@OuYangJinTing

Description

@OuYangJinTing

Summary

When target file path has a space(eg: 'README.md '), Rugged::Tree.diff(..., paths: ['README.md ']).patches return empty array.
This should be a problem caused by the ext or libgit2, but I have not learn C language and I can't fix it.
Can you help me deal with it? Thanks.

Steps to reproduce

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://gems.ruby-china.com'

  gem 'minitest'
  gem 'rugged'
end

require 'minitest/autorun'
require 'rugged'

class DiffBugTest < Minitest::Test
  def setup
    author = committer = { name: 'foobar', email: '[email protected]', time: Time.now }
    @repository = Rugged::Repository.init_at('/tmp/rugged_test_repository.git', true)

    @repository.index << { path: 'A', mode: 0o100644, oid: @repository.write('A file', :blob)}
    @repository.index << { path: 'B ', mode: 0o100644, oid: @repository.write('B file', :blob)} # NOTE: 'B ' path has a space
    @first_oid = Rugged::Commit.create(
      @repository,
      author: author,
      committer: committer,
      message: 'First commit',
      parents: [],
      tree: @repository.index.write_tree,
    )

    @repository.index << { path: 'A', mode: 0o100644, oid: @repository.write('Modified A file', :blob)}
    @repository.index << { path: 'B ', mode: 0o100644, oid: @repository.write('Modified B file', :blob)} # NOTE: 'B ' path has a space
    @second_oid = Rugged::Commit.create(
      @repository,
      author: author,
      committer: committer,
      message: 'Second commit',
      parents: [@first_oid],
      tree: @repository.index.write_tree,
    )
  end

  # NOTE: 'B ' path has a space
  def test_path_end_with_space
    # Success
    assert_equal 2, @repository.diff(@first_oid, @second_oid).patches.size
    assert_equal 2, @repository.diff(@first_oid, @second_oid, paths: ['A', 'B '], disable_pathspec_match: true).patches.size

    # Failure
    assert_equal 2, @repository.diff(@first_oid, @second_oid, paths: ['A', 'B ']).patches.size # => Missing 'B ' patch
  end
end

PS: English is not my native language; please excuse typing errors.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions