Source of file Git.php
Size: 6,904 Bytes - Last Modified: 2020-07-03T12:08:09+00:00
/var/www/docs.ssmods.com/process/src/src/Tasks/Helpers/Git.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 | <?php namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers; use Sunnysideup\UpgradeToSilverstripe4\Traits\HelperInst; class Git { use HelperInst; /** * @param string $dir * @param string $gitLink * @param string $gitRootDir * @param string $branchName * * @return Git */ public function Clone(string $dir, string $gitLink, string $gitRootDir, ?string $branchName = 'master'): Git { $this->mu()->execMe( $dir, 'git clone ' . $gitLink . ' ' . $gitRootDir, 'clone ' . $gitLink . ' into ' . $gitRootDir, false ); $this->mu()->execMe( $gitRootDir, 'git branch -a', 'check branch exists', false ); $this->mu()->execMe( $gitRootDir, 'git checkout ' . $branchName, 'checkout ' . $branchName, false ); $this->mu()->execMe( $gitRootDir, 'git branch', 'confirm branch', false ); $this->fetchAll($gitRootDir); return $this; } /** * @param string $dir * @param string $message * @param string $branchName * * @return Git */ public function CommitAndPush(string $dir, string $message, string $branchName): Git { $this->fetchAll($dir); $this->mu()->execMe( $dir, 'git add . -A', 'git add all', false ); $this->mu()->execMe( $dir, // 'if ! git diff --quiet; then git commit . -m "'.addslashes($message).'"; fi;', ' if [ -z "$(git status --porcelain)" ]; then echo \'OKI DOKI - Nothing to commit\'; else git commit . -m "' . addslashes($message) . '" fi', 'commit changes: ' . $message, false ); $this->mu()->execMe( $dir, 'git push origin ' . $branchName, 'pushing changes to origin on the ' . $branchName . ' branch', false ); return $this; } /** * @param string $dir * @param string $branchName * * @return Git */ public function deleteBranch(string $dir, string $branchName): Git { $this->fetchAll($dir); $this->deleteBranchLocally($dir, $branchName); $this->deleteBranchRemotely($dir, $branchName); return $this; } /** * @param string $dir * @param string $newBranchName * @param string $fromBranchName * @return Git */ public function createNewBranchIfItDoesNotExist(string $dir, string $newBranchName, string $fromBranchName): Git { $this->fetchAll($dir); $this->checkoutBranch($dir, $fromBranchName); $this->mu()->execMe( $dir, ' if $(git ls-remote --heads ${REPO} ${BRANCH} | grep -q ' . "'refs/heads/" . $newBranchName . "'" . '); then echo "branch exists" else git checkout -b ' . $newBranchName . ' ' . $fromBranchName . ' git push origin ' . $newBranchName . '; fi', 'create branch ' . $newBranchName . ' from the ' . $fromBranchName . ' branch in ' . $dir, false ); return $this; } /** * @param string $dir * @param string $newBranchName * @param string $fromBranch * @return Git */ public function createNewBranch(string $dir, string $newBranchName, string $fromBranch): Git { $this->fetchAll($dir); $this->checkoutBranch($dir, $fromBranch); $this->mu()->execMe( $dir, 'git checkout -b ' . $newBranchName . ' ' . $fromBranch, 'create and checkout new branch: ' . $newBranchName . ' from ' . $fromBranch, false ); $this->mu()->execMe( $dir, 'git push -u origin ' . $newBranchName, 'push it ' . $fromBranch, false ); return $this; } /** * @param string $dir * @param string $branch * @return Git */ public function checkoutBranch(string $dir, string $branch): Git { $this->fetchAll($dir); $this->mu()->execMe( $dir, 'git checkout ' . $branch, 'check out : ' . $branch . ' as a starting point', false ); $this->mu()->execMe( $this->mu()->getGitRootDir(), 'git pull origin ' . $branch, 'get the latest details for : ' . $branch . '', false ); return $this; } /** * @param string $dir * @param string $fromBranch * @param string $intoBranch * @return Git */ public function Merge(string $dir, string $fromBranch, string $intoBranch): Git { $this->fetchAll($dir); $this->mu()->execMe( $dir, ' git checkout ' . $fromBranch . ' git pull origin ' . $fromBranch . ' git checkout ' . $intoBranch . ' git merge --squash ' . $fromBranch . ' git commit . -m "MAJOR: upgrade merge" git push origin ' . $intoBranch . ' ', 'merging ' . $fromBranch . ' into ' . $intoBranch . ' in ' . $dir, false ); return $this; } /** * @param string $dir * @param string $branchName * * @return Git */ public function deleteBranchRemotely(string $dir, string $branchName): Git { $this->mu()->execMe( $dir, 'git push origin --delete ' . $branchName, 'delete branch (' . $branchName . ') remotely', false ); return $this; } /** * @param string $dir */ public function fetchAll(string $dir): Git { $this->mu()->execMe( $dir, 'git fetch --all && git branch -a && git status', 'get the latest', false, '', false //verbose = false! ); return $this; } /** * @param string $dir * @param string $branchName * * @return Git */ protected function deleteBranchLocally(string $dir, string $branchName): Git { $this->mu()->execMe( $dir, 'if git show-ref --quiet refs/heads/' . $branchName . '; then git branch -d ' . $branchName . '; git push origin --delete ' . $this->mu()->getNameOfTempBranch() . '; fi', 'delete branch (' . $branchName . ') locally', false ); return $this; } } |