![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/sebastianfeldmann/git/src/Operator/ |
<?php /** * This file is part of SebastianFeldmann\Git. * * (c) Sebastian Feldmann <[email protected]> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianFeldmann\Git\Operator; use SebastianFeldmann\Git\Command\Apply\ApplyPatch; use SebastianFeldmann\Git\Command\Diff\Compare; use SebastianFeldmann\Git\Command\DiffIndex\GetUnstagedPatch; use SebastianFeldmann\Git\Command\DiffTree\ChangedFiles; use SebastianFeldmann\Git\Command\WriteTree\CreateTreeObject; /** * Diff operator * * @package SebastianFeldmann\Git * @author Sebastian Feldmann <[email protected]> * @link https://github.com/sebastianfeldmann/git * @since Class available since Release 1.2.0 */ class Diff extends Base { /** * Returns a list of files and their changes. * * @param string $from * @param string $to * @return \SebastianFeldmann\Git\Diff\File[] */ public function compare(string $from, string $to): array { $compare = (new Compare($this->repo->getRoot()))->revisions($from, $to) ->ignoreWhitespacesAtEndOfLine(); $result = $this->runner->run($compare, new Compare\FullDiffList()); return $result->getFormattedOutput(); } /** * Returns a list of files and their changes staged for the next commit * * @param string $to * @return \SebastianFeldmann\Git\Diff\File[] */ public function compareIndexTo(string $to = 'head'): array { $compare = (new Compare($this->repo->getRoot()))->indexTo($to) ->withContextLines(0) ->ignoreWhitespacesAtEndOfLine(); $result = $this->runner->run($compare, new Compare\FullDiffList()); return $result->getFormattedOutput(); } /** * Returns a list of files and their changes not yet staged * * @param string $to * @return \SebastianFeldmann\Git\Diff\File[] */ public function compareTo(string $to = 'HEAD'): iterable { $compare = (new Compare($this->repo->getRoot()))->to($to) ->ignoreSubmodules() ->withContextLines(0); $result = $this->runner->run($compare, new Compare\FullDiffList()); return $result->getFormattedOutput(); } /** * Uses 'diff-tree' to list the files that changed between two revisions * * @param string $from * @param string $to * @param array<string> $filter * @return string[] */ public function getChangedFiles(string $from, string $to, array $filter = []): array { $cmd = (new ChangedFiles($this->repo->getRoot()))->fromRevision($from)->toRevision($to)->useFilter($filter); $result = $this->runner->run($cmd); return $result->getBufferedOutput(); } /** * Uses 'diff-tree' to list the files with a given suffix that changed between two revisions * * @param string $from * @param string $to * @param string $suffix * @param array<string> $filter * @return string[] */ public function getChangedFilesOfType(string $from, string $to, string $suffix, array $filter = []): array { $suffix = strtolower($suffix); $cmd = (new ChangedFiles($this->repo->getRoot()))->fromRevision($from) ->toRevision($to) ->useFilter($filter); $result = $this->runner->run($cmd); $files = $result->getBufferedOutput(); $filesByType = []; foreach ($files as $file) { $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); $filesByType[$ext][] = $file; } return $filesByType[$suffix] ?? []; } /** * Returns a binary diff of unstaged changes to the working tree that can be * applied with `git-apply`. * * @return string|null String patch, if there are unstaged changes; null otherwise. */ public function getUnstagedPatch(): ?string { $treeCmd = new CreateTreeObject($this->repo->getRoot()); $treeResult = $this->runner->run($treeCmd); $treeId = null; if ($treeResult->isSuccessful()) { $treeId = trim($treeResult->getStdOut()); } $cmd = (new GetUnstagedPatch($this->repo->getRoot()))->tree($treeId); $result = $this->runner->run($cmd); // A status code of 1 means there were differences and we have a patch. if ($result->getCode() === 1) { return $result->getStdOut(); } return null; } /** * Applies the supplied diff patches to files. * * @param string[] $patches An array of paths to patch files. * @param bool $disableAutoCrlfSetting If true, explicitly set core.autocrlf * to "false" to override the global Git configuration. * @return bool True if the patches apply cleanly. */ public function applyPatches(array $patches, bool $disableAutoCrlfSetting = false): bool { $cmd = (new ApplyPatch($this->repo->getRoot())) ->patches($patches) ->whitespace('nowarn'); if ($disableAutoCrlfSetting === true) { $cmd->setConfigParameter('core.autocrlf', false); } $result = $this->runner->run($cmd); return $result->isSuccessful(); } }