diff --git a/.commitlintrc.yml b/.commitlintrc.yml new file mode 100644 index 00000000..3e08fa81 --- /dev/null +++ b/.commitlintrc.yml @@ -0,0 +1,38 @@ +--- +extends: '@commitlint/config-conventional' + +rules: + # See: https://commitlint.js.org/reference/rules.html + # + # Rules are made up by a name and a configuration array. The configuration + # array contains: + # + # * Severity [0..2]: 0 disable rule, 1 warning if violated, or 2 error if + # violated + # * Applicability [always|never]: never inverts the rule + # * Value: value to use for this rule (if applicable) + # + # Run `npx commitlint --print-config` to see the current setting for all + # rules. + # + header-max-length: [2, always, 100] # Header can not exceed 100 chars + + type-case: [2, always, lower-case] # Type must be lower case + type-empty: [2, never] # Type must not be empty + + # Supported conventional commit types + type-enum: [2, always, [build, ci, chore, docs, feat, fix, perf, refactor, revert, style, test]] + + scope-case: [2, always, lower-case] # Scope must be lower case + + # Error if subject is one of these cases (encourages lower-case) + subject-case: [2, never, [sentence-case, start-case, pascal-case, upper-case]] + subject-empty: [2, never] # Subject must not be empty + subject-full-stop: [2, never, "."] # Subject must not end with a period + + body-leading-blank: [2, always] # Body must have a blank line before it + body-max-line-length: [2, always, 100] # Body lines can not exceed 100 chars + + footer-leading-blank: [2, always] # Footer must have a blank line before it + footer-max-line-length: [2, always, 100] # Footer lines can not exceed 100 chars + \ No newline at end of file diff --git a/.github/workflows/continuous_integration.yml b/.github/workflows/continuous_integration.yml index 52c6c4ea..c21e97cd 100644 --- a/.github/workflows/continuous_integration.yml +++ b/.github/workflows/continuous_integration.yml @@ -1,15 +1,19 @@ name: CI on: - push: - branches: [master,v1] pull_request: - branches: [master,v1] + branches: [master] workflow_dispatch: jobs: build: name: Ruby ${{ matrix.ruby }} on ${{ matrix.operating-system }} + + # Skip this job if triggered by a release PR + if: >- + github.event_name == 'workflow_dispatch' || + (github.event_name == 'pull_request' && !startsWith(github.event.pull_request.head.ref, 'release-please--')) + runs-on: ${{ matrix.operating-system }} continue-on-error: ${{ matrix.experimental == 'Yes' }} env: { JAVA_OPTS: -Djdk.io.File.enableADS=true } @@ -18,12 +22,12 @@ jobs: fail-fast: false matrix: # Only the latest versions of JRuby and TruffleRuby are tested - ruby: ["3.0", "3.1", "3.2", "3.3", "truffleruby-24.0.0", "jruby-9.4.5.0"] + ruby: ["3.1", "3.2", "3.3", "3.4", "truffleruby-24.1.2", "jruby-9.4.12.0"] operating-system: [ubuntu-latest] experimental: [No] include: - # Only test with minimal Ruby version on Windows - ruby: 3.0 + ruby: 3.1 operating-system: windows-latest steps: diff --git a/.github/workflows/enforce_conventional_commits.yml b/.github/workflows/enforce_conventional_commits.yml new file mode 100644 index 00000000..8aaa93f8 --- /dev/null +++ b/.github/workflows/enforce_conventional_commits.yml @@ -0,0 +1,28 @@ +--- +name: Conventional Commits + +permissions: + contents: read + +on: + pull_request: + branches: + - master + +jobs: + commit-lint: + name: Verify Conventional Commits + + # Skip this job if this is a release PR + if: (github.event_name == 'pull_request' && !startsWith(github.event.pull_request.head.ref, 'release-please--')) + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: { fetch-depth: 0 } + + - name: Check Commit Messages + uses: wagoid/commitlint-github-action@v6 + with: { configFile: .commitlintrc.yml } diff --git a/.github/workflows/experimental_continuous_integration.yml b/.github/workflows/experimental_continuous_integration.yml index 44dc7889..488ab797 100644 --- a/.github/workflows/experimental_continuous_integration.yml +++ b/.github/workflows/experimental_continuous_integration.yml @@ -2,12 +2,19 @@ name: CI Experimental on: push: - branches: [master,v1] + branches: [master] + workflow_dispatch: jobs: build: name: Ruby ${{ matrix.ruby }} on ${{ matrix.operating-system }} + + # Skip this job if triggered by pushing a release commit + if: >- + github.event_name == 'workflow_dispatch' || + (github.event_name == 'push' && !startsWith(github.event.head_commit.message, 'chore: release ')) + runs-on: ${{ matrix.operating-system }} continue-on-error: true env: { JAVA_OPTS: -Djdk.io.File.enableADS=true } diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 00000000..eaea43f1 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,52 @@ +--- +name: Release Gem + +description: | + This workflow creates a new release on GitHub and publishes the gem to + RubyGems.org. + + The workflow uses the `googleapis/release-please-action` to handle the + release creation process and the `rubygems/release-gem` action to publish + the gem to rubygems.org + +on: + push: + branches: ["master"] + + workflow_dispatch: + +jobs: + release: + runs-on: ubuntu-latest + + environment: + name: RubyGems + url: https://rubygems.org/gems/git + + permissions: + contents: write + pull-requests: write + id-token: write + + steps: + - name: Checkout project + uses: actions/checkout@v4 + + - name: Create release + uses: googleapis/release-please-action@v4 + id: release + with: + token: ${{ secrets.AUTO_RELEASE_TOKEN }} + config-file: release-please-config.json + manifest-file: .release-please-manifest.json + + - name: Setup ruby + uses: ruby/setup-ruby@v1 + if: ${{ steps.release.outputs.release_created }} + with: + bundler-cache: true + ruby-version: ruby + + - name: Push to RubyGems.org + uses: rubygems/release-gem@v1 + if: ${{ steps.release.outputs.release_created }} diff --git a/.gitignore b/.gitignore index 611ed70c..13dcea11 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ doc pkg rdoc Gemfile.lock +node_modules +package-lock.json \ No newline at end of file diff --git a/.husky/commit-msg b/.husky/commit-msg new file mode 100644 index 00000000..70bd3dd2 --- /dev/null +++ b/.husky/commit-msg @@ -0,0 +1 @@ +npx --no-install commitlint --edit "$1" diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 00000000..ada7355e --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "3.1.0" +} diff --git a/.yardopts b/.yardopts index ce1aff3c..105b79a9 100644 --- a/.yardopts +++ b/.yardopts @@ -7,5 +7,4 @@ README.md CHANGELOG.md CONTRIBUTING.md -RELEASING.md MAINTAINERS.md diff --git a/CHANGELOG.md b/CHANGELOG.md index f9120219..5602c70e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,100 @@ # Change Log +## [3.1.0](https://github.com/ruby-git/ruby-git/compare/v3.0.2...v3.1.0) (2025-05-18) + + +### Features + +* Make Git::Log support the git log --merges option ([df3b07d](https://github.com/ruby-git/ruby-git/commit/df3b07d0f14d79c6c77edc04550c1ad0207c920a)) + + +### Other Changes + +* Announce and document guidelines for using Conventional Commits ([a832259](https://github.com/ruby-git/ruby-git/commit/a832259314aa9c8bdd7719e50d425917df1df831)) +* Skip continuous integration workflow for release PRs ([f647a18](https://github.com/ruby-git/ruby-git/commit/f647a18c8a3ae78f49c8cd485db4660aa10a92fc)) +* Skip the experiemental build workflow if a release commit is pushed to master ([3dab0b3](https://github.com/ruby-git/ruby-git/commit/3dab0b34e41393a43437c53a53b96895fd3d2cc5)) + +## [3.0.2](https://github.com/ruby-git/ruby-git/compare/v3.0.1...v3.0.2) (2025-05-15) + + +### Bug Fixes + +* Trigger the release workflow on a change to 'master' insetad of 'main' ([c8611f1](https://github.com/ruby-git/ruby-git/commit/c8611f1e68e73825fd16bd475752a40b0088d4ae)) + + +### Other Changes + +* Automate continuous delivery workflow ([06480e6](https://github.com/ruby-git/ruby-git/commit/06480e65e2441348230ef10e05cc1c563d0e7ea8)) +* Enforce conventional commit messages with a GitHub action ([1da4c44](https://github.com/ruby-git/ruby-git/commit/1da4c44620a3264d4e837befd3f40416c5d8f1d8)) +* Enforce conventional commit messages with husky and commitlint ([7ebe0f8](https://github.com/ruby-git/ruby-git/commit/7ebe0f8626ecb2f0da023b903b82f7332d8afaf6)) + +## v3.0.1 (2025-05-14) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v3.0.0..v3.0.1) + +Changes since v3.0.0: + +* b47eedc Improved error message of rev_parse +* 9d44146 chore: update the development dependency on the minitar gem +* f407b92 feat: set the locale to en_US.UTF-8 for git commands +* b060e47 test: verify that command line envionment variables are set as expected +* 1a5092a chore: release v3.0.0 + +## v3.0.0 (2025-02-27) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.3..v3.0.0) + +Changes since v2.3.3: + +* 534fcf5 chore: use ProcessExecuter.run instead of the implementing it in this gem +* 629f3b6 feat: update dependenices +* 501d135 feat: add support for Ruby 3.4 and drop support for Ruby 3.0 +* 38c0eb5 build: update the CI build to use current versions to TruffleRuby and JRuby +* d3f3a9d chore: add frozen_string_literal: true magic comment + +## v2.3.3 (2024-12-04) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.2..v2.3.3) + +Changes since v2.3.2: + +* c25e5e0 test: add tests for spaces in the git binary path or the working dir +* 5f43a1a fix: open3 errors on binary paths with spaces +* 60b58ba test: add #run_command for tests to use instead of backticks + +## v2.3.2 (2024-11-19) + +[Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.3.1..v2.3.2) + +Changes since v2.3.1: + +* 7646e38 fix: improve error message for Git::Lib#branches_all + +## 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) + +Changes since v2.2.0: + +* f8bc987 Fix windows CI build error +* 471f5a8 Sanatize object ref sent to cat-file command +* 604a9a2 Make Git::Base#branch work when HEAD is detached + ## v2.2.0 (2024-08-26) [Full Changelog](https://github.com/ruby-git/ruby-git/compare/v2.1.1..v2.2.0) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 082a8853..653290f2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -3,26 +3,35 @@ # @title How To Contribute --> -* [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) - * [Commit your changes to a fork of `ruby-git`](#commit-your-changes-to-a-fork-of-ruby-git) - * [Create a pull request](#create-a-pull-request) - * [Get your pull request reviewed](#get-your-pull-request-reviewed) -* [Design philosophy](#design-philosophy) - * [Direct mapping to git commands](#direct-mapping-to-git-commands) - * [Parameter naming](#parameter-naming) - * [Output processing](#output-processing) -* [Coding standards](#coding-standards) - * [1 PR = 1 Commit](#1-pr--1-commit) - * [Unit tests](#unit-tests) - * [Continuous integration](#continuous-integration) - * [Documentation](#documentation) -* [Licensing](#licensing) - - # 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) + - [Commit your changes to a fork of `ruby-git`](#commit-your-changes-to-a-fork-of-ruby-git) + - [Create a pull request](#create-a-pull-request) + - [Get your pull request reviewed](#get-your-pull-request-reviewed) +- [Design philosophy](#design-philosophy) + - [Direct mapping to git commands](#direct-mapping-to-git-commands) + - [Parameter naming](#parameter-naming) + - [Output processing](#output-processing) +- [Coding standards](#coding-standards) + - [Commit message guidelines](#commit-message-guidelines) + - [What does this mean for contributors?](#what-does-this-mean-for-contributors) + - [What to know about Conventional Commits](#what-to-know-about-conventional-commits) + - [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) + +## Summary + Thank you for your interest in contributing to the `ruby-git` project. This document provides guidelines for contributing to the `ruby-git` project. While @@ -56,7 +65,8 @@ thoroughly as possible to describe the issue or feature request. There is a three-step process for submitting code or documentation changes: 1. [Commit your changes to a fork of - `ruby-git`](#commit-your-changes-to-a-fork-of-ruby-git) + `ruby-git`](#commit-your-changes-to-a-fork-of-ruby-git) using [Conventional + Commits](#commit-message-guidelines) 2. [Create a pull request](#create-a-pull-request) 3. [Get your pull request reviewed](#get-your-pull-request-reviewed) @@ -146,18 +156,88 @@ behavior. To ensure high-quality contributions, all pull requests must meet the following requirements: -### 1 PR = 1 Commit +### Commit message guidelines + +To enhance our development workflow, enable automated changelog generation, and pave +the way for Continuous Delivery, the `ruby-git` project has adopted the [Conventional +Commits standard](https://www.conventionalcommits.org/en/v1.0.0/) for all commit +messages. + +This structured approach to commit messages allows us to: + +- **Automate versioning and releases:** Tools can now automatically determine the + semantic version bump (patch, minor, major) based on the types of commits merged. +- **Generate accurate changelogs:** We can automatically create and update a + `CHANGELOG.md` file, providing a clear history of changes for users and + contributors. +- **Improve commit history readability:** A standardized format makes it easier for + everyone to understand the nature of changes at a glance. + +#### What does this mean for contributors? + +Going forward, all commits to this repository **MUST** adhere to the [Conventional +Commits standard](https://www.conventionalcommits.org/en/v1.0.0/). Commits not +adhering to this standard will cause the CI build to fail. PRs will not be merged if +they include non-conventional commits. + +A git pre-commit hook may be installed to validate your conventional commit messages +before pushing them to GitHub by running `bin/setup` in the project root. + +#### What to know about Conventional Commits + +The simplist conventional commit is in the form `type: description` where `type` +indicates the type of change and `description` is your usual commit message (with +some limitations). + +- Types include: `feat`, `fix`, `docs`, `test`, `refactor`, and `chore`. See the full + list of types supported in [.commitlintrc.yml](.commitlintrc.yml). +- The description must (1) not start with an upper case letter, (2) be no more than + 100 characters, and (3) not end with punctuation. + +Examples of valid commits: + +- `feat: add the --merges option to Git::Lib.log` +- `fix: exception thrown by Git::Lib.log when repo has no commits` +- `docs: add conventional commit announcement to README.md` -* All commits for a PR must be squashed into a single commit. -* To avoid an extra merge commit, the PR must be able to be merged as [a fast-forward - merge](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging). -* The easiest way to ensure a fast-forward merge is to rebase your local branch to - the `ruby-git` master branch. +Commits that include breaking changes must include an exclaimation mark before the +colon: + +- `feat!: removed Git::Base.commit_force` + +The commit messages will drive how the version is incremented for each release: + +- a release containing a **breaking change** will do a **major** version increment +- a release containing a **new feature** will do a **minor** increment +- a release containing **neither a breaking change nor a new feature** will do a + **patch** version increment + +The full conventional commit format is: + +```text +[optional scope][!]: + +[optional body] + +[optional footer(s)] +``` + +- `optional body` may include multiple lines of descriptive text limited to 100 chars + each +- `optional footers` only uses `BREAKING CHANGE: ` where description + should describe the nature of the backward incompatibility. + +Use of the `BREAKING CHANGE:` footer flags a backward incompatible change even if it +is not flagged with an exclaimation mark after the `type`. Other footers are allowed +by not acted upon. + +See [the Conventional Commits +specification](https://www.conventionalcommits.org/en/v1.0.0/) for more details. ### Unit tests -* All changes must be accompanied by new or modified unit tests. -* The entire test suite must pass when `bundle exec rake default` is run from the +- All changes must be accompanied by new or modified unit tests. +- The entire test suite must pass when `bundle exec rake default` is run from the project's local working copy. While working on specific features, you can run individual test files or a group of @@ -172,22 +252,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/README.md b/README.md index c3f788ca..74e6ad4c 100644 --- a/README.md +++ b/README.md @@ -9,17 +9,34 @@ [![Documentation](https://img.shields.io/badge/Documentation-Latest-green)](https://rubydoc.info/gems/git/) [![Change Log](https://img.shields.io/badge/CHANGELOG-Latest-green)](https://rubydoc.info/gems/git/file/CHANGELOG.md) [![Build Status](https://github.com/ruby-git/ruby-git/workflows/CI/badge.svg?branch=master)](https://github.com/ruby-git/ruby-git/actions?query=workflow%3ACI) -[![Code Climate](https://codeclimate.com/github/ruby-git/ruby-git.png)](https://codeclimate.com/github/ruby-git/ruby-git) - -* [Summary](#summary) -* [v2.x Release](#v2x-release) -* [Install](#install) -* [Major Objects](#major-objects) -* [Errors Raised By This Gem](#errors-raised-by-this-gem) -* [Specifying And Handling Timeouts](#specifying-and-handling-timeouts) -* [Examples](#examples) -* [Ruby version support policy](#ruby-version-support-policy) -* [License](#license) +[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org) + +- [📢 We've Switched to Conventional Commits 📢](#-weve-switched-to-conventional-commits-) +- [Summary](#summary) +- [Install](#install) +- [Major Objects](#major-objects) +- [Errors Raised By This Gem](#errors-raised-by-this-gem) +- [Specifying And Handling Timeouts](#specifying-and-handling-timeouts) +- [Examples](#examples) +- [Ruby version support policy](#ruby-version-support-policy) +- [License](#license) + +## 📢 We've Switched to Conventional Commits 📢 + +To enhance our development workflow, enable automated changelog generation, and pave +the way for Continuous Delivery, the `ruby-git` project has adopted the [Conventional +Commits standard](https://www.conventionalcommits.org/en/v1.0.0/) for all commit +messages. + +Going forward, all commits to this repository **MUST** adhere to the Conventional +Commits standard. Commits not adhering to this standard will cause the CI build to +fail. PRs will not be merged if they include non-conventional commits. + +A git pre-commit hook may be installed to validate your conventional commit messages +before pushing them to GitHub by running `bin/setup` in the project root. + +Read more about this change in the [Commit Message Guidelines section of +CONTRIBUTING.md](CONTRIBUTING.md#commit-message-guidelines) ## Summary @@ -34,31 +51,6 @@ Get started by obtaining a repository object by: Methods that can be called on a repository object are documented in [Git::Base](https://rubydoc.info/gems/git/Git/Base) -## v2.x Release - -git 2.0.0 has recently been released. Please give it a try. - -**If you have problems with the 2.x release, open an issue and use the 1.x version -instead.** We will do our best to fix your issues in a timely fashion. - -**JRuby on Windows is not yet supported by the 2.x release line. Users running JRuby -on Windows should continue to use the 1.x release line.** - -The changes in this major release include: - -* Added a dependency on the activesupport gem to use the deprecation functionality -* Create a policy of supported Ruby versions to support only non-EOL Ruby versions -* Create a policy of supported Git CLI versions (released 2020-12-25) -* Update the required Ruby version to at least 3.0 (released 2020-07-27) -* Update the required Git command line version to at least 2.28 -* Update how CLI commands are called to use the [process_executer](https://github.com/main-branch/process_executer) - gem which is built on top of [Kernel.spawn](https://ruby-doc.org/3.3.0/Kernel.html#method-i-spawn). - See [PR #684](https://github.com/ruby-git/ruby-git/pull/684) for more details - on the motivation for this implementation. - -The `master` branch will be used for `2.x` development. If needed, fixes for `1.x` -version will be done on the `v1` branch. - ## Install Install the gem and add to the application's Gemfile by executing: diff --git a/RELEASING.md b/RELEASING.md deleted file mode 100644 index ead6293a..00000000 --- a/RELEASING.md +++ /dev/null @@ -1,85 +0,0 @@ - - -# How to release a new git.gem - -Releasing a new version of the `git` gem requires these steps: - -* [Install Prerequisites](#install-prerequisites) -* [Determine the SemVer release type](#determine-the-semver-release-type) -* [Create the release](#create-the-release) -* [Review the CHANGELOG and release PR](#review-the-changelog-and-release-pr) -* [Manually merge the release PR](#manually-merge-the-release-pr) -* [Publish the git gem to RubyGems.org](#publish-the-git-gem-to-rubygemsorg) - -## Install Prerequisites - -The following tools need to be installed in order to create the release: - -* [create_githhub_release](https://github.com/main-branch/create_github_release) is used to create the release -* [git](https://git-scm.com) is used by `create-github-release` to interact with the local and remote repositories -* [gh](https://cli.github.com) is used by `create-github-release` to create the release and PR in GitHub - -On a Mac, these tools can be installed using [gem](https://guides.rubygems.org/rubygems-basics/) and [brew](https://brew.sh): - -```shell -$ gem install create_github_release -... -$ brew install git -... -$ brew install gh -... -$ -``` - -## Determine the SemVer release type - -Determine the SemVer version increment that should be applied for the new release: - -* `major`: when the release includes incompatible API or functional changes. -* `minor`: when the release adds functionality in a backward-compatible manner -* `patch`: when the release includes small user-facing changes that are - backward-compatible and do not introduce new functionality. - -## Create the release - -Create the release using the `create-github-release` command. If the release type -is `major`, the command is: - -```shell -create-github-release major -``` - -Follow the directions given by the `create-github-release` command to finish the -release. Where the instructions given by the command differ than the instructions -below, follow the instructions given by the command. - -## Review the CHANGELOG and release PR - -The `create-github-release` command will output a link to the CHANGELOG and the PR -it created for the release. Review the CHANGELOG and have someone review and approve -the release PR. - -## Manually merge the release PR - -It is important to manually merge the PR so a separate merge commit can be avoided. -Use the commands output by the `create-github-release` which will looks like this -if you are creating a 2.0.0 release: - -```shell -git checkout master -git merge --ff-only release-v2.0.0 -git push -``` - -This will automatically close the release PR. - -## Publish the git gem to RubyGems.org - -Finally, publish the git gem to RubyGems.org using the following command: - -```shell -rake release:rubygem_push -``` diff --git a/Rakefile b/Rakefile index e2d8ef2a..72b93352 100644 --- a/Rakefile +++ b/Rakefile @@ -58,3 +58,10 @@ task :'test:gem' => :install do puts 'Gem Test Succeeded' end + +# Make it so that calling `rake release` just calls `rake release:rubygem_push` to +# avoid creating and pushing a new tag. + +Rake::Task['release'].clear +desc 'Customized release task to avoid creating a new tag' +task release: 'release:rubygem_push' diff --git a/bin/command_line_test b/bin/command_line_test index 918e2024..99c67f38 100755 --- a/bin/command_line_test +++ b/bin/command_line_test @@ -91,7 +91,8 @@ class CommandLineParser option_parser.separator '' option_parser.separator 'Options:' %i[ - define_help_option define_stdout_option define_stderr_option + define_help_option define_stdout_option define_stdout_file_option + define_stderr_option define_stderr_file_option define_exitstatus_option define_signal_option define_duration_option ].each { |m| send(m) } end @@ -116,6 +117,15 @@ class CommandLineParser end end + # Define the stdout-file option + # @return [void] + # @api private + def define_stdout_file_option + option_parser.on('--stdout-file="file"', 'Send contents of file to stdout') do |filename| + @stdout = File.read(filename) + end + end + # Define the stderr option # @return [void] # @api private @@ -125,6 +135,15 @@ class CommandLineParser end end + # Define the stderr-file option + # @return [void] + # @api private + def define_stderr_file_option + option_parser.on('--stderr-file="file"', 'Send contents of file to stderr') do |filename| + @stderr = File.read(filename) + end + end + # Define the exitstatus option # @return [void] # @api private diff --git a/bin/setup b/bin/setup index dce67d86..f16ff654 100755 --- a/bin/setup +++ b/bin/setup @@ -5,4 +5,9 @@ set -vx bundle install -# Do any other automated setup that you need to do here +if [ -x "$(command -v npm)" ]; then + npm install +else + echo "npm is not installed" + echo "Install npm then re-run this script to enable the conventional commit git hook." +fi 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/git.gemspec b/git.gemspec index ea257473..f8c49bdc 100644 --- a/git.gemspec +++ b/git.gemspec @@ -29,13 +29,13 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'activesupport', '>= 5.0' s.add_runtime_dependency 'addressable', '~> 2.8' - s.add_runtime_dependency 'process_executer', '~> 1.1' - s.add_runtime_dependency 'rchardet', '~> 1.8' + s.add_runtime_dependency 'process_executer', '~> 1.3' + s.add_runtime_dependency 'rchardet', '~> 1.9' - s.add_development_dependency 'create_github_release', '~> 1.4' - s.add_development_dependency 'minitar', '~> 0.9' - s.add_development_dependency 'mocha', '~> 2.1' - s.add_development_dependency 'rake', '~> 13.1' + s.add_development_dependency 'create_github_release', '~> 2.1' + s.add_development_dependency 'minitar', '~> 1.0' + s.add_development_dependency 'mocha', '~> 2.7' + s.add_development_dependency 'rake', '~> 13.2' s.add_development_dependency 'test-unit', '~> 3.6' unless RUBY_PLATFORM == 'java' diff --git a/lib/git.rb b/lib/git.rb index e995e96c..34b70caf 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'active_support' require 'active_support/deprecation' @@ -381,4 +383,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/author.rb b/lib/git/author.rb index 86d33047..5cf7cc72 100644 --- a/lib/git/author.rb +++ b/lib/git/author.rb @@ -1,7 +1,9 @@ +# frozen_string_literal: true + module Git class Author attr_accessor :name, :email, :date - + def initialize(author_string) if m = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string) @name = m[1] @@ -9,6 +11,5 @@ def initialize(author_string) @date = Time.at(m[3].to_i) end end - end end diff --git a/lib/git/base.rb b/lib/git/base.rb index ae909dcc..3f01530e 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'logger' require 'open3' @@ -36,6 +38,26 @@ def self.config @@config ||= Config.new end + def self.binary_version(binary_path) + result = nil + status = nil + + begin + result, status = Open3.capture2e(binary_path, "-c", "core.quotePath=true", "-c", "color.ui=false", "version") + result = result.chomp + rescue Errno::ENOENT + raise RuntimeError, "Failed to get git version: #{binary_path} not found" + end + + 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]) @@ -67,9 +89,14 @@ def self.root_of_worktree(working_dir) result = working_dir status = nil - git_cmd = "#{Git::Base.config.binary_path} -c core.quotePath=true -c color.ui=false rev-parse --show-toplevel 2>&1" - result, status = Open3.capture2(git_cmd, chdir: File.expand_path(working_dir)) - result = result.chomp + raise ArgumentError, "'#{working_dir}' does not exist" unless Dir.exist?(working_dir) + + begin + result, status = Open3.capture2e(Git::Base.config.binary_path, "-c", "core.quotePath=true", "-c", "color.ui=false", "rev-parse", "--show-toplevel", chdir: File.expand_path(working_dir)) + result = result.chomp + rescue Errno::ENOENT + raise ArgumentError, "Failed to find the root of the worktree: git binary not found" + end raise ArgumentError, "'#{working_dir}' is not in a git working tree" unless status.success? result @@ -650,10 +677,17 @@ def ls_tree(objectish, opts = {}) end def cat_file(objectish) - self.lib.object_contents(objectish) + self.lib.cat_file(objectish) end - # returns the name of the branch the working directory is currently on + # The name of the branch HEAD refers to or 'HEAD' if detached + # + # Returns one of the following: + # * The branch name that HEAD refers to (even if it is an unborn branch) + # * 'HEAD' if in a detached HEAD state + # + # @return [String] the name of the branch HEAD refers to or 'HEAD' if detached + # def current_branch self.lib.branch_current end diff --git a/lib/git/branch.rb b/lib/git/branch.rb index f6780b03..43d31767 100644 --- a/lib/git/branch.rb +++ b/lib/git/branch.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'git/path' module Git diff --git a/lib/git/branches.rb b/lib/git/branches.rb index fc871db8..e173faab 100644 --- a/lib/git/branches.rb +++ b/lib/git/branches.rb @@ -1,15 +1,17 @@ +# frozen_string_literal: true + module Git - + # object that holds all the available branches class Branches include Enumerable - + def initialize(base) @branches = {} - + @base = base - + @base.lib.branches_all.each do |b| @branches[b[0]] = Git::Branch.new(@base, b[0]) end @@ -18,21 +20,21 @@ def initialize(base) def local self.select { |b| !b.remote } end - + def remote self.select { |b| b.remote } end - + # array like methods def size @branches.size - end - + end + def each(&block) @branches.values.each(&block) end - + # Returns the target branch # # Example: @@ -50,14 +52,14 @@ def [](branch_name) @branches.values.inject(@branches) do |branches, branch| branches[branch.full] ||= branch - # This is how Git (version 1.7.9.5) works. - # Lets you ignore the 'remotes' if its at the beginning of the branch full name (even if is not a real remote branch). + # This is how Git (version 1.7.9.5) works. + # Lets you ignore the 'remotes' if its at the beginning of the branch full name (even if is not a real remote branch). branches[branch.full.sub('remotes/', '')] ||= branch if branch.full =~ /^remotes\/.+/ - + branches end[branch_name.to_s] end - + def to_s out = '' @branches.each do |k, b| @@ -65,7 +67,6 @@ def to_s end out end - end end diff --git a/lib/git/command_line.rb b/lib/git/command_line.rb index 276cdc78..6228a144 100644 --- a/lib/git/command_line.rb +++ b/lib/git/command_line.rb @@ -189,13 +189,14 @@ def initialize(env, binary_path, global_opts, logger) # # @raise [Git::TimeoutError] if the command times out # - def run(*args, out:, err:, normalize:, chomp:, merge:, chdir: nil, timeout: nil) + def run(*args, out: nil, err: nil, normalize:, chomp:, merge:, chdir: nil, timeout: nil) git_cmd = build_git_cmd(args) - out ||= StringIO.new - err ||= (merge ? out : StringIO.new) - status = execute(git_cmd, out, err, chdir: (chdir || :not_set), timeout: timeout) - - process_result(git_cmd, status, out, err, normalize, chomp, timeout) + begin + result = ProcessExecuter.run(env, *git_cmd, out: out, err: err, merge:, chdir: (chdir || :not_set), timeout: timeout, raise_errors: false) + rescue ProcessExecuter::Command::ProcessIOError => e + raise Git::ProcessIOError.new(e.message), cause: e.exception.cause + end + process_result(result, normalize, chomp, timeout) end private @@ -210,121 +211,12 @@ def build_git_cmd(args) [binary_path, *global_opts, *args].map { |e| e.to_s } end - # Determine the output to return in the `CommandLineResult` - # - # If the writer can return the output by calling `#string` (such as a StringIO), - # then return the result of normalizing the encoding and chomping the output - # as requested. - # - # If the writer does not support `#string`, then return nil. The output is - # assumed to be collected by the writer itself such as when the writer - # is a file instead of a StringIO. - # - # @param writer [#string] the writer to post-process - # - # @return [String, nil] - # - # @api private - # - def post_process(writer, normalize, chomp) - if writer.respond_to?(:string) - output = writer.string.dup - output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize - output.chomp! if chomp - output - else - nil - end - end - - # Post-process all writers and return an array of the results - # - # @param writers [Array<#write>] the writers to post-process - # @param normalize [Boolean] whether to normalize the output of each writer - # @param chomp [Boolean] whether to chomp the output of each writer - # - # @return [Array] the output of each writer that supports `#string` - # - # @api private - # - def post_process_all(writers, normalize, chomp) - Array.new.tap do |result| - writers.each { |writer| result << post_process(writer, normalize, chomp) } - end - end - - # Raise an error when there was exception while collecting the subprocess output - # - # @param git_cmd [Array] the git command that was executed - # @param pipe_name [Symbol] the name of the pipe that raised the exception - # @param pipe [ProcessExecuter::MonitoredPipe] the pipe that raised the exception - # - # @raise [Git::ProcessIOError] - # - # @return [void] this method always raises an error - # - # @api private - # - def raise_pipe_error(git_cmd, pipe_name, pipe) - raise Git::ProcessIOError.new("Pipe Exception for #{git_cmd}: #{pipe_name}"), cause: pipe.exception - end - - # Execute the git command and collect the output - # - # @param cmd [Array] the git command to execute - # @param chdir [String] the directory to run the command in - # @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete - # - # If timeout is zero of nil, the command will not time out. If the command - # times out, it is killed via a SIGKILL signal and `Git::TimeoutError` is raised. - # - # If the command does not respond to SIGKILL, it will hang this method. - # - # @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output - # @raise [Git::TimeoutError] if the command times out - # - # @return [ProcessExecuter::Status] the status of the completed subprocess - # - # @api private - # - def spawn(cmd, out_writers, err_writers, chdir:, timeout:) - out_pipe = ProcessExecuter::MonitoredPipe.new(*out_writers, chunk_size: 10_000) - err_pipe = ProcessExecuter::MonitoredPipe.new(*err_writers, chunk_size: 10_000) - ProcessExecuter.spawn(env, *cmd, out: out_pipe, err: err_pipe, chdir: chdir, timeout: timeout) - ensure - out_pipe.close - err_pipe.close - raise_pipe_error(cmd, :stdout, out_pipe) if out_pipe.exception - raise_pipe_error(cmd, :stderr, err_pipe) if err_pipe.exception - end - - # The writers that will be used to collect stdout and stderr - # - # Additional writers could be added here if you wanted to tee output - # or send output to the terminal. - # - # @param out [#write] the object to write stdout to - # @param err [#write] the object to write stderr to - # - # @return [Array, Array<#write>>] the writers for stdout and stderr - # - # @api private - # - def writers(out, err) - out_writers = [out] - err_writers = [err] - [out_writers, err_writers] - end - # Process the result of the command and return a Git::CommandLineResult # # Post process output, log the command and result, and raise an error if the # command failed. # - # @param git_cmd [Array] the git command that was executed - # @param status [Process::Status] the status of the completed subprocess - # @param out [#write] the object that stdout was written to - # @param err [#write] the object that stderr was written to + # @param result [ProcessExecuter::Command::Result] the result it is a Process::Status and include command, stdout, and stderr # @param normalize [Boolean] whether to normalize the output of each writer # @param chomp [Boolean] whether to chomp the output of each writer # @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete @@ -338,40 +230,58 @@ def writers(out, err) # # @api private # - def process_result(git_cmd, status, out, err, normalize, chomp, timeout) - out_str, err_str = post_process_all([out, err], normalize, chomp) - logger.info { "#{git_cmd} exited with status #{status}" } - logger.debug { "stdout:\n#{out_str.inspect}\nstderr:\n#{err_str.inspect}" } - Git::CommandLineResult.new(git_cmd, status, out_str, err_str).tap do |result| - raise Git::TimeoutError.new(result, timeout) if status.timeout? - raise Git::SignaledError.new(result) if status.signaled? - raise Git::FailedError.new(result) unless status.success? + def process_result(result, normalize, chomp, timeout) + command = result.command + processed_out, processed_err = post_process_all([result.stdout, result.stderr], normalize, chomp) + logger.info { "#{command} exited with status #{result}" } + logger.debug { "stdout:\n#{processed_out.inspect}\nstderr:\n#{processed_err.inspect}" } + Git::CommandLineResult.new(command, result, processed_out, processed_err).tap do |processed_result| + raise Git::TimeoutError.new(processed_result, timeout) if result.timeout? + raise Git::SignaledError.new(processed_result) if result.signaled? + raise Git::FailedError.new(processed_result) unless result.success? end end - # Execute the git command and write the command output to out and err + # Post-process command output and return an array of the results # - # @param git_cmd [Array] the git command to execute - # @param out [#write] the object to write stdout to - # @param err [#write] the object to write stderr to - # @param chdir [String] the directory to run the command in - # @param timeout [Numeric, nil] the maximum seconds to wait for the command to complete + # @param raw_outputs [Array] the output to post-process + # @param normalize [Boolean] whether to normalize the output of each writer + # @param chomp [Boolean] whether to chomp the output of each writer # - # If timeout is zero of nil, the command will not time out. If the command - # times out, it is killed via a SIGKILL signal and `Git::TimeoutError` is raised. + # @return [Array] the processed output of each command output object that supports `#string` # - # If the command does not respond to SIGKILL, it will hang this method. + # @api private # - # @raise [Git::ProcessIOError] if an exception was raised while collecting subprocess output - # @raise [Git::TimeoutError] if the command times out + def post_process_all(raw_outputs, normalize, chomp) + Array.new.tap do |result| + raw_outputs.each { |raw_output| result << post_process(raw_output, normalize, chomp) } + end + end + + # Determine the output to return in the `CommandLineResult` # - # @return [Git::CommandLineResult] the result of the command to return to the caller + # If the writer can return the output by calling `#string` (such as a StringIO), + # then return the result of normalizing the encoding and chomping the output + # as requested. + # + # If the writer does not support `#string`, then return nil. The output is + # assumed to be collected by the writer itself such as when the writer + # is a file instead of a StringIO. + # + # @param raw_output [#string] the output to post-process + # @return [String, nil] # # @api private # - def execute(git_cmd, out, err, chdir:, timeout:) - out_writers, err_writers = writers(out, err) - spawn(git_cmd, out_writers, err_writers, chdir: chdir, timeout: timeout) + def post_process(raw_output, normalize, chomp) + if raw_output.respond_to?(:string) + output = raw_output.string.dup + output = output.lines.map { |l| Git::EncodingUtils.normalize_encoding(l) }.join if normalize + output.chomp! if chomp + output + else + nil + end end end end diff --git a/lib/git/config.rb b/lib/git/config.rb index 0a3fd71e..3dd35869 100644 --- a/lib/git/config.rb +++ b/lib/git/config.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git class Config diff --git a/lib/git/diff.rb b/lib/git/diff.rb index d40ddce4..303a0a89 100644 --- a/lib/git/diff.rb +++ b/lib/git/diff.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git # object that holds the last X commits on given branch diff --git a/lib/git/index.rb b/lib/git/index.rb index c27820dc..45e2de40 100644 --- a/lib/git/index.rb +++ b/lib/git/index.rb @@ -1,5 +1,6 @@ +# frozen_string_literal: true + module Git class Index < Git::Path - end end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 1742130e..692ceef9 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'git/command_line' require 'git/errors' require 'logger' @@ -292,6 +294,7 @@ def log_commits(opts = {}) # * 'tree' [String] the tree sha # * 'author' [String] the author of the commit and timestamp of when the changes were created # * 'committer' [String] the committer of the commit and timestamp of when the commit was applied + # * 'merges' [Boolean] if truthy, only include merge commits (aka commits with 2 or more parents) # # @raise [ArgumentError] if the revision range (specified with :between or :object) is a string starting with a hyphen # @@ -303,6 +306,7 @@ def full_log_commits(opts = {}) arr_opts << '--pretty=raw' arr_opts << "--skip=#{opts[:skip]}" if opts[:skip] + arr_opts << '--merges' if opts[:merges] arr_opts += log_path_options(opts) @@ -331,7 +335,7 @@ def full_log_commits(opts = {}) def rev_parse(revision) assert_args_are_not_options('rev', revision) - command('rev-parse', revision) + command('rev-parse', '--revs-only', '--end-of-options', revision, '--') end # For backwards compatibility with the old method name @@ -353,21 +357,105 @@ def name_rev(commit_ish) alias :namerev :name_rev - def object_type(sha) - command('cat-file', '-t', sha) + # Output the contents or other properties of one or more objects. + # + # @see https://git-scm.com/docs/git-cat-file git-cat-file + # + # @example Get the contents of a file without a block + # lib.cat_file_contents('README.md') # => "This is a README file\n" + # + # @example Get the contents of a file with a block + # lib.cat_file_contents('README.md') { |f| f.read } # => "This is a README file\n" + # + # @param object [String] the object whose contents to return + # + # @return [String] the object contents + # + # @raise [ArgumentError] if object is a string starting with a hyphen + # + def cat_file_contents(object, &block) + assert_args_are_not_options('object', object) + + if block_given? + Tempfile.create do |file| + # If a block is given, write the output from the process to a temporary + # file and then yield the file to the block + # + command('cat-file', "-p", object, out: file, err: file) + file.rewind + yield file + end + else + # If a block is not given, return the file contents as a string + command('cat-file', '-p', object) + end + end + + alias :object_contents :cat_file_contents + + # Get the type for the given object + # + # @see https://git-scm.com/docs/git-cat-file git-cat-file + # + # @param object [String] the object to get the type + # + # @return [String] the object type + # + # @raise [ArgumentError] if object is a string starting with a hyphen + # + def cat_file_type(object) + assert_args_are_not_options('object', object) + + command('cat-file', '-t', object) end - def object_size(sha) - command('cat-file', '-s', sha).to_i + alias :object_type :cat_file_type + + # Get the size for the given object + # + # @see https://git-scm.com/docs/git-cat-file git-cat-file + # + # @param object [String] the object to get the type + # + # @return [String] the object type + # + # @raise [ArgumentError] if object is a string starting with a hyphen + # + def cat_file_size(object) + assert_args_are_not_options('object', object) + + command('cat-file', '-s', object).to_i end - # returns useful array of raw commit object data - def commit_data(sha) - sha = sha.to_s - cdata = command_lines('cat-file', 'commit', sha) - process_commit_data(cdata, sha) + alias :object_size :cat_file_size + + # Return a hash of commit data + # + # @see https://git-scm.com/docs/git-cat-file git-cat-file + # + # @param object [String] the object to get the type + # + # @return [Hash] commit data + # + # The returned commit data has the following keys: + # * tree [String] + # * parent [Array] + # * author [String] the author name, email, and commit timestamp + # * committer [String] the committer name, email, and merge timestamp + # * message [String] the commit message + # * gpgsig [String] the public signing key of the commit (if signed) + # + # @raise [ArgumentError] if object is a string starting with a hyphen + # + def cat_file_commit(object) + assert_args_are_not_options('object', object) + + cdata = command_lines('cat-file', 'commit', object) + process_commit_data(cdata, object) end + alias :commit_data :cat_file_commit + def process_commit_data(data, sha) hsh = { 'sha' => sha, @@ -402,12 +490,50 @@ def each_cat_file_header(data) end end - def tag_data(name) - sha = sha.to_s - tdata = command_lines('cat-file', 'tag', name) - process_tag_data(tdata, name) + # Return a hash of annotated tag data + # + # Does not work with lightweight tags. List all annotated tags in your repository with the following command: + # + # ```sh + # git for-each-ref --format='%(refname:strip=2)' refs/tags | while read tag; do git cat-file tag $tag >/dev/null 2>&1 && echo $tag; done + # ``` + # + # @see https://git-scm.com/docs/git-cat-file git-cat-file + # + # @param object [String] the tag to retrieve + # + # @return [Hash] tag data + # + # Example tag data returned: + # ```ruby + # { + # "name" => "annotated_tag", + # "object" => "46abbf07e3c564c723c7c039a43ab3a39e5d02dd", + # "type" => "commit", + # "tag" => "annotated_tag", + # "tagger" => "Scott Chacon 1724799270 -0700", + # "message" => "Creating an annotated tag\n" + # } + # ``` + # + # The returned commit data has the following keys: + # * object [String] the sha of the tag object + # * type [String] + # * tag [String] tag name + # * tagger [String] the name and email of the user who created the tag and the timestamp of when the tag was created + # * message [String] the tag message + # + # @raise [ArgumentError] if object is a string starting with a hyphen + # + def cat_file_tag(object) + assert_args_are_not_options('object', object) + + tdata = command_lines('cat-file', 'tag', object) + process_tag_data(tdata, object) end + alias :tag_data :cat_file_tag + def process_tag_data(data, name) hsh = { 'name' => name } @@ -448,7 +574,7 @@ def process_commit_log_data(data) case key when 'commit' hsh_array << hsh if hsh - hsh = {'sha' => value, 'message' => '', 'parent' => []} + hsh = {'sha' => value, 'message' => +'', 'parent' => []} when 'parent' hsh['parent'] << value else @@ -461,22 +587,6 @@ def process_commit_log_data(data) return hsh_array end - def object_contents(sha, &block) - if block_given? - Tempfile.create do |file| - # If a block is given, write the output from the process to a temporary - # file and then yield the file to the block - # - command('cat-file', "-p", sha, out: file, err: file) - file.rewind - yield file - end - else - # If a block is not given, return stdout - command('cat-file', '-p', sha) - end - end - def ls_tree(sha, opts = {}) data = { 'blob' => {}, 'tree' => {}, 'commit' => {} } @@ -535,10 +645,13 @@ def change_head_branch(branch_name) /x def branches_all - command_lines('branch', '-a').map do |line| + lines = command_lines('branch', '-a') + lines.each_with_index.map do |line, line_index| match_data = line.match(BRANCH_LINE_REGEXP) - raise Git::UnexpectedResultError, 'Unexpected branch line format' unless match_data + + raise Git::UnexpectedResultError, unexpected_branch_line_error(lines, line, line_index) unless match_data next nil if match_data[:not_a_branch] || match_data[:detached_ref] + [ match_data[:refname], !match_data[:current].nil?, @@ -548,6 +661,18 @@ def branches_all end.compact end + def unexpected_branch_line_error(lines, line, index) + <<~ERROR + Unexpected line in output from `git branch -a`, line #{index + 1} + + Full output: + #{lines.join("\n ")} + + Line #{index + 1}: + "#{line}" + ERROR + end + def worktrees_all arr = [] directory = '' @@ -591,8 +716,54 @@ def list_files(ref_dir) files end + # The state and name of branch pointed to by `HEAD` + # + # HEAD can be in the following states: + # + # **:active**: `HEAD` points to a branch reference which in turn points to a + # commit representing the tip of that branch. This is the typical state when + # working on a branch. + # + # **:unborn**: `HEAD` points to a branch reference that does not yet exist + # because no commits have been made on that branch. This state occurs in two + # scenarios: + # + # * When a repository is newly initialized, and no commits have been made on the + # initial branch. + # * When a new branch is created using `git checkout --orphan `, starting + # a new branch with no history. + # + # **:detached**: `HEAD` points directly to a specific commit (identified by its + # SHA) rather than a branch reference. This state occurs when you check out a + # commit, a tag, or any state that is not directly associated with a branch. The + # branch name in this case is `HEAD`. + # + HeadState = Struct.new(:state, :name) + + # The current branch state which is the state of `HEAD` + # + # @return [HeadState] the state and name of the current branch + # + def current_branch_state + branch_name = command('branch', '--show-current') + return HeadState.new(:detached, 'HEAD') if branch_name.empty? + + state = + begin + command('rev-parse', '--verify', '--quiet', branch_name) + :active + rescue Git::FailedError => e + raise unless e.result.status.exitstatus == 1 && e.result.stderr.empty? + + :unborn + end + + return HeadState.new(state, branch_name) + end + def branch_current - branches_all.select { |b| b[1] }.first[0] rescue nil + branch_name = command('branch', '--show-current') + branch_name.empty? ? 'HEAD' : branch_name end def branch_contains(commit, branch_name="") @@ -982,8 +1153,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 @@ -1376,7 +1549,8 @@ def env_overrides 'GIT_DIR' => @git_dir, 'GIT_WORK_TREE' => @git_work_dir, 'GIT_INDEX_FILE' => @git_index_file, - 'GIT_SSH' => Git::Base.config.git_ssh + 'GIT_SSH' => Git::Base.config.git_ssh, + 'LC_ALL' => 'en_US.UTF-8' } end diff --git a/lib/git/log.rb b/lib/git/log.rb index 817d8635..7ac31622 100644 --- a/lib/git/log.rb +++ b/lib/git/log.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git # Return the last n commits that match the specified criteria @@ -131,11 +133,16 @@ def cherry return self end + def merges + dirty_log + @merges = true + return self + end + def to_s self.map { |c| c.to_s }.join("\n") end - # forces git log to run def size @@ -182,7 +189,7 @@ def run_log log = @base.lib.full_log_commits( count: @max_count, all: @all, object: @object, path_limiter: @path, since: @since, author: @author, grep: @grep, skip: @skip, until: @until, between: @between, - cherry: @cherry + cherry: @cherry, merges: @merges ) @commits = log.map { |c| Git::Object::Commit.new(@base, c['sha'], c) } end diff --git a/lib/git/object.rb b/lib/git/object.rb index 6c4aada9..9abbfa08 100644 --- a/lib/git/object.rb +++ b/lib/git/object.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'git/author' require 'git/diff' require 'git/errors' @@ -27,7 +29,7 @@ def sha end def size - @size ||= @base.lib.object_size(@objectish) + @size ||= @base.lib.cat_file_size(@objectish) end # Get the object's contents. @@ -38,9 +40,9 @@ def size # Use this for large files so that they are not held in memory. def contents(&block) if block_given? - @base.lib.object_contents(@objectish, &block) + @base.lib.cat_file_contents(@objectish, &block) else - @contents ||= @base.lib.object_contents(@objectish) + @contents ||= @base.lib.cat_file_contents(@objectish) end end @@ -237,7 +239,7 @@ def commit? def check_commit return if @tree - data = @base.lib.commit_data(@objectish) + data = @base.lib.cat_file_commit(@objectish) set_commit(data) end @@ -254,7 +256,7 @@ def initialize(base, sha, name) end def annotated? - @annotated ||= (@base.lib.object_type(self.name) == 'tag') + @annotated ||= (@base.lib.cat_file_type(self.name) == 'tag') end def message @@ -279,7 +281,7 @@ def check_tag if !self.annotated? @message = @tagger = nil else - tdata = @base.lib.tag_data(@name) + tdata = @base.lib.cat_file_tag(@name) @message = tdata['message'].chomp @tagger = Git::Author.new(tdata['tagger']) end @@ -300,7 +302,7 @@ def self.new(base, objectish, type = nil, is_tag = false) return Git::Object::Tag.new(base, sha, objectish) end - type ||= base.lib.object_type(objectish) + type ||= base.lib.cat_file_type(objectish) klass = case type when /blob/ then Blob diff --git a/lib/git/path.rb b/lib/git/path.rb index 4b20d9a7..a030fcb3 100644 --- a/lib/git/path.rb +++ b/lib/git/path.rb @@ -1,19 +1,21 @@ +# frozen_string_literal: true + module Git - + class Path - + attr_accessor :path - + def initialize(path, check_path=true) path = File.expand_path(path) - + if check_path && !File.exist?(path) raise ArgumentError, 'path does not exist', [path] end - + @path = path end - + def readable? File.readable?(@path) end @@ -21,11 +23,10 @@ def readable? def writable? File.writable?(@path) end - + def to_s @path end - end end diff --git a/lib/git/remote.rb b/lib/git/remote.rb index 9b2f3958..0615ff9b 100644 --- a/lib/git/remote.rb +++ b/lib/git/remote.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git class Remote < Path diff --git a/lib/git/repository.rb b/lib/git/repository.rb index 95f3bef6..00f2b529 100644 --- a/lib/git/repository.rb +++ b/lib/git/repository.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git class Repository < Path diff --git a/lib/git/stash.rb b/lib/git/stash.rb index 97de906c..43897a33 100644 --- a/lib/git/stash.rb +++ b/lib/git/stash.rb @@ -1,27 +1,28 @@ +# frozen_string_literal: true + module Git class Stash - + def initialize(base, message, existing=false) @base = base @message = message save unless existing end - + def save @saved = @base.lib.stash_save(@message) end - + def saved? @saved end - + def message @message end - + def to_s message end - end end \ No newline at end of file diff --git a/lib/git/stashes.rb b/lib/git/stashes.rb index 0ebb9bed..2ccc55d7 100644 --- a/lib/git/stashes.rb +++ b/lib/git/stashes.rb @@ -1,14 +1,16 @@ +# frozen_string_literal: true + module Git - + # object that holds all the available stashes class Stashes include Enumerable - + def initialize(base) @stashes = [] - + @base = base - + @base.lib.stashes_all.each do |id, message| @stashes.unshift(Git::Stash.new(@base, message, true)) end @@ -24,16 +26,16 @@ def initialize(base) def all @base.lib.stashes_all end - + def save(message) s = Git::Stash.new(@base, message) @stashes.unshift(s) if s.saved? end - + def apply(index=nil) @base.lib.stash_apply(index) end - + def clear @base.lib.stash_clear @stashes = [] @@ -42,14 +44,13 @@ def clear def size @stashes.size end - + def each(&block) @stashes.each(&block) end - + def [](index) @stashes[index.to_i] end - end end diff --git a/lib/git/status.rb b/lib/git/status.rb index 39ceace7..08deeccd 100644 --- a/lib/git/status.rb +++ b/lib/git/status.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git # The status class gets the status of a git repository # @@ -100,7 +102,7 @@ def untracked?(file) end def pretty - out = '' + out = +'' each do |file| out << pretty_file(file) end diff --git a/lib/git/version.rb b/lib/git/version.rb index 15f996be..0a293cc1 100644 --- a/lib/git/version.rb +++ b/lib/git/version.rb @@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Git # The current gem version # @return [String] the current gem version. - VERSION='2.2.0' + VERSION='3.1.0' end diff --git a/lib/git/working_directory.rb b/lib/git/working_directory.rb index 3f37f1a5..94520065 100644 --- a/lib/git/working_directory.rb +++ b/lib/git/working_directory.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git class WorkingDirectory < Git::Path end diff --git a/lib/git/worktree.rb b/lib/git/worktree.rb index 24e79b5b..9754f5ab 100644 --- a/lib/git/worktree.rb +++ b/lib/git/worktree.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'git/path' module Git diff --git a/lib/git/worktrees.rb b/lib/git/worktrees.rb index 0cc53ba6..859c5054 100644 --- a/lib/git/worktrees.rb +++ b/lib/git/worktrees.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Git # object that holds all the available worktrees class Worktrees diff --git a/package.json b/package.json new file mode 100644 index 00000000..2924004f --- /dev/null +++ b/package.json @@ -0,0 +1,10 @@ +{ + "devDependencies": { + "@commitlint/cli": "^19.8.0", + "@commitlint/config-conventional": "^19.8.0", + "husky": "^9.1.7" + }, + "scripts": { + "prepare": "husky" + } +} diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 00000000..b0c93860 --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,36 @@ +{ + "bootstrap-sha": "31374263eafea4e23352494ef4f6bea3ce62c1b5", + "packages": { + ".": { + "release-type": "ruby", + "package-name": "git", + "changelog-path": "CHANGELOG.md", + "version-file": "lib/git/version.rb", + "bump-minor-pre-major": true, + "bump-patch-for-minor-pre-major": true, + "draft": false, + "prerelease": false, + "include-component-in-tag": false, + "pull-request-title-pattern": "chore: release v${version}", + "changelog-sections": [ + { "type": "feat", "section": "Features", "hidden": false }, + { "type": "fix", "section": "Bug Fixes", "hidden": false }, + { "type": "build", "section": "Other Changes", "hidden": false }, + { "type": "chore", "section": "Other Changes", "hidden": false }, + { "type": "ci", "section": "Other Changes", "hidden": false }, + { "type": "docs", "section": "Other Changes", "hidden": false }, + { "type": "perf", "section": "Other Changes", "hidden": false }, + { "type": "refactor", "section": "Other Changes", "hidden": false }, + { "type": "revert", "section": "Other Changes", "hidden": false }, + { "type": "style", "section": "Other Changes", "hidden": false }, + { "type": "test", "section": "Other Changes", "hidden": false } + ] + } + }, + "plugins": [ + { + "type": "sentence-case" + } + ], + "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json" +} 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/test_helper.rb b/tests/test_helper.rb index 7be31378..f35a0fcd 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'date' require 'fileutils' require 'minitar' @@ -129,7 +131,7 @@ def append_file(name, contents) # # @return [void] # - def assert_command_line_eq(expected_command_line, method: :command, mocked_output: nil) + def assert_command_line_eq(expected_command_line, method: :command, mocked_output: '', include_env: false) actual_command_line = nil command_output = '' @@ -138,7 +140,11 @@ def assert_command_line_eq(expected_command_line, method: :command, mocked_outpu git = Git.init('test_project') git.lib.define_singleton_method(method) do |*cmd, **opts, &block| - actual_command_line = [*cmd, opts] + if include_env + actual_command_line = [env_overrides, *cmd, opts] + else + actual_command_line = [*cmd, opts] + end mocked_output end @@ -147,6 +153,8 @@ def assert_command_line_eq(expected_command_line, method: :command, mocked_outpu end end + expected_command_line = expected_command_line.call if expected_command_line.is_a?(Proc) + assert_equal(expected_command_line, actual_command_line) command_output @@ -162,4 +170,112 @@ def windows_platform? win_platform_regex = /mingw|mswin/ RUBY_PLATFORM =~ win_platform_regex || RUBY_DESCRIPTION =~ win_platform_regex end + + require 'delegate' + + # A wrapper around a ProcessExecuter::Status that also includes command output + # @api public + class CommandResult < SimpleDelegator + # Create a new CommandResult + # @example + # status = ProcessExecuter.spawn(*command, timeout:, out:, err:) + # CommandResult.new(status, out_buffer.string, err_buffer.string) + # @param status [ProcessExecuter::Status] The status of the process + # @param out [String] The standard output of the process + # @param err [String] The standard error of the process + def initialize(status, out, err) + super(status) + @out = out + @err = err + end + + # @return [String] The stdout output of the process + attr_reader :out + + # @return [String] The stderr output of the process + attr_reader :err + end + + # Run a command and return the status including stdout and stderr output + # + # @example + # command = %w[git status] + # status = run(command) + # status.success? # => true + # status.exitstatus # => 0 + # status.out # => "On branch master\nnothing to commit, working tree clean\n" + # status.err # => "" + # + # @param command [Array] The command to run + # @param timeout [Numeric, nil] Seconds to allow command to run before killing it or nil for no timeout + # @param raise_errors [Boolean] Raise an exception if the command fails + # @param error_message [String] The message to use when raising an exception + # + # @return [CommandResult] The result of running + # + def run_command(*command, timeout: nil, raise_errors: true, error_message: "#{command[0]} failed") + out_buffer = StringIO.new + out = ProcessExecuter::MonitoredPipe.new(out_buffer) + err_buffer = StringIO.new + err = ProcessExecuter::MonitoredPipe.new(err_buffer) + + status = ProcessExecuter.spawn(*command, timeout: timeout, out: out, err: err) + + raise "#{error_message}: #{err_buffer.string}" if raise_errors && !status.success? + + CommandResult.new(status, out_buffer.string, err_buffer.string) + end +end + +# Replace the default git binary with the given script +# +# This method creates a temporary directory and writes the given script to a file +# named `git` in a subdirectory named `bin`. This subdirectory name can be changed by +# passing a different value for the `subdir` parameter. +# +# On non-windows platforms, make sure the script starts with a hash bang. On windows, +# make sure the script has a `.bat` extension. +# +# On non-windows platforms, the script is made executable. +# +# `Git::Base.config.binary_path` set to the path to the script. +# +# The block is called passing the path to the mocked git binary. +# +# `Git::Base.config.binary_path` is reset to its original value after the block +# returns. +# +# @example mocked_git_script = <<~GIT_SCRIPT #!/bin/sh puts 'git version 1.2.3' +# GIT_SCRIPT +# +# mock_git_binary(mocked_git_script) do +# # Run Git commands here -- they will call the mocked git script +# end +# +# @param script [String] The bash script to run instead of the real git binary +# +# @param subdir [String] The subdirectory to place the mocked git binary in +# +# @yield Call the block while the git binary is mocked +# +# @yieldparam git_binary_path [String] The path to the mocked git binary +# +# @yieldreturn [void] the return value of the block is ignored +# +# @return [void] +# +def mock_git_binary(script, subdir: 'bin') + Dir.mktmpdir do |binary_dir| + binary_name = windows_platform? ? 'git.bat' : 'git' + git_binary_path = File.join(binary_dir, subdir, binary_name) + FileUtils.mkdir_p(File.dirname(git_binary_path)) + File.write(git_binary_path, script) + File.chmod(0755, git_binary_path) unless windows_platform? + saved_binary_path = Git::Base.config.binary_path + Git::Base.config.binary_path = git_binary_path + + yield git_binary_path + + Git::Base.config.binary_path = saved_binary_path + end end diff --git a/tests/units/test_archive.rb b/tests/units/test_archive.rb index 13c40f7a..96522e22 100644 --- a/tests/units/test_archive.rb +++ b/tests/units/test_archive.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_bare.rb b/tests/units/test_bare.rb index 4972a219..f168c724 100644 --- a/tests/units/test_bare.rb +++ b/tests/units/test_bare.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_base.rb b/tests/units/test_base.rb index b0d1a589..8cb24043 100644 --- a/tests/units/test_base.rb +++ b/tests/units/test_base.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' @@ -11,7 +11,7 @@ def setup def test_add in_temp_dir do |path| git = Git.clone(@wdir, 'test_add') - + create_file('test_add/test_file_1', 'content tets_file_1') create_file('test_add/test_file_2', 'content test_file_2') create_file('test_add/test_file_3', 'content test_file_3') @@ -19,7 +19,7 @@ def test_add create_file('test_add/test file with \' quote', 'content test_file_4') assert(!git.status.added.assoc('test_file_1')) - + # Adding a single file, usign String git.add('test_file_1') @@ -39,11 +39,11 @@ def test_add assert(git.status.added.assoc('test_file_3')) assert(git.status.added.assoc('test_file_4')) assert(git.status.added.assoc('test file with \' quote')) - + git.commit('test_add commit #1') assert(git.status.added.empty?) - + delete_file('test_add/test_file_3') update_file('test_add/test_file_4', 'content test_file_4 update #1') create_file('test_add/test_file_5', 'content test_file_5') @@ -54,24 +54,24 @@ def test_add assert(git.status.deleted.assoc('test_file_3')) assert(git.status.changed.assoc('test_file_4')) assert(git.status.added.assoc('test_file_5')) - + git.commit('test_add commit #2') - + assert(git.status.deleted.empty?) assert(git.status.changed.empty?) assert(git.status.added.empty?) - + delete_file('test_add/test_file_4') update_file('test_add/test_file_5', 'content test_file_5 update #1') create_file('test_add/test_file_6', 'content test_fiile_6') - + # Adding all files (new or updated), without params git.add - + assert(git.status.deleted.assoc('test_file_4')) assert(git.status.changed.assoc('test_file_5')) assert(git.status.added.assoc('test_file_6')) - + git.commit('test_add commit #3') assert(git.status.changed.empty?) @@ -82,7 +82,7 @@ def test_add def test_commit in_temp_dir do |path| git = Git.clone(@wdir, 'test_commit') - + create_file('test_commit/test_file_1', 'content tets_file_1') create_file('test_commit/test_file_2', 'content test_file_2') @@ -96,7 +96,7 @@ def test_commit original_commit_id = git.log[0].objectish create_file('test_commit/test_file_3', 'content test_file_3') - + git.add('test_file_3') git.commit(nil, :amend => true) @@ -105,5 +105,4 @@ def test_commit assert(git.log[1].objectish == base_commit_id) end end - end diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb index 2256f4cb..98edb8df 100644 --- a/tests/units/test_branch.rb +++ b/tests/units/test_branch.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' @@ -50,6 +50,136 @@ def setup end end + test 'Git::Base#branches when checked out branch is a remote branch' do + in_temp_dir do + Dir.mkdir('remote_git') + Dir.chdir('remote_git') do + run_command 'git', 'init', '--initial-branch=main' + File.write('file1.txt', 'This is file1') + run_command 'git', 'add', 'file1.txt' + run_command 'git', 'commit', '-m', 'Add file1.txt' + end + + run_command 'git', 'clone', File.join('remote_git', '.git'), 'local_git' + + Dir.chdir('local_git') do + run_command 'git', 'checkout', 'origin/main' + git = Git.open('.') + assert_nothing_raised { git.branches } + end + end + end + + # Git::Lib#current_branch_state + + test 'Git::Lib#current_branch_state -- empty repository' do + in_temp_dir do + `git init --initial-branch=my_initial_branch` + git = Git.open('.') + expected_state = Git::Lib::HeadState.new(:unborn, 'my_initial_branch') + assert_equal(expected_state, git.lib.current_branch_state) + end + end + + test 'Git::Lib#current_branch_state -- new orphan branch' do + in_temp_dir do + `git init --initial-branch=main` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + `git checkout --orphan orphan_branch 2> #{File::NULL}` + git = Git.open('.') + expected_state = Git::Lib::HeadState.new(:unborn, 'orphan_branch') + assert_equal(expected_state, git.lib.current_branch_state) + end + end + + test 'Git::Lib#current_branch_state -- active branch' do + in_temp_dir do + `git init --initial-branch=my_branch` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + git = Git.open('.') + expected_state = Git::Lib::HeadState.new(:active, 'my_branch') + assert_equal(expected_state, git.lib.current_branch_state) + end + end + + test 'Git::Lib#current_branch_state -- detached HEAD' do + in_temp_dir do + `git init --initial-branch=main` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + `echo "update" > file1.txt` + `git add file1.txt` + `git commit -m "Second commit"` + `git checkout HEAD~1 2> #{File::NULL}` + git = Git.open('.') + expected_state = Git::Lib::HeadState.new(:detached, 'HEAD') + assert_equal(expected_state, git.lib.current_branch_state) + end + end + + # Git::Lib#branch_current + + test 'Git::Lib#branch_current -- active branch' do + in_temp_dir do + `git init --initial-branch=main` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + git = Git.open('.') + assert_equal('main', git.lib.branch_current) + end + end + + test 'Git::Lib#branch_current -- unborn branch' do + in_temp_dir do + `git init --initial-branch=new_branch` + git = Git.open('.') + assert_equal('new_branch', git.lib.branch_current) + end + end + + test 'Git::Lib#branch_current -- detached HEAD' do + in_temp_dir do + `git init --initial-branch=main` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "First commit"` + `echo "update" > file1.txt` + `git add file1.txt` + `git commit -m "Second commit"` + `git checkout HEAD~1 2> #{File::NULL}` + git = Git.open('.') + assert_equal('HEAD', git.lib.branch_current) + end + end + + # Git::Base#branch + + test 'Git::Base#branch with detached head' do + in_temp_dir do + `git init` + `echo "hello world" > file1.txt` + `git add file1.txt` + `git commit -m "Initial commit"` + `echo "hello to another world" > file2.txt` + `git add file2.txt` + `git commit -m "Add another world"` + `git checkout HEAD~1 2> #{File::NULL}` + + git = Git.open('.') + branch = git.branch + + assert_equal('HEAD', branch.name) + end + end + + # Git::Base#branchs + test 'Git::Base#branchs with detached head' do in_temp_dir do git = Git.init('.', initial_branch: 'master') diff --git a/tests/units/test_checkout.rb b/tests/units/test_checkout.rb index a30b3fcc..94dba2ff 100644 --- a/tests/units/test_checkout.rb +++ b/tests/units/test_checkout.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestCheckout < Test::Unit::TestCase diff --git a/tests/units/test_command_line.rb b/tests/units/test_command_line.rb index eac144fb..7062d1aa 100644 --- a/tests/units/test_command_line.rb +++ b/tests/units/test_command_line.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' require 'tempfile' @@ -92,10 +94,10 @@ def merge args = ['--stdout=stdout output', '--stderr=stderr output'] result = command_line.run(*args, out: out_writer, err: err_writer, normalize: normalize, chomp: chomp, merge: merge) - assert_equal(['ruby', 'bin/command_line_test', '--stdout=stdout output', '--stderr=stderr output'], result.git_cmd) + assert_equal([{}, 'ruby', 'bin/command_line_test', '--stdout=stdout output', '--stderr=stderr output'], result.git_cmd) assert_equal('stdout output', result.stdout.chomp) assert_equal('stderr output', result.stderr.chomp) - assert(result.status.is_a? ProcessExecuter::Status) + assert(result.status.is_a? ProcessExecuter::Command::Result) assert_equal(0, result.status.exitstatus) end @@ -109,7 +111,7 @@ def merge # The error raised should include the result of the command result = error.result - assert_equal(['ruby', 'bin/command_line_test', '--exitstatus=1', '--stdout=O1', '--stderr=O2'], result.git_cmd) + assert_equal([{}, 'ruby', 'bin/command_line_test', '--exitstatus=1', '--stdout=O1', '--stderr=O2'], result.git_cmd) assert_equal('O1', result.stdout.chomp) assert_equal('O2', result.stderr.chomp) assert_equal(1, result.status.exitstatus) @@ -128,7 +130,7 @@ def merge # The error raised should include the result of the command result = error.result - assert_equal(['ruby', 'bin/command_line_test', '--signal=9', '--stdout=O1', '--stderr=O2'], result.git_cmd) + assert_equal([{}, 'ruby', 'bin/command_line_test', '--signal=9', '--stdout=O1', '--stderr=O2'], result.git_cmd) # If stdout is buffered, it may not be flushed when the process is killed # assert_equal('O1', result.stdout.chomp) assert_equal('O2', result.stderr.chomp) @@ -147,14 +149,7 @@ def merge test "run should normalize output if normalize is true" do command_line = Git::CommandLine.new(env, binary_path, global_opts, logger) - args = ['--stdout=stdout output'] - - def command_line.spawn(cmd, out_writers, err_writers, chdir: nil, timeout: nil) - out_writers.each { |w| w.write(File.read('tests/files/encoding/test1.txt')) } - `true` - ProcessExecuter::Status.new($?, false) # return status - end - + args = ['--stdout-file=tests/files/encoding/test1.txt'] normalize = true result = command_line.run(*args, out: out_writer, err: err_writer, normalize: normalize, chomp: chomp, merge: merge) @@ -165,28 +160,22 @@ def command_line.spawn(cmd, out_writers, err_writers, chdir: nil, timeout: nil) Φεθγιατ θρβανιτασ ρεπριμιqθε OUTPUT - assert_equal(expected_output, result.stdout) + assert_equal(expected_output, result.stdout.delete("\r")) end test "run should NOT normalize output if normalize is false" do command_line = Git::CommandLine.new(env, binary_path, global_opts, logger) - args = ['--stdout=stdout output'] - - def command_line.spawn(cmd, out_writers, err_writers, chdir: nil, timeout: nil) - out_writers.each { |w| w.write(File.read('tests/files/encoding/test1.txt')) } - `true` - ProcessExecuter::Status.new($?, false) # return status - end - + args = ['--stdout-file=tests/files/encoding/test1.txt'] normalize = false result = command_line.run(*args, out: out_writer, err: err_writer, normalize: normalize, chomp: chomp, merge: merge) - expected_output = <<~OUTPUT - \xCB\xEF\xF1\xE5\xEC \xE9\xF0\xF3\xE8\xEC \xE4\xEF\xEB\xEF\xF1 \xF3\xE9\xF4 - \xC7\xE9\xF3 \xE5\xEE \xF4\xEF\xF4\xE1 \xF3\xE8\xE1v\xE9\xF4\xE1\xF4\xE5 - \xCD\xEF \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3 - \xD6\xE5\xE8\xE3\xE9\xE1\xF4 \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3 \xF1\xE5\xF0\xF1\xE9\xEC\xE9q\xE8\xE5 - OUTPUT + eol = RUBY_PLATFORM =~ /mswin|mingw/ ? "\r\n" : "\n" + + expected_output = + "\xCB\xEF\xF1\xE5\xEC \xE9\xF0\xF3\xE8\xEC \xE4\xEF\xEB\xEF\xF1 \xF3\xE9\xF4#{eol}" \ + "\xC7\xE9\xF3 \xE5\xEE \xF4\xEF\xF4\xE1 \xF3\xE8\xE1v\xE9\xF4\xE1\xF4\xE5#{eol}" \ + "\xCD\xEF \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3#{eol}" \ + "\xD6\xE5\xE8\xE3\xE9\xE1\xF4 \xE8\xF1\xE2\xE1\xED\xE9\xF4\xE1\xF3 \xF1\xE5\xF0\xF1\xE9\xEC\xE9q\xE8\xE5#{eol}" assert_equal(expected_output, result.stdout) end diff --git a/tests/units/test_command_line_env_overrides.rb b/tests/units/test_command_line_env_overrides.rb new file mode 100644 index 00000000..a89da4d4 --- /dev/null +++ b/tests/units/test_command_line_env_overrides.rb @@ -0,0 +1,48 @@ + +# frozen_string_literal: true + +require 'test_helper' + +class TestCommandLineEnvOverrides < Test::Unit::TestCase + test 'it should set the expected environment variables' do + expected_command_line = nil + expected_command_line_proc = ->{ expected_command_line } + assert_command_line_eq(expected_command_line_proc, include_env: true) do |git| + expected_env = { + 'GIT_DIR' => git.lib.git_dir, + 'GIT_INDEX_FILE' => git.lib.git_index_file, + 'GIT_SSH' => nil, + 'GIT_WORK_TREE' => git.lib.git_work_dir, + 'LC_ALL' => 'en_US.UTF-8' + } + expected_command_line = [expected_env, 'checkout', {}] + + git.checkout + end + end + + test 'it should set the GIT_SSH environment variable from Git::Base.config.git_ssh' do + expected_command_line = nil + expected_command_line_proc = ->{ expected_command_line } + + saved_git_ssh = Git::Base.config.git_ssh + begin + Git::Base.config.git_ssh = 'ssh -i /path/to/key' + + assert_command_line_eq(expected_command_line_proc, include_env: true) do |git| + expected_env = { + 'GIT_DIR' => git.lib.git_dir, + 'GIT_INDEX_FILE' => git.lib.git_index_file, + 'GIT_SSH' => 'ssh -i /path/to/key', + 'GIT_WORK_TREE' => git.lib.git_work_dir, + 'LC_ALL' => 'en_US.UTF-8' + } + expected_command_line = [expected_env, 'checkout', {}] + + git.checkout + end + ensure + Git::Base.config.git_ssh = saved_git_ssh + end + end +end diff --git a/tests/units/test_command_line_error.rb b/tests/units/test_command_line_error.rb index 30b859ab..25c03765 100644 --- a/tests/units/test_command_line_error.rb +++ b/tests/units/test_command_line_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestCommandLineError < Test::Unit::TestCase diff --git a/tests/units/test_command_line_result.rb b/tests/units/test_command_line_result.rb index acec4bb6..e0cf1dd0 100644 --- a/tests/units/test_command_line_result.rb +++ b/tests/units/test_command_line_result.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestCommamndLineResult < Test::Unit::TestCase diff --git a/tests/units/test_commit_with_empty_message.rb b/tests/units/test_commit_with_empty_message.rb index 4bf04991..f896333b 100755 --- a/tests/units/test_commit_with_empty_message.rb +++ b/tests/units/test_commit_with_empty_message.rb @@ -1,4 +1,5 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true + require 'test_helper' class TestCommitWithEmptyMessage < Test::Unit::TestCase diff --git a/tests/units/test_commit_with_gpg.rb b/tests/units/test_commit_with_gpg.rb index b8a3e1ec..4bcdae70 100644 --- a/tests/units/test_commit_with_gpg.rb +++ b/tests/units/test_commit_with_gpg.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_config.rb b/tests/units/test_config.rb index b60e6c83..a72bc2e4 100644 --- a/tests/units/test_config.rb +++ b/tests/units/test_config.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_config_module.rb b/tests/units/test_config_module.rb index 060e41f6..04a1bbbb 100644 --- a/tests/units/test_config_module.rb +++ b/tests/units/test_config_module.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_describe.rb b/tests/units/test_describe.rb index 967fc753..c103c0ef 100644 --- a/tests/units/test_describe.rb +++ b/tests/units/test_describe.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_diff.rb b/tests/units/test_diff.rb index 89a476a9..3e859da5 100644 --- a/tests/units/test_diff.rb +++ b/tests/units/test_diff.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_diff_non_default_encoding.rb b/tests/units/test_diff_non_default_encoding.rb index 8bb0efa7..b9ee5231 100644 --- a/tests/units/test_diff_non_default_encoding.rb +++ b/tests/units/test_diff_non_default_encoding.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_diff_with_escaped_path.rb b/tests/units/test_diff_with_escaped_path.rb index ce0278cb..7e875be0 100644 --- a/tests/units/test_diff_with_escaped_path.rb +++ b/tests/units/test_diff_with_escaped_path.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true # encoding: utf-8 require 'test_helper' diff --git a/tests/units/test_each_conflict.rb b/tests/units/test_each_conflict.rb index f311c1ff..0854b616 100644 --- a/tests/units/test_each_conflict.rb +++ b/tests/units/test_each_conflict.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_escaped_path.rb b/tests/units/test_escaped_path.rb index ada6eafa..591429b9 100755 --- a/tests/units/test_escaped_path.rb +++ b/tests/units/test_escaped_path.rb @@ -1,4 +1,3 @@ -#!/usr/bin/env ruby # frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_failed_error.rb b/tests/units/test_failed_error.rb index 63b894f7..16a7c855 100644 --- a/tests/units/test_failed_error.rb +++ b/tests/units/test_failed_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestFailedError < Test::Unit::TestCase diff --git a/tests/units/test_git_alt_uri.rb b/tests/units/test_git_alt_uri.rb index b01ea1bb..0434223a 100644 --- a/tests/units/test_git_alt_uri.rb +++ b/tests/units/test_git_alt_uri.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test/unit' # Tests for the Git::GitAltURI class diff --git a/tests/units/test_git_base_root_of_worktree.rb b/tests/units/test_git_base_root_of_worktree.rb new file mode 100644 index 00000000..8b58af55 --- /dev/null +++ b/tests/units/test_git_base_root_of_worktree.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'test_helper' + +class TestGitBaseRootOfWorktree < Test::Unit::TestCase + def mocked_git_script(toplevel) = <<~GIT_SCRIPT + #!/bin/sh + # Loop through the arguments and check for the "rev-parse --show-toplevel" args + for arg in "$@"; do + if [ "$arg" = "version" ]; then + echo "git version 1.2.3" + exit 0 + elif [ "$arg" = "rev-parse" ]; then + REV_PARSE_ARG=true + elif [ "$REV_PARSE_ARG" = "true" ] && [ $arg = "--show-toplevel" ]; then + echo #{toplevel} + exit 0 + fi + done + exit 1 + GIT_SCRIPT + + def test_root_of_worktree + omit('Only implemented for non-windows platforms') if windows_platform? + + in_temp_dir do |toplevel| + `git init` + + mock_git_binary(mocked_git_script(toplevel)) do + working_dir = File.join(toplevel, 'config') + Dir.mkdir(working_dir) + + assert_equal(toplevel, Git::Base.root_of_worktree(working_dir)) + end + end + end + + def test_working_dir_has_spaces + omit('Only implemented for non-windows platforms') if windows_platform? + + in_temp_dir do |toplevel| + `git init` + + mock_git_binary(mocked_git_script(toplevel)) do + working_dir = File.join(toplevel, 'app config') + Dir.mkdir(working_dir) + + assert_equal(toplevel, Git::Base.root_of_worktree(working_dir)) + end + end + end + + def test_working_dir_does_not_exist + assert_raise ArgumentError do + Git::Base.root_of_worktree('/path/to/nonexistent/work_dir') + end + end + + def mocked_git_script2 = <<~GIT_SCRIPT + #!/bin/sh + # Loop through the arguments and check for the "rev-parse --show-toplevel" args + for arg in "$@"; do + if [ "$arg" = "version" ]; then + echo "git version 1.2.3" + exit 0 + elif [ "$arg" = "rev-parse" ]; then + REV_PARSE_ARG=true + elif [ "$REV_PARSE_ARG" = "true" ] && [ $arg = "--show-toplevel" ]; then + echo fatal: not a git repository 1>&2 + exit 128 + fi + done + exit 1 + GIT_SCRIPT + + def test_working_dir_not_in_work_tree + omit('Only implemented for non-windows platforms') if windows_platform? + + in_temp_dir do |temp_dir| + toplevel = File.join(temp_dir, 'my_repo') + Dir.mkdir(toplevel) do + `git init` + end + + mock_git_binary(mocked_git_script2) do + assert_raise ArgumentError do + Git::Base.root_of_worktree(temp_dir) + end + end + end + end +end diff --git a/tests/units/test_git_binary_version.rb b/tests/units/test_git_binary_version.rb new file mode 100644 index 00000000..74c7436e --- /dev/null +++ b/tests/units/test_git_binary_version.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'test_helper' + +class TestGitBinaryVersion < Test::Unit::TestCase + def mocked_git_script_windows = <<~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 mocked_git_script_linux = <<~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 mocked_git_script + if windows_platform? + mocked_git_script_windows + else + mocked_git_script_linux + end + end + + def test_binary_version + in_temp_dir do |path| + mock_git_binary(mocked_git_script) do |git_binary_path| + assert_equal([1, 2, 3], Git.binary_version(git_binary_path)) + end + end + end + + def test_binary_version_with_spaces + in_temp_dir do |path| + subdir = 'Git Bin Directory' + mock_git_binary(mocked_git_script, subdir: subdir) do |git_binary_path| + assert_equal([1, 2, 3], Git.binary_version(git_binary_path)) + end + 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_git_default_branch.rb b/tests/units/test_git_default_branch.rb index 3b1f64fd..bb829cec 100644 --- a/tests/units/test_git_default_branch.rb +++ b/tests/units/test_git_default_branch.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require File.dirname(__FILE__) + '/../test_helper' diff --git a/tests/units/test_git_dir.rb b/tests/units/test_git_dir.rb index b33827cf..61538261 100644 --- a/tests/units/test_git_dir.rb +++ b/tests/units/test_git_dir.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_git_path.rb b/tests/units/test_git_path.rb index 9944209e..446a3dad 100644 --- a/tests/units/test_git_path.rb +++ b/tests/units/test_git_path.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_ignored_files_with_escaped_path.rb b/tests/units/test_ignored_files_with_escaped_path.rb index 0d40711d..ad609960 100644 --- a/tests/units/test_ignored_files_with_escaped_path.rb +++ b/tests/units/test_ignored_files_with_escaped_path.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true # encoding: utf-8 require 'test_helper' diff --git a/tests/units/test_index_ops.rb b/tests/units/test_index_ops.rb index 6bee051b..c726e4e5 100644 --- a/tests/units/test_index_ops.rb +++ b/tests/units/test_index_ops.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_init.rb b/tests/units/test_init.rb index 99a87593..30a9e894 100644 --- a/tests/units/test_init.rb +++ b/tests/units/test_init.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' require 'stringio' diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 38694980..af613d1f 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' require "fileutils" @@ -24,14 +24,20 @@ def test_fetch_unshallow end end - def test_commit_data - data = @lib.commit_data('1cc8667014381') + def test_cat_file_commit + data = @lib.cat_file_commit('1cc8667014381') assert_equal('scott Chacon 1194561188 -0800', data['author']) assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', data['tree']) assert_equal("test\n", data['message']) assert_equal(["546bec6f8872efa41d5d97a369f669165ecda0de"], data['parent']) end + def test_cat_file_commit_with_bad_object + assert_raise(ArgumentError) do + @lib.cat_file_commit('--all') + end + end + def test_commit_with_date create_file("#{@wdir}/test_file_1", 'content tets_file_1') @lib.add('test_file_1') @@ -40,7 +46,7 @@ def test_commit_with_date @lib.commit('commit with date', date: author_date.strftime('%Y-%m-%dT%H:%M:%S %z')) - data = @lib.commit_data('HEAD') + data = @lib.cat_file_commit('HEAD') assert_equal("Scott Chacon #{author_date.strftime("%s %z")}", data['author']) end @@ -77,7 +83,7 @@ def test_commit_with_no_verify move_file(pre_commit_path_bak, pre_commit_path) # Verify the commit was created - data = @lib.commit_data('HEAD') + data = @lib.cat_file_commit('HEAD') assert_equal("commit with no verify and pre-commit file\n", data['message']) end @@ -193,7 +199,7 @@ def test_rev_parse_with_bad_revision end def test_rev_parse_with_unknown_revision - assert_raise(Git::FailedError) do + assert_raise_with_message(Git::FailedError, /exit 128, stderr: "fatal: bad revision 'NOTFOUND'"/) do @lib.rev_parse('NOTFOUND') end end @@ -208,64 +214,81 @@ def test_name_rev_with_invalid_commit_ish end end - def test_object_type - assert_equal('commit', @lib.object_type('1cc8667014381')) # commit - assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree - assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob - assert_equal('commit', @lib.object_type('v2.5')) + def test_cat_file_type + assert_equal('commit', @lib.cat_file_type('1cc8667014381')) # commit + assert_equal('tree', @lib.cat_file_type('1cc8667014381^{tree}')) #tree + assert_equal('blob', @lib.cat_file_type('v2.5:example.txt')) #blob + assert_equal('commit', @lib.cat_file_type('v2.5')) + end + + def test_cat_file_type_with_bad_object + assert_raise(ArgumentError) do + @lib.cat_file_type('--batch') + end + end + + def test_cat_file_size + assert_equal(265, @lib.cat_file_size('1cc8667014381')) # commit + assert_equal(72, @lib.cat_file_size('1cc8667014381^{tree}')) #tree + assert_equal(128, @lib.cat_file_size('v2.5:example.txt')) #blob + assert_equal(265, @lib.cat_file_size('v2.5')) end - def test_object_size - assert_equal(265, @lib.object_size('1cc8667014381')) # commit - assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree - assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob - assert_equal(265, @lib.object_size('v2.5')) + def test_cat_file_size_with_bad_object + assert_raise(ArgumentError) do + @lib.cat_file_size('--batch') + end end - def test_object_contents - commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n" + def test_cat_file_contents + commit = +"tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n" commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n" commit << "author scott Chacon 1194561188 -0800\n" commit << "committer scott Chacon 1194561188 -0800\n" commit << "\ntest" - assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit + assert_equal(commit, @lib.cat_file_contents('1cc8667014381')) # commit - tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n" + tree = +"040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n" tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt" - assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree + assert_equal(tree, @lib.cat_file_contents('1cc8667014381^{tree}')) #tree blob = "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2" - assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob - + assert_equal(blob, @lib.cat_file_contents('v2.5:example.txt')) #blob end - def test_object_contents_with_block - commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n" + def test_cat_file_contents_with_block + commit = +"tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n" commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n" commit << "author scott Chacon 1194561188 -0800\n" commit << "committer scott Chacon 1194561188 -0800\n" commit << "\ntest" - @lib.object_contents('1cc8667014381') do |f| + @lib.cat_file_contents('1cc8667014381') do |f| assert_equal(commit, f.read.chomp) end # commit - tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n" + tree = +"040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n" tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt" - @lib.object_contents('1cc8667014381^{tree}') do |f| + @lib.cat_file_contents('1cc8667014381^{tree}') do |f| assert_equal(tree, f.read.chomp) #tree end blob = "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2" - @lib.object_contents('v2.5:example.txt') do |f| + @lib.cat_file_contents('v2.5:example.txt') do |f| assert_equal(blob, f.read.chomp) #blob end end + def test_cat_file_contents_with_bad_object + assert_raise(ArgumentError) do + @lib.cat_file_contents('--all') + end + end + # returns Git::Branch object array def test_branches_all branches = @lib.branches_all @@ -276,6 +299,39 @@ def test_branches_all assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch end + test 'Git::Lib#branches_all with unexpected output from git branches -a' do + # Mock command lines to return unexpected branch data + def @lib.command_lines(*_command) + <<~COMMAND_LINES.split("\n") + * (HEAD detached at origin/master) + this line should result in a Git::UnexpectedResultError + master + remotes/origin/HEAD -> origin/master + remotes/origin/master + COMMAND_LINES + end + + begin + branches = @lib.branches_all + rescue Git::UnexpectedResultError => e + assert_equal(<<~MESSAGE, e.message) + Unexpected line in output from `git branch -a`, line 2 + + Full output: + * (HEAD detached at origin/master) + this line should result in a Git::UnexpectedResultError + master + remotes/origin/HEAD -> origin/master + remotes/origin/master + + Line 2: + " this line should result in a Git::UnexpectedResultError" + MESSAGE + else + raise RuntimeError, 'Expected Git::UnexpectedResultError' + end + end + def test_config_remote config = @lib.config_remote('working') assert_equal('../working.git', config['url']) @@ -395,4 +451,30 @@ def test_empty_when_empty assert_true(git.lib.empty?) end end + + def test_cat_file_tag + expected_cat_file_tag_keys = %w[name object type tag tagger message].sort + + in_temp_repo('working') do + # Creeate an annotated tag: + `git tag -a annotated_tag -m "Creating an annotated tag"` + + git = Git.open('.') + cat_file_tag = git.lib.cat_file_tag('annotated_tag') + + assert_equal(expected_cat_file_tag_keys, cat_file_tag.keys.sort) + assert_equal('annotated_tag', cat_file_tag['name']) + assert_equal('46abbf07e3c564c723c7c039a43ab3a39e5d02dd', cat_file_tag['object']) + assert_equal('commit', cat_file_tag['type']) + assert_equal('annotated_tag', cat_file_tag['tag']) + assert_match(/^Scott Chacon \d+ [+-]\d+$/, cat_file_tag['tagger']) + assert_equal("Creating an annotated tag\n", cat_file_tag['message']) + end + end + + def test_cat_file_tag_with_bad_object + assert_raise(ArgumentError) do + @lib.cat_file_tag('--all') + end + end end diff --git a/tests/units/test_lib_meets_required_version.rb b/tests/units/test_lib_meets_required_version.rb index 25c410bf..11521d92 100644 --- a/tests/units/test_lib_meets_required_version.rb +++ b/tests/units/test_lib_meets_required_version.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_lib_repository_default_branch.rb b/tests/units/test_lib_repository_default_branch.rb index 0e012895..4240865f 100644 --- a/tests/units/test_lib_repository_default_branch.rb +++ b/tests/units/test_lib_repository_default_branch.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require File.dirname(__FILE__) + '/../test_helper' diff --git a/tests/units/test_log.rb b/tests/units/test_log.rb index d220af03..f18fabf2 100644 --- a/tests/units/test_log.rb +++ b/tests/units/test_log.rb @@ -1,4 +1,5 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true + require 'logger' require 'test_helper' @@ -127,4 +128,9 @@ def test_log_cherry l = @git.log.between( 'master', 'cherry').cherry assert_equal( 1, l.size ) end + + def test_log_merges + expected_command_line = ['log', '--max-count=30', '--no-color', '--pretty=raw', '--merges', {:chdir=>nil}] + assert_command_line_eq(expected_command_line) { |git| git.log.merges.size } + end end diff --git a/tests/units/test_logger.rb b/tests/units/test_logger.rb index 470a2ed8..deadfe34 100644 --- a/tests/units/test_logger.rb +++ b/tests/units/test_logger.rb @@ -1,4 +1,5 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true + require 'logger' require 'test_helper' @@ -17,39 +18,40 @@ def unexpected_log_entry end def test_logger - log = Tempfile.new('logfile') - log.close + in_temp_dir do |path| + log_path = 'logfile.log' - logger = Logger.new(log.path) - logger.level = Logger::DEBUG + logger = Logger.new(log_path, level: Logger::DEBUG) - @git = Git.open(@wdir, :log => logger) - @git.branches.size + @git = Git.open(@wdir, :log => logger) + @git.branches.size - logc = File.read(log.path) + logc = File.read(log_path) - expected_log_entry = /INFO -- : \["git", "(?.*?)", "branch", "-a"/ - assert_match(expected_log_entry, logc, missing_log_entry) + expected_log_entry = /INFO -- : \[\{[^}]+}, "git", "(?.*?)", "branch", "-a"/ + assert_match(expected_log_entry, logc, missing_log_entry) - expected_log_entry = /DEBUG -- : stdout:\n" cherry/ - assert_match(expected_log_entry, logc, missing_log_entry) + expected_log_entry = /DEBUG -- : stdout:\n" cherry/ + assert_match(expected_log_entry, logc, missing_log_entry) + end end def test_logging_at_info_level_should_not_show_debug_messages - log = Tempfile.new('logfile') - log.close - logger = Logger.new(log.path) - logger.level = Logger::INFO + in_temp_dir do |path| + log_path = 'logfile.log' + + logger = Logger.new(log_path, level: Logger::INFO) - @git = Git.open(@wdir, :log => logger) - @git.branches.size + @git = Git.open(@wdir, :log => logger) + @git.branches.size - logc = File.read(log.path) + logc = File.read(log_path) - expected_log_entry = /INFO -- : \["git", "(?.*?)", "branch", "-a"/ - assert_match(expected_log_entry, logc, missing_log_entry) + expected_log_entry = /INFO -- : \[\{[^}]+}, "git", "(?.*?)", "branch", "-a"/ + assert_match(expected_log_entry, logc, missing_log_entry) - expected_log_entry = /DEBUG -- : stdout:\n" cherry/ - assert_not_match(expected_log_entry, logc, unexpected_log_entry) + expected_log_entry = /DEBUG -- : stdout:\n" cherry/ + assert_not_match(expected_log_entry, logc, unexpected_log_entry) + end end end diff --git a/tests/units/test_ls_files_with_escaped_path.rb b/tests/units/test_ls_files_with_escaped_path.rb index cdc890c0..2102a8ea 100644 --- a/tests/units/test_ls_files_with_escaped_path.rb +++ b/tests/units/test_ls_files_with_escaped_path.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true # encoding: utf-8 require 'test_helper' diff --git a/tests/units/test_ls_tree.rb b/tests/units/test_ls_tree.rb index 19d487a4..afa3181a 100644 --- a/tests/units/test_ls_tree.rb +++ b/tests/units/test_ls_tree.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestLsTree < Test::Unit::TestCase diff --git a/tests/units/test_merge.rb b/tests/units/test_merge.rb index 95ae33a8..2073c6af 100644 --- a/tests/units/test_merge.rb +++ b/tests/units/test_merge.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_merge_base.rb b/tests/units/test_merge_base.rb index 4a794993..a4a615de 100755 --- a/tests/units/test_merge_base.rb +++ b/tests/units/test_merge_base.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_object.rb b/tests/units/test_object.rb index 3f31b390..9837bef7 100644 --- a/tests/units/test_object.rb +++ b/tests/units/test_object.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' @@ -62,7 +62,7 @@ def test_object_to_s assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @blob.sha) end - def test_object_size + def test_cat_file_size assert_equal(265, @commit.size) assert_equal(72, @tree.size) assert_equal(128, @blob.size) diff --git a/tests/units/test_pull.rb b/tests/units/test_pull.rb index f9a514ab..0c0147a7 100644 --- a/tests/units/test_pull.rb +++ b/tests/units/test_pull.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestPull < Test::Unit::TestCase diff --git a/tests/units/test_push.rb b/tests/units/test_push.rb index 78cc9396..cb6e2bc0 100644 --- a/tests/units/test_push.rb +++ b/tests/units/test_push.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestPush < Test::Unit::TestCase diff --git a/tests/units/test_remotes.rb b/tests/units/test_remotes.rb index 00c4c31b..602e0212 100644 --- a/tests/units/test_remotes.rb +++ b/tests/units/test_remotes.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_repack.rb b/tests/units/test_repack.rb index 4a27e8f8..7f8ef720 100644 --- a/tests/units/test_repack.rb +++ b/tests/units/test_repack.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_rm.rb b/tests/units/test_rm.rb index 658ce9ca..c80d1e50 100644 --- a/tests/units/test_rm.rb +++ b/tests/units/test_rm.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_show.rb b/tests/units/test_show.rb index 8c2e46ae..5439180c 100644 --- a/tests/units/test_show.rb +++ b/tests/units/test_show.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_signaled_error.rb b/tests/units/test_signaled_error.rb index 6bf46c2b..d489cb6f 100644 --- a/tests/units/test_signaled_error.rb +++ b/tests/units/test_signaled_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestSignaledError < Test::Unit::TestCase diff --git a/tests/units/test_signed_commits.rb b/tests/units/test_signed_commits.rb index 871b92a5..f3c783c1 100644 --- a/tests/units/test_signed_commits.rb +++ b/tests/units/test_signed_commits.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' require "fileutils" @@ -24,7 +24,7 @@ def in_repo_with_signing_config(&block) end end - def test_commit_data + def test_cat_file_commit # Signed commits should work on windows, but this test is omitted until the setup # on windows can be figured out omit('Omit testing of signed commits on Windows') if windows_platform? @@ -34,7 +34,7 @@ def test_commit_data `git add README.md` `git commit -S -m "Signed, sealed, delivered"` - data = Git.open('.').lib.commit_data('HEAD') + data = Git.open('.').lib.cat_file_commit('HEAD') assert_match(SSH_SIGNATURE_REGEXP, data['gpgsig']) assert_equal("Signed, sealed, delivered\n", data['message']) diff --git a/tests/units/test_stashes.rb b/tests/units/test_stashes.rb index e147ae9c..78312651 100644 --- a/tests/units/test_stashes.rb +++ b/tests/units/test_stashes.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' @@ -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 diff --git a/tests/units/test_status.rb b/tests/units/test_status.rb index 36543bc1..fd446e02 100644 --- a/tests/units/test_status.rb +++ b/tests/units/test_status.rb @@ -1,5 +1,5 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_status_object.rb b/tests/units/test_status_object.rb index ee343cb6..3d5d0a29 100644 --- a/tests/units/test_status_object.rb +++ b/tests/units/test_status_object.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rbconfig' require 'securerandom' require 'test_helper' diff --git a/tests/units/test_status_object_empty_repo.rb b/tests/units/test_status_object_empty_repo.rb index 4a8c366c..71435b11 100644 --- a/tests/units/test_status_object_empty_repo.rb +++ b/tests/units/test_status_object_empty_repo.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rbconfig' require 'securerandom' require 'test_helper' diff --git a/tests/units/test_submodule.rb b/tests/units/test_submodule.rb index 009127f2..bdf7ffdc 100644 --- a/tests/units/test_submodule.rb +++ b/tests/units/test_submodule.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_tags.rb b/tests/units/test_tags.rb index 242af137..df62a8f2 100644 --- a/tests/units/test_tags.rb +++ b/tests/units/test_tags.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_thread_safety.rb b/tests/units/test_thread_safety.rb index 48b93ae7..a4a59259 100644 --- a/tests/units/test_thread_safety.rb +++ b/tests/units/test_thread_safety.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_timeout_error.rb b/tests/units/test_timeout_error.rb index 3bfc90b6..e3e4999a 100644 --- a/tests/units/test_timeout_error.rb +++ b/tests/units/test_timeout_error.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_helper' class TestTimeoutError < Test::Unit::TestCase diff --git a/tests/units/test_tree_ops.rb b/tests/units/test_tree_ops.rb index 82e65b49..2d8219fe 100644 --- a/tests/units/test_tree_ops.rb +++ b/tests/units/test_tree_ops.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true require 'test_helper' diff --git a/tests/units/test_windows_cmd_escaping.rb b/tests/units/test_windows_cmd_escaping.rb index d8b3ee54..9998fd89 100644 --- a/tests/units/test_windows_cmd_escaping.rb +++ b/tests/units/test_windows_cmd_escaping.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true # encoding: utf-8 require 'test_helper' diff --git a/tests/units/test_worktree.rb b/tests/units/test_worktree.rb index bbe377ce..910561ec 100644 --- a/tests/units/test_worktree.rb +++ b/tests/units/test_worktree.rb @@ -1,4 +1,4 @@ -#!/usr/bin/env ruby +# frozen_string_literal: true # require 'fileutils' # require 'pathname'