diff --git a/CHANGELOG.md b/CHANGELOG.md index 910fc4ea..c570e416 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,20 @@ # Change Log +## v2.3.1 (2024-10-23) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.0..v2.3.1) + +Changes since v2.3.0: + +* e236007 test: allow bin/test-in-docker to accept the test file(s) to run on command line +* f4747e1 test: rename bin/tests to bin/test-in-docker +* 51f781c test: remove duplicate test from test_stashes.rb +* 2e79dbe Fixed "unbranched" stash message support: +* da6fa6e Conatinerised the test suite with Docker: +* 2e23d47 Update instructions for building a specific version of Git +* 70565e3 Add Git.binary_version to return the version of the git command line + ## v2.3.0 (2024-09-01) [Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.2.0..v2.3.0) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 082a8853..10793a4a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,6 +3,9 @@ # @title How To Contribute --> +# Contributing to the git gem + +* [Summary](#summary) * [How to contribute](#how-to-contribute) * [How to report an issue or request a feature](#how-to-report-an-issue-or-request-a-feature) * [How to submit a code or documentation change](#how-to-submit-a-code-or-documentation-change) @@ -18,10 +21,14 @@ * [Unit tests](#unit-tests) * [Continuous integration](#continuous-integration) * [Documentation](#documentation) +* [Building a specific version of the Git command-line](#building-a-specific-version-of-the-git-command-line) + * [Install pre-requisites](#install-pre-requisites) + * [Obtain Git source code](#obtain-git-source-code) + * [Build git](#build-git) + * [Use the new Git version](#use-the-new-git-version) * [Licensing](#licensing) - -# Contributing to the git gem +## Summary Thank you for your interest in contributing to the `ruby-git` project. @@ -172,22 +179,100 @@ $ bin/test test_object test_archive # run all unit tests: $ bin/test + +# run unit tests with a different version of the git command line: +$ GIT_PATH=/Users/james/Downloads/git-2.30.2/bin-wrappers bin/test ``` ### Continuous integration -All tests must pass in the project's [GitHub Continuous Integration build](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI) before the pull request will be merged. +All tests must pass in the project's [GitHub Continuous Integration +build](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI) before the +pull request will be merged. -The [Continuous Integration workflow](https://github.com/ruby-git/ruby-git/blob/master/.github/workflows/continuous_integration.yml) runs both `bundle exec rake default` and `bundle exec rake test:gem` from the project's [Rakefile](https://github.com/ruby-git/ruby-git/blob/master/Rakefile). +The [Continuous Integration +workflow](https://github.com/ruby-git/ruby-git/blob/master/.github/workflows/continuous_integration.yml) +runs both `bundle exec rake default` and `bundle exec rake test:gem` from the +project's [Rakefile](https://github.com/ruby-git/ruby-git/blob/master/Rakefile). ### Documentation -New and updated public methods must include [YARD](https://yardoc.org/) documentation. +New and updated public methods must include [YARD](https://yardoc.org/) +documentation. + +New and updated public-facing features should be documented in the project's +[README.md](README.md). + +## Building a specific version of the Git command-line + +To test with a specific version of the Git command-line, you may need to build that +version from source code. The following instructions are adapted from Atlassian’s +[How to install Git](https://www.atlassian.com/git/tutorials/install-git) page for +building Git on macOS. + +### Install pre-requisites + +Prerequisites only need to be installed if they are not already present. + +From your terminal, install Xcode’s Command Line Tools: + +```shell +xcode-select --install +``` + +Install [Homebrew](http://brew.sh/) by following the instructions on the Homebrew +page. + +Using Homebrew, install OpenSSL: + +```shell +brew install openssl +``` + +### Obtain Git source code + +Download and extract the source tarball for the desired Git version from [this source +code mirror](https://mirrors.edge.kernel.org/pub/software/scm/git/). + +### Build git + +From your terminal, change to the root directory of the extracted source code and run +the build with following command: + +```shell +NO_GETTEXT=1 make CFLAGS="-I/usr/local/opt/openssl/include" LDFLAGS="-L/usr/local/opt/openssl/lib" +``` + +The build script will place the newly compiled Git executables in the `bin-wrappers` +directory (e.g., `bin-wrappers/git`). + +### Use the new Git version + +To configure programs that use the Git gem to utilize the newly built version, do the +following: + +```ruby +require 'git' + +# Set the binary path +Git.configure { |c| c.binary_path = '/Users/james/Downloads/git-2.30.2/bin-wrappers/git' } + +# Validate the version (if desired) +assert_equal([2, 30, 2], Git.binary_version) +``` + +Tests can be run using the newly built Git version as follows: + +```shell +GIT_PATH=/Users/james/Downloads/git-2.30.2/bin-wrappers bin/test +``` -New and updated public-facing features should be documented in the project's [README.md](README.md). +Note: `GIT_PATH` refers to the directory containing the `git` executable. ## Licensing -`ruby-git` uses [the MIT license](https://choosealicense.com/licenses/mit/) as declared in the [LICENSE](LICENSE) file. +`ruby-git` uses [the MIT license](https://choosealicense.com/licenses/mit/) as +declared in the [LICENSE](LICENSE) file. -Licensing is critical to open-source projects as it ensures the software remains available under the terms desired by the author. \ No newline at end of file +Licensing is critical to open-source projects as it ensures the software remains +available under the terms desired by the author. diff --git a/bin/test b/bin/test index 8024c5ab..599ecbd9 100755 --- a/bin/test +++ b/bin/test @@ -1,11 +1,17 @@ #!/usr/bin/env ruby # frozen_string_literal: true +# This script is used to run the tests for this project. +# +# bundle exec bin/test [test_file_name ...] +# +# If no test file names are provided, all tests in the `tests/units` directory will be run. + require 'bundler/setup' -`git config --global user.email "git@example.com"` if `git config user.email`.empty? -`git config --global user.name "GitExample"` if `git config user.name`.empty? -`git config --global init.defaultBranch master` if `git config init.defaultBranch`.empty? +`git config --global user.email "git@example.com"` if `git config --global user.email`.empty? +`git config --global user.name "GitExample"` if `git config --global user.name`.empty? +`git config --global init.defaultBranch master` if `git config --global init.defaultBranch`.empty? project_root = File.expand_path(File.join(__dir__, '..')) diff --git a/bin/test-in-docker b/bin/test-in-docker new file mode 100755 index 00000000..8775d56b --- /dev/null +++ b/bin/test-in-docker @@ -0,0 +1,17 @@ +#!/bin/bash -e + +# This script is used to run the tests for this project in a Docker container. +# +# bin/test-in-docker [test_file_name ...] +# +# If no test file names are provided, all tests in the `tests/units` directory will be run. + +cd "$( dirname "${BASH_SOURCE[0]}" )"/.. + +export COMPOSE_FILE=tests/docker-compose.yml +export COMPOSE_PROJECT_NAME=ruby-git_dev + +docker-compose rm -svf +docker-compose build --force-rm + +docker-compose run --rm tester "$@" && docker-compose rm -svf || ( docker-compose logs && exit 1 ) diff --git a/lib/git.rb b/lib/git.rb index e995e96c..6d0f3032 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -381,4 +381,15 @@ def self.ls_remote(location = nil, options = {}) def self.open(working_dir, options = {}) Base.open(working_dir, options) end + + # Return the version of the git binary + # + # @example + # Git.binary_version # => [2, 46, 0] + # + # @return [Array] the version of the git binary + # + def self.binary_version(binary_path = Git::Base.config.binary_path) + Base.binary_version(binary_path) + end end diff --git a/lib/git/base.rb b/lib/git/base.rb index 0df9a5e3..8a987313 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -36,6 +36,20 @@ def self.config @@config ||= Config.new end + def self.binary_version(binary_path) + git_cmd = "#{binary_path} -c core.quotePath=true -c color.ui=false version 2>&1" + result, status = Open3.capture2(git_cmd) + result = result.chomp + + if status.success? + version = result[/\d+(\.\d+)+/] + version_parts = version.split('.').collect { |i| i.to_i } + version_parts.fill(0, version_parts.length...3) + else + raise RuntimeError, "Failed to get git version: #{status}\n#{result}" + end + end + # (see Git.init) def self.init(directory = '.', options = {}) normalize_paths(options, default_working_directory: directory, default_repository: directory, bare: options[:bare]) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index f0cd2713..83865b85 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1134,8 +1134,10 @@ def stashes_all if File.exist?(filename) File.open(filename) do |f| f.each_with_index do |line, i| - m = line.match(/:(.*)$/) - arr << [i, m[1].strip] + _, msg = line.split("\t") + # NOTE this logic may be removed/changed in 3.x + m = msg.match(/^[^:]+:(.*)$/) + arr << [i, (m ? m[1] : msg).strip] end end end diff --git a/lib/git/version.rb b/lib/git/version.rb index 33cf0b9b..abc0e3a7 100644 --- a/lib/git/version.rb +++ b/lib/git/version.rb @@ -1,5 +1,5 @@ module Git # The current gem version # @return [String] the current gem version. - VERSION='2.3.0' + VERSION='2.3.1' end diff --git a/tests/Dockerfile b/tests/Dockerfile new file mode 100644 index 00000000..85690f59 --- /dev/null +++ b/tests/Dockerfile @@ -0,0 +1,12 @@ +FROM ruby + +WORKDIR /ruby-git + + +ADD Gemfile git.gemspec .git* ./ +ADD lib/git/version.rb ./lib/git/version.rb +RUN bundle install + +ADD . . + +ENTRYPOINT ["bundle", "exec", "bin/test"] diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml new file mode 100644 index 00000000..c8337d44 --- /dev/null +++ b/tests/docker-compose.yml @@ -0,0 +1,5 @@ +services: + tester: + build: + context: .. + dockerfile: tests/Dockerfile diff --git a/tests/units/test_git_binary_version.rb b/tests/units/test_git_binary_version.rb new file mode 100644 index 00000000..09afc1a1 --- /dev/null +++ b/tests/units/test_git_binary_version.rb @@ -0,0 +1,54 @@ +require 'test_helper' + +class TestGitBinaryVersion < Test::Unit::TestCase + def windows_mocked_git_binary = <<~GIT_SCRIPT + @echo off + # Loop through the arguments and check for the version command + for %%a in (%*) do ( + if "%%a" == "version" ( + echo git version 1.2.3 + exit /b 0 + ) + ) + exit /b 1 + GIT_SCRIPT + + def linux_mocked_git_binary = <<~GIT_SCRIPT + #!/bin/sh + # Loop through the arguments and check for the version command + for arg in "$@"; do + if [ "$arg" = "version" ]; then + echo "git version 1.2.3" + exit 0 + fi + done + exit 1 + GIT_SCRIPT + + def test_binary_version_windows + omit('Only implemented for Windows') unless windows_platform? + + in_temp_dir do |path| + git_binary_path = File.join(path, 'my_git.bat') + File.write(git_binary_path, windows_mocked_git_binary) + assert_equal([1, 2, 3], Git.binary_version(git_binary_path)) + end + end + + def test_binary_version_linux + omit('Only implemented for Linux') if windows_platform? + + in_temp_dir do |path| + git_binary_path = File.join(path, 'my_git.bat') + File.write(git_binary_path, linux_mocked_git_binary) + File.chmod(0755, git_binary_path) + assert_equal([1, 2, 3], Git.binary_version(git_binary_path)) + end + end + + def test_binary_version_bad_binary_path + assert_raise RuntimeError do + Git.binary_version('/path/to/nonexistent/git') + end + end +end diff --git a/tests/units/test_stashes.rb b/tests/units/test_stashes.rb index e147ae9c..0516f273 100644 --- a/tests/units/test_stashes.rb +++ b/tests/units/test_stashes.rb @@ -26,7 +26,7 @@ def test_stash_unstash end end - def test_stashes_all + test 'Git::Lib#stashes_all' do in_bare_repo_clone do |g| assert_equal(0, g.branch.stashes.size) new_file('test-file1', 'blahblahblah1') @@ -39,9 +39,94 @@ def test_stashes_all g.branch.stashes.save('testing-stash-all') - stashes = g.branch.stashes.all + # puts `cat .git/logs/refs/stash` + # 0000000000000000000000000000000000000000 b9b008cd179b0e8c4b8cda35bac43f7011a0836a James Couball 1729463252 -0700 On master: testing-stash-all - assert(stashes[0].include?('testing-stash-all')) + stashes = assert_nothing_raised { g.lib.stashes_all } + + expected_stashes = [ + [0, 'testing-stash-all'] + ] + + assert_equal(expected_stashes, stashes) + end + end + + test 'Git::Lib#stashes_all - stash message has colon' do + in_bare_repo_clone do |g| + assert_equal(0, g.branch.stashes.size) + new_file('test-file1', 'blahblahblah1') + new_file('test-file2', 'blahblahblah2') + assert(g.status.untracked.assoc('test-file1')) + + g.add + + assert(g.status.added.assoc('test-file1')) + + g.branch.stashes.save('saving: testing-stash-all') + + # puts `cat .git/logs/refs/stash` + # 0000000000000000000000000000000000000000 b9b008cd179b0e8c4b8cda35bac43f7011a0836a James Couball 1729463252 -0700 On master: saving: testing-stash-all + + stashes = assert_nothing_raised { g.lib.stashes_all } + + expected_stashes = [ + [0, 'saving: testing-stash-all'] + ] + + assert_equal(expected_stashes, stashes) + end + end + + test 'Git::Lib#stashes_all -- git stash message with no branch and no colon' do + in_temp_dir do + `git init` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + `echo "update" > file1.txt` + commit = `git stash create "stash message"`.chomp + # Create a stash with this message: 'custom message' + `git stash store -m "custom message" #{commit}` + + # puts `cat .git/logs/refs/stash` + # 0000000000000000000000000000000000000000 0550a54ed781eda364ca3c22fcc46c37acae4bd6 James Couball 1729460302 -0700 custom message + + git = Git.open('.') + + stashes = assert_nothing_raised { git.lib.stashes_all } + + expected_stashes = [ + [0, 'custom message'] + ] + + assert_equal(expected_stashes, stashes) + end + end + + test 'Git::Lib#stashes_all -- git stash message with no branch and explicit colon' do + in_temp_dir do + `git init` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + `echo "update" > file1.txt` + commit = `git stash create "stash message"`.chomp + # Create a stash with this message: 'custom message' + `git stash store -m "testing: custom message" #{commit}` + + # puts `cat .git/logs/refs/stash` + # 0000000000000000000000000000000000000000 eadd7858e53ea4fb8b1383d69cade1806d948867 James Couball 1729462039 -0700 testing: custom message + + git = Git.open('.') + + stashes = assert_nothing_raised { git.lib.stashes_all } + + expected_stashes = [ + [0, 'custom message'] + ] + + assert_equal(expected_stashes, stashes) end end end