Implement FileMode.fromBits utility in TreeWalk
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / pgm / LsTree.java
blob3620a268911caf92f22c626f086f15248621d983
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.pgm;
19 import java.io.File;
21 import org.spearce.jgit.lib.Constants;
22 import org.spearce.jgit.lib.FileMode;
23 import org.spearce.jgit.treewalk.FileTreeIterator;
24 import org.spearce.jgit.treewalk.TreeWalk;
26 class LsTree extends TextBuiltin {
27 @Override
28 void execute(final String[] args) throws Exception {
29 final TreeWalk walk = new TreeWalk(db);
30 int argi = 0;
31 for (; argi < args.length; argi++) {
32 final String a = args[argi];
33 if ("--".equals(a)) {
34 argi++;
35 break;
36 } else if ("-r".equals(a))
37 walk.setRecursive(true);
38 else
39 break;
42 if (argi == args.length)
43 throw die("usage: [-r] treename");
44 else if (argi + 1 < args.length)
45 throw die("too many arguments");
47 final String n = args[argi];
48 if (is_WorkDir(n))
49 walk.addTree(new FileTreeIterator(new File(n)));
50 else
51 walk.addTree(resolve(n));
53 while (walk.next()) {
54 final FileMode mode = walk.getFileMode(0);
55 if (mode == FileMode.TREE)
56 out.print('0');
57 out.print(mode);
58 out.print(' ');
59 out.print(Constants.typeString(mode.getObjectType()));
61 out.print(' ');
62 out.print(walk.getObjectId(0));
64 out.print('\t');
65 out.print(walk.getPathString());
66 out.println();
70 private boolean is_WorkDir(final String name) {
71 return new File(name).isDirectory();