Skip to content

Commit e507dbb

Browse files
committed
Test old git versions, and analyse the tests and the libgit2 driver
CI builds git 1.8.4, the oldest supported version, and 2.25.1, and runs the suite with them: a test of this branch passed --bare with --origin, which git refuses before 2.38.2. The workflow only reads the repository, checks the tarballs it builds, keys the libgit2 cache on its build flags, uses actions/cache 6, checks that the archive Composer installs only ships the code, the docs and the license (export-ignore), and Dependabot updates the actions and the tools. PHPStan analyses the tests, with phpstan-phpunit, and the libgit2 driver, ignoring only the calls to FFI it can't see: it found real type issues fixed here. PHP-CS-Fixer applies php_unit_strict. PHPUnit fails on notices and on its own deprecations. psr/log, in the API, is required. Blob::getMimetype() returns the mimetype alone, or throws, instead of false. The fixture's refs are pinned: they must never move.
1 parent 04cbe10 commit e507dbb

36 files changed

Lines changed: 517 additions & 219 deletions

‎.gitattributes‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
* text=auto
22
/tests/fixtures/*.bundle binary
3+
4+
# Left out of the archive Composer installs: only src/, the docs and the license ship.
5+
/.gitattributes export-ignore
6+
/.github export-ignore
7+
/.gitignore export-ignore
8+
/.php-cs-fixer.php export-ignore
9+
/benchmark.php export-ignore
10+
/castor.php export-ignore
11+
/phpstan-baseline.neon export-ignore
12+
/phpstan.neon export-ignore
13+
/phpunit.xml.dist export-ignore
14+
/test-git-versions.php export-ignore
15+
/tests export-ignore
16+
/tools export-ignore

‎.github/CONTRIBUTING.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We accept contributions via pull requests on GitHub. Please review these guideli
66

77
## Guidelines
88

9-
* Please follow the [PSR-12 Coding Style Guide](https://www.php-fig.org/psr/psr-12/).
9+
* Follow the coding standards of PHP-CS-Fixer, configured in `.php-cs-fixer.php` (the Symfony and PhpCsFixer rule sets): `castor cs` applies them.
1010
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant.
1111
* Send a coherent commit history, making sure each commit in your pull request is meaningful.
1212
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
@@ -62,10 +62,13 @@ locally, ready to receive new commits. `build` rebuilds `tests/fixtures/foobar.b
6262
from that directory, restricted to the refs listed in `tests/fixtures/bundle-refs.txt` —
6363
add your new branch or tag there first if you introduced one.
6464

65-
Then update the commit SHA constants in `AbstractTestCase` to match, and run
66-
`tests/fixtures/verify-bundle.sh`. It checks the bundle's integrity, its ref list against
67-
`bundle-refs.txt`, and its size, since GitHub renders any change to this binary file as an
68-
opaque diff. If your change intentionally adds a ref or grows the file, update
65+
Existing refs must not move, since the tests rely on the commits they point to: add your
66+
commits on new branches or tags, and constants in `AbstractTestCase` for the ones your
67+
tests use. Then add the new refs to `tests/fixtures/bundle-heads.txt` (the output of
68+
`git bundle list-heads tests/fixtures/foobar.bundle`), and run
69+
`tests/fixtures/verify-bundle.sh`. It checks the bundle's integrity, its refs against
70+
`bundle-refs.txt` and `bundle-heads.txt`, and its size, since GitHub renders any change to
71+
this binary file as an opaque diff. If your change intentionally adds a ref or grows the file, update
6972
`tests/fixtures/bundle-refs.txt` or `MAX_SIZE_KB` in that script as part of the same pull
7073
request, so the reason for the change is explicit and reviewable rather than a silent
7174
binary diff.

‎.github/dependabot.yml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: github-actions
4+
directory: /
5+
schedule:
6+
interval: monthly
7+
8+
- package-ecosystem: composer
9+
directories:
10+
- /tools/php-cs-fixer
11+
- /tools/phpstan
12+
schedule:
13+
interval: monthly

‎.github/workflows/tests.yml‎

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches: [ main ]
66
pull_request:
77

8+
permissions:
9+
contents: read
10+
811
jobs:
912
verify-fixture:
1013
name: Verify Test Fixture Bundle
@@ -34,6 +37,16 @@ jobs:
3437
- name: Run CS check
3538
run: castor cs --dry-run
3639

40+
check-archive:
41+
name: Check the Archive
42+
runs-on: ubuntu-24.04
43+
steps:
44+
- name: Checkout Code
45+
uses: actions/checkout@v7
46+
47+
- name: Ship only the code, the docs and the license
48+
run: diff <(git archive HEAD | tar -t | cut -d/ -f1 | sort -u) <(printf '%s\n' CHANGELOG.md LICENSE README.md UPGRADE-2.0.md composer.json doc src | sort)
49+
3750
phpstan:
3851
name: Static Analysis
3952
runs-on: ubuntu-24.04
@@ -91,6 +104,52 @@ jobs:
91104
- name: Execute PHPUnit
92105
run: vendor/bin/phpunit
93106

107+
tests-git:
108+
name: Test git ${{ matrix.git }}
109+
runs-on: ubuntu-24.04
110+
strategy:
111+
fail-fast: false
112+
matrix:
113+
# The oldest supported git, and the one of Ubuntu 20.04: the other jobs test the latest one.
114+
include:
115+
- git: '1.8.4'
116+
sha256: '5e0efe7cd30e258bc3be9ae023790743974e76eb1b24ecac24de6fc2af2b2786'
117+
- git: '2.25.1'
118+
sha256: 'aaef0cd609a7730cfbae530d7c91214a941d7e26054d2314cc99794880156b33'
119+
120+
steps:
121+
- name: Checkout Code
122+
uses: actions/checkout@v7
123+
124+
- name: Cache git
125+
id: cache-git
126+
uses: actions/cache@v6
127+
with:
128+
path: ~/git
129+
key: git-${{ matrix.git }}-ubuntu-24.04
130+
131+
- name: Build git
132+
if: steps.cache-git.outputs.cache-hit != 'true'
133+
run: |
134+
curl -fsSL "https://github.com/git/git/archive/refs/tags/v${{ matrix.git }}.tar.gz" -o git.tar.gz
135+
echo "${{ matrix.sha256 }} git.tar.gz" | sha256sum --check
136+
tar xzf git.tar.gz
137+
make -C "git-${{ matrix.git }}" prefix="$HOME/git" CFLAGS='-std=gnu17 -O2' NO_OPENSSL=1 NO_CURL=1 NO_EXPAT=1 NO_GETTEXT=1 NO_PERL=1 NO_PYTHON=1 NO_TCLTK=1 --jobs="$(nproc)" install
138+
139+
- name: Setup PHP
140+
uses: shivammathur/setup-php@v2
141+
with:
142+
php-version: '8.4'
143+
144+
- name: Setup Problem Matchers
145+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
146+
147+
- name: Install Composer dependencies
148+
run: composer update --prefer-dist --no-interaction
149+
150+
- name: Execute PHPUnit
151+
run: GIT_COMMAND="$HOME/git/bin/git" vendor/bin/phpunit
152+
94153
tests-ffi:
95154
name: Test PHP ${{ matrix.php }} (LibGit2Driver)
96155
runs-on: ubuntu-24.04
@@ -101,23 +160,27 @@ jobs:
101160
env:
102161
# LibGit2Driver only supports libgit2 1.9, which Ubuntu 24.04 doesn't ship.
103162
LIBGIT2_VERSION: '1.9.1'
163+
LIBGIT2_SHA256: '14cab3014b2b7ad75970ff4548e83615f74d719afe00aa479b4a889c1e13fc00'
164+
LIBGIT2_CMAKE_FLAGS: '-DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=OFF -DBUILD_CLI=OFF -DUSE_HTTPS=OFF -DUSE_SSH=OFF'
104165

105166
steps:
106167
- name: Checkout Code
107168
uses: actions/checkout@v7
108169

109170
- name: Cache libgit2
110171
id: cache-libgit2
111-
uses: actions/cache@v4
172+
uses: actions/cache@v6
112173
with:
113174
path: ~/libgit2
114-
key: libgit2-${{ env.LIBGIT2_VERSION }}-ubuntu-24.04
175+
key: libgit2-${{ env.LIBGIT2_VERSION }}-${{ env.LIBGIT2_CMAKE_FLAGS }}-ubuntu-24.04
115176

116177
- name: Build libgit2
117178
if: steps.cache-libgit2.outputs.cache-hit != 'true'
118179
run: |
119-
curl -fsSL "https://github.com/libgit2/libgit2/archive/refs/tags/v${LIBGIT2_VERSION}.tar.gz" | tar xz
120-
cmake -S "libgit2-${LIBGIT2_VERSION}" -B libgit2-build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$HOME/libgit2" -DCMAKE_INSTALL_LIBDIR=lib -DBUILD_TESTS=OFF -DBUILD_CLI=OFF -DUSE_HTTPS=OFF -DUSE_SSH=OFF
180+
curl -fsSL "https://github.com/libgit2/libgit2/archive/refs/tags/v${LIBGIT2_VERSION}.tar.gz" -o libgit2.tar.gz
181+
echo "${LIBGIT2_SHA256} libgit2.tar.gz" | sha256sum --check
182+
tar xzf libgit2.tar.gz
183+
cmake -S "libgit2-${LIBGIT2_VERSION}" -B libgit2-build -DCMAKE_INSTALL_PREFIX="$HOME/libgit2" -DCMAKE_INSTALL_LIBDIR=lib $LIBGIT2_CMAKE_FLAGS
121184
cmake --build libgit2-build --parallel
122185
cmake --install libgit2-build
123186

‎.php-cs-fixer.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'@Symfony' => true,
4040
'@Symfony:risky' => true,
4141
'php_unit_internal_class' => false, // From @PhpCsFixer but we don't want it
42+
'php_unit_strict' => true, // assertSame() instead of assertEquals()
4243
'php_unit_test_class_requires_covers' => false, // From @PhpCsFixer but we don't want it
4344
'phpdoc_add_missing_param_annotation' => false, // From @PhpCsFixer but we don't want it
4445
'header_comment' => ['header' => $fileHeaderComment],

‎composer.json‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"require": {
3838
"php": "^8.4",
3939
"ext-pcre": "*",
40+
"psr/log": "^1.1 || ^2.0 || ^3.0",
4041
"symfony/deprecation-contracts": "^2.5 || ^3",
4142
"symfony/process": "^6.4 || ^7.4 || ^8.1",
4243
"symfony/string": "^7.4.6 || ^8.0.6"
@@ -45,15 +46,14 @@
4546
"ext-fileinfo": "*",
4647
"ext-zlib": "*",
4748
"phpunit/phpunit": "^12.0",
48-
"psr/log": "^3.0",
4949
"symfony/console": "^8.1"
5050
},
5151
"suggest": {
52-
"ext-ffi": "To use the experimental LibGit2Driver (in-process libgit2) instead of the default CliDriver — also requires libgit2 1.9 to be installed on the system."
52+
"ext-ffi": "To use the experimental LibGit2Driver (in-process libgit2) instead of the default CliDriver — also requires libgit2 1.9 to be installed on the system.",
53+
"ext-fileinfo": "To read the mimetype of a blob with Blob::getMimetype()."
5354
},
5455
"config": {
5556
"preferred-install": "dist",
5657
"sort-packages": true
57-
},
58-
"prefer-stable": true
58+
}
5959
}

‎phpstan.neon‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
includes:
22
- phpstan-baseline.neon
3+
- tools/phpstan/vendor/phpstan/phpstan-phpunit/extension.neon
4+
- tools/phpstan/vendor/phpstan/phpstan-phpunit/rules.neon
35

46
parameters:
57
level: 9
68
paths:
79
- src
10+
- tests/Gitonomy
11+
- tests/bootstrap.php
812
tmpDir: 'var/phpstan/tmp'
913
inferPrivatePropertyTypeFromConstructor: true
10-
excludePaths:
11-
analyse:
12-
# PHP FFI is dynamic by design: `$ffi->some_c_function()` and
13-
# `$cdata->some_field` are resolved at runtime against the cdef
14-
# loaded into that specific FFI/CData instance, not against any
15-
# PHP class PHPStan can see. There is no stub format for this, so
16-
# every call/property access here reads as "undefined" to static
17-
# analysis even though it's correct — verified by actually
18-
# exercising this driver against a live libgit2 and the full test
19-
# suite (`GITLIB_DRIVER=ffi vendor/bin/phpunit`), not by type-checking it.
20-
- src/Gitonomy/Git/Driver/Ffi/*
14+
ignoreErrors:
15+
# PHP FFI is dynamic by design: `$ffi->some_c_function()` and
16+
# `$cdata->some_field` are resolved at runtime against the cdef loaded
17+
# into that FFI instance, which PHPStan can't see. The tests run with
18+
# GITLIB_DRIVER=ffi check them instead.
19+
-
20+
identifier: method.notFound
21+
path: src/Gitonomy/Git/Driver/Ffi/*
22+
-
23+
identifier: property.notFound
24+
path: src/Gitonomy/Git/Driver/Ffi/*

‎phpunit.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
colors="true"
66
displayDetailsOnTestsThatTriggerDeprecations="true"
77
failOnDeprecation="true"
8+
failOnNotice="true"
9+
failOnPhpunitDeprecation="true"
810
failOnRisky="true"
911
failOnWarning="true"
1012
beStrictAboutOutputDuringTests="true"

‎src/Gitonomy/Git/Blob.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Gitonomy\Git;
1414

15+
use Gitonomy\Git\Exception\RuntimeException;
16+
1517
/**
1618
* Representation of a Blob commit.
1719
*
@@ -26,7 +28,7 @@ final class Blob
2628

2729
private ?string $content = null;
2830

29-
private string|false|null $mimetype = null;
31+
private ?string $mimetype = null;
3032

3133
private ?bool $text = null;
3234

@@ -60,16 +62,13 @@ public function getContent(): string
6062
}
6163

6264
/**
63-
* Determine the mimetype of the blob.
65+
* @return string The mimetype of the content, like "text/plain"
66+
*
67+
* @throws RuntimeException fileinfo can't read the content
6468
*/
65-
public function getMimetype(): string|false
69+
public function getMimetype(): string
6670
{
67-
if (null === $this->mimetype) {
68-
$finfo = new \finfo(\FILEINFO_MIME);
69-
$this->mimetype = $finfo->buffer($this->getContent());
70-
}
71-
72-
return $this->mimetype;
71+
return $this->mimetype ??= new \finfo(\FILEINFO_MIME_TYPE)->buffer($this->getContent()) ?: throw new RuntimeException(\sprintf('Unable to read the mimetype of the blob "%s".', $this->hash));
7372
}
7473

7574
/**

‎src/Gitonomy/Git/Diff/Diff.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* Representation of a diff.
2121
*
2222
* @author Alexandre Salomé <alexandre.salome@gmail.com>
23+
*
24+
* @phpstan-import-type FileArray from File
2325
*/
2426
final readonly class Diff
2527
{
@@ -93,7 +95,7 @@ public function getRawDiff(): string
9395
/**
9496
* Export a diff as array.
9597
*
96-
* @return array{rawDiff: string, files: list<array<string, mixed>>}
98+
* @return array{rawDiff: string, files: list<FileArray>}
9799
*/
98100
public function toArray(): array
99101
{
@@ -106,7 +108,7 @@ public function toArray(): array
106108
/**
107109
* Create a new instance of Diff from an array.
108110
*
109-
* @param array{rawDiff: string, files: list<array{old_name: string|null, new_name: string|null, old_mode: string|null, new_mode: string|null, old_index: string|null, new_index: string|null, is_binary: bool, is_copy?: bool, changes: list<array{range_old_start: int, range_old_count: int, range_new_start: int, range_new_count: int, lines: list<array{int, string}>}>}>} $array
111+
* @param array{rawDiff: string, files: list<FileArray>} $array
110112
*/
111113
public static function fromArray(array $array, ?Repository $repository = null): self
112114
{

0 commit comments

Comments
 (0)