blob: 5f94a70ca9617e1993e755decfdc617e11e7e7d3 [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(
Pontus Jaenssonc144de92021-10-29 11:13:23 +020035 Field.SHA,
36 Field.TREE,
37 Field.PARENTS,
38 Field.AUTHOR,
39 Field.COMMITTER,
40 Field.MESSAGE,
41 Field.NOTES);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070042
Dave Borowitz77cf5f22015-10-26 11:05:07 -040043 public static class Log {
44 public List<Commit> log;
45 public String previous;
46 public String next;
47 }
48
Dave Borowitz08f2d9e2014-07-09 20:25:35 -070049 public static class Ident {
50 public String name;
51 public String email;
52 public String time;
53 }
54
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070055 static class Commit {
56 String commit;
Ken Rockotb40eb172014-05-12 14:42:58 -070057 String tree;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070058 List<String> parents;
59 Ident author;
60 Ident committer;
61 String message;
Pontus Jaenssonc144de92021-10-29 11:13:23 +020062 String notes;
Dave Borowitz90d1db92014-03-16 14:16:15 -070063
64 List<Diff> treeDiff;
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070065 }
66
Dave Borowitz90d1db92014-03-16 14:16:15 -070067 /** @see DiffEntry */
68 static class Diff {
69 String type;
70 String oldId;
71 int oldMode;
72 String oldPath;
73 String newId;
74 int newMode;
75 String newPath;
76 Integer score;
77 }
78
Jonathan Niederb49306a2019-03-07 14:10:57 -080079 Commit toJsonData(HttpServletRequest req, RevWalk walk, RevCommit c, DateFormatter df)
Dave Borowitzee2d77c2014-03-16 13:49:37 -070080 throws IOException {
Jonathan Niederb49306a2019-03-07 14:10:57 -080081 return toJsonData(req, walk, c, DEFAULT_FIELDS, df);
82 }
83
84 Commit toJsonData(
85 HttpServletRequest req, RevWalk walk, RevCommit c, Set<Field> fs, DateFormatter df)
86 throws IOException {
87 CommitData cd = new CommitData.Builder().build(req, walk, c, fs);
Dave Borowitzee2d77c2014-03-16 13:49:37 -070088
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070089 Commit result = new Commit();
Dave Borowitzee2d77c2014-03-16 13:49:37 -070090 if (cd.sha != null) {
91 result.commit = cd.sha.name();
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -070092 }
Ken Rockotb40eb172014-05-12 14:42:58 -070093 if (cd.tree != null) {
94 result.tree = cd.tree.name();
95 }
Dave Borowitzee2d77c2014-03-16 13:49:37 -070096 if (cd.parents != null) {
97 result.parents = Lists.newArrayListWithCapacity(cd.parents.size());
98 for (RevCommit parent : cd.parents) {
99 result.parents.add(parent.name());
100 }
101 }
102 if (cd.author != null) {
103 result.author = toJsonData(cd.author, df);
104 }
105 if (cd.committer != null) {
106 result.committer = toJsonData(cd.committer, df);
107 }
108 if (cd.message != null) {
109 result.message = cd.message;
110 }
Pontus Jaenssonc144de92021-10-29 11:13:23 +0200111 if (cd.notes != null && !cd.notes.isEmpty()){
112 result.notes = cd.notes;
113 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700114 if (cd.diffEntries != null) {
115 result.treeDiff = toJsonData(cd.diffEntries);
116 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700117 return result;
118 }
119
Dave Borowitz97a70312014-04-28 15:50:23 -0700120 private static Ident toJsonData(PersonIdent ident, DateFormatter df) {
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700121 Ident result = new Ident();
122 result.name = ident.getName();
123 result.email = ident.getEmailAddress();
Dave Borowitz97a70312014-04-28 15:50:23 -0700124 result.time = df.format(ident);
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700125 return result;
126 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700127
128 private static List<Diff> toJsonData(DiffList dl) {
Dave Borowitz82bc27b2014-04-30 14:25:11 -0700129 if (dl.entries == null) {
130 return ImmutableList.of();
131 }
Dave Borowitz90d1db92014-03-16 14:16:15 -0700132 List<Diff> result = Lists.newArrayListWithCapacity(dl.entries.size());
133 for (DiffEntry de : dl.entries) {
134 Diff d = new Diff();
135 d.type = de.getChangeType().name().toLowerCase();
136 d.oldId = de.getOldId().name();
137 d.oldMode = de.getOldMode().getBits();
138 d.oldPath = de.getOldPath();
139 d.newId = de.getNewId().name();
140 d.newMode = de.getNewMode().getBits();
141 d.newPath = de.getNewPath();
142
143 switch (de.getChangeType()) {
144 case COPY:
145 case RENAME:
146 d.score = de.getScore();
147 break;
David Pursehousecb91aaf2016-06-15 22:05:24 +0900148 case ADD:
149 case DELETE:
150 case MODIFY:
Dave Borowitz90d1db92014-03-16 14:16:15 -0700151 default:
152 break;
153 }
154
155 result.add(d);
156 }
157 return result;
158 }
Dave Borowitzd6ebc2d2013-11-01 11:44:06 -0700159}