Use mode from parent directory to avoid world writable
[phorkie.git] / src / phorkie / Repositories.php
index dc26a171eebecafc575d0325bc8c78e353726f4c..e0a3985aa461a73fa4be139b5862857fb11744f6 100644 (file)
@@ -16,18 +16,23 @@ class Repositories
     {
         chdir($this->gitDir);
         $dirs = glob('*.git', GLOB_ONLYDIR);
-        array_walk($dirs, function ($dir) { return substr($dir, 0, -4); });
+        array_walk(
+            $dirs,
+            function ($dir) {
+                return substr($dir, 0, -4);
+            }
+        );
         sort($dirs, SORT_NUMERIC);
         $n = end($dirs) + 1;
 
         chdir($this->workDir);
         $dir = $this->workDir . '/' . $n . '/';
-        mkdir($dir, 0777);//FIXME
+        mkdir($dir, fileperms($this->workDir) & 0777);
         $r = new Repository();
         $r->id = $n;
         $r->workDir = $dir;
         $r->gitDir = $this->gitDir . '/' . $n . '.git/';
-        mkdir($r->gitDir, 0777);//FIXME
+        mkdir($r->gitDir, fileperms($this->gitDir) & 0777);
 
         return $r;
     }
@@ -35,7 +40,7 @@ class Repositories
     /**
      * Get a list of repository objects
      *
-     * @param integer $page    Page number, beginning with 0
+     * @param integer $page    Page number, beginning with 0, or "last"
      * @param integer $perPage Number of repositories per page
      *
      * @return array Array of Repositories first, number of repositories second
@@ -45,15 +50,34 @@ class Repositories
         chdir($this->gitDir);
         $dirs = glob('*.git', GLOB_ONLYDIR);
         sort($dirs, SORT_NUMERIC);
+        if ($page === 'last') {
+            //always show the last 10
+            $page = intval(count($dirs) / $perPage);
+            $start = count($dirs) - $perPage;
+            if ($start < 0) {
+                $start = 0;
+            }
+            $some = array_slice($dirs, $start, $perPage);
+        } else {
+            $some = array_slice($dirs, $page * $perPage, $perPage);
+        }
 
-        $some = array_slice($dirs, $page * $perPage, $perPage);
         $repos = array();
         foreach ($some as $oneDir) {
             $r = new Repository();
-            $r->loadById(substr($oneDir, 0, -4));
+            try {
+                $r->loadById(substr($oneDir, 0, -4));
+            } catch (\VersionControl_Git_Exception $e) {
+                if (strpos($e->getMessage(), 'does not have any commits') !== false) {
+                    //the git repo is broken as the initial commit
+                    // has not been finished
+                    continue;
+                }
+                throw $e;
+            }
             $repos[] = $r;
         }
-        return array($repos, count($dirs));
+        return array($repos, count($dirs), $page);
     }
 }