1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | <?php require_once(GITPHP_BASEDIR . 'lib/php-diff/lib/Diff.php'); require_once(GITPHP_BASEDIR . 'lib/php-diff/lib/Diff/Renderer/Text/Unified.php'); /** * Diffs two blobs * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Git */ class GitPHP_FileDiff { /** * Whether diff info has been read * * @var boolean */ protected $diffInfoRead = false; /** * Whether diff data has been read * * @var boolean */ protected $diffDataRead = false; /** * Diff data * * @var string */ protected $diffData; /** * Whether split diff data has been read * * @var boolean */ protected $diffDataSplitRead = false; /** * Diff data split up by left/right changes * * @var array */ protected $diffDataSplit; /** * Filename used on last data diff * * @var string */ protected $diffDataName; /** * From file mode * * @var string */ protected $fromMode; /** * To file mode * * @var string */ protected $toMode; /** * From blob hash * * @var string */ protected $fromHash; /** * To blob hash * * @var string */ protected $toHash; /** * Change status * * @var string */ protected $status; /** * File similarity * * @var string */ protected $similarity; /** * From filename * * @var string */ protected $fromFile; /** * To filename * * @var string */ protected $toFile; /** * From file type * * @var string */ protected $fromFileType; /** * To file type * * @var string */ protected $toFileType; /** * Project * * @var GitPHP_Project */ protected $project; /** * The hash of the commit that caused this filediff * * @var string */ protected $commitHash; /** * Cache instance * * @var GitPHP_Cache */ protected $cache = null; /** * Constructor * * @param GitPHP_Project $project project * @param string $fromHash source hash, can also be a diff-tree info line * @param string $toHash target hash, required if $fromHash is a hash */ public function __construct($project, $fromHash, $toHash = '') { $this->project = $project; if ($this->ParseDiffTreeLine($fromHash)) return; if (!(preg_match('/^[0-9a-fA-F]{40}$/', $fromHash) && preg_match('/^[0-9a-fA-F]{40}$/', $toHash))) { throw new Exception('Invalid parameters for FileDiff'); } $this->fromHash = $fromHash; $this->toHash = $toHash; } /** * Gets the project * * @return GitPHP_Project project */ public function GetProject() { return $this->project; } /** * Get the cache instance * * @return GitPHP_Cache|null object cache */ public function GetCache() { return $this->cache; } /** * Set the cache instance * * @param GitPHP_Cache|null $cache object cache */ public function SetCache($cache) { $this->cache = $cache; } /** * Reads data from a git-formatted diff tree line * * @param string $diffTreeLine line from difftree * @return boolean true if data was read from line */ private function ParseDiffTreeLine($diffTreeLine) { if (preg_match('/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/', $diffTreeLine, $regs)) { $this->diffInfoRead = true; $this->fromMode = $regs[1]; $this->toMode = $regs[2]; $this->fromHash = $regs[3]; $this->toHash = $regs[4]; $this->status = $regs[5]; $this->similarity = ltrim($regs[6], '0'); $this->fromFile = strtok($regs[7], "\t"); $this->toFile = strtok("\t"); if ($this->toFile === false) { /* no filename change */ $this->toFile = $this->fromFile; } return true; } return false; } /** * Reads file diff info */ protected function ReadDiffInfo() { $this->diffInfoRead = true; /* TODO: read a single difftree line on-demand */ } /** * Gets the from file mode (full a/u/g/o) * * @return string from file mode */ public function GetFromMode() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->fromMode; } /** * Gets the from file mode in short form (standard u/g/o) * * @return string short from file mode */ public function GetFromModeShort() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return substr($this->fromMode, -4); } /** * Gets the to file mode (full a/u/g/o) * * @return string to file mode */ public function GetToMode() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->toMode; } /** * Gets the to file mode in short form (standard u/g/o) * * @return string short to file mode */ public function GetToModeShort() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return substr($this->toMode, -4); } /** * Gets the from hash * * @return string from hash */ public function GetFromHash() { return $this->fromHash; } /** * Gets the to hash * * @return string to hash */ public function GetToHash() { return $this->toHash; } /** * Gets the from file blob * * @return GitPHP_Blob|null blob object */ public function GetFromBlob() { if (empty($this->fromHash)) return null; return $this->GetProject()->GetObjectManager()->GetBlob($this->fromHash); } /** * Gets the to file blob * * @return GitPHP_Blob|null blob object */ public function GetToBlob() { if (empty($this->toHash)) return null; return $this->GetProject()->GetObjectManager()->GetBlob($this->toHash); } /** * Gets the status of the change * * @return string status */ public function GetStatus() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->status; } /** * Gets the similarity * * @return string similarity */ public function GetSimilarity() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->similarity; } /** * Gets the from file name * * @return string from file */ public function GetFromFile() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->fromFile; } /** * Gets the to file name * * @return string to file */ public function GetToFile() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->toFile; } /** * Gets the from file type * * @return int from file type */ public function GetFromFileType() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return GitPHP_FilesystemObject::ObjectType($this->fromMode); } /** * Gets the to file type * * @return int to file type */ public function GetToFileType() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return GitPHP_FilesystemObject::ObjectType($this->toMode); } /** * Gets the from label * * @param string $file override file name * @return string from label */ public function GetFromLabel($file = null) { if ($this->status == 'A') return '/dev/null'; if (!empty($file)) return 'a/' . $file; $fromFile = $this->GetFromFile(); if (!empty($fromFile)) return 'a/' . $fromFile; return 'a/' . $this->GetFromHash(); } /** * Gets the to label * * @param string $file override file name * @return string to label */ public function GetToLabel($file = null) { if (!$this->diffInfoRead) $this->ReadDiffInfo(); if ($this->status == 'D') return '/dev/null'; if (!empty($file)) return 'b/' . $file; $toFile = $this->GetToFile(); if (!empty($toFile)) return 'b/' . $toFile; return 'b/' . $this->GetToHash(); } /** * Gets whether one or both files are binary files * * @return boolean true if binary */ public function IsBinary() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); if (($this->status != 'A') && $this->GetFromBlob()->IsBinary()) return true; if (($this->status != 'D') && $this->GetToBlob()->IsBinary()) return true; return false; } /** * Tests if filetype changed * * @return boolean true if file type changed */ public function FileTypeChanged() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0x17000) != (octdec($this->toMode) & 0x17000); } /** * Tests if file mode changed * * @return boolean true if file mode changed */ public function FileModeChanged() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0777) != (octdec($this->toMode) & 0777); } /** * Tests if the from file is a regular file * * @return boolean true if from file is regular */ public function FromFileIsRegular() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0x8000) == 0x8000; } /** * Tests if the to file is a regular file * * @return boolean true if to file is regular */ public function ToFileIsRegular() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->toMode) & 0x8000) == 0x8000; } /** * Gets the diff output * * @param string $file override the filename on the diff * @param boolean $readFileData whether file info data should also be read * @param boolean $explode whether data should be exploded into an array of lines * @return string|string[] diff output */ public function GetDiff($file = '', $readFileData = true, $explode = false) { if ($this->diffDataRead && ($file == $this->diffDataName)) { if ($explode) return explode("\n", $this->diffData); else return $this->diffData; } if ((!$this->diffInfoRead) && $readFileData) $this->ReadDiffInfo(); $this->diffDataName = $file; $this->diffDataRead = true; if ((!empty($this->status)) && ($this->status != 'A') && ($this->status != 'D') && ($this->status != 'M')) { $this->diffData = ''; return; } $this->diffData = $this->GetDiffData(3, true, $file); if ($explode) return explode("\n", $this->diffData); else return $this->diffData; } /** * Construct the side by side diff data from the git data * * The result is an array of ternary arrays with 3 elements each: * First the mode ("" or "-added" or "-deleted" or "-modified"), * then the first column, then the second. * * @author Mattias Ulbrich * * @return array an array of line elements */ public function GetDiffSplit() { if ($this->diffDataSplitRead) { return $this->diffDataSplit; } $this->diffDataSplitRead = true; $fromBlob = $this->GetFromBlob(); $blob = $fromBlob->GetData(true); $diffLines = explode("\n", $this->GetDiffData(0, false)); // // parse diffs $diffs = array(); $currentDiff = FALSE; foreach($diffLines as $d) { $d = trim($d); if(strlen($d) == 0) continue; switch($d[0]) { case '@': if($currentDiff) { if (count($currentDiff['left']) == 0 && count($currentDiff['right']) > 0) { if ($this->UseXDiff()) { $currentDiff['line']++; // HACK to make added blocks align correctly } } $diffs[] = $currentDiff; } $comma = strpos($d, ","); $line = -intval(substr($d, 2, $comma-2)); $currentDiff = array("line" => $line, "left" => array(), "right" => array()); break; case '+': if($currentDiff) $currentDiff["right"][] = substr($d, 1); break; case '-': if($currentDiff) $currentDiff["left"][] = substr($d, 1); break; case ' ': echo "should not happen!"; if($currentDiff) { $currentDiff["left"][] = substr($d, 1); $currentDiff["right"][] = substr($d, 1); } break; } } if($currentDiff) { if (count($currentDiff['left']) == 0 && count($currentDiff['right']) > 0) { if ($this->UseXDiff()) { $currentDiff['line']++; // HACK to make added blocks align correctly } } $diffs[] = $currentDiff; } // // iterate over diffs $output = array(); $idx = 0; foreach($diffs as $d) { while($idx+1 < $d['line']) { $h = $blob[$idx]; $output[] = array('', $h, $h); $idx ++; } if(count($d['left']) == 0) { $mode = 'added'; } elseif(count($d['right']) == 0) { $mode = 'deleted'; } else { $mode = 'modified'; } for($i = 0; $i < count($d['left']) || $i < count($d['right']); $i++) { $left = $i < count($d['left']) ? $d['left'][$i] : FALSE; $right = $i < count($d['right']) ? $d['right'][$i] : FALSE; $output[] = array($mode, $left, $right); } $idx += count($d['left']); } while($idx < count($blob)) { $h = $blob[$idx]; $output[] = array('', $h, $h); $idx ++; } $this->diffDataSplit = $output; return $output; } /** * Get diff data * * @param integer $context number of context lines * @param boolean $header true to include file header * @param string $file override file name * @return string diff data */ private function GetDiffData($context = 3, $header = true, $file = null) { $fromData = ''; $toData = ''; if (empty($this->status) || ($this->status == 'M') || ($this->status == 'D')) { $fromBlob = $this->GetFromBlob(); $fromData = $fromBlob->GetData(false); } if (empty($this->status) || ($this->status == 'M') || ($this->status == 'A')) { $toBlob = $this->GetToBlob(); $toData = $toBlob->GetData(false); } $output = ''; if ($header) { $output = '--- ' . $this->GetFromLabel($file) . "\n" . '+++ ' . $this->GetToLabel($file) . "\n"; } $diffOutput = false; $cacheKey = null; if ($this->cache) { $cacheKey = 'project|' . $this->project->GetProject() . '|diff|' . $context . '|' . $this->fromHash . '|' . $this->toHash; $diffOutput = $this->cache->Get($cacheKey); } if ($diffOutput === false) { if ($this->UseXDiff()) { $diffOutput = $this->GetXDiff($fromData, $toData, $context); } else { $diffOutput = $this->GetPhpDiff($fromData, $toData, $context); } if ($this->cache) { $this->cache->Set($cacheKey, $diffOutput); } } $output .= $diffOutput; return $output; } /** * Get diff using php-diff * * @param string $fromData from file data * @param string $toData to file data * @param integer $context context lines * @return string diff content */ private function GetPhpDiff($fromData, $toData, $context = 3) { $options = array('context' => $context); $diffObj = new Diff(explode("\n", $fromData), explode("\n", $toData), $options); $renderer = new Diff_Renderer_Text_Unified; return $diffObj->render($renderer); } /** * Returns whether xdiff should be used * * @return boolean true if xdiff should be used */ private function UseXDiff() { return function_exists('xdiff_string_diff'); } /** * Get diff using xdiff * * @param string $fromData from file data * @param string $toData to file data * @param integer $context context lines * @return string diff content */ private function GetXDiff($fromData, $toData, $context = 3) { return xdiff_string_diff($fromData, $toData, $context); } /** * Gets the commit for this filediff * * @return GitPHP_Commit commit object */ public function GetCommit() { return $this->GetProject()->GetCommit($this->commitHash); } /** * Sets the commit for this filediff * * @param GitPHP_Commit $commit commit object */ public function SetCommit($commit) { if (!$commit) return; $this->SetCommitHash($commit->GetHash()); } /** * Sets the hash of the commit for this filediff * * @param string $hash hash */ public function SetCommitHash($hash) { if (!preg_match('/^[0-9A-Fa-f]{40}$/', $hash)) return; $this->commitHash = $hash; } } |