| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 17 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 18 | |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.util.List; |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 22 | import org.eclipse.jgit.annotations.Nullable; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 23 | import org.eclipse.jgit.lib.Constants; |
| 24 | import org.eclipse.jgit.lib.FileMode; |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame] | 25 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 26 | import org.eclipse.jgit.treewalk.TreeWalk; |
| 27 | |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 28 | class TreeJsonData { |
| 29 | static class Tree { |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame] | 30 | String id; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 31 | List<Entry> entries; |
| 32 | } |
| 33 | |
| 34 | static class Entry { |
| 35 | int mode; |
| 36 | String type; |
| 37 | String id; |
| 38 | String name; |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 39 | |
| 40 | @Nullable String target; |
| 41 | @Nullable Long size; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| Han-Wen Nienhuys | 0dc9387 | 2016-05-03 15:21:42 +0200 | [diff] [blame] | 44 | static Tree toJsonData(ObjectId id, TreeWalk tw, boolean includeSizes, boolean recursive) |
| 45 | throws IOException { |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 46 | Tree tree = new Tree(); |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame] | 47 | tree.id = id.name(); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 48 | 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 Nienhuys | 0dc9387 | 2016-05-03 15:21:42 +0200 | [diff] [blame] | 55 | e.name = recursive ? tw.getPathString() : tw.getNameString(); |
| Han-Wen Nienhuys | 8aefdb8 | 2016-05-02 16:49:35 +0200 | [diff] [blame] | 56 | |
| 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 Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 65 | tree.entries.add(e); |
| 66 | } |
| 67 | return tree; |
| 68 | } |
| 69 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 70 | private TreeJsonData() {} |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 71 | } |