blob: 1db047b9a10e788def1d48bea5b7073909718b97 [file] [log] [blame]
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -07001// 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
15package com.google.gitiles;
16
Dave Borowitz82bc27b2014-04-30 14:25:11 -070017import com.google.common.collect.ImmutableList;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070018import com.google.common.collect.ImmutableSet;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070019import com.google.common.collect.Lists;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070020import com.google.common.collect.Sets;
Dave Borowitz90d1db92014-03-16 14:16:15 -070021import com.google.gitiles.CommitData.DiffList;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070022import com.google.gitiles.CommitData.Field;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070023
Dave Borowitz90d1db92014-03-16 14:16:15 -070024import org.eclipse.jgit.diff.DiffEntry;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070025import org.eclipse.jgit.lib.PersonIdent;
26import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070027import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070028
Dave Borowitzee2d77c2014-03-16 13:49:37 -070029import java.io.IOException;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070030import java.util.List;
Dave Borowitz90d1db92014-03-16 14:16:15 -070031import java.util.Set;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070032
Dave Borowitzee2d77c2014-03-16 13:49:37 -070033import javax.annotation.Nullable;
34import javax.servlet.http.HttpServletRequest;
35
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070036public class CommitJsonData {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020037 static final ImmutableSet<Field> DEFAULT_FIELDS =
38 Sets.immutableEnumSet(
39 Field.SHA, Field.TREE, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070040
Dave Borowitz77cf5f22015-10-26 11:05:07 -040041 public static class Log {
42 public List<Commit> log;
43 public String previous;
44 public String next;
45 }
46
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070047 public static class Ident {
48 public String name;
49 public String email;
50 public String time;
51 }
52
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070053 static class Commit {
54 String commit;
Ken Rockotb40eb172014-05-12 14:42:58 -070055 String tree;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070056 List<String> parents;
57 Ident author;
58 Ident committer;
59 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070060
61 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070062 }
63
Dave Borowitz90d1db92014-03-16 14:16:15 -070064 /** @see DiffEntry */
65 static class Diff {
66 String type;
67 String oldId;
68 int oldMode;
69 String oldPath;
70 String newId;
71 int newMode;
72 String newPath;
73 Integer score;
74 }
75
Dave Borowitzee2d77c2014-03-16 13:49:37 -070076 private RevWalk walk;
77
78 CommitJsonData setRevWalk(@Nullable RevWalk walk) {
79 this.walk = walk;
80 return this;
81 }
82
Dave Borowitz97a70312014-04-28 15:50:23 -070083 Commit toJsonData(HttpServletRequest req, RevCommit c, DateFormatter df) throws IOException {
Dave Borowitz90d1db92014-03-16 14:16:15 -070084 return toJsonData(req, c, DEFAULT_FIELDS, df);
85 }
86
Dave Borowitz97a70312014-04-28 15:50:23 -070087 Commit toJsonData(HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070088 throws IOException {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020089 CommitData cd = new CommitData.Builder().setRevWalk(walk).build(req, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070090
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070091 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070092 if (cd.sha != null) {
93 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070094 }
Ken Rockotb40eb172014-05-12 14:42:58 -070095 if (cd.tree != null) {
96 result.tree = cd.tree.name();
97 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070098 if (cd.parents != null) {
99 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
100 for (RevCommit parent : cd.parents) {
101 result.parents.add(parent.name());
102 }
103 }
104 if (cd.author != null) {
105 result.author = toJsonData(cd.author, df);
106 }
107 if (cd.committer != null) {
108 result.committer = toJsonData(cd.committer, df);
109 }
110 if (cd.message != null) {
111 result.message = cd.message;
112 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700113 if (cd.diffEntries != null) {
114 result.treeDiff = toJsonData(cd.diffEntries);
115 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700116 return result;
117 }
118
Dave Borowitz97a70312014-04-28 15:50:23 -0700119 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700120 Ident result = new Ident();
121 result.name = ident.getName();
122 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700123 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700124 return result;
125 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700126
127 private static List<Diff> toJsonData(DiffList dl) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700128 if (dl.entries == null) {
129 return ImmutableList.of();
130 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700131 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
132 for (DiffEntry de : dl.entries) {
133 Diff d = new Diff();
134 d.type = de.getChangeType().name().toLowerCase();
135 d.oldId = de.getOldId().name();
136 d.oldMode = de.getOldMode().getBits();
137 d.oldPath = de.getOldPath();
138 d.newId = de.getNewId().name();
139 d.newMode = de.getNewMode().getBits();
140 d.newPath = de.getNewPath();
141
142 switch (de.getChangeType()) {
143 case COPY:
144 case RENAME:
145 d.score = de.getScore();
146 break;
147 default:
148 break;
149 }
150
151 result.add(d);
152 }
153 return result;
154 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700155}