Пример #1
0
 function testCommitIntoBranch()
 {
     $Git = new Git($this->__repos[1]);
     $this->assertTrue($Git->create());
     $this->assertTrue(file_exists(TMP . 'tests/git/repo/test.git'));
     $this->assertTrue(file_exists(TMP . 'tests/git/working/test/master/.git'));
     $Git->cd();
     $Git->checkout(array('-b', 'new'));
     $Git->push('origin', 'new');
     $Git->branch('new', true);
     $this->assertTrue(file_exists(TMP . 'tests/git/working/test/new/.git'));
     $Git->cd();
     $Git->checkout(array('-b', 'new', 'origin/new'));
     $Git->cd();
     $Git->run('pull');
     $File = new File(TMP . 'tests/git/working/test/new/a.txt');
     $this->assertTrue($File->write('this is something new'));
     $Git->commit(array("-m", "'Adding a.txt'"));
     $Git->push('origin', 'new');
     $Git->cd();
     $result = $Git->read();
     $this->assertEqual($result['message'], "Adding a.txt");
     // pr($Git->debug);
     // pr($Git->response);
     // die();
 }
Пример #2
0
 /**
  * Compare revisions and returns array of files to upload:.
  *
  *      [
  *          'upload' => $filesToUpload,
  *          'delete' => $filesToDelete
  *      ];
  *
  * @param string $localRevision
  *
  * @throws \Exception if unknown git diff status
  *
  * @return array
  */
 public function compare($localRevision)
 {
     $remoteRevision = null;
     $filesToUpload = [];
     $filesToDelete = [];
     if ($this->currentSubmoduleName) {
         $this->dotRevision = $this->currentSubmoduleName . '/' . $this->dotRevisionFilename;
     } else {
         $this->dotRevision = $this->dotRevisionFilename;
     }
     // Fetch the .revision file from the server and write it to $tmpFile
     $this->debug("Fetching {$this->dotRevision} file");
     if ($this->connection->has($this->dotRevision)) {
         $remoteRevision = $this->connection->read($this->dotRevision);
         $this->debug('Remote revision: <bold>' . $remoteRevision);
     } else {
         $this->cli->comment('No revision found - uploading everything...');
     }
     // Checkout the specified Git branch
     if (!empty($this->servers[$this->currentlyDeploying]['branch'])) {
         $output = $this->git->checkout($this->servers[$this->currentlyDeploying]['branch'], $this->repo);
         if (isset($output[0])) {
             if (strpos($output[0], 'error') === 0) {
                 throw new \Exception('Stash your modifications before deploying.');
             }
         }
         if (isset($output[1])) {
             if ($output[1][0] === 'M') {
                 throw new \Exception('Stash your modifications before deploying.');
             }
         }
         if (isset($output[0])) {
             $this->cli->out($output[0]);
         }
     }
     $output = $this->git->diff($remoteRevision, $localRevision, $this->repo);
     $this->debug(implode("\r\n", $output));
     /*
      * Git Status Codes
      *
      * A: addition of a file
      * C: copy of a file into a new one
      * D: deletion of a file
      * M: modification of the contents or mode of a file
      * R: renaming of a file
      * T: change in the type of the file
      * U: file is unmerged (you must complete the merge before it can be committed)
      * X: "unknown" change type (most probably a bug, please report it)
      */
     if (!empty($remoteRevision)) {
         foreach ($output as $line) {
             $status = $line[0];
             if (strpos($line, 'warning: CRLF will be replaced by LF in') !== false) {
                 continue;
             } elseif (strpos($line, 'The file will have its original line endings in your working directory.') !== false) {
                 continue;
             } elseif ($status === 'A' or $status === 'C' or $status === 'M' or $status === 'T') {
                 $filesToUpload[] = trim(substr($line, 1));
             } elseif ($status == 'D') {
                 $filesToDelete[] = trim(substr($line, 1));
             } elseif ($status === 'R') {
                 list(, $oldFile, $newFile) = preg_split('/\\s+/', $line);
                 $filesToDelete[] = trim($oldFile);
                 $filesToUpload[] = trim($newFile);
             } else {
                 throw new \Exception("Unknown git-diff status. Use '--sync' to update remote revision or use '--debug' to see what's wrong.");
             }
         }
     } else {
         $filesToUpload = $output;
     }
     $filteredFilesToUpload = $this->filterIgnoredFiles($filesToUpload);
     $filteredFilesToDelete = $this->filterIgnoredFiles($filesToDelete);
     $filteredFilesToInclude = isset($this->filesToInclude[$this->currentlyDeploying]) ? $this->filterIncludedFiles($this->filesToInclude[$this->currentlyDeploying]) : [];
     $filesToUpload = array_merge($filteredFilesToUpload['files'], $filteredFilesToInclude);
     $filesToDelete = $filteredFilesToDelete['files'];
     $filesToSkip = array_merge($filteredFilesToUpload['filesToSkip'], $filteredFilesToDelete['filesToSkip']);
     return [$this->currentlyDeploying => ['delete' => $filesToDelete, 'upload' => $filesToUpload, 'exclude' => $filesToSkip]];
 }
Пример #3
0
     break;
 case 'diff':
     if (isset($_GET['repo']) && isset($_GET['path'])) {
         $result = $git->diff(getWorkspacePath($_GET['repo']), $_GET['path']);
         if ($result === false) {
             echo '{"status":"error","message":"Failed to get diff!"}';
         } else {
             echo $result;
         }
     } else {
         echo '{"status":"error","message":"Missing parameter!"}';
     }
     break;
 case 'checkout':
     if (isset($_GET['repo']) && isset($_GET['path'])) {
         if ($git->checkout(getWorkspacePath($_GET['repo']), $_GET['path'])) {
             echo '{"status":"success","message":"Changes reverted!"}';
         } else {
             echo '{"status":"error","message":"Failed to undo changes!"}';
         }
     } else {
         echo '{"status":"error","message":"Missing parameter!"}';
     }
     break;
 case 'getRemotes':
     if (isset($_GET['path'])) {
         $result = $git->getRemotes(getWorkspacePath($_GET['path']));
         if ($result === false) {
             echo '{"status":"error","message":"Failed to get remotes!"}';
         } else {
             echo '{"status":"success","data":' . json_encode($result) . '}';