blob: 998d5469f91020f1b76e3c0297a57d8a507916e6 [file] [log] [blame]
Dave Borowitz2387b142014-05-02 16:56:27 -07001// Copyright (C) 2014 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
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020017import static java.nio.charset.StandardCharsets.UTF_8;
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020018
Dave Borowitz2387b142014-05-02 16:56:27 -070019import com.google.common.collect.Lists;
20
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020021import org.eclipse.jgit.annotations.Nullable;
Dave Borowitz2387b142014-05-02 16:56:27 -070022import org.eclipse.jgit.lib.Constants;
23import org.eclipse.jgit.lib.FileMode;
Ken Rockotb40eb172014-05-12 14:42:58 -070024import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz2387b142014-05-02 16:56:27 -070025import org.eclipse.jgit.treewalk.TreeWalk;
26
27import java.io.IOException;
28import java.util.List;
29
30class TreeJsonData {
31 static class Tree {
Ken Rockotb40eb172014-05-12 14:42:58 -070032 String id;
Dave Borowitz2387b142014-05-02 16:56:27 -070033 List<Entry> entries;
34 }
35
36 static class Entry {
37 int mode;
38 String type;
39 String id;
40 String name;
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020041
42 @Nullable String target;
43 @Nullable Long size;
Dave Borowitz2387b142014-05-02 16:56:27 -070044 }
45
Han-Wen Nienhuys0dc93872016-05-03 15:21:42 +020046 static Tree toJsonData(ObjectId id, TreeWalk tw, boolean includeSizes, boolean recursive)
47 throws IOException {
Dave Borowitz2387b142014-05-02 16:56:27 -070048 Tree tree = new Tree();
Ken Rockotb40eb172014-05-12 14:42:58 -070049 tree.id = id.name();
Dave Borowitz2387b142014-05-02 16:56:27 -070050 tree.entries = Lists.newArrayList();
51 while (tw.next()) {
52 Entry e = new Entry();
53 FileMode mode = tw.getFileMode(0);
54 e.mode = mode.getBits();
55 e.type = Constants.typeString(mode.getObjectType());
56 e.id = tw.getObjectId(0).name();
Han-Wen Nienhuys0dc93872016-05-03 15:21:42 +020057 e.name = recursive ? tw.getPathString() : tw.getNameString();
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020058
59 if (includeSizes) {
60 if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_FILE) {
61 e.size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
62 } else if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_SYMLINK) {
63 e.target =
64 new String(tw.getObjectReader().open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
65 }
66 }
Dave Borowitz2387b142014-05-02 16:56:27 -070067 tree.entries.add(e);
68 }
69 return tree;
70 }
71
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020072 private TreeJsonData() {}
Dave Borowitz2387b142014-05-02 16:56:27 -070073}