+ $default_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD"));
+ $this->setConfig("defaultbranch", $default_branch);
+ $this->saveConfig();
+ }
+
+ private function getActiveBranch() {
+ if($this->activeSession)
+ return $this->activeSession;
+ else if(isset($_SESSION[self::SESSION_PREFIX.'branch']))
+ return $_SESSION[self::SESSION_PREFIX.'branch'];
+ else
+ return $this->getConfig("defaultbranch");
+ }
+
+ private function setActiveBranch($branch, $remember) {
+ $this->activeSession = $branch;
+ if($remember)
+ $_SESSION[self::SESSION_PREFIX.'branch'] = $branch;
+ }
+
+ private function getLocalUntrackedFiles() {
+ $default_branch = $this->getConfig("defaultbranch");
+ $tracked_files = $this->gitcmd("ls-tree", $default_branch, "--full-name", "--name-only");
+ $local_files = shell_exec("find ".escapeshellarg($this->localdir));
+ $untracked_files = array();
+
+ $tracked_files = explode("\n", str_replace(array("\r"), array(""), $tracked_files));
+ $local_files = explode("\n", str_replace(array("\r"), array(""), $local_files));
+
+ foreach($local_files as $local_file) {
+ if(!$local_file)
+ continue;
+ if($strip_local || (($strip_local = strlen($this->localdir)) && substr($local_file, 0, $strip_local) == $this->localdir)) {
+ $local_file = substr($local_file, $strip_local);
+ }
+ $tracked = false;
+ foreach($tracked_files as $tracked_file) {
+ if($tracked_file == $local_file) {
+ $tracked = true;
+ break;
+ }
+ }
+ if(!$tracked) {
+ $untracked_files[] = $local_file;
+ }
+ }
+ return $untracked_files;
+ }
+
+ private function branchExists($branch) {
+ //check if branch exists
+ $gitret = $this->gitcmd("rev-list", "--max-count=1", $branch);
+ if(!preg_match("#([a-z0-9]{40})#", $gitret, $match))
+ return false;
+ else
+ return $match[1];
+ }
+
+ private function localBranchPath($branch, $create = false) {
+ $default_branch = $this->getConfig("defaultbranch");
+ if($branch == $default_branch)
+ $dir = $this->localdir;
+ else
+ $dir = $this->workdir.'branch_'.str_replace(array('/'), array('_'), $branch).'/';
+ if(file_exists($dir))
+ return $dir;
+ else if($create) {
+ mkdir($dir);
+ return $dir;
+ } else
+ return false;
+ }
+
+ private function updateBranch($branch, $path, $force = false) {
+ if(substr($path, -1) != '/')
+ $path .= '/';
+ $current_branch = str_replace(array("\r", "\n"), array("", ""), $this->gitcmd("rev-parse", "--abbrev-ref", "HEAD"));
+ $this->gitcmd("fetch");
+ $this->gitcmd("reset", "--hard", "origin/".$branch);
+ $gitret = $this->gitcmd("rev-list", "--max-count=1", $branch);
+ preg_match("#([a-z0-9]{40})#", $gitret, $match);
+ $newest_version = $match[1];
+
+ $deleted_files = array();
+ if(($current_version = $this->getConfig('version_'.$branch))) {
+ if($current_version == $newest_version && !$force)
+ return;
+ else {
+ $override_all = true;
+ $delfiles = $this->gitcmd("diff", "--diff-filter=D", "--name-only", $current_version, $newest_version);
+ $delfiles = explode("\n", str_replace(array("\r"), array(""), $delfiles));
+ foreach($delfiles as $file) {
+ if(!$file)
+ continue;
+ $deleted_files[] = $file;
+ }
+ }
+ } else
+ $override_all = true;
+ if($override_all) {
+ $rsync_present = preg_match("#rsync#", `which rsync`);
+ if($rsync_present)
+ shell_exec('rsync -avz --exclude ".git" '.escapeshellarg($this->workdir."repository/").' '.escapeshellarg($path));
+ else
+ shell_exec('tar -c --exclude ".git" -C '.escapeshellarg($this->workdir."repository").' . | tar -x -C '.escapeshellarg($path));
+
+ // remove deleted files
+ foreach($deleted_files as $file) {
+ unlink($path.$file);
+ }
+ }
+ $this->setConfig('version_'.$branch, $newest_version);
+ $this->saveConfig();