blob: 4ad65629d9b033f292d0b1912b7f5aaa7603b91c [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
17import com.google.common.collect.Lists;
18
19import org.eclipse.jgit.lib.Constants;
20import org.eclipse.jgit.lib.FileMode;
Ken Rockotb40eb172014-05-12 14:42:58 -070021import org.eclipse.jgit.lib.ObjectId;
Dave Borowitz2387b142014-05-02 16:56:27 -070022import org.eclipse.jgit.treewalk.TreeWalk;
23
24import java.io.IOException;
25import java.util.List;
26
27class TreeJsonData {
28 static class Tree {
Ken Rockotb40eb172014-05-12 14:42:58 -070029 String id;
Dave Borowitz2387b142014-05-02 16:56:27 -070030 List<Entry> entries;
31 }
32
33 static class Entry {
34 int mode;
35 String type;
36 String id;
37 String name;
38 }
39
Ken Rockotb40eb172014-05-12 14:42:58 -070040 static Tree toJsonData(ObjectId id, TreeWalk tw) throws IOException {
Dave Borowitz2387b142014-05-02 16:56:27 -070041 Tree tree = new Tree();
Ken Rockotb40eb172014-05-12 14:42:58 -070042 tree.id = id.name();
Dave Borowitz2387b142014-05-02 16:56:27 -070043 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}