| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 1 | // Copyright (C) 2013 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | 82bc27b | 2014-04-30 14:25:11 -0700 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 21 | import com.google.gitiles.CommitData.DiffList; |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 22 | import com.google.gitiles.CommitData.Field; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 23 | import java.io.IOException; |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 26 | import javax.servlet.http.HttpServletRequest; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 27 | import org.eclipse.jgit.diff.DiffEntry; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 28 | import org.eclipse.jgit.lib.PersonIdent; |
| 29 | import org.eclipse.jgit.revwalk.RevCommit; |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 30 | import org.eclipse.jgit.revwalk.RevWalk; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 31 | |
| Dave Borowitz | 08f2d9e | 2014-07-09 20:25:35 -0700 | [diff] [blame] | 32 | public class CommitJsonData { |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 33 | static final ImmutableSet<Field> DEFAULT_FIELDS = |
| 34 | Sets.immutableEnumSet( |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 35 | Field.SHA, |
| 36 | Field.TREE, |
| 37 | Field.PARENTS, |
| 38 | Field.AUTHOR, |
| 39 | Field.COMMITTER, |
| 40 | Field.MESSAGE, |
| 41 | Field.NOTES); |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 42 | |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 43 | public static class Log { |
| 44 | public List<Commit> log; |
| 45 | public String previous; |
| 46 | public String next; |
| 47 | } |
| 48 | |
| Dave Borowitz | 08f2d9e | 2014-07-09 20:25:35 -0700 | [diff] [blame] | 49 | public static class Ident { |
| 50 | public String name; |
| 51 | public String email; |
| 52 | public String time; |
| 53 | } |
| 54 | |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 55 | static class Commit { |
| 56 | String commit; |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame] | 57 | String tree; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 58 | List<String> parents; |
| 59 | Ident author; |
| 60 | Ident committer; |
| 61 | String message; |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 62 | String notes; |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 63 | |
| 64 | List<Diff> treeDiff; |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 67 | /** @see DiffEntry */ |
| 68 | static class Diff { |
| 69 | String type; |
| 70 | String oldId; |
| 71 | int oldMode; |
| 72 | String oldPath; |
| 73 | String newId; |
| 74 | int newMode; |
| 75 | String newPath; |
| 76 | Integer score; |
| 77 | } |
| 78 | |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 79 | Commit toJsonData(HttpServletRequest req, RevWalk walk, RevCommit c, DateFormatter df) |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 80 | throws IOException { |
| Jonathan Nieder | b49306a | 2019-03-07 14:10:57 -0800 | [diff] [blame] | 81 | return toJsonData(req, walk, c, DEFAULT_FIELDS, df); |
| 82 | } |
| 83 | |
| 84 | Commit toJsonData( |
| 85 | HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs, DateFormatter df) |
| 86 | throws IOException { |
| 87 | CommitData cd = new CommitData.Builder().build(req, walk, c, fs); |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 88 | |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 89 | Commit result = new Commit(); |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 90 | if (cd.sha != null) { |
| 91 | result.commit = cd.sha.name(); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 92 | } |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame] | 93 | if (cd.tree != null) { |
| 94 | result.tree = cd.tree.name(); |
| 95 | } |
| Dave Borowitz | ee2d77c | 2014-03-16 13:49:37 -0700 | [diff] [blame] | 96 | if (cd.parents != null) { |
| 97 | result.parents = Lists.newArrayListWithCapacity(cd.parents.size()); |
| 98 | for (RevCommit parent : cd.parents) { |
| 99 | result.parents.add(parent.name()); |
| 100 | } |
| 101 | } |
| 102 | if (cd.author != null) { |
| 103 | result.author = toJsonData(cd.author, df); |
| 104 | } |
| 105 | if (cd.committer != null) { |
| 106 | result.committer = toJsonData(cd.committer, df); |
| 107 | } |
| 108 | if (cd.message != null) { |
| 109 | result.message = cd.message; |
| 110 | } |
| Pontus Jaensson | c144de9 | 2021-10-29 11:13:23 +0200 | [diff] [blame] | 111 | if (cd.notes != null && !cd.notes.isEmpty()){ |
| 112 | result.notes = cd.notes; |
| 113 | } |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 114 | if (cd.diffEntries != null) { |
| 115 | result.treeDiff = toJsonData(cd.diffEntries); |
| 116 | } |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 117 | return result; |
| 118 | } |
| 119 | |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 120 | private static Ident toJsonData(PersonIdent ident, DateFormatter df) { |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 121 | Ident result = new Ident(); |
| 122 | result.name = ident.getName(); |
| 123 | result.email = ident.getEmailAddress(); |
| Dave Borowitz | 97a7031 | 2014-04-28 15:50:23 -0700 | [diff] [blame] | 124 | result.time = df.format(ident); |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 125 | return result; |
| 126 | } |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 127 | |
| 128 | private static List<Diff> toJsonData(DiffList dl) { |
| Dave Borowitz | 82bc27b | 2014-04-30 14:25:11 -0700 | [diff] [blame] | 129 | if (dl.entries == null) { |
| 130 | return ImmutableList.of(); |
| 131 | } |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 132 | List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size()); |
| 133 | for (DiffEntry de : dl.entries) { |
| 134 | Diff d = new Diff(); |
| 135 | d.type = de.getChangeType().name().toLowerCase(); |
| 136 | d.oldId = de.getOldId().name(); |
| 137 | d.oldMode = de.getOldMode().getBits(); |
| 138 | d.oldPath = de.getOldPath(); |
| 139 | d.newId = de.getNewId().name(); |
| 140 | d.newMode = de.getNewMode().getBits(); |
| 141 | d.newPath = de.getNewPath(); |
| 142 | |
| 143 | switch (de.getChangeType()) { |
| 144 | case COPY: |
| 145 | case RENAME: |
| 146 | d.score = de.getScore(); |
| 147 | break; |
| David Pursehouse | cb91aaf | 2016-06-15 22:05:24 +0900 | [diff] [blame] | 148 | case ADD: |
| 149 | case DELETE: |
| 150 | case MODIFY: |
| Dave Borowitz | 90d1db9 | 2014-03-16 14:16:15 -0700 | [diff] [blame] | 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | result.add(d); |
| 156 | } |
| 157 | return result; |
| 158 | } |
| Dave Borowitz | d6ebc2d | 2013-11-01 11:44:06 -0700 | [diff] [blame] | 159 | } |