ruby-git/ruby-git

Nameruby-git
DescriptionRuby/Git is a Ruby library that can be used to create, read and manipulate Git repositories by wrapping system calls to the git binary.
LanguageRuby
Topics
Watchers1,610
Forks496
Open issues41
Last push9/10/2021 12:27:03 AM +00:00
Github URLhttps://github.com/ruby-git/ruby-git
Clone URLhttps://github.com/ruby-git/ruby-git.git
SSH URL[email protected]:ruby-git/ruby-git.git

Most active issues

Topic# Comments
### Your checklist for this pull request
🚨Please review the [guidelines for contributing](https://github.com/ruby-git/ruby-git/blob/master/CONTRIBUTING.md) to this repository.

- [x] Ensure all commits include DCO sign-off.
- [x] Ensure that your contributions pass unit testing.
- [x] Ensure that your contributions contain documentation if applicable.

### Description

`index` when not given, defaults to `"#{work_tree}/.git/index"`. This only works as long as `git_dir` is `"#{work_tree}/.git"`. In the general case, the `index` default should be `#{git_dir}/index`.

This PR adds two tests:
* `TestGitDir#test_index_calculated_from_git_dir` which tests that `index` is calculated correctly when `git_dir` is not `"#{work_tree}/.git/index"`
* `TestGitDir#test_git_dir_outside_work_tree` which tests that when `git_dir` is in a non-standard location, Git.open and option functions work.

Fix the case in `Base#open` where the `--git-dir` is a file as is the case with Submodules and (non-main) Worktrees. This case was handled for `Base.init` but not `Base.open`.

`tests/test_helper.rb` was changed so that MiniTest classes that do not call `set_file_paths` do not output a warning because `@tmp_path` is referenced without being defined.


Read comments
18
### Subject of the issue
Unicode characters in filenames breaks diffing

### Your environment
* git version 2.20.1
* git (1.5.0)
* ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-linux]

### Steps to reproduce
bash
$ mkdir -p /tmp/test && cd /tmp/test
$ git init
$ printf '\xE2\x98\xA0' # print a skull and crossbones symbol
$ touch 'some_file'
$ git add .
$ git commit -m"init"
$ touch 'my_other_file_\xE2\x98\xA0'
$ git add .
$ git commit -m"commit"

ruby
git = Git.init '/tmp/test'
git.diff('@^').each { |x| ... }


### Expected behaviour
The diff to be produced as usual

### Actual behaviour
``NoMethodError: undefined method `[]' for nil:NilClass``

The first line of a diff is usually something like `git --diff a/somefile b/somefile`, but when the filename contains a unicode character, the filenames are escaped, (e.g. `diff --git "a/my_other_file_\\xE2\\x98\\xA0" "b/my_other_file_\\xE2\\x98\\xA0"`) and [this regex](https://github.com/ruby-git/ruby-git/blob/9bd4407c56068e1604f14a1b6c0c5a84868e6378/lib/git/diff.rb#L136) fails to detect the line.

Read comments
18
Signed-off-by: James Couball

### Your checklist for this pull request
🚨Please review the [guidelines for contributing](../CONTRIBUTING.md) to this repository.

- [x] Ensure all commits include DCO sign-off.
- [x] Ensure that your contributions pass unit testing.
- [x] Ensure that your contributions contain documentation if applicable.

### Description

ruby-git relies on shelling out to the `git` command to interact with a `git` repository. When the `git` command has output that is not in the default Ruby encoding (usually UTF-8), `ruby-git` may raise an exception when the string containing the command output is used.

This change makes `ruby-git` check if the command output string contains any invalid code points and if it does, attempts to guess at the actual encoding of the command output on a line-by-line basis. Each line is converted from the actual encoding to Ruby's default encoding (usually UTF-8). Unknown code points are replaced by a special character.

This solution contrasts with PR #404 which simply tries to replace all invalid code points with a special character using `String#scrub`.

### Background

`git diff` (and other git commands) can have output that has mixed encodings. Not only does `git` output its own text in the default system/shell encoding, but when text from a repository file is output, it is not in any particular encoding: `git` just passes the bytes from the file to the output. These files are stored in whatever encoding the user wrote them in.

In Ruby, `popen` and `backtick` collect the output from the command they run as bytes. They shove these bytes into the string returned. They make no attempt to guess the *actual* encoding of the string returned.

In later versions of Ruby (maybe 1.9+), the encoding of the string returned from `popen` or `backtick` is ALWAYS set to `Encoding.default_external`. This is usually ‘UTF-8’. Ruby uses the value of the command `locale charmap` for the initial value of `Encoding.default_external`. `popen` and `backtick` use this encoding even though the *actual* encoding of the command output is not known.

The bottom line is that the *actual* encoding of the string returned from `popen` or `backtick` is unknowable. Sometimes it is a happy coincidence that the *actual* encoding matches what is retuned by `String#encoding`. When `Encoding.default_external` is ‘UTF-8’ this coincidence happens more often than not.

I think the best that can be done is to sanitize the command output as it is read in `Git::Lib#run_command` in an attempt to “guard the gates”. That way the rest of the code in this gem can assume the command output has a valid encoding and avoid unexpected exceptions being raised.

### What has changed?

* `git.gemspec`: added a runtime dependency on [rchardet](https://github.com/jmhodges/rchardet) to guess the encoding of command output in the case where the command output contains invalid code points.
* `lib/git/lib.rb`: change `Git::Lib#run_command` to ensure that the string that collects command line output is converted to the String encoding used by Ruby and only has valid code points for that encoding.
* `lib/git/diff.rb`: remove encoding handling since this is now handled in `Git::Lib#run_command`
* `tests/units/test_diff_non_default_encoding.rb`: Added two tests: (1) diff changes to a file that has iso-8859-7-greek encoding and (2) diff changes to a file where the encoding was changed from euc-jp-japanese to euc-kr-korean
* `tests/files/encoding/**/*`: a repository used in the tests above


Read comments
18
- [x] Ensure all commits include DCO sign-off.
- [x] Ensure that your contributions pass unit testing.
- [x] Ensure that your contributions contain documentation if applicable.

### Description

I faced error `invalid byte sequence in UTF-8` when call `modified_files` and I found this error occurs by `split` at [here](https://github.com/ruby-git/ruby-git/blob/master/lib/git/diff.rb#L135) in case of `@full_diff` contains invalid byte sequence.
Using `scrub` for `@full_diff` to remove invalid byte sequence can remove this error.

Related: #392, #369

Read comments
17
When writing command-line tools that use ruby-git to interface with git, there are often ways to achieve better performance by running git query commands in parallel. This is unworkable with `ruby-git` given it's extensive use of the non-thread-safe `Dir.chdir` call. With all implicit calls to `Dir.chdir` removed, `ruby-git` is one step closer to being usable amongst threads.

From the [`Dir.chdir` documentation](http://www.ruby-doc.org/core-2.1.5/Dir.html#method-c-chdir):

> The return value of chdir is the value of the block. chdir blocks can be nested, but in a multi-threaded program an error will be raised if a thread attempts to open a chdir block while another thread has one open.


Read comments
16
Hi @schacon seems like this repo has not being supported for a while. Are you willing to transfer the ownership to someone who can continue gem maintenance?

Thanks in advance for the reply.


Read comments
15
### Your checklist for this pull request
🚨Please review the [guidelines for contributing](../CONTRIBUTING.md) to this repository.

- [x] Ensure all commits include DCO sign-off.
- [x] Ensure that your contributions pass unit testing.
- [x] Ensure that your contributions contain documentation if applicable.

### Description

[Git 2.1.2 introduced setting values even if they did not previously exist.](https://github.com/git/git/blob/master/Documentation/RelNotes/2.1.2.txt#L16-L17)
The git-ruby library [keeps compatibility](https://github.com/ruby-git/ruby-git/blob/master/lib/git.rb#L25) with Git [1.6](https://github.com/ruby-git/ruby-git/blob/master/lib/git/lib.rb#L887), this fixes the
problem of older versions not being able to create new values.

#### Other options

- bump the minimal required git version to `2.1.2`

Read comments
14
This allows the user to see if an arbitrary commit is contained within a
branch. For example you can check to see if a branch was merged into
master with `git.branch('master').contains?('feature')`. Or you can see
if your feature branch has the latest master commit with
`git.branch('feature').contains?('master')`


Read comments
14
### Your checklist for this pull request
🚨Please review the [guidelines for contributing](../CONTRIBUTING.md) to this repository.

- [x] Ensure all commits include DCO sign-off.
- [x] Ensure that your contributions pass unit testing.
- [x] Ensure that your contributions contain documentation if applicable.

### Description

The Git module has config and global_config methods that are available only when Git module is included. Refactored the code to remove duplication and to make it more explicit we are managing `git config` not the library setup.


Read comments
13
Given I visit https://rubygems.org/gems/git
When I press the 'Source Code' link
I expect to be taken to 'https://github.com/ruby-git/ruby-git/'
I am actually taken to 'https://github.com/schacon/ruby-git'

Similar applies to the home page.

Read comments
12