blob: 953c19c3df9f754ca434f765fd4bdefe528b3114 [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;
Dave Borowitz3b744b12016-08-19 16:11:10 -040026import javax.servlet.http.HttpServletRequest;
Dave Borowitz90d1db92014-03-16 14:16:15 -070027import org.eclipse.jgit.diff.DiffEntry;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070028import org.eclipse.jgit.lib.PersonIdent;
29import org.eclipse.jgit.revwalk.RevCommit;
Dave Borowitzee2d77c2014-03-16 13:49:37 -070030import org.eclipse.jgit.revwalk.RevWalk;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070031
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070032public class CommitJsonData {
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020033 static final ImmutableSet<Field> DEFAULT_FIELDS =
34 Sets.immutableEnumSet(
35 Field.SHA, Field.TREE, Field.PARENTS, Field.AUTHOR, Field.COMMITTER, Field.MESSAGE);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070036
Dave Borowitz77cf5f22015-10-26 11:05:07 -040037 public static class Log {
38 public List<Commit> log;
39 public String previous;
40 public String next;
41 }
42
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070043 public static class Ident {
44 public String name;
45 public String email;
46 public String time;
47 }
48
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070049 static class Commit {
50 String commit;
Ken Rockotb40eb172014-05-12 14:42:58 -070051 String tree;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070052 List<String> parents;
53 Ident author;
54 Ident committer;
55 String message;
Dave Borowitz90d1db92014-03-16 14:16:15 -070056
57 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070058 }
59
Dave Borowitz90d1db92014-03-16 14:16:15 -070060 /** @see DiffEntry */
61 static class Diff {
62 String type;
63 String oldId;
64 int oldMode;
65 String oldPath;
66 String newId;
67 int newMode;
68 String newPath;
69 Integer score;
70 }
71
Jonathan Niederb49306a2019-03-07 14:10:57 -080072 Commit toJsonData(HttpServletRequest req, RevWalk walk, RevCommit c, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070073 throws IOException {
Jonathan Niederb49306a2019-03-07 14:10:57 -080074 return toJsonData(req, walk, c, DEFAULT_FIELDS, df);
75 }
76
77 Commit toJsonData(
78 HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs, DateFormatter df)
79 throws IOException {
80 CommitData cd = new CommitData.Builder().build(req, walk, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070081
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070082 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070083 if (cd.sha != null) {
84 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070085 }
Ken Rockotb40eb172014-05-12 14:42:58 -070086 if (cd.tree != null) {
87 result.tree = cd.tree.name();
88 }
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) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700119 if (dl.entries == null) {
120 return ImmutableList.of();
121 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700122 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
123 for (DiffEntry de : dl.entries) {
124 Diff d = new Diff();
125 d.type = de.getChangeType().name().toLowerCase();
126 d.oldId = de.getOldId().name();
127 d.oldMode = de.getOldMode().getBits();
128 d.oldPath = de.getOldPath();
129 d.newId = de.getNewId().name();
130 d.newMode = de.getNewMode().getBits();
131 d.newPath = de.getNewPath();
132
133 switch (de.getChangeType()) {
134 case COPY:
135 case RENAME:
136 d.score = de.getScore();
137 break;
David Pursehousecb91aaf2016-06-15 22:05:24 +0900138 case ADD:
139 case DELETE:
140 case MODIFY:
Dave Borowitz90d1db92014-03-16 14:16:15 -0700141 default:
142 break;
143 }
144
145 result.add(d);
146 }
147 return result;
148 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700149}