/** * Execute the command. * * @param InputInterface $input * @param OutputInterface $output * @return void */ protected function execute(InputInterface $input, OutputInterface $output) { $this->setIo($input, $output); /** @var Repository $config */ $config = $this->container['config']; $sourceRepoPath = $config['source.directory']; $sourceRepoUrl = $config['source.git.url']; $sourceRepoBranch = $config['source.git.branch']; $this->checkSourceRepoSettings($sourceRepoPath, $sourceRepoUrl); $output->writeln(["<b>steak pull configuration:</b>", " Source repository remote is <path>{$sourceRepoUrl}</path>", " Source repository branch is <path>{$sourceRepoBranch}</path>", " Path to local repository is <path>{$sourceRepoPath}</path>"], OutputInterface::VERBOSITY_VERBOSE); if ($this->files->exists($sourceRepoPath)) { $workingCopy = $this->git->workingCopy($sourceRepoPath); if (!$workingCopy->isCloned()) { throw new RuntimeException("<path>{$sourceRepoPath}</path> exists but is not a git repository."); } if ($workingCopy->getBranches()->head() != $sourceRepoBranch) { throw new RuntimeException("<path>{$sourceRepoPath}</path> exists but isn't on the <path>{$sourceRepoBranch}</path> branch."); } $this->git->streamOutput(); $workingCopy->pull(); } else { $output->writeln(["The source directory <path>{$sourceRepoPath}</path> does not exist.", " Attempting clone from {$sourceRepoUrl}"]); $this->git->streamOutput(); $this->git->cloneRepository($sourceRepoUrl, $sourceRepoPath, ['single-branch' => true, 'branch' => $sourceRepoBranch]); $output->writeln("<info>Clone complete! Edit your sources in <path>{$sourceRepoPath}</path></info>"); } $output->writeln("Try <comment>steak serve</comment> to fire up the local development server..."); }
public function deploy() { // Create the wrapper. $wrapper = new GitWrapper(); $wrapper->streamOutput(); // Iterate through each project. foreach ($this->projects as $name => $project) { // Check out all branches. foreach ($project['branches'] as $branch => $destination) { // Build our git interface. $git = null; if (!is_dir($destination)) { $git = $wrapper->cloneRepository($project['repo'], $destination); } else { $git = new GitWorkingCopy($wrapper, $destination); } // Fetch the latest. $git->fetch('origin'); // Checkout the desired branch. $git->checkout($branch, array('force' => true)); // Reset any local changes. $git->reset(array('hard' => true)); // Pull the latest from the branch. $git->pull('origin', $branch, array('force' => true)); } } }
/** * @param LocalPackage $package * * @return GitWorkingCopy */ public function execute(LocalPackage $package) { $git = $this->gitWrapper->cloneRepository($package->getForkSSHRepository(), $package->getFolder()); // @todo parameters $git->config('user.name', 'BOTrelli')->config('user.email', '*****@*****.**'); $git->remote('add', 'upstream', $package->getRepository()); $this->dispatchEvent(StepsEvents::GIT_REMOTE_ADDED, new GitEventMade($git)); $git->checkout($package->getLocalBranch(), array('b' => true)); $this->dispatchEvent(StepsEvents::GIT_CHECKOUT_DONE, new GitEventMade($git)); $git->fetch('upstream'); $this->dispatchEvent(StepsEvents::GIT_FETCHED, new GitEventMade($git)); $git->merge('upstream/master'); $this->dispatchEvent(StepsEvents::GIT_MERGED, new GitEventMade($git)); $git->rebase('master'); $this->dispatchEvent(StepsEvents::GIT_MERGED, new GitEventMade($git)); return $git; }
/** * Clones the source code for this project. */ public function init($path = null) { $path = is_null($path) ? $this->environment->path : $path; // Check if clone already exists at this path. If so we can safely skip. if (file_exists($path)) { $wrapper = new GitWrapper(); try { $working_copy = new GitWorkingCopy($wrapper, $path); $output = $working_copy->remote('-v'); } catch (GitException $e) { throw new \Exception('Path already exists.'); } // if repo exists in the remotes already, this working copy is ok. if (strpos(strtolower($output), strtolower($this->app->repo)) !== false) { return true; } else { throw new Exception('Git clone already exists at that path, but it is not for this app.'); } } try { // Create App folder mkdir($path, 0755, TRUE); chdir($path); // Clone repo $wrapper = new GitWrapper(); $wrapper->streamOutput(); $wrapper->cloneRepository($this->app->repo, $path); // Checkout correct version. $git = new GitWorkingCopy($wrapper, $this->getSourcePath()); $git->checkout($this->environment->version); } catch (\GitWrapper\GitException $e) { // If exception is because there is no git ref, continue. if (strpos($e->getMessage(), 'error: pathspec') !== 0) { return false; } } chdir($path); $wrapper->git('branch'); $wrapper->git('status'); $this->loadConfig(); // Run the build hooks if (!empty($this->config['hooks']['build'])) { chdir($this->getSourcePath()); $process = new Process($this->config['hooks']['build']); $process->run(function ($type, $buffer) { if (Process::ERR === $type) { echo $buffer; } else { echo $buffer; } }); } return $this->writeConfig(); }
/** * Execute the console command. * * @return mixed */ public function handle() { $currentUser = get_current_user(); $privateKey = php_uname('s') == 'Darwin' ? realpath("/Users/{$currentUser}/.ssh/id_rsa") : realpath("/home/{$currentUser}/.ssh/id_rsa"); $blog = base_path('resources/blog/'); $git = new GitWrapper(); $git->setPrivateKey($privateKey); if (!file_exists($blog)) { $git->cloneRepository(config('blog.repository'), $blog); } $git->git('pull origin master', $blog); }
/** * @param string $source * @param string $destination * @param array $options * * @return string */ public function process($source, $destination, $options = array()) { $parameters = array(); $options += array('arguments' => array()); foreach ($options['arguments'] as $param => $value) { if (is_numeric($param)) { $parameters[$value] = true; } else { $parameters[$param] = $value; } } $this->wrapper->cloneRepository($source, $destination, $parameters); }
/** * @inheritdoc */ public function run(&$cmdParams, &$params) { $res = false; $taskRunner = $this->taskRunner; $gitCmd = !empty($cmdParams[0]) ? $taskRunner->parseStringAliases($cmdParams[0]) : ''; if (empty($gitCmd)) { throw new Exception('git: Command cannot be empty'); } $this->controller->stdout('Running git ' . $gitCmd . '...'); if (!$this->controller->dryRun) { $wrapper = false; try { $wrapper = new GitWrapper(); } catch (GitException $e) { Log::throwException($e->getMessage()); } if ($wrapper) { switch ($gitCmd) { case 'clone': $repo = !empty($cmdParams[1]) ? $cmdParams[1] : ''; $destDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : ''; if (empty($repo) || empty($destDir)) { throw new Exception('git: repository and destination directory cannot be empty'); } $gitOptions = !empty($cmdParams[3]) ? $cmdParams[3] : []; $res = $wrapper->cloneRepository($repo, $destDir, $gitOptions); break; default: $this->controller->stdout("\n"); $this->controller->stderr("Command not supported: {$gitCmd}", Console::FG_RED); break; } } } else { $this->controller->stdout(' [dry run]', Console::FG_YELLOW); } $this->controller->stdout("\n"); return !empty($res); }
/** * @param $projectId * * @return int The exit code */ public function actionIndex($projectId) { $exitCode = 0; // TODO: read info from the database $projectInfo = (object) (require __DIR__ . '/../projects_tmp/' . $projectId . '.php'); // ------------------------ $home = Shell::getHomeDir(); $this->workspace = $home . DIRECTORY_SEPARATOR . 'workspace' . DIRECTORY_SEPARATOR . $projectInfo->id . "_" . time(); $this->stdout('Fetching into '); $this->stdout($this->workspace . "\n", Console::FG_CYAN); $gitClone = false; try { $wrapper = new GitWrapper(); $gitOptions = []; if (!empty($projectInfo->branch)) { $gitOptions['branch'] = $projectInfo->branch; } $gitClone = $wrapper->cloneRepository($projectInfo->repo, $this->workspace, $gitOptions); } catch (GitException $e) { $this->stderr($e->getMessage(), Console::FG_RED); $exitCode = 1; } if (!empty($projectInfo->rootFolder)) { $this->workspace .= '/' . $projectInfo->rootFolder; } if ($gitClone && $this->run) { // TODO: parametrise deployii folder name / path (relative to workspace) $buildFile = $this->workspace . '/' . $this->getScriptFolder() . '/build.php'; if (file_exists($buildFile)) { $taskRunner = new TaskRunner($this, $buildFile); $exitCode = $taskRunner->run($this->target); } else { $this->stderr("Build file not found: " . $buildFile, Console::FG_RED); $exitCode = 1; } } $this->stdout("\n"); return $exitCode; }
$codingApi->selectProject($repositoryName); } $wrapper = new GitWrapper(); $wrapper->setTimeout(3600); $defaultRepositoryTotal = count($defaultRepository); // 总数量 $i = 1; // 只要一个仓库新建任务就可以了 requestAPI::logging('当前用户 ' . $codingUserName . ' 需要执行 ' . $defaultRepositoryTotal . ' 个仓库', 'info'); foreach ($defaultRepository as $repositoryName => $repositoryUrl) { requestAPI::logging('执行第 ' . $i . ' 个仓库', 'info'); $projectPathName = $repositoryDir . '/' . $repositoryName; // 本地没有这个库 就去 git clone 下来 if (!is_dir($projectPathName)) { requestAPI::logging('本地没有此仓库: ' . $repositoryName . ' 开始从 ' . $repositoryUrl . ' Clone ....', 'info'); $git = $wrapper->cloneRepository($repositoryUrl, $projectPathName); if (!$git) { requestAPI::logging('Clone 失败 ' . $repositoryName . ' 退出...', 'error', true); } } else { $git = $wrapper->workingCopy($projectPathName); } //http push 免密码 $codingUrl = 'https://' . $codingUserName . ':' . $auth[1] . '@git.coding.net/' . $codingUserName . '/' . $repositoryName . '.git'; requestAPI::logging('当前用户的Coding远程仓库 Url: ' . $codingUrl, 'info'); $git->remote('show'); $output = str_replace("\n", '', $git->getOutput()); if (strpos($output, 'coding') !== false) { $git->remote('rm', 'coding'); } $git->remote('add', 'coding', $codingUrl);
/** * @inheritDoc */ protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); $output->writeln([$this->getApplication()->getLongVersion(), '']); $question = new Question('<question>Please enter project name in title case:</question> '); $name = $helper->ask($input, $output, $question); if (empty($name)) { $output->writeln(['', '<error>Error: Project name cannot be empty</error>']); return 1; } if (false === ctype_upper($name[0])) { $output->writeln(['', '<error>Error: Project name must be title case</error>']); return 1; } $nameCan = Strings\dasherize($name); $projectPath = getcwd() . '/' . $nameCan; if (file_exists($projectPath) || is_dir($projectPath)) { $output->writeln(['', '<error>Error: Project with name "' . $nameCan . '" already exists</error>']); return 1; } $projectTypes = ['Component', 'Tool', 'Bundle', 'Action']; $question = new Question('<question>Please enter project type (Component):</question> ', 'Component'); $question->setAutocompleterValues($projectTypes); $projectType = $helper->ask($input, $output, $question); if (empty($projectType)) { $output->writeln(['', '<error>Error: Project type cannot be empty</error>']); return 1; } if (false === ctype_upper($projectType[0])) { $output->writeln(['', '<error>Error: Project type must be title case</error>']); return 1; } $author = rtrim(shell_exec("git config --get user.name")); $authorSug = ''; if (null !== $author) { $authorSug = '(' . $author . ')'; } $question = new Question('<question>Please enter author ' . $authorSug . ':</question> ', $author); $author = $helper->ask($input, $output, $question); if (empty($author)) { $output->writeln(['', '<error>Error: Author cannot be empty</error>']); return 1; } $email = rtrim(shell_exec("git config --get user.email")); $emailSug = ''; if (null !== $email) { $emailSug = '(' . $email . ')'; } $question = new Question('<question>Please enter email ' . $emailSug . ':</question> ', $email); $email = $helper->ask($input, $output, $question); if (empty($email)) { $output->writeln(['', '<error>Error: Email cannot be empty</error>']); return 1; } $question = new Question('<question>Please enter git repository:</question> '); $repo = $helper->ask($input, $output, $question); if (empty($email)) { $output->writeln(['', '<error>Error: Git repository cannot be empty</error>']); return 1; } $output->writeln(['']); $year = date('Y'); $output->write('<info>Cloning project template: </info>'); $wrapper = new GitWrapper(); $wrapper->cloneRepository('git@github.com:ThrusterIO/project-template.git', $projectPath); $output->writeln('<comment>Done</comment>'); $fs = new Filesystem(); $output->write('<info>Removing .git folder: </info>'); $fs->remove($projectPath . '/.git'); $output->writeln('<comment>Done</comment>'); $output->write('<info>Removing src/.gitkeep file: </info>'); $fs->remove($projectPath . '/src/.gitkeep'); $output->writeln('<comment>Done</comment>'); $output->write('<info>Initialized empty Git repository: </info>'); $git = $wrapper->init($projectPath); $output->writeln('<comment>Done</comment>'); $finder = new Finder(); $finder->in($projectPath)->ignoreDotFiles(false)->files(); $output->writeln(['', '<info>Applying variables to files: </info>']); $progress = new ProgressBar($output, count($finder)); $progress->setFormat('debug'); $progress->start(); /** @var SplFileInfo $file */ foreach ($finder as $file) { $searchFor = ['${NAME}$', '${NAME_CAN}$', '${PROJECT_TYPE}$', '${YEAR}$', '${AUTHOR}$', '${EMAIL}$']; $replaceWith = [$name, $nameCan, $projectType, $year, $author, $email]; $progress->setMessage($file->getFilename()); file_put_contents($file->getRealPath(), str_replace($searchFor, $replaceWith, $file->getContents())); $progress->advance(); } $progress->finish(); $git->remote('add', 'origin', $repo); $git->add('./'); $output->writeln(['', '', '<comment>Finished.</comment>']); return 0; }