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.

WorkDirCheckout.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  3. * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Roger C. Soares <rogersoares@intelinet.com.br>
  5. * Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.File;
  48. import java.io.FileNotFoundException;
  49. import java.io.IOException;
  50. import java.util.ArrayList;
  51. import java.util.HashMap;
  52. import org.eclipse.jgit.errors.CheckoutConflictException;
  53. import org.eclipse.jgit.lib.GitIndex.Entry;
  54. /**
  55. * This class handles checking out one or two trees merging
  56. * with the index (actually a tree too).
  57. *
  58. * Three-way merges are no performed. See {@link #setFailOnConflict(boolean)}.
  59. */
  60. public class WorkDirCheckout {
  61. Repository repo;
  62. File root;
  63. GitIndex index;
  64. private boolean failOnConflict = true;
  65. Tree merge;
  66. /**
  67. * If <code>true</code>, will scan first to see if it's possible to check out,
  68. * otherwise throw {@link CheckoutConflictException}. If <code>false</code>,
  69. * it will silently deal with the problem.
  70. * @param failOnConflict
  71. */
  72. public void setFailOnConflict(boolean failOnConflict) {
  73. this.failOnConflict = failOnConflict;
  74. }
  75. WorkDirCheckout(Repository repo, File workDir,
  76. GitIndex oldIndex, GitIndex newIndex) throws IOException {
  77. this.repo = repo;
  78. this.root = workDir;
  79. this.index = oldIndex;
  80. this.merge = repo.mapTree(newIndex.writeTree());
  81. }
  82. /**
  83. * Create a checkout class for checking out one tree, merging with the index
  84. *
  85. * @param repo
  86. * @param root workdir
  87. * @param index current index
  88. * @param merge tree to check out
  89. */
  90. public WorkDirCheckout(Repository repo, File root,
  91. GitIndex index, Tree merge) {
  92. this.repo = repo;
  93. this.root = root;
  94. this.index = index;
  95. this.merge = merge;
  96. }
  97. /**
  98. * Create a checkout class for merging and checking our two trees and the index.
  99. *
  100. * @param repo
  101. * @param root workdir
  102. * @param head
  103. * @param index
  104. * @param merge
  105. */
  106. public WorkDirCheckout(Repository repo, File root, Tree head, GitIndex index, Tree merge) {
  107. this(repo, root, index, merge);
  108. this.head = head;
  109. }
  110. /**
  111. * Execute this checkout
  112. *
  113. * @throws IOException
  114. */
  115. public void checkout() throws IOException {
  116. if (head == null)
  117. prescanOneTree();
  118. else prescanTwoTrees();
  119. if (!conflicts.isEmpty()) {
  120. if (failOnConflict) {
  121. String[] entries = conflicts.toArray(new String[0]);
  122. throw new CheckoutConflictException(entries);
  123. }
  124. }
  125. cleanUpConflicts();
  126. if (head == null)
  127. checkoutOutIndexNoHead();
  128. else checkoutTwoTrees();
  129. }
  130. private void checkoutTwoTrees() throws FileNotFoundException, IOException {
  131. for (String path : removed) {
  132. index.remove(root, new File(root, path));
  133. }
  134. for (java.util.Map.Entry<String, ObjectId> entry : updated.entrySet()) {
  135. Entry newEntry = index.addEntry(merge.findBlobMember(entry.getKey()));
  136. index.checkoutEntry(root, newEntry);
  137. }
  138. }
  139. ArrayList<String> conflicts = new ArrayList<String>();
  140. ArrayList<String> removed = new ArrayList<String>();
  141. Tree head = null;
  142. HashMap<String, ObjectId> updated = new HashMap<String, ObjectId>();
  143. private void checkoutOutIndexNoHead() throws IOException {
  144. new IndexTreeWalker(index, merge, root, new AbstractIndexTreeVisitor() {
  145. public void visitEntry(TreeEntry m, Entry i, File f) throws IOException {
  146. if (m == null) {
  147. index.remove(root, f);
  148. return;
  149. }
  150. boolean needsCheckout = false;
  151. if (i == null)
  152. needsCheckout = true;
  153. else if (i.getObjectId().equals(m.getId())) {
  154. if (i.isModified(root, true))
  155. needsCheckout = true;
  156. } else needsCheckout = true;
  157. if (needsCheckout) {
  158. Entry newEntry = index.addEntry(m);
  159. index.checkoutEntry(root, newEntry);
  160. }
  161. }
  162. }).walk();
  163. }
  164. private void cleanUpConflicts() throws CheckoutConflictException {
  165. for (String c : conflicts) {
  166. File conflict = new File(root, c);
  167. if (!conflict.delete())
  168. throw new CheckoutConflictException("Cannot delete file: " + c);
  169. removeEmptyParents(conflict);
  170. }
  171. for (String r : removed) {
  172. File file = new File(root, r);
  173. file.delete();
  174. removeEmptyParents(file);
  175. }
  176. }
  177. private void removeEmptyParents(File f) {
  178. File parentFile = f.getParentFile();
  179. while (!parentFile.equals(root)) {
  180. if (parentFile.list().length == 0)
  181. parentFile.delete();
  182. else break;
  183. parentFile = parentFile.getParentFile();
  184. }
  185. }
  186. void prescanOneTree() throws IOException {
  187. new IndexTreeWalker(index, merge, root, new AbstractIndexTreeVisitor() {
  188. public void visitEntry(TreeEntry m, Entry i, File file) throws IOException {
  189. if (m != null) {
  190. if (!file.isFile()) {
  191. checkConflictsWithFile(file);
  192. }
  193. } else {
  194. if (file.exists()) {
  195. removed.add(i.getName());
  196. conflicts.remove(i.getName());
  197. }
  198. }
  199. }
  200. }).walk();
  201. conflicts.removeAll(removed);
  202. }
  203. private ArrayList<String> listFiles(File file) {
  204. ArrayList<String> list = new ArrayList<String>();
  205. listFiles(file, list);
  206. return list;
  207. }
  208. private void listFiles(File dir, ArrayList<String> list) {
  209. for (File f : dir.listFiles()) {
  210. if (f.isDirectory())
  211. listFiles(f, list);
  212. else {
  213. list.add(Repository.stripWorkDir(root, f));
  214. }
  215. }
  216. }
  217. /**
  218. * @return a list of conflicts created by this checkout
  219. */
  220. public ArrayList<String> getConflicts() {
  221. return conflicts;
  222. }
  223. /**
  224. * @return a list of all files removed by this checkout
  225. */
  226. public ArrayList<String> getRemoved() {
  227. return removed;
  228. }
  229. void prescanTwoTrees() throws IOException {
  230. new IndexTreeWalker(index, head, merge, root, new AbstractIndexTreeVisitor() {
  231. public void visitEntry(TreeEntry treeEntry, TreeEntry auxEntry,
  232. Entry indexEntry, File file) throws IOException {
  233. if (treeEntry instanceof Tree || auxEntry instanceof Tree) {
  234. throw new IllegalArgumentException("Can't pass me a tree!");
  235. }
  236. processEntry(treeEntry, auxEntry, indexEntry);
  237. }
  238. @Override
  239. public void finishVisitTree(Tree tree, Tree auxTree, String curDir) throws IOException {
  240. if (curDir.length() == 0) return;
  241. if (auxTree != null) {
  242. if (index.getEntry(curDir) != null)
  243. removed.add(curDir);
  244. }
  245. }
  246. }).walk();
  247. // if there's a conflict, don't list it under
  248. // to-be-removed, since that messed up our next
  249. // section
  250. removed.removeAll(conflicts);
  251. for (String path : updated.keySet()) {
  252. if (index.getEntry(path) == null) {
  253. File file = new File(root, path);
  254. if (file.isFile())
  255. conflicts.add(path);
  256. else if (file.isDirectory()) {
  257. checkConflictsWithFile(file);
  258. }
  259. }
  260. }
  261. conflicts.removeAll(removed);
  262. }
  263. void processEntry(TreeEntry h, TreeEntry m, Entry i) throws IOException {
  264. ObjectId iId = (i == null ? null : i.getObjectId());
  265. ObjectId mId = (m == null ? null : m.getId());
  266. ObjectId hId = (h == null ? null : h.getId());
  267. String name = (i != null ? i.getName() :
  268. (h != null ? h.getFullName() :
  269. m.getFullName()));
  270. if (i == null) {
  271. /*
  272. I (index) H M Result
  273. -------------------------------------------------------
  274. 0 nothing nothing nothing (does not happen)
  275. 1 nothing nothing exists use M
  276. 2 nothing exists nothing remove path from index
  277. 3 nothing exists exists use M */
  278. if (h == null) {
  279. updated.put(name,mId);
  280. } else if (m == null) {
  281. removed.add(name);
  282. } else {
  283. updated.put(name, mId);
  284. }
  285. } else if (h == null) {
  286. /*
  287. clean I==H I==M H M Result
  288. -----------------------------------------------------
  289. 4 yes N/A N/A nothing nothing keep index
  290. 5 no N/A N/A nothing nothing keep index
  291. 6 yes N/A yes nothing exists keep index
  292. 7 no N/A yes nothing exists keep index
  293. 8 yes N/A no nothing exists fail
  294. 9 no N/A no nothing exists fail */
  295. if (m == null || mId.equals(iId)) {
  296. if (hasParentBlob(merge, name)) {
  297. if (i.isModified(root, true)) {
  298. conflicts.add(name);
  299. } else {
  300. removed.add(name);
  301. }
  302. }
  303. } else {
  304. conflicts.add(name);
  305. }
  306. } else if (m == null) {
  307. /*
  308. 10 yes yes N/A exists nothing remove path from index
  309. 11 no yes N/A exists nothing fail
  310. 12 yes no N/A exists nothing fail
  311. 13 no no N/A exists nothing fail
  312. */
  313. if (hId.equals(iId)) {
  314. if (i.isModified(root, true)) {
  315. conflicts.add(name);
  316. } else {
  317. removed.add(name);
  318. }
  319. } else {
  320. conflicts.add(name);
  321. }
  322. } else {
  323. if (!hId.equals(mId) && !hId.equals(iId)
  324. && !mId.equals(iId)) {
  325. conflicts.add(name);
  326. } else if (hId.equals(iId) && !mId.equals(iId)) {
  327. if (i.isModified(root, true))
  328. conflicts.add(name);
  329. else updated.put(name, mId);
  330. }
  331. }
  332. }
  333. private boolean hasParentBlob(Tree t, String name) throws IOException {
  334. if (name.indexOf("/") == -1) return false;
  335. String parent = name.substring(0, name.lastIndexOf("/"));
  336. if (t.findBlobMember(parent) != null)
  337. return true;
  338. return hasParentBlob(t, parent);
  339. }
  340. private void checkConflictsWithFile(File file) {
  341. if (file.isDirectory()) {
  342. ArrayList<String> childFiles = listFiles(file);
  343. conflicts.addAll(childFiles);
  344. } else {
  345. File parent = file.getParentFile();
  346. while (!parent.equals(root)) {
  347. if (parent.isDirectory())
  348. break;
  349. if (parent.isFile()) {
  350. conflicts.add(Repository.stripWorkDir(root, parent));
  351. break;
  352. }
  353. parent = parent.getParentFile();
  354. }
  355. }
  356. }
  357. }