libgit2 / libgit2/rugged

fnmatch patterns has preblem in diff

Open
#882 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
2.3k
Forks
293
PR merge metrics
No merged PRs in 30d

Description

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: 'foobar@test.com', 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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the inline Minitest reproduction and confirm the failure for the trailing-space path. Then trace path filtering across Rugged's extension and the libgit2 boundary, using the existing diff calls as the entry point. Done means the test includes the B patch while the unfiltered and disabled-match cases continue to pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, ruby
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.