blob: 96f7f2f2a87a7b58d10427436204a2ea93e89586 [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 Borowitz3b744b12016-08-19 16:11:10 -040023import java.io.IOException;
24import java.util.List;
25import java.util.Set;
26import javax.annotation.Nullable;
27import javax.servlet.http.HttpServletRequest;
Dave Borowitz90d1db92014-03-16 14:16:15 -070028import org.eclipse.jgit.diff.DiffEntry;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070029import org.eclipse.jgit.lib.PersonIdent;
30import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070031import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070032
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070033public class CommitJsonData {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020034 static final ImmutableSet<Field> DEFAULT_FIELDS =
35 Sets.immutableEnumSet(
36 Field.SHA, Field.TREE, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070037
Dave Borowitz77cf5f22015-10-26 11:05:07 -040038 public static class Log {
39 public List<Commit> log;
40 public String previous;
41 public String next;
42 }
43
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070044 public static class Ident {
45 public String name;
46 public String email;
47 public String time;
48 }
49
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070050 static class Commit {
51 String commit;
Ken Rockotb40eb172014-05-12 14:42:58 -070052 String tree;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070053 List<String> parents;
54 Ident author;
55 Ident committer;
56 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070057
58 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070059 }
60
Dave Borowitz90d1db92014-03-16 14:16:15 -070061 /** @see DiffEntry */
62 static class Diff {
63 String type;
64 String oldId;
65 int oldMode;
66 String oldPath;
67 String newId;
68 int newMode;
69 String newPath;
70 Integer score;
71 }
72
Dave Borowitzee2d77c2014-03-16 13:49:37 -070073 private RevWalk walk;
74
75 CommitJsonData setRevWalk(@Nullable RevWalk walk) {
76 this.walk = walk;
77 return this;
78 }
79
Dave Borowitz97a70312014-04-28 15:50:23 -070080 Commit toJsonData(HttpServletRequest req, RevCommit c, DateFormatter df) throws IOException {
Dave Borowitz90d1db92014-03-16 14:16:15 -070081 return toJsonData(req, c, DEFAULT_FIELDS, df);
82 }
83
Dave Borowitz97a70312014-04-28 15:50:23 -070084 Commit toJsonData(HttpServletRequest req, RevCommit c, Set<Field> fs, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070085 throws IOException {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020086 CommitData cd = new CommitData.Builder().setRevWalk(walk).build(req, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070087
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070088 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070089 if (cd.sha != null) {
90 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070091 }
Ken Rockotb40eb172014-05-12 14:42:58 -070092 if (cd.tree != null) {
93 result.tree = cd.tree.name();
94 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070095 if (cd.parents != null) {
96 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
97 for (RevCommit parent : cd.parents) {
98 result.parents.add(parent.name());
99 }
100 }
101 if (cd.author != null) {
102 result.author = toJsonData(cd.author, df);
103 }
104 if (cd.committer != null) {
105 result.committer = toJsonData(cd.committer, df);
106 }
107 if (cd.message != null) {
108 result.message = cd.message;
109 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700110 if (cd.diffEntries != null) {
111 result.treeDiff = toJsonData(cd.diffEntries);
112 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700113 return result;
114 }
115
Dave Borowitz97a70312014-04-28 15:50:23 -0700116 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700117 Ident result = new Ident();
118 result.name = ident.getName();
119 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700120 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700121 return result;
122 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700123
124 private static List<Diff> toJsonData(DiffList dl) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700125 if (dl.entries == null) {
126 return ImmutableList.of();
127 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700128 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
129 for (DiffEntry de : dl.entries) {
130 Diff d = new Diff();
131 d.type = de.getChangeType().name().toLowerCase();
132 d.oldId = de.getOldId().name();
133 d.oldMode = de.getOldMode().getBits();
134 d.oldPath = de.getOldPath();
135 d.newId = de.getNewId().name();
136 d.newMode = de.getNewMode().getBits();
137 d.newPath = de.getNewPath();
138
139 switch (de.getChangeType()) {
140 case COPY:
141 case RENAME:
142 d.score = de.getScore();
143 break;
David Pursehousecb91aaf2016-06-15 22:05:24 +0900144 case ADD:
145 case DELETE:
146 case MODIFY:
Dave Borowitz90d1db92014-03-16 14:16:15 -0700147 default:
148 break;
149 }
150
151 result.add(d);
152 }
153 return result;
154 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700155}