Skip to content

Commit

Permalink
Ignore untracked files with ignore_unstaged_changes parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
yguedidi committed Mar 22, 2023
1 parent d19e76c commit b06c29c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions doc/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
19 changes: 19 additions & 0 deletions spec/Event/Subscriber/StashUnstagedChangesSubscriberSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
Expand Down
26 changes: 22 additions & 4 deletions src/Event/Subscriber/StashUnstagedChangesSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(['<fg=yellow>Detected unstaged changes... Stashing them!</fg=yellow>']);
$this->repository->run('stash', ['save', '--keep-index', uniqid('grumphp')]);
if ($hasPending && !$hasUntracked) {
$this->io->write(['<fg=yellow>Detected unstaged changes... Stashing them!</fg=yellow>']);
}

if (!$hasPending && $hasUntracked) {
$this->io->write(['<fg=yellow>Detected untracked files... Stashing them!</fg=yellow>']);
}

if ($hasPending && $hasUntracked) {
$this->io->write([
'<fg=yellow>Detected unstaged changes and untracked files... Stashing them!</fg=yellow>',
]);
}

$this->repository->run(
'stash',
['save', '--keep-index', '--include-untracked', uniqid('grumphp')]
);
} catch (Exception $e) {
// No worries ...
$this->io->write([sprintf('<fg=red>Failed stashing changes: %s</fg=red>', $e->getMessage())]);
Expand Down

0 comments on commit b06c29c

Please sign in to comment.