View Javadoc
1   /*
2    * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
3    * Copyright (C) 2009, Johannes E. Schindelin
4    * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.pgm;
47  
48  import static java.lang.Integer.valueOf;
49  import static org.eclipse.jgit.lib.Constants.HEAD;
50  import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
51  
52  import java.io.BufferedOutputStream;
53  import java.io.IOException;
54  import java.text.MessageFormat;
55  import java.util.List;
56  import java.util.concurrent.TimeUnit;
57  
58  import org.eclipse.jgit.diff.DiffAlgorithm;
59  import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
60  import org.eclipse.jgit.diff.DiffEntry;
61  import org.eclipse.jgit.diff.DiffFormatter;
62  import org.eclipse.jgit.diff.RawTextComparator;
63  import org.eclipse.jgit.diff.RenameDetector;
64  import org.eclipse.jgit.dircache.DirCacheIterator;
65  import org.eclipse.jgit.lib.ObjectId;
66  import org.eclipse.jgit.lib.ObjectReader;
67  import org.eclipse.jgit.lib.Repository;
68  import org.eclipse.jgit.lib.TextProgressMonitor;
69  import org.eclipse.jgit.pgm.internal.CLIText;
70  import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
71  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
72  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
73  import org.eclipse.jgit.treewalk.FileTreeIterator;
74  import org.eclipse.jgit.treewalk.filter.TreeFilter;
75  import org.eclipse.jgit.util.io.ThrowingPrintWriter;
76  import org.kohsuke.args4j.Argument;
77  import org.kohsuke.args4j.Option;
78  
79  @Command(common = true, usage = "usage_ShowDiffs")
80  class Diff extends TextBuiltin {
81  	private DiffFormatter diffFmt;
82  
83  	@Argument(index = 0, metaVar = "metaVar_treeish")
84  	private AbstractTreeIterator oldTree;
85  
86  	@Argument(index = 1, metaVar = "metaVar_treeish")
87  	private AbstractTreeIterator newTree;
88  
89  	@Option(name = "--cached", usage = "usage_cached")
90  	private boolean cached;
91  
92  	@Option(name = "--", metaVar = "metaVar_paths", handler = PathTreeFilterHandler.class)
93  	private TreeFilter pathFilter = TreeFilter.ALL;
94  
95  	// BEGIN -- Options shared with Log
96  	@Option(name = "-p", usage = "usage_showPatch")
97  	boolean showPatch;
98  
99  	@Option(name = "-M", usage = "usage_detectRenames")
100 	private Boolean detectRenames;
101 
102 	@Option(name = "--no-renames", usage = "usage_noRenames")
103 	void noRenames(@SuppressWarnings("unused") boolean on) {
104 		detectRenames = Boolean.FALSE;
105 	}
106 
107 	@Option(name = "--algorithm", metaVar = "metaVar_diffAlg", usage = "usage_diffAlgorithm")
108 	void setAlgorithm(SupportedAlgorithm s) {
109 		diffFmt.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(s));
110 	}
111 
112 	@Option(name = "-l", usage = "usage_renameLimit")
113 	private Integer renameLimit;
114 
115 	@Option(name = "--name-status", usage = "usage_nameStatus")
116 	private boolean showNameAndStatusOnly;
117 
118 	@Option(name = "--ignore-space-at-eol")
119 	void ignoreSpaceAtEol(@SuppressWarnings("unused") boolean on) {
120 		diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_TRAILING);
121 	}
122 
123 	@Option(name = "--ignore-leading-space")
124 	void ignoreLeadingSpace(@SuppressWarnings("unused") boolean on) {
125 		diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_LEADING);
126 	}
127 
128 	@Option(name = "-b", aliases = { "--ignore-space-change" })
129 	void ignoreSpaceChange(@SuppressWarnings("unused") boolean on) {
130 		diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_CHANGE);
131 	}
132 
133 	@Option(name = "-w", aliases = { "--ignore-all-space" })
134 	void ignoreAllSpace(@SuppressWarnings("unused") boolean on) {
135 		diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
136 	}
137 
138 	@Option(name = "-U", aliases = { "--unified" }, metaVar = "metaVar_linesOfContext")
139 	void unified(int lines) {
140 		diffFmt.setContext(lines);
141 	}
142 
143 	@Option(name = "--abbrev", metaVar = "metaVar_n")
144 	void abbrev(int lines) {
145 		diffFmt.setAbbreviationLength(lines);
146 	}
147 
148 	@Option(name = "--full-index")
149 	void abbrev(@SuppressWarnings("unused") boolean on) {
150 		diffFmt.setAbbreviationLength(OBJECT_ID_STRING_LENGTH);
151 	}
152 
153 	@Option(name = "--src-prefix", usage = "usage_srcPrefix")
154 	void sourcePrefix(String path) {
155 		diffFmt.setOldPrefix(path);
156 	}
157 
158 	@Option(name = "--dst-prefix", usage = "usage_dstPrefix")
159 	void dstPrefix(String path) {
160 		diffFmt.setNewPrefix(path);
161 	}
162 
163 	@Option(name = "--no-prefix", usage = "usage_noPrefix")
164 	void noPrefix(@SuppressWarnings("unused") boolean on) {
165 		diffFmt.setOldPrefix(""); //$NON-NLS-1$
166 		diffFmt.setNewPrefix(""); //$NON-NLS-1$
167 	}
168 
169 	// END -- Options shared with Log
170 
171 	@Override
172 	protected void init(final Repository repository, final String gitDir) {
173 		super.init(repository, gitDir);
174 		diffFmt = new DiffFormatter(new BufferedOutputStream(outs));
175 	}
176 
177 	@Override
178 	protected void run() throws Exception {
179 		diffFmt.setRepository(db);
180 		try {
181 			if (cached) {
182 				if (oldTree == null) {
183 					ObjectId head = db.resolve(HEAD + "^{tree}"); //$NON-NLS-1$
184 					if (head == null)
185 						die(MessageFormat.format(CLIText.get().notATree, HEAD));
186 					CanonicalTreeParser p = new CanonicalTreeParser();
187 					try (ObjectReader reader = db.newObjectReader()) {
188 						p.reset(reader, head);
189 					}
190 					oldTree = p;
191 				}
192 				newTree = new DirCacheIterator(db.readDirCache());
193 			} else if (oldTree == null) {
194 				oldTree = new DirCacheIterator(db.readDirCache());
195 				newTree = new FileTreeIterator(db);
196 			} else if (newTree == null)
197 				newTree = new FileTreeIterator(db);
198 
199 			TextProgressMonitor pm = new TextProgressMonitor(errw);
200 			pm.setDelayStart(2, TimeUnit.SECONDS);
201 			diffFmt.setProgressMonitor(pm);
202 			diffFmt.setPathFilter(pathFilter);
203 			if (detectRenames != null)
204 				diffFmt.setDetectRenames(detectRenames.booleanValue());
205 			if (renameLimit != null && diffFmt.isDetectRenames()) {
206 				RenameDetector rd = diffFmt.getRenameDetector();
207 				rd.setRenameLimit(renameLimit.intValue());
208 			}
209 
210 			if (showNameAndStatusOnly) {
211 				nameStatus(outw, diffFmt.scan(oldTree, newTree));
212 				outw.flush();
213 
214 			} else {
215 				diffFmt.format(oldTree, newTree);
216 				diffFmt.flush();
217 			}
218 		} finally {
219 			diffFmt.close();
220 		}
221 	}
222 
223 	static void nameStatus(ThrowingPrintWriter out, List<DiffEntry> files)
224 			throws IOException {
225 		for (DiffEntry ent : files) {
226 			switch (ent.getChangeType()) {
227 			case ADD:
228 				out.println("A\t" + ent.getNewPath()); //$NON-NLS-1$
229 				break;
230 			case DELETE:
231 				out.println("D\t" + ent.getOldPath()); //$NON-NLS-1$
232 				break;
233 			case MODIFY:
234 				out.println("M\t" + ent.getNewPath()); //$NON-NLS-1$
235 				break;
236 			case COPY:
237 				out.format("C%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
238 						ent.getOldPath(), ent.getNewPath());
239 				out.println();
240 				break;
241 			case RENAME:
242 				out.format("R%1$03d\t%2$s\t%3$s", valueOf(ent.getScore()), // //$NON-NLS-1$
243 						ent.getOldPath(), ent.getNewPath());
244 				out.println();
245 				break;
246 			}
247 		}
248 	}
249 }