blob: 212099cf8418dd81a868bb3bf01ad250bd10c0e9 [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;
Dave Borowitz3b744b12016-08-19 16:11:10 -040020import java.io.IOException;
21import java.util.List;
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020022import org.eclipse.jgit.annotations.Nullable;
Dave Borowitz2387b142014-05-02 16:56:27 -070023import org.eclipse.jgit.lib.Constants;
24import org.eclipse.jgit.lib.FileMode;
Ken Rockotb40eb172014-05-12 14:42:58 -070025import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz2387b142014-05-02 16:56:27 -070026import org.eclipse.jgit.treewalk.TreeWalk;
27
Dave Borowitz2387b142014-05-02 16:56:27 -070028class TreeJsonData {
29 static class Tree {
Ken Rockotb40eb172014-05-12 14:42:58 -070030 String id;
Dave Borowitz2387b142014-05-02 16:56:27 -070031 List<Entry> entries;
32 }
33
34 static class Entry {
35 int mode;
36 String type;
37 String id;
38 String name;
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020039
40 @Nullable String target;
41 @Nullable Long size;
Dave Borowitz2387b142014-05-02 16:56:27 -070042 }
43
Han-Wen Nienhuys0dc93872016-05-03 15:21:42 +020044 static Tree toJsonData(ObjectId id, TreeWalk tw, boolean includeSizes, boolean recursive)
45 throws IOException {
Dave Borowitz2387b142014-05-02 16:56:27 -070046 Tree tree = new Tree();
Ken Rockotb40eb172014-05-12 14:42:58 -070047 tree.id = id.name();
Dave Borowitz2387b142014-05-02 16:56:27 -070048 tree.entries = Lists.newArrayList();
49 while (tw.next()) {
50 Entry e = new Entry();
51 FileMode mode = tw.getFileMode(0);
52 e.mode = mode.getBits();
53 e.type = Constants.typeString(mode.getObjectType());
54 e.id = tw.getObjectId(0).name();
Han-Wen Nienhuys0dc93872016-05-03 15:21:42 +020055 e.name = recursive ? tw.getPathString() : tw.getNameString();
Han-Wen Nienhuys8aefdb82016-05-02 16:49:35 +020056
57 if (includeSizes) {
58 if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_FILE) {
59 e.size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
60 } else if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_SYMLINK) {
61 e.target =
62 new String(tw.getObjectReader().open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
63 }
64 }
Dave Borowitz2387b142014-05-02 16:56:27 -070065 tree.entries.add(e);
66 }
67 return tree;
68 }
69
Han-Wen Nienhuysc0200f62016-05-02 17:34:51 +020070 private TreeJsonData() {}
Dave Borowitz2387b142014-05-02 16:56:27 -070071}