blob: 01f5ed04a2ea0176087fb88659d8c978a7aa3344 [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 Borowitzee2d77c2014-03-16 13:49:37 -070017import com.google.common.collect.ImmutableSet;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070018import com.google.common.collect.Lists;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070019import com.google.common.collect.Sets;
Dave Borowitz90d1db92014-03-16 14:16:15 -070020import com.google.gitiles.CommitData.DiffList;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070021import com.google.gitiles.CommitData.Field;
Dave Borowitz97a70312014-04-28 15:50:23 -070022import com.google.gitiles.DateFormatterBuilder.DateFormatter;
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 Borowitzd6ebc2d2013-11-01 11:44:06 -070036class CommitJsonData {
Dave Borowitz90d1db92014-03-16 14:16:15 -070037 static final ImmutableSet<Field> DEFAULT_FIELDS = Sets.immutableEnumSet(
Dave Borowitzee2d77c2014-03-16 13:49:37 -070038 Field.SHA, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
39
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070040 static class Commit {
41 String commit;
42 List<String> parents;
43 Ident author;
44 Ident committer;
45 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070046
47 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070048 }
49
50 static class Ident {
51 String name;
52 String email;
53 String time;
54 }
55
Dave Borowitz90d1db92014-03-16 14:16:15 -070056 /** @see DiffEntry */
57 static class Diff {
58 String type;
59 String oldId;
60 int oldMode;
61 String oldPath;
62 String newId;
63 int newMode;
64 String newPath;
65 Integer score;
66 }
67
Dave Borowitzee2d77c2014-03-16 13:49:37 -070068 private RevWalk walk;
69
70 CommitJsonData setRevWalk(@Nullable RevWalk walk) {
71 this.walk = walk;
72 return this;
73 }
74
Dave Borowitz97a70312014-04-28 15:50:23 -070075 Commit toJsonData(HttpServletRequest req, RevCommit c, DateFormatter df) throws IOException {
Dave Borowitz90d1db92014-03-16 14:16:15 -070076 return toJsonData(req, c, DEFAULT_FIELDS, df);
77 }
78
Dave Borowitz97a70312014-04-28 15:50:23 -070079 Commit toJsonData(HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070080 throws IOException {
81 CommitData cd = new CommitData.Builder()
82 .setRevWalk(walk)
Dave Borowitz90d1db92014-03-16 14:16:15 -070083 .build(req, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070084
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070085 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070086 if (cd.sha != null) {
87 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070088 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070089 if (cd.parents != null) {
90 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
91 for (RevCommit parent : cd.parents) {
92 result.parents.add(parent.name());
93 }
94 }
95 if (cd.author != null) {
96 result.author = toJsonData(cd.author, df);
97 }
98 if (cd.committer != null) {
99 result.committer = toJsonData(cd.committer, df);
100 }
101 if (cd.message != null) {
102 result.message = cd.message;
103 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700104 if (cd.diffEntries != null) {
105 result.treeDiff = toJsonData(cd.diffEntries);
106 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700107 return result;
108 }
109
Dave Borowitz97a70312014-04-28 15:50:23 -0700110 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700111 Ident result = new Ident();
112 result.name = ident.getName();
113 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700114 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700115 return result;
116 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700117
118 private static List<Diff> toJsonData(DiffList dl) {
119 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
120 for (DiffEntry de : dl.entries) {
121 Diff d = new Diff();
122 d.type = de.getChangeType().name().toLowerCase();
123 d.oldId = de.getOldId().name();
124 d.oldMode = de.getOldMode().getBits();
125 d.oldPath = de.getOldPath();
126 d.newId = de.getNewId().name();
127 d.newMode = de.getNewMode().getBits();
128 d.newPath = de.getNewPath();
129
130 switch (de.getChangeType()) {
131 case COPY:
132 case RENAME:
133 d.score = de.getScore();
134 break;
135 default:
136 break;
137 }
138
139 result.add(d);
140 }
141 return result;
142 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700143}