blob: 20cb9cddd304cf39595fff37d260bf1d3a4a8fce [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 {
Dave Borowitz90d1db92014-03-16 14:16:15 -070037 static final ImmutableSet<Field> DEFAULT_FIELDS = Sets.immutableEnumSet(
Ken Rockotb40eb172014-05-12 14:42:58 -070038 Field.SHA, Field.TREE, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070039
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070040 public static class Ident {
41 public String name;
42 public String email;
43 public String time;
44 }
45
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070046 static class Commit {
47 String commit;
Ken Rockotb40eb172014-05-12 14:42:58 -070048 String tree;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070049 List<String> parents;
50 Ident author;
51 Ident committer;
52 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070053
54 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070055 }
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 }
Ken Rockotb40eb172014-05-12 14:42:58 -070090 if (cd.tree != null) {
91 result.tree = cd.tree.name();
92 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070093 if (cd.parents != null) {
94 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
95 for (RevCommit parent : cd.parents) {
96 result.parents.add(parent.name());
97 }
98 }
99 if (cd.author != null) {
100 result.author = toJsonData(cd.author, df);
101 }
102 if (cd.committer != null) {
103 result.committer = toJsonData(cd.committer, df);
104 }
105 if (cd.message != null) {
106 result.message = cd.message;
107 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700108 if (cd.diffEntries != null) {
109 result.treeDiff = toJsonData(cd.diffEntries);
110 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700111 return result;
112 }
113
Dave Borowitz97a70312014-04-28 15:50:23 -0700114 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700115 Ident result = new Ident();
116 result.name = ident.getName();
117 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700118 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700119 return result;
120 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700121
122 private static List<Diff> toJsonData(DiffList dl) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700123 if (dl.entries == null) {
124 return ImmutableList.of();
125 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700126 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
127 for (DiffEntry de : dl.entries) {
128 Diff d = new Diff();
129 d.type = de.getChangeType().name().toLowerCase();
130 d.oldId = de.getOldId().name();
131 d.oldMode = de.getOldMode().getBits();
132 d.oldPath = de.getOldPath();
133 d.newId = de.getNewId().name();
134 d.newMode = de.getNewMode().getBits();
135 d.newPath = de.getNewPath();
136
137 switch (de.getChangeType()) {
138 case COPY:
139 case RENAME:
140 d.score = de.getScore();
141 break;
142 default:
143 break;
144 }
145
146 result.add(d);
147 }
148 return result;
149 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700150}