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 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | <?php /** * GitPHP File Diff * * Represents a single file difference * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Git */ require_once(GITPHP_GITOBJECTDIR . 'Blob.class.php'); require_once(GITPHP_GITOBJECTDIR . 'TmpDir.class.php'); require_once(GITPHP_GITOBJECTDIR . 'DiffExe.class.php'); /** * Commit class * * @package GitPHP * @subpackage Git */ class GitPHP_FileDiff { /** * diffInfoRead * * Stores whether diff info has been read * * @access protected */ protected $diffInfoRead = false; /** * diffDataRead * * Stores whether diff data has been read * * @access protected */ protected $diffDataRead = false; /** * diffData * * Stores the diff data * * @access protected */ protected $diffData; /** * diffDataSplitRead * * Stores whether split diff data has been read * * @access protected */ protected $diffDataSplitRead = false; /** * diffDataSplit * * Stores the diff data split up by left/right changes * * @access protected */ protected $diffDataSplit; /** * diffDataName * * Filename used on last data diff * * @access protected */ protected $diffDataName; /** * fromMode * * Stores the from file mode * * @access protected */ protected $fromMode; /** * toMode * * Stores the to file mode * * @access protected */ protected $toMode; /** * fromHash * * Stores the from hash * * @access protected */ protected $fromHash; /** * toHash * * Stores the to hash * * @access protected */ protected $toHash; /** * status * * Stores the status * * @access protected */ protected $status; /** * similarity * * Stores the similarity * * @access protected */ protected $similarity; /** * fromFile * * Stores the from filename * * @access protected */ protected $fromFile; /** * toFile * * Stores the to filename * * @access protected */ protected $toFile; /** * fromFileType * * Stores the from file type * * @access protected */ protected $fromFileType; /** * toFileType * * Stores the to file type * * @access protected */ protected $toFileType; /** * project * * Stores the project * * @access protected */ protected $project; /** * commit * * Stores the commit that caused this filediff * * @access protected */ protected $commit; /** * __construct * * Constructor * * @access public * @param mixed $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 * @return mixed FileDiff object * @throws Exception on invalid parameters */ 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; } /** * ParseDiffTreeLine * * @access private * @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; } /** * ReadDiffInfo * * Reads file diff info * * @access protected */ protected function ReadDiffInfo() { $this->diffInfoRead = true; /* TODO: read a single difftree line on-demand */ } /** * GetFromMode * * Gets the from file mode * (full a/u/g/o) * * @access public * @return string from file mode */ public function GetFromMode() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->fromMode; } /** * GetFromModeShort * * Gets the from file mode in short form * (standard u/g/o) * * @access public * @return string short from file mode */ public function GetFromModeShort() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return substr($this->fromMode, -4); } /** * GetToMode * * Gets the to file mode * (full a/u/g/o) * * @access public * @return string to file mode */ public function GetToMode() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->toMode; } /** * GetToModeShort * * Gets the to file mode in short form * (standard u/g/o) * * @access public * @return string short to file mode */ public function GetToModeShort() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return substr($this->toMode, -4); } /** * GetFromHash * * Gets the from hash * * @access public * @return string from hash */ public function GetFromHash() { return $this->fromHash; } /** * GetToHash * * Gets the to hash * * @access public * @return string to hash */ public function GetToHash() { return $this->toHash; } /** * GetFromBlob * * Gets the from file blob * * @access public * @return mixed blob object */ public function GetFromBlob() { if (empty($this->fromHash)) return null; return $this->project->GetBlob($this->fromHash); } /** * GetToBlob * * Gets the to file blob * * @access public * @return mixed blob object */ public function GetToBlob() { if (empty($this->toHash)) return null; return $this->project->GetBlob($this->toHash); } /** * GetStatus * * Gets the status of the change * * @access public * @return string status */ public function GetStatus() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->status; } /** * GetSimilarity * * Gets the similarity * * @access public * @return string similarity */ public function GetSimilarity() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->similarity; } /** * GetFromFile * * Gets the from file name * * @access public * @return string from file */ public function GetFromFile() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->fromFile; } /** * GetToFile * * Gets the to file name * * @access public * @return string to file */ public function GetToFile() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return $this->toFile; } /** * GetFromFileType * * Gets the from file type * * @access public * @param boolean $local true if caller wants localized type * @return string from file type */ public function GetFromFileType($local = false) { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return GitPHP_Blob::FileType($this->fromMode, $local); } /** * GetToFileType * * Gets the to file type * * @access public * @param boolean $local true if caller wants localized type * @return string to file type */ public function GetToFileType($local = false) { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return GitPHP_Blob::FileType($this->toMode, $local); } /** * FileTypeChanged * * Tests if filetype changed * * @access public * @return boolean true if file type changed */ public function FileTypeChanged() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0x17000) != (octdec($this->toMode) & 0x17000); } /** * FileModeChanged * * Tests if file mode changed * * @access public * @return boolean true if file mode changed */ public function FileModeChanged() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0777) != (octdec($this->toMode) & 0777); } /** * FromFileIsRegular * * Tests if the from file is a regular file * * @access public * @return boolean true if from file is regular */ public function FromFileIsRegular() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->fromMode) & 0x8000) == 0x8000; } /** * ToFileIsRegular * * Tests if the to file is a regular file * * @access public * @return boolean true if to file is regular */ public function ToFileIsRegular() { if (!$this->diffInfoRead) $this->ReadDiffInfo(); return (octdec($this->toMode) & 0x8000) == 0x8000; } /** * GetDiff * * Gets the diff output * * @access public * @param string $file override the filename on the diff * @return 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; } if (function_exists('xdiff_string_diff')) { $this->diffData = $this->GetXDiff(3, true, $file); } else { $tmpdir = GitPHP_TmpDir::GetInstance(); $pid = 0; if (function_exists('posix_getpid')) $pid = posix_getpid(); else $pid = rand(); $fromTmpFile = null; $toTmpFile = null; $fromName = null; $toName = null; if ((empty($this->status)) || ($this->status == 'D') || ($this->status == 'M')) { $fromBlob = $this->GetFromBlob(); $fromTmpFile = 'gitphp_' . $pid . '_from'; $tmpdir->AddFile($fromTmpFile, $fromBlob->GetData()); $fromName = 'a/'; if (!empty($file)) { $fromName .= $file; } else if (!empty($this->fromFile)) { $fromName .= $this->fromFile; } else { $fromName .= $this->fromHash; } } if ((empty($this->status)) || ($this->status == 'A') || ($this->status == 'M')) { $toBlob = $this->GetToBlob(); $toTmpFile = 'gitphp_' . $pid . '_to'; $tmpdir->AddFile($toTmpFile, $toBlob->GetData()); $toName = 'b/'; if (!empty($file)) { $toName .= $file; } else if (!empty($this->toFile)) { $toName .= $this->toFile; } else { $toName .= $this->toHash; } } $this->diffData = GitPHP_DiffExe::Diff((empty($fromTmpFile) ? null : escapeshellarg($tmpdir->GetDir() . $fromTmpFile)), $fromName, (empty($toTmpFile) ? null : escapeshellarg($tmpdir->GetDir() . $toTmpFile)), $toName); if (!empty($fromTmpFile)) { $tmpdir->RemoveFile($fromTmpFile); } if (!empty($toTmpFile)) { $tmpdir->RemoveFile($toTmpFile); } } if ($explode) return explode("\n", $this->diffData); else return $this->diffData; } /** * GetDiffSplit * * 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 * * @access public * @return an array of line elements (see above) */ public function GetDiffSplit() { if ($this->diffDataSplitRead) { return $this->diffDataSplit; } $this->diffDataSplitRead = true; $exe = new GitPHP_GitExe($this->project); $fromBlob = $this->GetFromBlob(); $blob = $fromBlob->GetData(true); $diffLines = ''; if (function_exists('xdiff_string_diff')) { $diffLines = explode("\n", $this->GetXDiff(0, false)); } else { $diffLines = explode("\n", $exe->Execute(GIT_DIFF, array("-U0", $this->fromHash, $this->toHash))); } unset($exe); // // parse diffs $diffs = array(); $currentDiff = FALSE; foreach($diffLines as $d) { if(strlen($d) == 0) continue; switch($d[0]) { case '@': if($currentDiff) { if (count($currentDiff['left']) == 0 && count($currentDiff['right']) > 0) $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) $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; } /** * GetXDiff * * Get diff using xdiff * * @access private * @param int $context number of context lines * @param boolean $header true to include standard diff header * @param string $file override the file name * @return string diff content */ private function GetXDiff($context = 3, $header = true, $file = null) { if (!function_exists('xdiff_string_diff')) return ''; $fromData = ''; $toData = ''; $isBinary = false; $fromName = '/dev/null'; $toName = '/dev/null'; if (empty($this->status) || ($this->status == 'M') || ($this->status == 'D')) { $fromBlob = $this->GetFromBlob(); $isBinary = $isBinary || $fromBlob->IsBinary(); $fromData = $fromBlob->GetData(false); $fromName = 'a/'; if (!empty($file)) { $fromName .= $file; } else if (!empty($this->fromFile)) { $fromName .= $this->fromFile; } else { $fromName .= $this->fromHash; } } if (empty($this->status) || ($this->status == 'M') || ($this->status == 'A')) { $toBlob = $this->GetToBlob(); $isBinary = $isBinary || $toBlob->IsBinary(); $toData = $toBlob->GetData(false); $toName = 'b/'; if (!empty($file)) { $toName .= $file; } else if (!empty($this->toFile)) { $toName .= $this->toFile; } else { $toName .= $this->toHash; } } $output = ''; if ($isBinary) { $output = sprintf(__('Binary files %1$s and %2$s differ'), $fromName, $toName) . "\n"; } else { if ($header) { $output = '--- ' . $fromName . "\n" . '+++ ' . $toName . "\n"; } $output .= xdiff_string_diff($fromData, $toData, $context); } return $output; } /** * GetCommit * * Gets the commit for this filediff * * @access public * @return commit object */ public function GetCommit() { return $this->commit; } /** * SetCommit * * Sets the commit for this filediff * * @access public * @param mixed $commit commit object */ public function SetCommit($commit) { $this->commit = $commit; } } |