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 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 | <?php /** * GitPHP Project * * Represents a single git project * * @author Christopher Han <xiphux@gmail.com> * @copyright Copyright (c) 2010 Christopher Han * @package GitPHP * @subpackage Git */ require_once(GITPHP_GITOBJECTDIR . 'GitExe.class.php'); require_once(GITPHP_GITOBJECTDIR . 'Commit.class.php'); require_once(GITPHP_GITOBJECTDIR . 'Head.class.php'); require_once(GITPHP_GITOBJECTDIR . 'Tag.class.php'); /** * Project class * * @package GitPHP * @subpackage Git */ class GitPHP_Project { /** * project * * Stores the project internally * * @access protected */ protected $project; /** * owner * * Stores the owner internally * * @access protected */ protected $owner = ""; /** * readOwner * * Stores whether the file owner has been read * * @access protected */ protected $readOwner = false; /** * description * * Stores the description internally * * @access protected */ protected $description; /** * readDescription * * Stores whether the description has been * read from the file yet * * @access protected */ protected $readDescription = false; /** * category * * Stores the category internally * * @access protected */ protected $category = ''; /** * epoch * * Stores the project epoch internally * * @access protected */ protected $epoch; /** * epochRead * * Stores whether the project epoch has been read yet * * @access protected */ protected $epochRead = false; /** * head * * Stores the head hash internally * * @access protected */ protected $head; /** * readHeadRef * * Stores whether the head ref has been read yet * * @access protected */ protected $readHeadRef = false; /** * refs * * Stores the refs for the project * * @access protected */ protected $refs = array(); /** * readRefs * * Stores whether refs have been read yet * * @access protected */ protected $readRefs = false; /** * heads * * Stores the heads for the project * * @access protected */ protected $heads = array(); /** * readHeads * * Stores whether heads have been read yet * * @access protected */ protected $readHeads = false; /** * tags * * Stores the tags for the project * * @access protected */ protected $tags = array(); /** * readTags * * Stores whether tags have been read yet * * @access protected */ protected $readTags = false; /** * cloneUrl * * Stores the clone url internally * * @access protected */ protected $cloneUrl = null; /** * pushUrl * * Stores the push url internally * * @access protected */ protected $pushUrl = null; /** * bugUrl * * Stores the bug url internally * * @access protected */ protected $bugUrl = null; /** * bugPattern * * Stores the bug pattern internally * * @access protected */ protected $bugPattern = null; /** * commitCache * * Caches fetched commit objects in case of * repeated requests for the same object * * @access protected */ protected $commitCache = array(); /** * __construct * * Class constructor * * @access public * @throws Exception if project is invalid or outside of projectroot */ public function __construct($project) { $this->SetProject($project); } /** * SetProject * * Attempts to set the project * * @access private * @throws Exception if project is invalid or outside of projectroot */ private function SetProject($project) { $projectRoot = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('projectroot')); $realProjectRoot = realpath($projectRoot); $path = $projectRoot . $project; $fullPath = realpath($path); if (!is_dir($fullPath)) { throw new Exception(sprintf(__('%1$s is not a directory'), $project)); } if (!is_file($fullPath . '/HEAD')) { throw new Exception(sprintf(__('%1$s is not a git repository'), $project)); } if (preg_match('/(^|\/)\.{0,2}(\/|$)/', $project)) { throw new Exception(sprintf(__('%1$s is attempting directory traversal'), $project)); } $pathPiece = substr($fullPath, 0, strlen($realProjectRoot)); if ((!is_link($path)) && (strcmp($pathPiece, $realProjectRoot) !== 0)) { throw new Exception(sprintf(__('%1$s is outside of the projectroot'), $project)); } $this->project = $project; } /** * GetOwner * * Gets the project's owner * * @access public * @return string project owner */ public function GetOwner() { if (empty($this->owner) && !$this->readOwner) { $exe = new GitPHP_GitExe($this); $args = array(); $args[] = 'gitweb.owner'; $this->owner = $exe->Execute(GIT_CONFIG, $args); unset($exe); if (empty($this->owner) && function_exists('posix_getpwuid')) { $uid = fileowner($this->GetPath()); if ($uid !== false) { $data = posix_getpwuid($uid); if (isset($data['gecos']) && !empty($data['gecos'])) { $this->owner = $data['gecos']; } elseif (isset($data['name']) && !empty($data['name'])) { $this->owner = $data['name']; } } } $this->readOwner = true; } return $this->owner; } /** * SetOwner * * Sets the project's owner (from an external source) * * @access public * @param string $owner the owner */ public function SetOwner($owner) { $this->owner = $owner; } /** * GetProject * * Gets the project * * @access public * @return string the project */ public function GetProject() { return $this->project; } /** * GetSlug * * Gets the project as a filename/url friendly slug * * @access public * @return string the slug */ public function GetSlug() { $from = array( '/', '.git' ); $to = array( '-', '' ); return str_replace($from, $to, $this->project); } /** * GetPath * * Gets the full project path * * @access public * @return string project path */ public function GetPath() { $projectRoot = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('projectroot')); return $projectRoot . $this->project; } /** * GetDescription * * Gets the project description * * @access public * @param $trim length to trim description to (0 for no trim) * @return string project description */ public function GetDescription($trim = 0) { if (!$this->readDescription) { $this->description = file_get_contents($this->GetPath() . '/description'); } if (($trim > 0) && (strlen($this->description) > $trim)) { return substr($this->description, 0, $trim) . '…'; } return $this->description; } /** * SetDescription * * Overrides the project description * * @access public * @param string $descr description */ public function SetDescription($descr) { $this->description = $descr; $this->readDescription = true; } /** * GetDaemonEnabled * * Returns whether gitdaemon is allowed for this project * * @access public * @return boolean git-daemon-export-ok? */ public function GetDaemonEnabled() { return file_exists($this->GetPath() . '/git-daemon-export-ok'); } /** * GetCategory * * Gets the project's category * * @access public * @return string category */ public function GetCategory() { return $this->category; } /** * SetCategory * * Sets the project's category * * @access public * @param string $category category */ public function SetCategory($category) { $this->category = $category; } /** * GetCloneUrl * * Gets the clone URL for this repository, if specified * * @access public * @return string clone url */ public function GetCloneUrl() { if ($this->cloneUrl !== null) return $this->cloneUrl; $cloneurl = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('cloneurl', ''), false); if (!empty($cloneurl)) $cloneurl .= $this->project; return $cloneurl; } /** * SetCloneUrl * * Overrides the clone URL for this repository * * @access public * @param string $cUrl clone url */ public function SetCloneUrl($cUrl) { $this->cloneUrl = $cUrl; } /** * GetPushUrl * * Gets the push URL for this repository, if specified * * @access public * @return string push url */ public function GetPushUrl() { if ($this->pushUrl !== null) return $this->pushUrl; $pushurl = GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('pushurl', ''), false); if (!empty($pushurl)) $pushurl .= $this->project; return $pushurl; } /** * SetPushUrl * * Overrides the push URL for this repository * * @access public * @param string $pUrl push url */ public function SetPushUrl($pUrl) { $this->pushUrl = $pUrl; } /** * GetBugUrl * * Gets the bug URL for this repository, if specified * * @access public * @return string bug url */ public function GetBugUrl() { if ($this->bugUrl != null) return $this->bugUrl; return GitPHP_Config::GetInstance()->GetValue('bugurl', ''); } /** * SetBugUrl * * Overrides the bug URL for this repository * * @access public * @param string $bUrl bug url */ public function SetBugUrl($bUrl) { $this->bugUrl = $bUrl; } /** * GetBugPattern * * Gets the bug pattern for this repository, if specified * * @access public * @return string bug pattern */ public function GetBugPattern() { if ($this->bugPattern != null) return $this->bugPattern; return GitPHP_Config::GetInstance()->GetValue('bugpattern', ''); } /** * SetBugPattern * * Overrides the bug pattern for this repository * * @access public * @param string $bPat bug pattern */ public function SetBugPattern($bPat) { $this->bugPattern = $bPat; } /** * GetHeadCommit * * Gets the head commit for this project * Shortcut for getting the tip commit of the HEAD branch * * @access public * @return mixed head commit */ public function GetHeadCommit() { if (!$this->readHeadRef) $this->ReadHeadCommit(); return $this->GetCommit($this->head); } /** * ReadHeadCommit * * Reads the head commit hash * * @access protected */ public function ReadHeadCommit() { $this->readHeadRef = true; $exe = new GitPHP_GitExe($this); $args = array(); $args[] = '--verify'; $args[] = 'HEAD'; $this->head = trim($exe->Execute(GIT_REV_PARSE, $args)); } /** * GetCommit * * Get a commit for this project * * @access public */ public function GetCommit($hash) { if (empty($hash)) return null; if ($hash === 'HEAD') return $this->GetHeadCommit(); if (substr_compare($hash, 'refs/heads/', 0, 11) === 0) { $head = $this->GetHead(substr($hash, 11)); if ($head != null) return $head->GetCommit(); } else if (substr_compare($hash, 'refs/tags/', 0, 10) === 0) { $tag = $this->GetTag(substr($hash, 10)); if ($tag != null) { $obj = $tag->GetObject(); if ($obj != null) { return $obj; } } } if (!isset($this->commitCache[$hash])) { $cacheKey = 'project|' . $this->project . '|commit|' . $hash; $cached = GitPHP_Cache::GetInstance()->Get($cacheKey); if ($cached) $this->commitCache[$hash] = $cached; else $this->commitCache[$hash] = new GitPHP_Commit($this, $hash); } return $this->commitCache[$hash]; } /** * CompareProject * * Compares two projects by project name * * @access public * @static * @param mixed $a first project * @param mixed $b second project * @return integer comparison result */ public static function CompareProject($a, $b) { $catCmp = strcmp($a->GetCategory(), $b->GetCategory()); if ($catCmp !== 0) return $catCmp; return strcmp($a->GetProject(), $b->GetProject()); } /** * CompareDescription * * Compares two projects by description * * @access public * @static * @param mixed $a first project * @param mixed $b second project * @return integer comparison result */ public static function CompareDescription($a, $b) { $catCmp = strcmp($a->GetCategory(), $b->GetCategory()); if ($catCmp !== 0) return $catCmp; return strcmp($a->GetDescription(), $b->GetDescription()); } /** * CompareOwner * * Compares two projects by owner * * @access public * @static * @param mixed $a first project * @param mixed $b second project * @return integer comparison result */ public static function CompareOwner($a, $b) { $catCmp = strcmp($a->GetCategory(), $b->GetCategory()); if ($catCmp !== 0) return $catCmp; return strcmp($a->GetOwner(), $b->GetOwner()); } /** * CompareAge * * Compares two projects by age * * @access public * @static * @param mixed $a first project * @param mixed $b second project * @return integer comparison result */ public static function CompareAge($a, $b) { $catCmp = strcmp($a->GetCategory(), $b->GetCategory()); if ($catCmp !== 0) return $catCmp; if ($a->GetAge() === $b->GetAge()) return 0; return ($a->GetAge() < $b->GetAge() ? -1 : 1); } /** * GetRefs * * Gets the list of refs for the project * * @access public * @return array array of refs */ public function GetRefs() { if (!$this->readRefs) $this->ReadRefList(); return $this->refs; } /** * ReadRefList * * Reads the list of refs for this project * * @access protected */ public function ReadRefList() { $this->readRefs = true; $exe = new GitPHP_GitExe($this); $args = array(); $args[] = '--heads'; $args[] = '--tags'; $args[] = '--dereference'; $ret = $exe->Execute(GIT_SHOW_REF, $args); unset($exe); $lines = explode("\n", $ret); foreach ($lines as $line) { if (preg_match('/^([0-9a-fA-F]{40}) refs\/(tags|heads)\/([^^]+)(\^{})?$/', $line, $regs)) { try { $key = 'refs/' . $regs[2] . '/' . $regs[3]; if ($regs[2] == 'tags') { if ((!empty($regs[4])) && ($regs[4] == '^{}')) { $derefCommit = $this->GetCommit($regs[1]); if ($derefCommit && isset($this->refs[$key])) { $this->refs[$key]->SetCommit($derefCommit); } } else if (!isset($this->refs[$key])) { $this->refs[$key] = $this->LoadTag($regs[3], $regs[1]); } } else if ($regs[2] == 'heads') { $this->refs[$key] = new GitPHP_Head($this, $regs[3], $regs[1]); } } catch (Exception $e) { } } } } /** * GetTags * * Gets list of tags for this project * * @access public * @return array array of tags */ public function GetTags() { if (!$this->readTags) $this->ReadTagList(); return $this->tags; } /** * GetTag * * Gets a single tag * * @access public * @param string $tag tag to find * @return mixed tag object */ public function GetTag($tag) { if (empty($tag)) return null; if (!isset($this->tags[$tag])) { $this->tags[$tag] = $this->LoadTag($tag); } return $this->tags[$tag]; } /** * LoadTag * * Attempts to load a cached tag, or creates a new object * * @access private * @param string $tag tag to find * @return mixed tag object */ private function LoadTag($tag, $hash = '') { if (empty($tag)) return; $cacheKey = 'project|' . $this->project . '|tag|' . $tag; $cached = GitPHP_Cache::GetInstance()->Get($cacheKey); if ($cached) { return $cached; } else { return new GitPHP_Tag($this, $tag, $hash); } } /** * ReadTagList * * Reads tag list * * @access protected */ protected function ReadTagList() { $this->readTags = true; $exe = new GitPHP_GitExe($this); $args = array(); $args[] = '--tags'; $args[] = '--dereference'; $ret = $exe->Execute(GIT_SHOW_REF, $args); unset($exe); $lines = explode("\n", $ret); foreach ($lines as $line) { if (preg_match('/^([0-9a-fA-F]{40}) refs\/tags\/([^^]+)(\^{})?$/', $line, $regs)) { try { if ((!empty($regs[3])) && ($regs[3] == '^{}')) { $derefCommit = $this->GetCommit($regs[1]); if ($derefCommit && isset($this->tags[$regs[2]])) { $this->tags[$regs[2]]->SetCommit($derefCommit); } } else if (!isset($this->tags[$regs[2]])) { $this->tags[$regs[2]] = $this->LoadTag($regs[2], $regs[1]); } } catch (Exception $e) { } } } uasort($this->tags, array('GitPHP_Tag', 'CompareAge')); } /** * GetHeads * * Gets list of heads for this project * * @access public * @return array array of heads */ public function GetHeads() { if (!$this->readHeads) $this->ReadHeadList(); return $this->heads; } /** * GetHead * * Gets a single head * * @access public * @param string $head head to find * @return mixed head object */ public function GetHead($head) { if (empty($head)) return null; if (!$this->readHeads) $this->ReadHeadList(); foreach ($this->heads as $h) { if ($h->GetName() === $head) { return $h; } } return null; } /** * ReadHeadList * * Reads head list * * @access protected */ protected function ReadHeadList() { $this->readHeads = true; $exe = new GitPHP_GitExe($this); $args = array(); $args[] = '--heads'; $ret = $exe->Execute(GIT_SHOW_REF, $args); unset($exe); $lines = explode("\n", $ret); foreach ($lines as $line) { if (preg_match('/^([0-9a-fA-F]{40}) refs\/heads\/(.+)$/', $line, $regs)) { try { $this->heads[] = new GitPHP_Head($this, $regs[2], $regs[1]); } catch (Exception $e) { } } } usort($this->heads, array('GitPHP_Head', 'CompareAge')); } /** * GetLogHash * * Gets log entries as an array of hashes * * @access public * @param string $hash hash to start the log at * @param integer $count number of entries to get * @param integer $skip number of entries to skip * @return array array of hashes */ public function GetLogHash($hash, $count = 50, $skip = 0) { return $this->RevList($hash, $count, $skip); } /** * GetLog * * Gets log entries as an array of commit objects * * @access public * @param string $hash hash to start the log at * @param integer $count number of entries to get * @param integer $skip number of entries to skip * @return array array of commit objects */ public function GetLog($hash, $count = 50, $skip = 0) { $log = $this->GetLogHash($hash, $count, $skip); $len = count($log); for ($i = 0; $i < $len; ++$i) { $log[$i] = $this->GetCommit($log[$i]); } return $log; } /** * GetBlob * * Gets a blob from this project * * @access public * @param string $hash blob hash */ public function GetBlob($hash) { if (empty($hash)) return null; $cacheKey = 'project|' . $this->project . '|blob|' . $hash; $cached = GitPHP_Cache::GetInstance()->Get($cacheKey); if ($cached) return $cached; return new GitPHP_Blob($this, $hash); } /** * GetTree * * Gets a tree from this project * * @access public * @param string $hash tree hash */ public function GetTree($hash) { if (empty($hash)) return null; $cacheKey = 'project|' . $this->project . '|tree|' . $hash; $cached = GitPHP_Cache::GetInstance()->Get($cacheKey); if ($cached) return $cached; return new GitPHP_Tree($this, $hash); } /** * SearchCommit * * Gets a list of commits with commit messages matching the given pattern * * @access public * @param string $pattern search pattern * @param string $hash hash to start searching from * @param integer $count number of results to get * @param integer $skip number of results to skip * @return array array of matching commits */ public function SearchCommit($pattern, $hash = 'HEAD', $count = 50, $skip = 0) { if (empty($pattern)) return; $args = array(); $exe = new GitPHP_GitExe($this); if ($exe->CanIgnoreRegexpCase()) $args[] = '--regexp-ignore-case'; unset($exe); $args[] = '--grep=\'' . $pattern . '\''; $ret = $this->RevList($hash, $count, $skip, $args); $len = count($ret); for ($i = 0; $i < $len; ++$i) { $ret[$i] = $this->GetCommit($ret[$i]); } return $ret; } /** * SearchAuthor * * Gets a list of commits with authors matching the given pattern * * @access public * @param string $pattern search pattern * @param string $hash hash to start searching from * @param integer $count number of results to get * @param integer $skip number of results to skip * @return array array of matching commits */ public function SearchAuthor($pattern, $hash = 'HEAD', $count = 50, $skip = 0) { if (empty($pattern)) return; $args = array(); $exe = new GitPHP_GitExe($this); if ($exe->CanIgnoreRegexpCase()) $args[] = '--regexp-ignore-case'; unset($exe); $args[] = '--author=\'' . $pattern . '\''; $ret = $this->RevList($hash, $count, $skip, $args); $len = count($ret); for ($i = 0; $i < $len; ++$i) { $ret[$i] = $this->GetCommit($ret[$i]); } return $ret; } /** * SearchCommitter * * Gets a list of commits with committers matching the given pattern * * @access public * @param string $pattern search pattern * @param string $hash hash to start searching from * @param integer $count number of results to get * @param integer $skip number of results to skip * @return array array of matching commits */ public function SearchCommitter($pattern, $hash = 'HEAD', $count = 50, $skip = 0) { if (empty($pattern)) return; $args = array(); $exe = new GitPHP_GitExe($this); if ($exe->CanIgnoreRegexpCase()) $args[] = '--regexp-ignore-case'; unset($exe); $args[] = '--committer=\'' . $pattern . '\''; $ret = $this->RevList($hash, $count, $skip, $args); $len = count($ret); for ($i = 0; $i < $len; ++$i) { $ret[$i] = $this->GetCommit($ret[$i]); } return $ret; } /** * RevList * * Common code for using rev-list command * * @access private * @param string $hash hash to list from * @param integer $count number of results to get * @param integer $skip number of results to skip * @param array $args args to give to rev-list * @return array array of hashes */ private function RevList($hash, $count = 50, $skip = 0, $args = array()) { if ($count < 1) return; $exe = new GitPHP_GitExe($this); $canSkip = true; if ($skip > 0) $canSkip = $exe->CanSkip(); if ($canSkip) { $args[] = '--max-count=' . $count; if ($skip > 0) { $args[] = '--skip=' . $skip; } } else { $args[] = '--max-count=' . ($count + $skip); } $args[] = $hash; $revlist = explode("\n", $exe->Execute(GIT_REV_LIST, $args)); if (!$revlist[count($revlist)-1]) { /* the last newline creates a null entry */ array_splice($revlist, -1, 1); } if (($skip > 0) && (!$exe->CanSkip())) { return array_slice($revlist, $skip, $count); } return $revlist; } /** * GetEpoch * * Gets this project's epoch * (time of last change) * * @access public * @return integer timestamp */ public function GetEpoch() { if (!$this->epochRead) $this->ReadEpoch(); return $this->epoch; } /** * GetAge * * Gets this project's age * (time since most recent change) * * @access public * @return integer age */ public function GetAge() { if (!$this->epochRead) $this->ReadEpoch(); return time() - $this->epoch; } /** * ReadEpoch * * Reads this project's epoch * (timestamp of most recent change) * * @access private */ private function ReadEpoch() { $this->epochRead = true; $exe = new GitPHP_GitExe($this); $args = array(); $args[] = '--format="%(committer)"'; $args[] = '--sort=-committerdate'; $args[] = '--count=1'; $args[] = 'refs/heads'; $epochstr = trim($exe->Execute(GIT_FOR_EACH_REF, $args)); if (preg_match('/ (\d+) [-+][01]\d\d\d$/', $epochstr, $regs)) { $this->epoch = $regs[1]; } unset($exe); } } |