Implement FileMode.fromBits utility in TreeWalk
[egit/fonseca.git] / org.spearce.jgit / src / org / spearce / jgit / pgm / DiffTree.java
blob0fa59460ed04bb577a6207ec35049651cde6c429
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.util.ArrayList;
20 import java.util.List;
22 import org.spearce.jgit.lib.FileMode;
23 import org.spearce.jgit.treewalk.TreeWalk;
24 import org.spearce.jgit.treewalk.filter.AndTreeFilter;
25 import org.spearce.jgit.treewalk.filter.OrTreeFilter;
26 import org.spearce.jgit.treewalk.filter.PathFilter;
27 import org.spearce.jgit.treewalk.filter.TreeFilter;
29 class DiffTree extends TextBuiltin {
30 @Override
31 void execute(String[] args) throws Exception {
32 final TreeWalk walk = new TreeWalk(db);
33 final List<String> argList = new ArrayList<String>();
34 List<TreeFilter> pathLimiter = null;
35 for (final String a : args) {
36 if (pathLimiter != null)
37 pathLimiter.add(PathFilter.create(a));
38 else if ("--".equals(a))
39 pathLimiter = new ArrayList<TreeFilter>();
40 else if ("-r".equals(a))
41 walk.setRecursive(true);
42 else
43 argList.add(a);
46 final TreeFilter pathFilter;
47 if (pathLimiter == null || pathLimiter.isEmpty())
48 pathFilter = TreeFilter.ALL;
49 else if (pathLimiter.size() == 1)
50 pathFilter = pathLimiter.get(0);
51 else
52 pathFilter = OrTreeFilter.create(pathLimiter);
53 walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
55 if (argList.size() == 0)
56 argList.add("HEAD");
57 if (argList.size() == 1) {
58 final String a = argList.get(0);
59 argList.clear();
60 argList.add(a + "^^{tree}");
61 argList.add(a + "^{tree}");
63 for (final String a : argList)
64 walk.addTree(resolve(a));
66 final int nTree = walk.getTreeCount();
67 while (walk.next()) {
68 for (int i = 1; i < nTree; i++)
69 out.print(':');
70 for (int i = 0; i < nTree; i++) {
71 final FileMode m = walk.getFileMode(i);
72 final String s = m.toString();
73 for (int pad = 6 - s.length(); pad > 0; pad--)
74 out.print('0');
75 out.print(s);
76 out.print(' ');
79 for (int i = 0; i < nTree; i++) {
80 out.print(walk.getObjectId(i));
81 out.print(' ');
84 char chg = 'M';
85 if (nTree == 2) {
86 final int m0 = walk.getRawMode(0);
87 final int m1 = walk.getRawMode(1);
88 if (m0 == 0 && m1 != 0)
89 chg = 'A';
90 else if (m0 != 0 && m1 == 0)
91 chg = 'D';
92 else if (m0 != m1 && walk.idEqual(0, 1))
93 chg = 'T';
95 out.print(chg);
97 out.print('\t');
98 out.print(walk.getPathString());
99 out.println();