envSet) { $root = escapeshellcmd($this->root); putenv("GIT_DIR=$root/.git"); $this->envSet = TRUE; } } /** * Invoke git to fetch a list of local branches in the repository, including * the SHA1 of the current branch tip and the branch name. */ public function fetchBranches() { $branches = array(); $data = array( 'repo_id' => $this->repo_id, 'action' => VERSIONCONTROL_ACTION_MODIFIED, 'label_id' => NULL, ); $logs = $this->exec('git show-ref --heads'); while (($branchdata = next($logs)) !== FALSE) { list($data['tip'], $data['name']) = explode(' ', trim($branchdata)); $data['name'] = substr($data['name'], 11); $branches[$data['name']] = new VersioncontrolBranch($this->backend); $branches[$data['name']]->build($data); } return $branches; } /** * Invoke git to fetch a list of local tags in the repository, including * the SHA1 of the commit to which the tag is attached. */ public function fetchTags() { $tags = array(); $data = array( 'repo_id' => $this->repo_id, 'action' => VERSIONCONTROL_ACTION_MODIFIED, 'label_id' => NULL, ); $logs = $this->exec('git show-ref --tags'); while (($tagdata = next($logs)) !== FALSE) { list($data['tip'], $data['name']) = explode(' ', trim($tagdata)); $data['name'] = substr($data['name'], 10); $tags[$data['name']] = new VersioncontrolTag(); $tags[$data['name']]->build($data); } return $tags; } public function fetchCommits($branch_name = NULL) { $logs = $this->exec('git rev-list ' . (empty($branch_name) ? '--all' : $branch_name)); $commits = array(); while (($line = next($logs)) !== FALSE) { $commits[] = trim($line); } return $commits; } /** * Execute a Git command using the root context and the command to be * executed. * * @param string $command * Command to execute. * @return mixed * Logged output from the command; an array of either strings or file * pointers. */ protected function exec($cmds) { if (!$this->envSet) { $this->setEnv(); } $logs = array(); exec($cmds, $logs); array_unshift($logs, ''); reset($logs); // Reset the array pointer, so that we can use next(). return $logs; } }