75530840a3ba9e196fc92b4295662065bffa73fc
[phpgit.git] / library / Git / Repo.php
blob75530840a3ba9e196fc92b4295662065bffa73fc
1 <?php
3 /**
4 * Represents a Git repository, and allows access to history, snapshots,
5 * commits, et cetera.
6 */
7 class Git_Repo {
9 const DAEMON_EXPORT_FILE = 'git-daemon-export-ok';
11 /**
12 * Path to this repository.
14 public $path;
16 /**
17 * Whether or not this is a bare repository.
19 public $bare;
21 /**
22 * Interface to Git itself.
24 public $git;
26 /**
27 * Current working directory.
29 public $wd;
31 /**
32 * @param $path Path to the repository (either working copy or bare)
34 public function __construct($path = null) {
35 $path = $path ? $path : getcwd();
36 $path = realpath($path); // I believe PHP automatically expands ~
37 if ($path === false) {
38 throw new Git_Exception("No such path '$path'");
40 $curpath = $path;
41 while ($curpath) {
42 if ($this->isGitDir($curpath)) {
43 $this->bare = true;
44 $this->path = $this->wd = $curpath;
46 $gitpath = "$curpath/.git";
47 if ($this->isGitDir($gitpath)) {
48 $this->bare = false;
49 $this->path = realpath($gitpath);
50 $this->wd = $curpath;
51 break;
53 $temp = dirname($curpath);
54 if (!$temp || $temp == $curpath) break;
56 if (is_null($this->path)) throw new Git_Exception_InvalidRepository($path);
57 $this->git = new Git($this->wd);
60 /**
61 * Determines whether or not this is a Git directory.
62 * @note Taken from setup.c:is_git_directory
64 protected function isGitDir($d) {
65 if (is_dir($d) && is_dir("$d/objects") && is_dir("$d/refs")) {
66 $headref = "$d/HEAD";
67 return is_file($headref) ||
68 (is_link($headref) && strncmp(readlink($headref), 'refs', 4));
69 } else return false;
72 // * means will implement soon
74 // public function description() {
75 // * public function heads() {
76 // * public function branches() {return $this->heads();}
77 // public function tags() {
78 // * public function commits($start, $max_count, $skip) {
79 // public function commitsBetween($frm, $to) {
80 // public function commitsSince($start = 'master', $since) {
81 // public function commitCount($start = 'master') {
82 // * public function commit($id) {
83 // public function commitDeltasFrom($other_repo, $ref = 'master', $other_ref = 'master') {
84 /**
85 * Returns the Git_Tree object for a given treeish reference.
86 * @param $treeish Reference to retrieve, can be branch name, or filename
87 * @param $paths Optional array of directory paths to restrict to.
88 * @return Git_Tree for that path.
90 public function tree($treeish = 'master', $paths = array()) {
91 return Git_Tree::construct($this, $treeish, $paths);
93 // * public function blob($id) {
95 /**
96 * Returns the commit log for a treeish entity.
97 * @param $commit Commit to get log of, or commit range like begin..end
98 * @param $path Path (or paths) to get logs for
99 * @param $kwargs Extra arguments, see Git->transformArgs()
101 public function log($commit = 'master', $path = array(), $kwargs = array()) {
102 $options = array_merge(array('pretty' => 'raw'), $kwargs);
103 if ($path) {
104 $arg = array_merge(array($commit, '--'), $path);
105 } else {
106 $arg = array($commit);
108 // This call is kinda weird, I know, but otherwise we have
109 // to do user_func_call_array. Perhaps we should patch __call
110 // to work with numeric arrays? A thought...
111 $commits = $this->git->__call('log', array_merge($arg, array($options)));
112 return Git_Commit::listFromString($this, $commits);
115 // public function diff($a, $b, $paths = array()) {
116 // public function commitDiff($commit) {
117 // public static function initBare($path, $kwargs) {
118 // public static function forkBare($path, $kwargs) {
119 // public function archiveTar($treeish = 'master', $prefix = null) {
120 // public function archiveTarGz($treeish = 'master', $prefix = null) {
121 // public function enableDaemonServe() {
122 // public function disableDaemonServe() {
123 // ? private function _getAlternates() {
124 // ? private function _setAlternates() {
126 public function __toString() {
127 return '(Git_Repo "'. $this->path .'")';