Skip to content

Commit 1ee409e

Browse files
committed
Parse the diffs of git before 2.16, and set up the tests with git 1.7.1
Before 2.16, `git diff --raw` writes "..." after abbreviated hashes, which the diff parser rejected: WorkingCopy::getDiffPending() and getDiffStaged() failed with git 1.8 and older. --no-abbrev writes full hashes on every version. `git config --local` (git 1.7.3) and `git commit --allow-empty-message` (git 1.7.2) made three tests fail to set up with git 1.7.1.
1 parent 8642621 commit 1ee409e

4 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎src/Gitonomy/Git/Driver/Cli/CliDriver.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
*/
3434
final readonly class CliDriver implements DriverInterface
3535
{
36-
private const array DIFF_ARGS = ['-r', '-p', '--raw', '-m', '-M', '--full-index'];
36+
// --no-abbrev: before git 2.16, --raw abbreviates hashes with a trailing "...".
37+
private const array DIFF_ARGS = ['-r', '-p', '--raw', '-m', '-M', '--full-index', '--no-abbrev'];
3738

3839
public function __construct(
3940
private GitProcessFactory $processFactory,

‎tests/Gitonomy/Git/Tests/LogTest.php‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ public function testIterable(Repository $repository): void
7676

7777
public function testFirstMessageEmpty(): void
7878
{
79-
$repository = self::createEmptyRepository(false);
80-
self::git($repository, 'config', ['--local', 'user.name', '"Unit Test"']);
81-
self::git($repository, 'config', ['--local', 'user.email', '"unit_test@unit-test.com"']);
82-
83-
// Edge case: first commit lacks a message.
84-
file_put_contents($repository->getWorkingDir().'/file', 'foo');
85-
self::git($repository, 'add', ['.']);
86-
self::git($repository, 'commit', ['--allow-empty-message', '--no-edit']);
79+
$repository = self::createEmptyRepository();
80+
$tree = self::writeObject($repository, 'tree', '');
81+
// Edge case: first commit lacks a message. Written by hand: git only
82+
// has --allow-empty-message since 1.7.2.
83+
$commit = self::writeObject($repository, 'commit', "tree {$tree}\nauthor Unit Test <unit_test@unit-test.com> 1000000000 +0000\ncommitter Unit Test <unit_test@unit-test.com> 1000000000 +0000\n\n");
84+
self::git($repository, 'update-ref', ['refs/heads/master', $commit]);
8785

8886
$commits = $repository->getLog()->getCommits();
8987
$this->assertCount(1, $commits);
88+
$this->assertSame('', $commits[0]->getMessage());
9089
}
9190

9291
public function testParsesCommitsWithAndWithoutGitButlerHeaders(): void

‎tests/Gitonomy/Git/Tests/ReferenceTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public function testIsBranchMergedToMaster(): void
201201
{
202202
$repository = self::createFoobarRepository(false);
203203

204-
self::git($repository, 'config', ['--local', 'user.name', '"Unit Test"']);
205-
self::git($repository, 'config', ['--local', 'user.email', '"unit_test@unit-test.com"']);
204+
self::git($repository, 'config', ['user.name', 'Unit Test']);
205+
self::git($repository, 'config', ['user.email', 'unit_test@unit-test.com']);
206206

207207
$master = $repository->getReferences()->getBranch('master');
208208
$references = $repository->getReferences();

‎tests/Gitonomy/Git/Tests/WorkingCopyTest.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ public function testDiffPending(): void
7373
$this->assertCount(1, $diffPending->getFiles());
7474
}
7575

76+
public function testDiffPendingWithAbbreviatedHashesLikeOldGit(): void
77+
{
78+
$this->skipUnlessCliDriver();
79+
80+
$repository = self::createFoobarRepository(false);
81+
file_put_contents($repository->getWorkingDir().'/test.sh', 'test');
82+
// Before 2.16, git always wrote "..." after the abbreviated hashes of --raw.
83+
$repository = new Repository($repository->getPath(), array_merge(self::getOptions(), ['environment_variables' => ['GIT_PRINT_SHA1_ELLIPSIS' => 'yes']]));
84+
85+
$files = $repository->getWorkingCopy()->getDiffPending()->getFiles();
86+
87+
$this->assertCount(1, $files);
88+
$this->assertSame(40, \strlen($files[0]->getOldIndex()));
89+
}
90+
7691
public function testCheckoutUnexisting(): void
7792
{
7893
$this->expectException(RuntimeException::class);
@@ -116,8 +131,8 @@ public function testGetUntracked(): void
116131
public function testMerge(): void
117132
{
118133
$repository = self::createFoobarRepository(false);
119-
self::git($repository, 'config', ['--local', 'user.name', '"Unit Test"']);
120-
self::git($repository, 'config', ['--local', 'user.email', '"unit_test@unit-test.com"']);
134+
self::git($repository, 'config', ['user.name', 'Unit Test']);
135+
self::git($repository, 'config', ['user.email', 'unit_test@unit-test.com']);
121136
$wc = $repository->getWorkingCopy();
122137
$wc->checkout('master');
123138

0 commit comments

Comments
 (0)