Browse code

Merge pull request #356 from gza/master

Add contextual Readme display

Klaus Silveira authored on 02/09/2014 20:05:55
Showing 2 changed files
... ...
@@ -41,7 +41,7 @@ class TreeController implements ControllerProviderInterface
41 41
                 'breadcrumbs'    => $breadcrumbs,
42 42
                 'branches'       => $repository->getBranches(),
43 43
                 'tags'           => $repository->getTags(),
44
-                'readme'         => $app['util.repository']->getReadme($repository, $branch),
44
+                'readme'         => $app['util.repository']->getReadme($repository, $branch, $tree ? "$tree" : ""),
45 45
             ));
46 46
         })->assert('repo', $app['util.routing']->getRepositoryRegex())
47 47
           ->assert('commitishPath', $app['util.routing']->getCommitishPathRegex())
... ...
@@ -164,22 +164,26 @@ class Repository
164 164
         return false;
165 165
     }
166 166
 
167
-    public function getReadme($repository, $branch = null)
167
+    public function getReadme($repository, $branch = null, $path = "")
168 168
     {
169
-        $files = $repository->getTree($branch)->output();
170 169
         if ($branch === null) {
171 170
            $branch = $repository->getHead();
172 171
         }
173 172
 
173
+        if ($path != "") $path = "$path/";
174
+
175
+        $files = $repository->getTree($path != "" ? "$branch:$path" : $branch)->output();
176
+
174 177
         foreach ($files as $file) {
175 178
             if (preg_match('/^readme*/i', $file['name'])) {
176 179
                 return array(
177 180
                     'filename' => $file['name'],
178
-                    'content'  => $repository->getBlob("$branch:\"{$file['name']}\"")->output()
181
+                    'content'  => $repository->getBlob("$branch:\"$path{$file['name']}\"")->output()
179 182
                 );
180 183
             }
181 184
         }
182
-
185
+		// No contextual readme, try to catch the main one if we are in deeper context
186
+		if ($path != "") return $this->getReadme($repository, $branch, "");
183 187
         return array();
184 188
     }
185 189