From b06c29cc3c294b639ac91705fe4cbed026abd3c0 Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Wed, 22 Mar 2023 13:58:56 +0100 Subject: [PATCH] Ignore untracked files with ignore_unstaged_changes parameter --- doc/faq.md | 4 +-- .../StashUnstagedChangesSubscriberSpec.php | 19 ++++++++++++++ .../StashUnstagedChangesSubscriber.php | 26 ++++++++++++++++--- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/doc/faq.md b/doc/faq.md index c92301ed2..a8140ec9e 100644 --- a/doc/faq.md +++ b/doc/faq.md @@ -73,10 +73,10 @@ You can resolve this problem by adding the following (or similar) to the compose [up](#table-of-content) -## Why is the unstaged file state being used? +## Why is the unstaged/untracked file state being used? GrumPHP can only work with the actual files on the filesystem. -This means that your unstaged changes will be staged when GrumPHP checks your codebase. +This means that your unstaged changes and untracked files will be staged when GrumPHP checks your codebase. It is possible to use the staged files by stashing your changes with the `ignore_unstaged_changes` parameter. Do note that this parameter is risky and won't work with partial commits. [More information can be found here](https://github.com/phpro/grumphp/blob/master/doc/parameters.md). diff --git a/spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php b/spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php index c084e235b..ca6c35923 100644 --- a/spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php +++ b/spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php @@ -27,6 +27,7 @@ function let(GitStashConfig $config, GitRepository $repository, IOInterface $io, $repository->getWorkingCopy()->willReturn($workingCopy); $workingCopy->getDiffPending()->willReturn($unstaged); $unstaged->getFiles()->willReturn(['file1.php']); + $workingCopy->getUntrackedFiles()->willReturn(['untracked.php']); $this->beConstructedWith($config, $repository, $io); } @@ -109,6 +110,24 @@ function it_should_stash_changes(GitRepository $repository) $this->saveStash($event); } + function it_should_stash_changes_without_indexed_changes(GitRepository $repository) + { + $event = new RunnerEvent(new TasksCollection(), new GitPreCommitContext(new FilesCollection()), new TaskResultCollection()); + + $repository->run('stash', Argument::containing('--keep-index'))->shouldBeCalled(); + + $this->saveStash($event); + } + + function it_should_stash_changes_with_untracked_files(GitRepository $repository) + { + $event = new RunnerEvent(new TasksCollection(), new GitPreCommitContext(new FilesCollection()), new TaskResultCollection()); + + $repository->run('stash', Argument::containing('--include-untracked'))->shouldBeCalled(); + + $this->saveStash($event); + } + function it_should_pop_changes(GitRepository $repository) { $event = new RunnerEvent(new TasksCollection(), new GitPreCommitContext(new FilesCollection()), new TaskResultCollection()); diff --git a/src/Event/Subscriber/StashUnstagedChangesSubscriber.php b/src/Event/Subscriber/StashUnstagedChangesSubscriber.php index 5fdbb83f1..a6f0cb26e 100644 --- a/src/Event/Subscriber/StashUnstagedChangesSubscriber.php +++ b/src/Event/Subscriber/StashUnstagedChangesSubscriber.php @@ -97,14 +97,32 @@ public function handleErrors(): void */ private function doSaveStash(): void { - $pending = $this->repository->getWorkingCopy()->getDiffPending(); - if (!\count($pending->getFiles())) { + $hasPending = \count($this->repository->getWorkingCopy()->getDiffPending()->getFiles()) > 0; + $hasUntracked = \count($this->repository->getWorkingCopy()->getUntrackedFiles()) > 0; + + if (!$hasPending && !$hasUntracked) { return; } try { - $this->io->write(['Detected unstaged changes... Stashing them!']); - $this->repository->run('stash', ['save', '--keep-index', uniqid('grumphp')]); + if ($hasPending && !$hasUntracked) { + $this->io->write(['Detected unstaged changes... Stashing them!']); + } + + if (!$hasPending && $hasUntracked) { + $this->io->write(['Detected untracked files... Stashing them!']); + } + + if ($hasPending && $hasUntracked) { + $this->io->write([ + 'Detected unstaged changes and untracked files... Stashing them!', + ]); + } + + $this->repository->run( + 'stash', + ['save', '--keep-index', '--include-untracked', uniqid('grumphp')] + ); } catch (Exception $e) { // No worries ... $this->io->write([sprintf('Failed stashing changes: %s', $e->getMessage())]);