[Notice] Announcing the End of Demo Server [Read me]
Yi EungJun 2012-09-21
code: Add branch filter to the Code History page.
@fdd1b74ceff36a31d43cdb6217a1c64a6e23a600
app/controllers/CodeHistoryApp.java
--- app/controllers/CodeHistoryApp.java
+++ app/controllers/CodeHistoryApp.java
@@ -24,7 +24,13 @@
 
     private static final int HISTORY_ITEM_LIMIT = 25;
 
-    public static Result history(String userName, String projectName) throws IOException,
+    public static Result historyUntilHead(String userName, String projectName) throws IOException,
+            UnsupportedOperationException, ServletException, GitAPIException,
+            SVNException {
+        return history(userName, projectName, null);
+    }
+
+    public static Result history(String userName, String projectName, String branch) throws IOException,
             UnsupportedOperationException, ServletException, GitAPIException,
             SVNException {
         Project project = Project.findByName(projectName);
@@ -37,8 +43,8 @@
 
         try {
             List<Commit> commits = RepositoryService.getRepository(project).getHistory(page,
-                    HISTORY_ITEM_LIMIT);
-            return ok(history.render(url, project, commits, page));
+                    HISTORY_ITEM_LIMIT, branch);
+            return ok(history.render(url, project, commits, page, branch));
         } catch (NoHeadException e) {
             return ok(nohead.render(url, project));
         }
app/playRepository/GitRepository.java
--- app/playRepository/GitRepository.java
+++ app/playRepository/GitRepository.java
@@ -17,8 +17,6 @@
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.treewalk.filter.PathFilter;
 
-import com.google.common.collect.Lists;
-
 import play.Logger;
 import play.libs.Json;
 import utils.FileUtil;
@@ -178,13 +176,16 @@
     }
 
     @Override
-    public List<Commit> getHistory(int page, int limit) throws AmbiguousObjectException,
-            IOException, NoHeadException, GitAPIException {
+    public List<Commit> getHistory(int page, int limit, String until)
+            throws AmbiguousObjectException, IOException, NoHeadException, GitAPIException {
         // Get the list of commits from HEAD to the given page.
-        Iterable<RevCommit> iter = new Git(repository).log()
-                .setMaxCount(page * limit + limit).call();
+        LogCommand logCommand = new Git(repository).log();
+        if (until != null) {
+            logCommand.add(repository.resolve(until));
+        }
+        Iterable<RevCommit> iter = logCommand.setMaxCount(page * limit + limit).call();
         List<RevCommit> list = new LinkedList<RevCommit>();
-        for(RevCommit commit : iter) {
+        for (RevCommit commit : iter) {
             if (list.size() >= limit) {
                 list.remove(0);
             }
@@ -192,11 +193,16 @@
         }
 
         List<Commit> result = new ArrayList<Commit>();
-        for(RevCommit commit : list) {
+        for (RevCommit commit : list) {
             result.add(new GitCommit(commit));
         }
 
         return result;
     }
 
-}
+    @Override
+    public List<String> getBranches() {
+        return new ArrayList<String>(repository.getAllRefs().keySet());
+    }
+
+}
(No newline at end of file)
app/playRepository/PlayRepository.java
--- app/playRepository/PlayRepository.java
+++ app/playRepository/PlayRepository.java
@@ -25,7 +25,9 @@
     public String getPatch(String commitId) throws GitAPIException, MissingObjectException,
             IncorrectObjectTypeException, IOException, SVNException;
 
-    public List<Commit> getHistory(int page, int limit) throws AmbiguousObjectException,
+    public List<Commit> getHistory(int page, int limit, String branch) throws AmbiguousObjectException,
             IOException, NoHeadException, GitAPIException, SVNException;
 
+    public List<String> getBranches();
+
 }
(No newline at end of file)
app/playRepository/SVNRepository.java
--- app/playRepository/SVNRepository.java
+++ app/playRepository/SVNRepository.java
@@ -131,7 +131,7 @@
     }
 
     @Override
-    public List<Commit> getHistory(int page, int limit) throws AmbiguousObjectException,
+    public List<Commit> getHistory(int page, int limit, String until) throws AmbiguousObjectException,
             IOException, NoHeadException, GitAPIException, SVNException {
         // Get the repository
         SVNURL svnURL = SVNURL.fromFile(new File(repoPrefix + userName + "/" + projectName));
@@ -144,7 +144,7 @@
         long startRevision = repository.getLatestRevision();
         long endRevision = startRevision - limit;
         if (endRevision < 1) {
-            endRevision = 1;
+            return new ArrayList<Commit>();
         }
 
         // Get the logs
@@ -156,4 +156,8 @@
         return result;
     }
 
-}
+    @Override
+    public List<String> getBranches() {
+        return new ArrayList<String>();
+    }
+}
(No newline at end of file)
app/views/code/diff.scala.html
--- app/views/code/diff.scala.html
+++ app/views/code/diff.scala.html
@@ -11,7 +11,6 @@
     <pre><code>@patch</code></pre>
 </div>
 <script type="text/javascript">
-  var fn = nforge.require('code.diff');
   $("code").highlight('diff');
 </script>
 }
app/views/code/gitView.scala.html
--- app/views/code/gitView.scala.html
+++ app/views/code/gitView.scala.html
@@ -1,7 +1,7 @@
 @(url : String, project:Project) @main("코드", project){
 <ul class="nav nav-tabs">
     <a href="@routes.CodeApp.codeBrowser(project.owner, project.name)">@Messages("Files")</a></li>
-    <a href="@routes.CodeHistoryApp.history(project.owner, project.name)">@Messages("Commits")</a></li>
+    <a href="@routes.CodeHistoryApp.historyUntilHead(project.owner, project.name)">@Messages("Commits")</a></li>
 </ul>
 
 <div class="well">Clone this repository : git clone @url</div>
app/views/code/history.scala.html
--- app/views/code/history.scala.html
+++ app/views/code/history.scala.html
@@ -1,33 +1,54 @@
-@(url: String, project: Project, history: List[playRepository.Commit], page: Integer)
+@(url: String, project: Project, history: List[playRepository.Commit], page: Integer, selectedBranch: String)
+
+@import playRepository.RepositoryService
+@import java.net.URLEncoder
 
 @main(Messages("Commit History"), project) {
 
+@defining(RepositoryService.getRepository(project).getBranches()) { branches =>
+    @if(branches.length > 0) {
+        <select id="branch" name="branch">
+            @branches.map { branch =>
+                @defining(routes.CodeHistoryApp.history(project.owner, project.name, URLEncoder.encode(branch, "UTF-8"))) { url =>
+                    <option @(if(branch == selectedBranch) "selected" else "") value="@url" >@branch</option>
+                }
+            }
+        </select>
+    }
+}
+
 <ul class="nav nav-tabs">
     <a href="@routes.CodeApp.codeBrowser(project.owner, project.name)">@Messages("Files")</a></li>
-    <a href="@routes.CodeHistoryApp.history(project.owner, project.name)">@Messages("Commits")</a></li>
+    <a href="@routes.CodeHistoryApp.history(project.owner, project.name, selectedBranch)">@Messages("Commits")</a></li>
 </ul>
 
-<div class="row" id="history">
-    <table class="table">
-        <tbody>
-        @for(commit <- history.iterator()) {
-            <tr>
-                <td>@commit.getAuthorName()</td>
-                <td><pre>@commit.getMessage()</pre></td>
-                <td>@commit.getAuthorDate()</td>
-                <td><a href="@routes.CodeHistoryApp.show(project.owner, project.name, commit.getId())">@commit.getShortId()</a></td>
-            </tr>
-        }
-        </tbody>
-    </table>
-    <ul class="pager">
-        @if(page > 0) {
-        <li><a href="@routes.CodeHistoryApp.history(project.owner, project.name)?page=@(page - 1)">@Messages("Newer")</a></li>
-        }
+@if(history.size() > 0) {
+    <div class="row" id="history">
+        <table class="table">
+            <tbody>
+            @for(commit <- history.iterator()) {
+                <tr>
+                    <td>@commit.getAuthorName()</td>
+                    <td><pre>@commit.getMessage()</pre></td>
+                    <td>@commit.getAuthorDate()</td>
+                    <td><a href="@routes.CodeHistoryApp.show(project.owner, project.name, commit.getId())">@commit.getShortId()</a></td>
+                </tr>
+            }
+            </tbody>
+        </table>
+        <ul class="pager">
+            @if(page > 0) {
+            <li><a href="@routes.CodeHistoryApp.history(project.owner, project.name, selectedBranch)?page=@(page - 1)">@Messages("Newer")</a></li>
+            }
 
-        @if(history.get(history.size() - 1).getParentCount() > 0) {
-        <li><a href="@routes.CodeHistoryApp.history(project.owner, project.name)?page=@(page + 1)">@Messages("Older")</a></li>
-        }
-        </ul>
-</div>
+            @if(history.get(history.size() - 1).getParentCount() > 0) {
+            <li><a href="@routes.CodeHistoryApp.history(project.owner, project.name, selectedBranch)?page=@(page + 1)">@Messages("Older")</a></li>
+            }
+            </ul>
+    </div>
+    <script type="text/javascript">
+    nforge.require('code.branch');
+    </script>
+}
+
 }
app/views/code/svnView.scala.html
--- app/views/code/svnView.scala.html
+++ app/views/code/svnView.scala.html
@@ -1,7 +1,7 @@
 @(url : String, project:Project) @main("코드", project){
 <ul class="nav nav-tabs">
     <a href="@routes.CodeApp.codeBrowser(project.owner, project.name)">@Messages("Files")</a></li>
-    <a href="@routes.CodeHistoryApp.history(project.owner, project.name)">@Messages("Commits")</a></li>
+    <a href="@routes.CodeHistoryApp.history(project.owner, project.name, null)">@Messages("Commits")</a></li>
 </ul>
 
 <div class="well">Checkout this repository : svn checkout @url</div>
conf/routes
--- conf/routes
+++ conf/routes
@@ -96,7 +96,8 @@
 GET     /:user/:projectName/code/*path                  controllers.CodeApp.showRawFile(user:String, projectName:String, path:String)
 
 # Commits
-GET     /:user/:projectName/commits                     controllers.CodeHistoryApp.history(user:String, projectName:String)
+GET     /:user/:projectName/commits                     controllers.CodeHistoryApp.historyUntilHead(user:String, projectName:String)
+GET     /:user/:projectName/commits/:branch             controllers.CodeHistoryApp.history(user:String, projectName:String, branch:String)
 GET     /:user/:projectName/commit/:id                  controllers.CodeHistoryApp.show(user:String, projectName:String, id:String)
 
 # Search
public/javascripts/modules/code.js
--- public/javascripts/modules/code.js
+++ public/javascripts/modules/code.js
@@ -1,3 +1,12 @@
 nforge.namespace('code');
-nforge.code.diff = function () {
+nforge.code.branch = function () {
+    return {
+        init: function() {
+            $('#branch').click(this.update);
+        },
+
+        update: function() {
+            window.location.replace($('#branch').val());
+        }
+    };
 };
test/playRepository/GitRepositoryTest.java
--- test/playRepository/GitRepositoryTest.java
+++ test/playRepository/GitRepositoryTest.java
@@ -128,8 +128,8 @@
 
         GitRepository gitRepo = new GitRepository(userName, projectName + "/");
 
-        List<Commit> history2 = gitRepo.getHistory(0, 2);
-        List<Commit> history5 = gitRepo.getHistory(0, 5);
+        List<Commit> history2 = gitRepo.getHistory(0, 2, "HEAD");
+        List<Commit> history5 = gitRepo.getHistory(0, 5, "HEAD");
 
         // then
         assertThat(history2.size()).isEqualTo(2);
Add a comment
List