| 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 | |
| 17 | import com.google.common.collect.Lists; |
| 18 | |
| 19 | import org.eclipse.jgit.lib.Constants; |
| 20 | import org.eclipse.jgit.lib.FileMode; |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame^] | 21 | import org.eclipse.jgit.lib.ObjectId; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 22 | import org.eclipse.jgit.treewalk.TreeWalk; |
| 23 | |
| 24 | import java.io.IOException; |
| 25 | import java.util.List; |
| 26 | |
| 27 | class TreeJsonData { |
| 28 | static class Tree { |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame^] | 29 | String id; |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 30 | List<Entry> entries; |
| 31 | } |
| 32 | |
| 33 | static class Entry { |
| 34 | int mode; |
| 35 | String type; |
| 36 | String id; |
| 37 | String name; |
| 38 | } |
| 39 | |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame^] | 40 | static Tree toJsonData(ObjectId id, TreeWalk tw) throws IOException { |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 41 | Tree tree = new Tree(); |
| Ken Rockot | b40eb17 | 2014-05-12 14:42:58 -0700 | [diff] [blame^] | 42 | tree.id = id.name(); |
| Dave Borowitz | 2387b14 | 2014-05-02 16:56:27 -0700 | [diff] [blame] | 43 | tree.entries = Lists.newArrayList(); |
| 44 | while (tw.next()) { |
| 45 | Entry e = new Entry(); |
| 46 | FileMode mode = tw.getFileMode(0); |
| 47 | e.mode = mode.getBits(); |
| 48 | e.type = Constants.typeString(mode.getObjectType()); |
| 49 | e.id = tw.getObjectId(0).name(); |
| 50 | e.name = tw.getNameString(); |
| 51 | tree.entries.add(e); |
| 52 | } |
| 53 | return tree; |
| 54 | } |
| 55 | |
| 56 | private TreeJsonData() { |
| 57 | } |
| 58 | } |