/**
  * Launch an editor and edit the content. The edited content will be
  * returned.
  *
  * @return string    Edited content.
  * @throws Exception The editor exited abnormally or something untoward
  *                   occurred.
  *
  * @task edit
  */
 public function editInteractively()
 {
     $name = $this->getName();
     $content = $this->getContent();
     if (phutil_is_windows()) {
         $content = str_replace("\n", "\r\n", $content);
     }
     $tmp = Filesystem::createTemporaryDirectory('edit.');
     $path = $tmp . DIRECTORY_SEPARATOR . $name;
     try {
         Filesystem::writeFile($path, $content);
     } catch (Exception $ex) {
         Filesystem::remove($tmp);
         throw $ex;
     }
     $editor = $this->getEditor();
     $offset = $this->getLineOffset();
     $err = $this->invokeEditor($editor, $path, $offset);
     if ($err) {
         Filesystem::remove($tmp);
         throw new Exception("Editor exited with an error code (#{$err}).");
     }
     try {
         $result = Filesystem::readFile($path);
         Filesystem::remove($tmp);
     } catch (Exception $ex) {
         Filesystem::remove($tmp);
         throw $ex;
     }
     if (phutil_is_windows()) {
         $result = str_replace("\r\n", "\n", $result);
     }
     $this->setContent($result);
     return $this->getContent();
 }
Beispiel #2
0
 public function __construct($filename = null, $dir = null)
 {
     $this->dir = Filesystem::createTemporaryDirectory();
     if ($filename === null) {
         $this->file = tempnam($this->dir, getmypid() . '-');
     } else {
         $this->file = $this->dir . '/' . $filename;
     }
     Filesystem::writeFile($this, '');
 }
Beispiel #3
0
 /**
  * Create a new temporary file.
  *
  * @param string? Filename hint. This is useful if you intend to edit the
  *                file with an interactive editor, so the user's editor shows
  *                "commit-message" instead of "p3810hf-1z9b89bas".
  * @task create
  */
 public function __construct($filename = null)
 {
     $this->dir = Filesystem::createTemporaryDirectory();
     if ($filename === null) {
         $this->file = tempnam($this->dir, getmypid() . '-');
     } else {
         $this->file = $this->dir . '/' . $filename;
     }
     // If we fatal (e.g., call a method on NULL), destructors are not called.
     // Make sure our destructor is invoked.
     register_shutdown_function(array($this, '__destruct'));
     Filesystem::writeFile($this, '');
 }
 public function testDirectoryCacheSpecialDirectoryRules()
 {
     $cache = new PhutilDirectoryKeyValueCache();
     $dir = Filesystem::createTemporaryDirectory();
     $dir = $dir . '/dircache/';
     $cache->setCacheDirectory($dir);
     $cache->setKey('a', 1);
     $this->assertEqual(true, Filesystem::pathExists($dir . '/a.cache'));
     $cache->setKey('a/b', 1);
     $this->assertEqual(true, Filesystem::pathExists($dir . '/a/'));
     $this->assertEqual(true, Filesystem::pathExists($dir . '/a/b.cache'));
     $cache->deleteKey('a/b');
     $this->assertEqual(false, Filesystem::pathExists($dir . '/a/'));
     $this->assertEqual(false, Filesystem::pathExists($dir . '/a/b.cache'));
     $cache->destroyCache();
     $this->assertEqual(false, Filesystem::pathExists($dir));
 }
 public function writeToDisk($path)
 {
     $changes = $this->getChanges();
     $change_list = array();
     foreach ($changes as $change) {
         $change_list[] = $change->toDictionary();
     }
     $hunks = array();
     foreach ($change_list as $change_key => $change) {
         foreach ($change['hunks'] as $key => $hunk) {
             $hunks[] = $hunk['corpus'];
             $change_list[$change_key]['hunks'][$key]['corpus'] = count($hunks) - 1;
         }
     }
     $blobs = array();
     foreach ($change_list as $change) {
         if (!empty($change['metadata']['old:binary-phid'])) {
             $blobs[$change['metadata']['old:binary-phid']] = null;
         }
         if (!empty($change['metadata']['new:binary-phid'])) {
             $blobs[$change['metadata']['new:binary-phid']] = null;
         }
     }
     foreach ($blobs as $phid => $null) {
         $blobs[$phid] = $this->getBlob($phid);
     }
     $meta_info = array('version' => 5, 'projectName' => $this->getProjectID(), 'baseRevision' => $this->getBaseRevision(), 'revisionID' => $this->getRevisionID(), 'encoding' => $this->getEncoding(), 'authorName' => $this->getAuthorName(), 'authorEmail' => $this->getAuthorEmail());
     $dir = Filesystem::createTemporaryDirectory();
     Filesystem::createDirectory($dir . '/hunks');
     Filesystem::createDirectory($dir . '/blobs');
     Filesystem::writeFile($dir . '/changes.json', json_encode($change_list));
     Filesystem::writeFile($dir . '/meta.json', json_encode($meta_info));
     foreach ($hunks as $key => $hunk) {
         Filesystem::writeFile($dir . '/hunks/' . $key, $hunk);
     }
     foreach ($blobs as $key => $blob) {
         Filesystem::writeFile($dir . '/blobs/' . $key, $blob);
     }
     execx('(cd %s; tar -czf %s *)', $dir, Filesystem::resolvePath($path));
     Filesystem::remove($dir);
 }
 private function getBulkFileDataAtRevision($paths, $revision)
 {
     // Calling 'hg cat' on each file individually is slow (1 second per file
     // on a large repo) because mercurial has to decompress and parse the
     // entire manifest every time. Do it in one large batch instead.
     // hg cat will write the file data to files in a temp directory
     $tmpdir = Filesystem::createTemporaryDirectory();
     // Mercurial doesn't create the directories for us :(
     foreach ($paths as $path) {
         $tmppath = $tmpdir . '/' . $path;
         Filesystem::createDirectory(dirname($tmppath), 0755, true);
     }
     list($err, $stdout) = $this->execManualLocal('cat --rev %s --output %s -- %C', $revision, $tmpdir . '/%p', implode(' ', $paths));
     $filedata = array();
     foreach ($paths as $path) {
         $tmppath = $tmpdir . '/' . $path;
         if (Filesystem::pathExists($tmppath)) {
             $filedata[$path] = Filesystem::readFile($tmppath);
         }
     }
     Filesystem::remove($tmpdir);
     return $filedata;
 }
 public static function newEmptyFixture()
 {
     $obj = new PhutilDirectoryFixture();
     $obj->path = Filesystem::createTemporaryDirectory();
     return $obj;
 }