]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve `arc` handling of various binary file operations in Git
authorepriestley <git@epriestley.com>
Fri, 19 Oct 2012 17:10:25 +0000 (10:10 -0700)
committerepriestley <git@epriestley.com>
Fri, 19 Oct 2012 17:10:25 +0000 (10:10 -0700)
Summary:
See T866, D3521. Additional things this fixes:

  - `arc export` now exports binary data correctly.
  - `ArcanistBundle` unit tests now load and apply binary data correctly.
  - `arc patch` no longer relies on `base` configuration.
  - Adds tests to the tarball:

  commit df340e88d8aba12e8f2b8827f01f0cd9f35eb758
  Author: epriestley <git@epriestley.com>
  Date:   Wed Oct 17 15:46:11 2012 -0700

      Remove binary image.

  commit 3f5c6d735e64c25a04f83be48ef184b25b5282f0
  Author: epriestley <git@epriestley.com>
  Date:   Wed Oct 17 15:45:58 2012 -0700

      Copy binary image.

  commit b454edb3bb29890ee5b3af5ef66ce6a24d15d882
  Author: epriestley <git@epriestley.com>
  Date:   Wed Oct 17 15:45:35 2012 -0700

      Move binary image.

  commit 5de5f3dfda1b7db2eb054e57699f05aaf1f4483e
  Author: epriestley <git@epriestley.com>
  Date:   Wed Oct 17 15:45:09 2012 -0700

      Add a binary image.

Test Plan: Ran unit tests, `arc patch`, `arc export`, `arc diff`, `arc upgrade`.

Reviewers: vrana, btrahan

Reviewed By: btrahan

CC: aran
Maniphest Tasks: T866

Differential Revision: https://secure.phabricator.com/D3732

13 files changed:
src/parser/ArcanistBundle.php
src/parser/ArcanistDiffParser.php
src/parser/__tests__/ArcanistBundleTestCase.php
src/parser/__tests__/bundle.git.tgz
src/parser/diff/ArcanistDiffChange.php
src/repository/api/ArcanistGitAPI.php
src/repository/api/ArcanistMercurialAPI.php
src/repository/api/ArcanistRepositoryAPI.php
src/workflow/ArcanistAmendWorkflow.php
src/workflow/ArcanistDiffWorkflow.php
src/workflow/ArcanistExportWorkflow.php
src/workflow/ArcanistPatchWorkflow.php
src/workflow/ArcanistUpgradeWorkflow.php

index ca89659df36ae099092b131f3e95a92749cfc499..6663c86c8bd436321139ad86194796f6c493fcdf 100644 (file)
@@ -312,15 +312,14 @@ final class ArcanistBundle {
 
     $old_file_phids = array();
     foreach ($changes as $change) {
+      if (!$this->isGitBinaryChange($change)) {
+        continue;
+      }
+
       $type = $change->getType();
       if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) {
-        $file_type = $change->getFileType();
-        $is_binary = ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
-                      $file_type == ArcanistDiffChangeType::FILE_IMAGE);
-        if ($is_binary) {
-          foreach ($change->getAwayPaths() as $path) {
-            $old_file_phids[$path] = $change->getMetadata('old:binary-phid');
-          }
+        foreach ($change->getAwayPaths() as $path) {
+          $old_file_phids[$path] = $change->getMetadata('old:binary-phid');
         }
       }
     }
@@ -348,8 +347,7 @@ final class ArcanistBundle {
       $old_mode = idx($change->getOldProperties(), 'unix:filemode', '100644');
       $new_mode = idx($change->getNewProperties(), 'unix:filemode', '100644');
 
-      $is_binary = ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
-                    $file_type == ArcanistDiffChangeType::FILE_IMAGE);
+      $is_binary = $this->isGitBinaryChange($change);
 
       if ($is_binary) {
         $old_phid = idx($old_file_phids, $this->getCurrentPath($change));
@@ -427,6 +425,12 @@ final class ArcanistBundle {
     return $this->convertNonUTF8Diff($diff);
   }
 
+  private function isGitBinaryChange(ArcanistDiffChange $change) {
+    $file_type = $change->getFileType();
+    return ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
+            $file_type == ArcanistDiffChangeType::FILE_IMAGE);
+  }
+
   private function convertNonUTF8Diff($diff) {
     if ($this->encoding) {
       $diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8');
@@ -615,7 +619,7 @@ final class ArcanistBundle {
     return $this;
   }
 
-  private function getBlob($phid) {
+  private function getBlob($phid, $name = null) {
     if ($this->loadFileDataCallback) {
       return call_user_func($this->loadFileDataCallback, $phid);
     }
@@ -625,8 +629,14 @@ final class ArcanistBundle {
       return $blob_data;
     }
 
+    $console = PhutilConsole::getConsole();
+
     if ($this->conduit) {
-      echo "Downloading binary data...\n";
+      if ($name) {
+        $console->writeErr("Downloading binary data for '%s'...\n", $name);
+      } else {
+        $console->writeErr("Downloading binary data...\n");
+      }
       $data_base64 = $this->conduit->callMethodSynchronous(
         'file.download',
         array(
@@ -642,23 +652,37 @@ final class ArcanistBundle {
     $old_phid = idx($change->getAllMetadata(), 'old:binary-phid', $old_phid);
     $new_phid = $change->getMetadata('new:binary-phid');
 
-    if (!$old_phid) {
+    $old_data = null;
+    if ($change->getOriginalFileData() !== null) {
+      $old_data = $change->getOriginalFileData();
+    } else if ($old_phid) {
+      $name = basename($change->getOldPath());
+      $old_data = $this->getBlob($old_phid, $name);
+    }
+
+    $old_length = strlen($old_data);
+
+    if ($old_data === null) {
       $old_data = '';
-      $old_length = 0;
       $old_sha1 = str_repeat('0', 40);
     } else {
-      $old_data = $this->getBlob($old_phid);
-      $old_length = strlen($old_data);
       $old_sha1 = sha1("blob {$old_length}\0{$old_data}");
     }
 
-    if (!$new_phid) {
+    $new_data = null;
+    if ($change->getCurrentFileData() !== null) {
+      $new_data = $change->getCurrentFileData();
+    } else if ($new_phid) {
+      $name = basename($change->getCurrentPath());
+      $new_data = $this->getBlob($new_phid, $name);
+    }
+
+    $new_length = strlen($new_data);
+
+    if ($new_data === null) {
       $new_data = '';
-      $new_length = 0;
       $new_sha1 = str_repeat('0', 40);
     } else {
-      $new_data = $this->getBlob($new_phid);
-      $new_length = strlen($new_data);
       $new_sha1 = sha1("blob {$new_length}\0{$new_data}");
     }
 
index ae58281512d421b2a6a86a66a2b4c9e32ad73331..cfcdd2b7be1a069e7dbe57ef11c9da46346acb43 100644 (file)
@@ -1140,4 +1140,49 @@ final class ArcanistDiffParser {
       return $name;
     }
   }
+
+  public function loadSyntheticData(
+    array $changes,
+    ArcanistRepositoryAPI $repository_api) {
+    assert_instances_of($changes, 'ArcanistDiffChange');
+
+    foreach ($changes as $change) {
+      $path = $change->getCurrentPath();
+
+      // Certain types of changes (moves and copies) don't contain change data
+      // when expressed in raw "git diff" form. Augment any such diffs with
+      // textual data.
+      if ($change->getNeedsSyntheticGitHunks()) {
+        $diff = $repository_api->getRawDiffText($path, $moves = false);
+
+        $raw_changes = $this->parseDiff($diff);
+        foreach ($raw_changes as $raw_change) {
+          if ($raw_change->getCurrentPath() == $path) {
+            $change->setFileType($raw_change->getFileType());
+            foreach ($raw_change->getHunks() as $hunk) {
+              // Git thinks that this file has been added. But we know that it
+              // has been moved or copied without a change.
+              $hunk->setCorpus(
+                preg_replace('/^\+/m', ' ', $hunk->getCorpus()));
+              $change->addHunk($hunk);
+            }
+            break;
+          }
+        }
+
+        $change->setNeedsSyntheticGitHunks(false);
+      }
+
+      if ($change->getFileType() != ArcanistDiffChangeType::FILE_BINARY &&
+          $change->getFileType() != ArcanistDiffChangeType::FILE_IMAGE) {
+        continue;
+      }
+
+      $change->setOriginalFileData($repository_api->getOriginalFileData($path));
+      $change->setCurrentFileData($repository_api->getCurrentFileData($path));
+    }
+
+    return $changes;
+  }
+
 }
index cb52dd68ad825790db996dbe144994de5ad5e145..45837687101fc90051be66d346588736a05bdf34 100644 (file)
@@ -71,6 +71,8 @@ final class ArcanistBundleTestCase extends ArcanistTestCase {
 
     foreach ($commits as $commit) {
       list($commit_hash, $tree_hash, $subject) = explode(' ', $commit, 3);
+      execx('git reset --hard %s --', $commit_hash);
+
       list($diff) = execx(
         'git diff %s^ %s --',
         $commit_hash,
@@ -78,12 +80,20 @@ final class ArcanistBundleTestCase extends ArcanistTestCase {
 
       $parser = new ArcanistDiffParser();
       $changes = $parser->parseDiff($diff);
+
+      $repository_api = new ArcanistGitAPI($fixture->getPath());
+      $repository_api->setRelativeCommit($commit_hash.'^');
+
+      $parser->loadSyntheticData($changes, $repository_api);
+
       $bundle = ArcanistBundle::newFromChanges($changes);
 
       execx('git reset --hard %s^ --', $commit_hash);
 
+      $patch = $bundle->toGitPatch();
+
       id(new ExecFuture('git apply --index --reject'))
-        ->write($bundle->toGitPatch())
+        ->write($patch)
         ->resolvex();
 
       execx('git commit -m %s', $subject);
index 41e85594279eaeae04b1555b874dbf3af0e829d1..99178e93552775bd8f074e2503ffb522145d3d26 100644 (file)
Binary files a/src/parser/__tests__/bundle.git.tgz and b/src/parser/__tests__/bundle.git.tgz differ
index 88da67b2d208c3e3edf9a1f348b4c88cd810b332..d811249b3b0acc384014c8636692a5bdf59fbafe 100644 (file)
@@ -40,6 +40,27 @@ final class ArcanistDiffChange {
 
   private $needsSyntheticGitHunks;
 
+  private $currentFileData;
+  private $originalFileData;
+
+  public function setOriginalFileData($original_file_data) {
+    $this->originalFileData = $original_file_data;
+    return $this;
+  }
+
+  public function getOriginalFileData() {
+    return $this->originalFileData;
+  }
+
+  public function setCurrentFileData($current_file_data) {
+    $this->currentFileData = $current_file_data;
+    return $this;
+  }
+
+  public function getCurrentFileData() {
+    return $this->currentFileData;
+  }
+
   public function toDictionary() {
     $hunks = array();
     foreach ($this->hunks as $hunk) {
index f640da077a4fadf67c24d4249cd9756702a98e9e..38865f8eb1bdb4c3abc47492e7186e102f911f8b 100644 (file)
@@ -689,6 +689,11 @@ final class ArcanistGitAPI extends ArcanistRepositoryAPI {
     return true;
   }
 
+  public function setDefaultBaseCommit() {
+    $this->setRelativeCommit('HEAD^');
+    return $this;
+  }
+
   public function hasLocalCommit($commit) {
     try {
       if (!$this->getCanonicalRevisionName($commit)) {
index 59915aec4ed81d4c17e7790001a6fc9c15352a59..a552481f8f5427d51cece39a499a792b5aa90018 100644 (file)
@@ -427,6 +427,11 @@ final class ArcanistMercurialAPI extends ArcanistRepositoryAPI {
     return true;
   }
 
+  public function setDefaultBaseCommit() {
+    $this->setRelativeCommit('.^');
+    return $this;
+  }
+
   public function hasLocalCommit($commit) {
     try {
       $this->getCanonicalRevisionName($commit);
index d96afd7cc47dc3fd99b3d277ec4ee5e8ab391a2c..4524ecd7601541778125ea754250b4892b5763e5 100644 (file)
@@ -182,6 +182,21 @@ abstract class ArcanistRepositoryAPI {
     ConduitClient $conduit,
     array $query);
 
+  /**
+   * Set the base commit to a reasonable default value so that working copy
+   * status checks can do something meaningful and won't invoke configured
+   * 'base' rules.
+   *
+   * This is primarily useful for workflows which do not operate on commit
+   * ranges but need to verify the working copy is not dirty, like "amend",
+   * "upgrade" and "patch".
+   *
+   * @return this
+   */
+  public function setDefaultBaseCommit() {
+    throw new ArcanistCapabilityNotSupportedException($this);
+  }
+
   public function amendCommit($message) {
     throw new ArcanistCapabilityNotSupportedException($this);
   }
index 31f5a82b0a2dd021138f5b31fd650183f9b277cc..947a8be99c07ae48abb765ef362313cc969eaa15 100644 (file)
@@ -106,10 +106,8 @@ EOTEXT
       $revision_id = $this->normalizeRevisionID($this->getArgument('revision'));
     }
 
-    if ($repository_api instanceof ArcanistGitAPI) {
-      $repository_api->setRelativeCommit('HEAD^');
-    } else if ($repository_api instanceof ArcanistMercurialAPI) {
-      $repository_api->setRelativeCommit('.^');
+    if ($repository_api->supportsRelativeLocalCommits()) {
+      $repository_api->setDefaultBaseCommit();
     }
 
     $in_working_copy = $repository_api->loadWorkingCopyDifferentialRevisions(
index f27954a786a49328f2c23133d5a94dcea552c15a..175a4a84f57af87ead686a9388afed538c760b34 100644 (file)
@@ -1019,41 +1019,18 @@ EOTEXT
       }
     }
 
-    foreach ($changes as $change) {
-      $path = $change->getCurrentPath();
-
-      // Certain types of changes (moves and copies) don't contain change data
-      // when expressed in raw "git diff" form. Augment any such diffs with
-      // textual data.
-      if ($change->getNeedsSyntheticGitHunks()) {
-        $diff = $repository_api->getRawDiffText($path, $moves = false);
-        $parser = $this->newDiffParser();
-
-        $raw_changes = $parser->parseDiff($diff);
-        foreach ($raw_changes as $raw_change) {
-          if ($raw_change->getCurrentPath() == $path) {
-            $change->setFileType($raw_change->getFileType());
-            foreach ($raw_change->getHunks() as $hunk) {
-              // Git thinks that this file has been added. But we know that it
-              // has been moved or copied without a change.
-              $hunk->setCorpus(
-                preg_replace('/^\+/m', ' ', $hunk->getCorpus()));
-              $change->addHunk($hunk);
-            }
-            break;
-          }
-        }
-
-        $change->setNeedsSyntheticGitHunks(false);
-      }
+    $changes = $parser->loadSyntheticData($changes, $repository_api);
 
+    foreach ($changes as $change) {
       if ($change->getFileType() != ArcanistDiffChangeType::FILE_BINARY) {
         continue;
       }
 
+      $path = $change->getCurrentPath();
+
       $name = basename($path);
 
-      $old_file = $repository_api->getOriginalFileData($path);
+      $old_file = $change->getOriginalFileData();
       $old_dict = $this->uploadFile($old_file, $name, 'old binary');
       if ($old_dict['guid']) {
         $change->setMetadata('old:binary-phid', $old_dict['guid']);
@@ -1061,7 +1038,7 @@ EOTEXT
       $change->setMetadata('old:file:size',      $old_dict['size']);
       $change->setMetadata('old:file:mime-type', $old_dict['mime']);
 
-      $new_file = $repository_api->getCurrentFileData($path);
+      $new_file = $change->getCurrentFileData();
       $new_dict = $this->uploadFile($new_file, $name, 'new binary');
       if ($new_dict['guid']) {
         $change->setMetadata('new:binary-phid', $new_dict['guid']);
index 6b92f25c95e906aae1a25f6a12076e10e2fe7364..510037084accf9a6c472ee08a70f17bfd7ce062d 100644 (file)
@@ -204,6 +204,8 @@ EOTEXT
             $paths);
         }
 
+        $changes = $parser->loadSyntheticData($changes, $repository_api);
+
         $bundle = ArcanistBundle::newFromChanges($changes);
         $bundle->setProjectID($this->getWorkingCopy()->getProjectID());
         $bundle->setBaseRevision(
index efd0b2511a73bb80555d25e2383bffc2eb987c43..6d2e2dfd1e03d81b5d0b0614aa80d27c776dcf4c 100644 (file)
@@ -837,6 +837,11 @@ EOTEXT
    * Do the best we can to prevent PEBKAC and id10t issues.
    */
   private function sanityCheck(ArcanistBundle $bundle) {
+    $repository_api = $this->getRepositoryAPI();
+
+    if ($repository_api->supportsRelativeLocalCommits()) {
+      $repository_api->setDefaultBaseCommit();
+    }
 
     // Require clean working copy
     $this->requireCleanWorkingCopy();
@@ -870,7 +875,6 @@ EOTEXT
 
     // Check to see if the bundle's base revision matches the working copy
     // base revision
-    $repository_api = $this->getRepositoryAPI();
     if ($repository_api->supportsRelativeLocalCommits()) {
       $bundle_base_rev = $bundle->getBaseRevision();
       if (empty($bundle_base_rev)) {
index 554ecbeea5de75756322019d4c3a3d3207fd3317..47bd3ba5d9022cbdc00c6ae2eeb7e5ee9e68bffd 100644 (file)
@@ -66,10 +66,10 @@ EOTEXT
       $repository_api = ArcanistRepositoryAPI::newAPIFromWorkingCopyIdentity(
         $working_copy);
 
-      // Force the range to HEAD^..HEAD, which is meaningless but keeps us
-      // from triggering "base" rules or other commit range resolution rules
-      // that might prompt the user when we pull the working copy status.
-      $repository_api->setRelativeCommit('HEAD^');
+      if ($repository_api->supportsRelativeLocalCommits()) {
+        $repository_api->setDefaultBaseCommit();
+      }
+
       $this->setRepositoryAPI($repository_api);
 
       // Require no local changes.