private function createHunk($oldOffset, $oldLen, $newOffset, $newLen, $changes)
 {
     $hunk = new DifferentialHunk();
     $hunk->setOldOffset($oldOffset);
     $hunk->setOldLen($oldLen);
     $hunk->setNewOffset($newOffset);
     $hunk->setNewLen($newLen);
     $hunk->setChanges($changes);
     return $hunk;
 }
 public function testMakeChanges()
 {
     $root = dirname(__FILE__) . '/hunk/';
     $hunk = new DifferentialHunk();
     $hunk->setChanges(Filesystem::readFile($root . 'basic.diff'));
     $hunk->setOldOffset(1);
     $hunk->setNewOffset(11);
     $old = Filesystem::readFile($root . 'old.txt');
     $this->assertEqual($old, $hunk->makeOldFile());
     $new = Filesystem::readFile($root . 'new.txt');
     $this->assertEqual($new, $hunk->makeNewFile());
     $added = array(12 => "1 quack\n", 13 => "1 quack\n", 16 => "5 drake\n");
     $this->assertEqual($added, $hunk->getAddedLines());
     $hunk = new DifferentialHunk();
     $hunk->setChanges(Filesystem::readFile($root . 'newline.diff'));
     $hunk->setOldOffset(1);
     $hunk->setNewOffset(11);
     $this->assertEqual("a\n", $hunk->makeOldFile());
     $this->assertEqual("a", $hunk->makeNewFile());
     $this->assertEqual(array(11 => "a"), $hunk->getAddedLines());
 }
 public function testDiffChangesets()
 {
     $hunk = new DifferentialHunk();
     $hunk->setChanges("+a\n b\n-c");
     $hunk->setNewOffset(1);
     $hunk->setNewLen(2);
     $left = new DifferentialChangeset();
     $left->attachHunks(array($hunk));
     $tests = array("+a\n b\n-c" => array(array(), array()), "+a\n x\n-c" => array(array(), array()), "+aa\n b\n-c" => array(array(1), array(11)), " b\n-c" => array(array(1), array()), "+a\n b\n c" => array(array(), array(13)), "+a\n x\n c" => array(array(), array(13)));
     foreach ($tests as $changes => $expected) {
         $hunk = new DifferentialHunk();
         $hunk->setChanges($changes);
         $hunk->setNewOffset(11);
         $hunk->setNewLen(3);
         $right = new DifferentialChangeset();
         $right->attachHunks(array($hunk));
         $parser = new DifferentialChangesetParser();
         $parser->setOriginals($left, $right);
         $this->assertEqual($expected, $parser->diffOriginals(), $changes);
     }
 }
 public static function newFromRawChanges(array $changes)
 {
     $diff = new DifferentialDiff();
     $lines = 0;
     foreach ($changes as $change) {
         $changeset = new DifferentialChangeset();
         $add_lines = 0;
         $del_lines = 0;
         $hunks = $change->getHunks();
         if ($hunks) {
             foreach ($hunks as $hunk) {
                 $dhunk = new DifferentialHunk();
                 $dhunk->setOldOffset($hunk->getOldOffset());
                 $dhunk->setOldLen($hunk->getOldLength());
                 $dhunk->setNewOffset($hunk->getNewOffset());
                 $dhunk->setNewLen($hunk->getNewLength());
                 $dhunk->setChanges($hunk->getCorpus());
                 $changeset->addUnsavedHunk($dhunk);
                 $add_lines += $hunk->getAddLines();
                 $del_lines += $hunk->getDelLines();
                 $lines += $add_lines + $del_lines;
             }
         } else {
             // This happens when you add empty files.
             $changeset->attachHunks(array());
         }
         $changeset->setOldFile($change->getOldPath());
         $changeset->setFilename($change->getCurrentPath());
         $changeset->setChangeType($change->getType());
         $changeset->setFileType($change->getFileType());
         $changeset->setMetadata($change->getAllMetadata());
         $changeset->setOldProperties($change->getOldProperties());
         $changeset->setNewProperties($change->getNewProperties());
         $changeset->setAwayPaths($change->getAwayPaths());
         $changeset->setAddLines($add_lines);
         $changeset->setDelLines($del_lines);
         $diff->addUnsavedChangeset($changeset);
     }
     $diff->setLineCount($lines);
     return $diff;
 }
 public static function newFromRawChanges(array $changes)
 {
     assert_instances_of($changes, 'ArcanistDiffChange');
     $diff = new DifferentialDiff();
     $lines = 0;
     foreach ($changes as $change) {
         $changeset = new DifferentialChangeset();
         $add_lines = 0;
         $del_lines = 0;
         $first_line = PHP_INT_MAX;
         $hunks = $change->getHunks();
         if ($hunks) {
             foreach ($hunks as $hunk) {
                 $dhunk = new DifferentialHunk();
                 $dhunk->setOldOffset($hunk->getOldOffset());
                 $dhunk->setOldLen($hunk->getOldLength());
                 $dhunk->setNewOffset($hunk->getNewOffset());
                 $dhunk->setNewLen($hunk->getNewLength());
                 $dhunk->setChanges($hunk->getCorpus());
                 $changeset->addUnsavedHunk($dhunk);
                 $add_lines += $hunk->getAddLines();
                 $del_lines += $hunk->getDelLines();
                 $added_lines = $hunk->getChangedLines('new');
                 if ($added_lines) {
                     $first_line = min($first_line, head_key($added_lines));
                 }
             }
             $lines += $add_lines + $del_lines;
         } else {
             // This happens when you add empty files.
             $changeset->attachHunks(array());
         }
         $metadata = $change->getAllMetadata();
         if ($first_line != PHP_INT_MAX) {
             $metadata['line:first'] = $first_line;
         }
         $changeset->setOldFile($change->getOldPath());
         $changeset->setFilename($change->getCurrentPath());
         $changeset->setChangeType($change->getType());
         $changeset->setFileType($change->getFileType());
         $changeset->setMetadata($metadata);
         $changeset->setOldProperties($change->getOldProperties());
         $changeset->setNewProperties($change->getNewProperties());
         $changeset->setAwayPaths($change->getAwayPaths());
         $changeset->setAddLines($add_lines);
         $changeset->setDelLines($del_lines);
         $diff->addUnsavedChangeset($changeset);
     }
     $diff->setLineCount($lines);
     $diff->detectCopiedCode();
     return $diff;
 }