blob: 99e7980896c2e740a808ff80bc7550536ecc9405 [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 Borowitz97a70312014-04-28 15:50:23 -070023import com.google.gitiles.DateFormatterBuilder.DateFormatter;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070024
Dave Borowitz90d1db92014-03-16 14:16:15 -070025import org.eclipse.jgit.diff.DiffEntry;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070026import org.eclipse.jgit.lib.PersonIdent;
27import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070028import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070029
Dave Borowitzee2d77c2014-03-16 13:49:37 -070030import java.io.IOException;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070031import java.util.List;
Dave Borowitz90d1db92014-03-16 14:16:15 -070032import java.util.Set;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070033
Dave Borowitzee2d77c2014-03-16 13:49:37 -070034import javax.annotation.Nullable;
35import javax.servlet.http.HttpServletRequest;
36
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070037class CommitJsonData {
Dave Borowitz90d1db92014-03-16 14:16:15 -070038 static final ImmutableSet<Field> DEFAULT_FIELDS = Sets.immutableEnumSet(
Dave Borowitzee2d77c2014-03-16 13:49:37 -070039 Field.SHA, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
40
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070041 static class Commit {
42 String commit;
43 List<String> parents;
44 Ident author;
45 Ident committer;
46 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070047
48 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070049 }
50
51 static class Ident {
52 String name;
53 String email;
54 String time;
55 }
56
Dave Borowitz90d1db92014-03-16 14:16:15 -070057 /** @see DiffEntry */
58 static class Diff {
59 String type;
60 String oldId;
61 int oldMode;
62 String oldPath;
63 String newId;
64 int newMode;
65 String newPath;
66 Integer score;
67 }
68
Dave Borowitzee2d77c2014-03-16 13:49:37 -070069 private RevWalk walk;
70
71 CommitJsonData setRevWalk(@Nullable RevWalk walk) {
72 this.walk = walk;
73 return this;
74 }
75
Dave Borowitz97a70312014-04-28 15:50:23 -070076 Commit toJsonData(HttpServletRequest req, RevCommit c, DateFormatter df) throws IOException {
Dave Borowitz90d1db92014-03-16 14:16:15 -070077 return toJsonData(req, c, DEFAULT_FIELDS, df);
78 }
79
Dave Borowitz97a70312014-04-28 15:50:23 -070080 Commit toJsonData(HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070081 throws IOException {
82 CommitData cd = new CommitData.Builder()
83 .setRevWalk(walk)
Dave Borowitz90d1db92014-03-16 14:16:15 -070084 .build(req, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070085
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070086 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070087 if (cd.sha != null) {
88 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070089 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070090 if (cd.parents != null) {
91 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
92 for (RevCommit parent : cd.parents) {
93 result.parents.add(parent.name());
94 }
95 }
96 if (cd.author != null) {
97 result.author = toJsonData(cd.author, df);
98 }
99 if (cd.committer != null) {
100 result.committer = toJsonData(cd.committer, df);
101 }
102 if (cd.message != null) {
103 result.message = cd.message;
104 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700105 if (cd.diffEntries != null) {
106 result.treeDiff = toJsonData(cd.diffEntries);
107 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700108 return result;
109 }
110
Dave Borowitz97a70312014-04-28 15:50:23 -0700111 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700112 Ident result = new Ident();
113 result.name = ident.getName();
114 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700115 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700116 return result;
117 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700118
119 private static List<Diff> toJsonData(DiffList dl) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700120 if (dl.entries == null) {
121 return ImmutableList.of();
122 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700123 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
124 for (DiffEntry de : dl.entries) {
125 Diff d = new Diff();
126 d.type = de.getChangeType().name().toLowerCase();
127 d.oldId = de.getOldId().name();
128 d.oldMode = de.getOldMode().getBits();
129 d.oldPath = de.getOldPath();
130 d.newId = de.getNewId().name();
131 d.newMode = de.getNewMode().getBits();
132 d.newPath = de.getNewPath();
133
134 switch (de.getChangeType()) {
135 case COPY:
136 case RENAME:
137 d.score = de.getScore();
138 break;
139 default:
140 break;
141 }
142
143 result.add(d);
144 }
145 return result;
146 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700147}