]> source.dussan.org Git - gitblit.git/commitdiff
Improve pushlog api and model class
authorJames Moger <james.moger@gitblit.com>
Sun, 6 Jan 2013 16:02:52 +0000 (11:02 -0500)
committerJames Moger <james.moger@gitblit.com>
Sun, 6 Jan 2013 16:02:52 +0000 (11:02 -0500)
src/com/gitblit/models/PushLogEntry.java
src/com/gitblit/models/RepositoryCommit.java
src/com/gitblit/utils/PushLogUtils.java

index 32a7d0073fe2c3a8977aa1cfa3aabcbbf1aa374a..f625c2a3db81dd7eef62df1fee144e12fe5e5332 100644 (file)
@@ -20,13 +20,16 @@ import java.text.MessageFormat;
 import java.util.ArrayList;\r
 import java.util.Collections;\r
 import java.util.Date;\r
+import java.util.HashMap;\r
 import java.util.HashSet;\r
 import java.util.LinkedHashSet;\r
 import java.util.List;\r
+import java.util.Map;\r
 import java.util.Set;\r
 \r
 import org.eclipse.jgit.lib.Constants;\r
 import org.eclipse.jgit.revwalk.RevCommit;\r
+import org.eclipse.jgit.transport.ReceiveCommand;\r
 \r
 /**\r
  * Model class to represent a push into a repository.\r
@@ -44,6 +47,8 @@ public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
        public final UserModel user;\r
 \r
        private final Set<RepositoryCommit> commits;\r
+       \r
+       private final Map<String, ReceiveCommand.Type> refUpdates;\r
 \r
        /**\r
         * Constructor for specified duration of push from start date.\r
@@ -60,6 +65,19 @@ public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
                this.date = date;\r
                this.user = user;\r
                this.commits = new LinkedHashSet<RepositoryCommit>();\r
+               this.refUpdates = new HashMap<String, ReceiveCommand.Type>();\r
+       }\r
+       \r
+       /**\r
+        * Tracks the change type for the specified ref.\r
+        * \r
+        * @param ref\r
+        * @param type\r
+        */\r
+       public void updateRef(String ref, ReceiveCommand.Type type) {\r
+               if (!refUpdates.containsKey(ref)) {\r
+                       refUpdates.put(ref, type);\r
+               }\r
        }\r
 \r
        /**\r
@@ -79,6 +97,20 @@ public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
                return null;\r
        }\r
        \r
+       /**\r
+        * Returns true if this push contains a non-fastforward ref update.\r
+        * \r
+        * @return true if this is a non-fastforward push\r
+        */\r
+       public boolean isNonFastForward() {\r
+               for (Map.Entry<String, ReceiveCommand.Type> entry : refUpdates.entrySet()) {\r
+                       if (ReceiveCommand.Type.UPDATE_NONFASTFORWARD.equals(entry.getValue())) {\r
+                               return true;\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+       \r
        /**\r
         * Returns the list of branches changed by the push.\r
         * \r
@@ -105,9 +137,9 @@ public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
         */\r
        protected List<String> getChangedRefs(String baseRef) {\r
                Set<String> refs = new HashSet<String>();\r
-               for (RepositoryCommit commit : commits) {\r
-                       if (baseRef == null || commit.branch.startsWith(baseRef)) {\r
-                               refs.add(commit.branch);\r
+               for (String ref : refUpdates.keySet()) {\r
+                       if (baseRef == null || ref.startsWith(baseRef)) {\r
+                               refs.add(ref);\r
                        }\r
                }\r
                List<String> list = new ArrayList<String>(refs);\r
@@ -160,7 +192,17 @@ public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
        \r
        @Override\r
        public String toString() {\r
-               return MessageFormat.format("{0,date,yyyy-MM-dd HH:mm}: {1} pushed {2,number,0} commit{3} to {4} ",\r
-                               date, user.getDisplayName(), commits.size(), commits.size() == 1 ? "":"s", repository);\r
+               StringBuilder sb = new StringBuilder();\r
+               sb.append(MessageFormat.format("{0,date,yyyy-MM-dd HH:mm}: {1} pushed {2,number,0} commit{3} to {4} ",\r
+                               date, user.getDisplayName(), commits.size(), commits.size() == 1 ? "":"s", repository));\r
+               for (Map.Entry<String, ReceiveCommand.Type> entry : refUpdates.entrySet()) {\r
+                       String ref = entry.getKey();\r
+                       ReceiveCommand.Type type = entry.getValue();\r
+                       sb.append("\n  ").append(ref).append(' ').append(type.name()).append('\n');\r
+                       for (RepositoryCommit commit : getCommits(ref)) {\r
+                               sb.append("    ").append(commit.toString()).append('\n');\r
+                       }\r
+               }\r
+               return sb.toString();\r
        }\r
 }\r
index 3a98f61f9f8e98cdf1bca483947574641de8f50f..e68e8613e60549494179f6f57a7a2f059c802f4b 100644 (file)
@@ -16,6 +16,7 @@
 package com.gitblit.models;
 
 import java.io.Serializable;
+import java.text.MessageFormat;
 import java.util.List;
 
 import org.eclipse.jgit.lib.PersonIdent;
@@ -101,4 +102,11 @@ public class RepositoryCommit implements Serializable, Comparable<RepositoryComm
                }
                return 0;
        }
+       
+       @Override
+       public String toString() {
+               return MessageFormat.format("{0} {1} {2,date,yyyy-MM-dd HH:mm} {3} {4}", 
+                               getShortName(), branch, getCommitterIdent().getWhen(), getAuthorIdent().getName(),
+                               getShortMessage());
+       }
 }
\ No newline at end of file
index a3b1d66b0439370c420ab4906a076151544bcfd9..665533b340db38de37bcbae62fe43adda240b7c1 100644 (file)
@@ -321,20 +321,20 @@ public class PushLogUtils {
                        for (PathChangeModel change : changedRefs) {
                                switch (change.changeType) {
                                case DELETE:
+                                       log.updateRef(change.path, ReceiveCommand.Type.DELETE);
                                        break;
                                case ADD:
-                               case MODIFY:
+                                       log.updateRef(change.path, ReceiveCommand.Type.CREATE);
+                               default:
                                        String content = JGitUtils.getStringContent(repository, push.getTree(), change.path);
                                        String [] fields = content.split(" ");
+                                       log.updateRef(change.path, ReceiveCommand.Type.valueOf(fields[0]));
                                        String oldId = fields[1];
                                        String newId = fields[2];
                                        List<RevCommit> pushedCommits = JGitUtils.getRevLog(repository, oldId, newId);
                                        for (RevCommit pushedCommit : pushedCommits) {
                                                log.addCommit(change.path, pushedCommit);
                                        }
-                                       break;
-                               default:
-                                       break;
                                }
                        }
                }