Switch jgit library to the EDL (3-clause BSD)
[jgit.git] / org.spearce.jgit / src / org / spearce / jgit / pgm / DiffTree.java
blob74e8d2f6863bb2434cc888b84aeed5c6f31cf4a2
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.pgm;
40 import java.util.ArrayList;
41 import java.util.List;
43 import org.spearce.jgit.lib.FileMode;
44 import org.spearce.jgit.treewalk.TreeWalk;
45 import org.spearce.jgit.treewalk.filter.AndTreeFilter;
46 import org.spearce.jgit.treewalk.filter.OrTreeFilter;
47 import org.spearce.jgit.treewalk.filter.PathFilter;
48 import org.spearce.jgit.treewalk.filter.TreeFilter;
50 class DiffTree extends TextBuiltin {
51 @Override
52 void execute(String[] args) throws Exception {
53 final TreeWalk walk = new TreeWalk(db);
54 final List<String> argList = new ArrayList<String>();
55 List<TreeFilter> pathLimiter = null;
56 for (final String a : args) {
57 if (pathLimiter != null)
58 pathLimiter.add(PathFilter.create(a));
59 else if ("--".equals(a))
60 pathLimiter = new ArrayList<TreeFilter>();
61 else if ("-r".equals(a))
62 walk.setRecursive(true);
63 else
64 argList.add(a);
67 final TreeFilter pathFilter;
68 if (pathLimiter == null || pathLimiter.isEmpty())
69 pathFilter = TreeFilter.ALL;
70 else if (pathLimiter.size() == 1)
71 pathFilter = pathLimiter.get(0);
72 else
73 pathFilter = OrTreeFilter.create(pathLimiter);
74 walk.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, pathFilter));
76 if (argList.size() == 0)
77 argList.add("HEAD");
78 if (argList.size() == 1) {
79 final String a = argList.get(0);
80 argList.clear();
81 argList.add(a + "^^{tree}");
82 argList.add(a + "^{tree}");
84 for (final String a : argList)
85 walk.addTree(resolve(a));
87 final int nTree = walk.getTreeCount();
88 while (walk.next()) {
89 for (int i = 1; i < nTree; i++)
90 out.print(':');
91 for (int i = 0; i < nTree; i++) {
92 final FileMode m = walk.getFileMode(i);
93 final String s = m.toString();
94 for (int pad = 6 - s.length(); pad > 0; pad--)
95 out.print('0');
96 out.print(s);
97 out.print(' ');
100 for (int i = 0; i < nTree; i++) {
101 out.print(walk.getObjectId(i));
102 out.print(' ');
105 char chg = 'M';
106 if (nTree == 2) {
107 final int m0 = walk.getRawMode(0);
108 final int m1 = walk.getRawMode(1);
109 if (m0 == 0 && m1 != 0)
110 chg = 'A';
111 else if (m0 != 0 && m1 == 0)
112 chg = 'D';
113 else if (m0 != m1 && walk.idEqual(0, 1))
114 chg = 'T';
116 out.print(chg);
118 out.print('\t');
119 out.print(walk.getPathString());
120 out.println();