You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DiffEntry.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. import java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Arrays;
  47. import java.util.List;
  48. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.FileMode;
  51. import org.eclipse.jgit.lib.MutableObjectId;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.treewalk.TreeWalk;
  54. /** A value class representing a change to a file */
  55. public class DiffEntry {
  56. private static final AbbreviatedObjectId A_ZERO = AbbreviatedObjectId
  57. .fromObjectId(ObjectId.zeroId());
  58. /** Magical file name used for file adds or deletes. */
  59. public static final String DEV_NULL = "/dev/null";
  60. /** General type of change a single file-level patch describes. */
  61. public static enum ChangeType {
  62. /** Add a new file to the project */
  63. ADD,
  64. /** Modify an existing file in the project (content and/or mode) */
  65. MODIFY,
  66. /** Delete an existing file from the project */
  67. DELETE,
  68. /** Rename an existing file to a new location */
  69. RENAME,
  70. /** Copy an existing file to a new location, keeping the original */
  71. COPY;
  72. }
  73. /**
  74. * Convert the TreeWalk into DiffEntry headers.
  75. *
  76. * @param walk
  77. * the TreeWalk to walk through. Must have exactly two trees.
  78. * @return headers describing the changed files.
  79. * @throws IOException
  80. * the repository cannot be accessed.
  81. */
  82. public static List<DiffEntry> scan(TreeWalk walk) throws IOException {
  83. List<DiffEntry> r = new ArrayList<DiffEntry>();
  84. MutableObjectId idBuf = new MutableObjectId();
  85. while (walk.next()) {
  86. DiffEntry entry = new DiffEntry();
  87. walk.getObjectId(idBuf, 0);
  88. entry.oldId = AbbreviatedObjectId.fromObjectId(idBuf);
  89. walk.getObjectId(idBuf, 1);
  90. entry.newId = AbbreviatedObjectId.fromObjectId(idBuf);
  91. entry.oldMode = walk.getFileMode(0);
  92. entry.newMode = walk.getFileMode(1);
  93. entry.newName = entry.oldName = walk.getPathString();
  94. if (entry.oldMode == FileMode.MISSING) {
  95. entry.oldName = DiffEntry.DEV_NULL;
  96. entry.changeType = ChangeType.ADD;
  97. r.add(entry);
  98. } else if (entry.newMode == FileMode.MISSING) {
  99. entry.newName = DiffEntry.DEV_NULL;
  100. entry.changeType = ChangeType.DELETE;
  101. r.add(entry);
  102. } else {
  103. entry.changeType = ChangeType.MODIFY;
  104. if (RenameDetector.sameType(entry.oldMode, entry.newMode))
  105. r.add(entry);
  106. else
  107. r.addAll(breakModify(entry));
  108. }
  109. }
  110. return r;
  111. }
  112. static DiffEntry add(String path, AnyObjectId id) {
  113. DiffEntry e = new DiffEntry();
  114. e.oldId = A_ZERO;
  115. e.oldMode = FileMode.MISSING;
  116. e.oldName = DEV_NULL;
  117. e.newId = AbbreviatedObjectId.fromObjectId(id);
  118. e.newMode = FileMode.REGULAR_FILE;
  119. e.newName = path;
  120. e.changeType = ChangeType.ADD;
  121. return e;
  122. }
  123. static DiffEntry delete(String path, AnyObjectId id) {
  124. DiffEntry e = new DiffEntry();
  125. e.oldId = AbbreviatedObjectId.fromObjectId(id);
  126. e.oldMode = FileMode.REGULAR_FILE;
  127. e.oldName = path;
  128. e.newId = A_ZERO;
  129. e.newMode = FileMode.MISSING;
  130. e.newName = DEV_NULL;
  131. e.changeType = ChangeType.DELETE;
  132. return e;
  133. }
  134. static DiffEntry modify(String path) {
  135. DiffEntry e = new DiffEntry();
  136. e.oldMode = FileMode.REGULAR_FILE;
  137. e.oldName = path;
  138. e.newMode = FileMode.REGULAR_FILE;
  139. e.newName = path;
  140. e.changeType = ChangeType.MODIFY;
  141. return e;
  142. }
  143. static List<DiffEntry> breakModify(DiffEntry entry) {
  144. DiffEntry del = new DiffEntry();
  145. del.oldId = entry.getOldId();
  146. del.oldMode = entry.getOldMode();
  147. del.oldName = entry.getOldName();
  148. del.newId = A_ZERO;
  149. del.newMode = FileMode.MISSING;
  150. del.newName = DiffEntry.DEV_NULL;
  151. del.changeType = ChangeType.DELETE;
  152. DiffEntry add = new DiffEntry();
  153. add.oldId = A_ZERO;
  154. add.oldMode = FileMode.MISSING;
  155. add.oldName = DiffEntry.DEV_NULL;
  156. add.newId = entry.getNewId();
  157. add.newMode = entry.getNewMode();
  158. add.newName = entry.getNewName();
  159. add.changeType = ChangeType.ADD;
  160. return Arrays.asList(del, add);
  161. }
  162. static DiffEntry pair(ChangeType changeType, DiffEntry src, DiffEntry dst,
  163. int score) {
  164. DiffEntry r = new DiffEntry();
  165. r.oldId = src.oldId;
  166. r.oldMode = src.oldMode;
  167. r.oldName = src.oldName;
  168. r.newId = dst.newId;
  169. r.newMode = dst.newMode;
  170. r.newName = dst.newName;
  171. r.changeType = changeType;
  172. r.score = score;
  173. return r;
  174. }
  175. /** File name of the old (pre-image). */
  176. protected String oldName;
  177. /** File name of the new (post-image). */
  178. protected String newName;
  179. /** Old mode of the file, if described by the patch, else null. */
  180. protected FileMode oldMode;
  181. /** New mode of the file, if described by the patch, else null. */
  182. protected FileMode newMode;
  183. /** General type of change indicated by the patch. */
  184. protected ChangeType changeType;
  185. /** Similarity score if {@link #changeType} is a copy or rename. */
  186. protected int score;
  187. /** ObjectId listed on the index line for the old (pre-image) */
  188. protected AbbreviatedObjectId oldId;
  189. /** ObjectId listed on the index line for the new (post-image) */
  190. protected AbbreviatedObjectId newId;
  191. /**
  192. * Get the old name associated with this file.
  193. * <p>
  194. * The meaning of the old name can differ depending on the semantic meaning
  195. * of this patch:
  196. * <ul>
  197. * <li><i>file add</i>: always <code>/dev/null</code></li>
  198. * <li><i>file modify</i>: always {@link #getNewName()}</li>
  199. * <li><i>file delete</i>: always the file being deleted</li>
  200. * <li><i>file copy</i>: source file the copy originates from</li>
  201. * <li><i>file rename</i>: source file the rename originates from</li>
  202. * </ul>
  203. *
  204. * @return old name for this file.
  205. */
  206. public String getOldName() {
  207. return oldName;
  208. }
  209. /**
  210. * Get the new name associated with this file.
  211. * <p>
  212. * The meaning of the new name can differ depending on the semantic meaning
  213. * of this patch:
  214. * <ul>
  215. * <li><i>file add</i>: always the file being created</li>
  216. * <li><i>file modify</i>: always {@link #getOldName()}</li>
  217. * <li><i>file delete</i>: always <code>/dev/null</code></li>
  218. * <li><i>file copy</i>: destination file the copy ends up at</li>
  219. * <li><i>file rename</i>: destination file the rename ends up at/li>
  220. * </ul>
  221. *
  222. * @return new name for this file.
  223. */
  224. public String getNewName() {
  225. return newName;
  226. }
  227. /** @return the old file mode, if described in the patch */
  228. public FileMode getOldMode() {
  229. return oldMode;
  230. }
  231. /** @return the new file mode, if described in the patch */
  232. public FileMode getNewMode() {
  233. return newMode;
  234. }
  235. /** @return the type of change this patch makes on {@link #getNewName()} */
  236. public ChangeType getChangeType() {
  237. return changeType;
  238. }
  239. /**
  240. * @return similarity score between {@link #getOldName()} and
  241. * {@link #getNewName()} if {@link #getChangeType()} is
  242. * {@link ChangeType#COPY} or {@link ChangeType#RENAME}.
  243. */
  244. public int getScore() {
  245. return score;
  246. }
  247. /**
  248. * Get the old object id from the <code>index</code>.
  249. *
  250. * @return the object id; null if there is no index line
  251. */
  252. public AbbreviatedObjectId getOldId() {
  253. return oldId;
  254. }
  255. /**
  256. * Get the new object id from the <code>index</code>.
  257. *
  258. * @return the object id; null if there is no index line
  259. */
  260. public AbbreviatedObjectId getNewId() {
  261. return newId;
  262. }
  263. }