Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement apply command for applying diff patches #29

Merged

Conversation

ramsey
Copy link
Contributor

@ramsey ramsey commented Apr 8, 2021

Implement the apply command to provide the ability to apply diff patches to the working tree.

This PR also includes the commit from #28. I wanted to submit them separately for separate review, but if you look at the total changes in this PR, you'll see the changes for both commits, since #28 is required to make this work.

Example

In this example, you should be able to get an idea of the direction I'm heading in order to implement the CaptainHook feature I proposed here: captainhookphp/captainhook#122

use SebastianFeldmann\Git;

$repo = new Git\Repository(__DIR__);
$diff = $repo->getDiffOperator();

// Create patch from working tree.
$patchFile = tempnam(sys_get_temp_dir(), 'CaptainHook');
file_put_contents($patchFile, $diff->getUnstagedPatch());

// Reset working tree.
exec('git checkout -- .', $output, $exitCode);

$applied = true;

try {
    // Apply patch back to working tree.
    if (!$diff->applyPatches([$patchFile])) {
        if (!$diff->applyPatches([$patchFile], true)) {
            $applied = false;
        }
    }
} finally {
    if ($applied) {
        fwrite(STDOUT, 'Cleanly applied patch file to working tree: ' . $patchFile);
    } else {
        fwrite(STDERR, 'Could not cleanly apply patch file to working tree: ' . $patchFile);
    }
}

Copy link
Owner

@sebastianfeldmann sebastianfeldmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@sebastianfeldmann sebastianfeldmann merged commit c9d9e00 into sebastianfeldmann:main Apr 8, 2021
@ramsey ramsey deleted the feature/command-apply branch April 9, 2021 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants