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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.HashMap;
  53. import org.eclipse.jgit.JGitText;
  54. import org.eclipse.jgit.errors.CheckoutConflictException;
  55. import org.eclipse.jgit.lib.GitIndex.Entry;
  56. /**
  57. * This class handles checking out one or two trees merging
  58. * with the index (actually a tree too).
  59. *
  60. * Three-way merges are no performed. See {@link #setFailOnConflict(boolean)}.
  61. *
  62. * @deprecated Use org.eclipse.jgit.dircache.DirCacheCheckout.
  63. */
  64. @Deprecated
  65. public class WorkDirCheckout {
  66. File root;
  67. GitIndex index;
  68. private boolean failOnConflict = true;
  69. Tree merge;
  70. /**
  71. * If <code>true</code>, will scan first to see if it's possible to check out,
  72. * otherwise throw {@link CheckoutConflictException}. If <code>false</code>,
  73. * it will silently deal with the problem.
  74. * @param failOnConflict
  75. */
  76. public void setFailOnConflict(boolean failOnConflict) {
  77. this.failOnConflict = failOnConflict;
  78. }
  79. WorkDirCheckout(Repository repo, File workDir,
  80. GitIndex oldIndex, GitIndex newIndex) throws IOException {
  81. this.root = workDir;
  82. this.index = oldIndex;
  83. this.merge = repo.mapTree(newIndex.writeTree());
  84. }
  85. /**
  86. * Create a checkout class for checking out one tree, merging with the index
  87. *
  88. * @param repo
  89. * @param root workdir
  90. * @param index current index
  91. * @param merge tree to check out
  92. */
  93. public WorkDirCheckout(Repository repo, File root,
  94. GitIndex index, Tree merge) {
  95. this.root = root;
  96. this.index = index;
  97. this.merge = merge;
  98. }
  99. /**
  100. * Create a checkout class for merging and checking our two trees and the index.
  101. *
  102. * @param repo
  103. * @param root workdir
  104. * @param head
  105. * @param index
  106. * @param merge
  107. */
  108. public WorkDirCheckout(Repository repo, File root, Tree head, GitIndex index, Tree merge) {
  109. this(repo, root, index, merge);
  110. this.head = head;
  111. }
  112. /**
  113. * Execute this checkout
  114. *
  115. * @throws IOException
  116. */
  117. public void checkout() throws IOException {
  118. if (head == null)
  119. prescanOneTree();
  120. else prescanTwoTrees();
  121. if (!conflicts.isEmpty()) {
  122. if (failOnConflict) {
  123. String[] entries = conflicts.toArray(new String[0]);
  124. throw new CheckoutConflictException(entries);
  125. }
  126. }
  127. cleanUpConflicts();
  128. if (head == null)
  129. checkoutOutIndexNoHead();
  130. else checkoutTwoTrees();
  131. }
  132. private void checkoutTwoTrees() throws FileNotFoundException, IOException {
  133. for (String path : removed) {
  134. index.remove(root, new File(root, path));
  135. }
  136. for (java.util.Map.Entry<String, ObjectId> entry : updated.entrySet()) {
  137. Entry newEntry = index.addEntry(merge.findBlobMember(entry.getKey()));
  138. index.checkoutEntry(root, newEntry);
  139. }
  140. }
  141. ArrayList<String> conflicts = new ArrayList<String>();
  142. ArrayList<String> removed = new ArrayList<String>();
  143. Tree head = null;
  144. HashMap<String, ObjectId> updated = new HashMap<String, ObjectId>();
  145. private void checkoutOutIndexNoHead() throws IOException {
  146. new IndexTreeWalker(index, merge, root, new AbstractIndexTreeVisitor() {
  147. public void visitEntry(TreeEntry m, Entry i, File f) throws IOException {
  148. // TODO remove this once we support submodules
  149. if (f.getName().equals(".gitmodules"))
  150. throw new UnsupportedOperationException(
  151. JGitText.get().submodulesNotSupported);
  152. if (m == null) {
  153. index.remove(root, f);
  154. return;
  155. }
  156. boolean needsCheckout = false;
  157. if (i == null)
  158. needsCheckout = true;
  159. else if (i.getObjectId().equals(m.getId())) {
  160. if (i.isModified(root, true))
  161. needsCheckout = true;
  162. } else needsCheckout = true;
  163. if (needsCheckout) {
  164. Entry newEntry = index.addEntry(m);
  165. index.checkoutEntry(root, newEntry);
  166. }
  167. }
  168. }).walk();
  169. }
  170. private void cleanUpConflicts() throws CheckoutConflictException {
  171. for (String c : conflicts) {
  172. File conflict = new File(root, c);
  173. if (!conflict.delete())
  174. throw new CheckoutConflictException(MessageFormat.format(JGitText.get().cannotDeleteFile, c));
  175. removeEmptyParents(conflict);
  176. }
  177. for (String r : removed) {
  178. File file = new File(root, r);
  179. file.delete();
  180. removeEmptyParents(file);
  181. }
  182. }
  183. private void removeEmptyParents(File f) {
  184. File parentFile = f.getParentFile();
  185. while (!parentFile.equals(root)) {
  186. if (parentFile.list().length == 0)
  187. parentFile.delete();
  188. else break;
  189. parentFile = parentFile.getParentFile();
  190. }
  191. }
  192. void prescanOneTree() throws IOException {
  193. new IndexTreeWalker(index, merge, root, new AbstractIndexTreeVisitor() {
  194. public void visitEntry(TreeEntry m, Entry i, File file) throws IOException {
  195. if (m != null) {
  196. if (!file.isFile()) {
  197. checkConflictsWithFile(file);
  198. }
  199. } else {
  200. if (file.exists()) {
  201. removed.add(i.getName());
  202. conflicts.remove(i.getName());
  203. }
  204. }
  205. }
  206. }).walk();
  207. conflicts.removeAll(removed);
  208. }
  209. private ArrayList<String> listFiles(File file) {
  210. ArrayList<String> list = new ArrayList<String>();
  211. listFiles(file, list);
  212. return list;
  213. }
  214. private void listFiles(File dir, ArrayList<String> list) {
  215. for (File f : dir.listFiles()) {
  216. if (f.isDirectory())
  217. listFiles(f, list);
  218. else {
  219. list.add(Repository.stripWorkDir(root, f));
  220. }
  221. }
  222. }
  223. /**
  224. * @return a list of conflicts created by this checkout
  225. */
  226. public ArrayList<String> getConflicts() {
  227. return conflicts;
  228. }
  229. /**
  230. * @return a list of all files removed by this checkout
  231. */
  232. public ArrayList<String> getRemoved() {
  233. return removed;
  234. }
  235. void prescanTwoTrees() throws IOException {
  236. new IndexTreeWalker(index, head, merge, root, new AbstractIndexTreeVisitor() {
  237. public void visitEntry(TreeEntry treeEntry, TreeEntry auxEntry,
  238. Entry indexEntry, File file) throws IOException {
  239. if (treeEntry instanceof Tree || auxEntry instanceof Tree) {
  240. throw new IllegalArgumentException(JGitText.get().cantPassMeATree);
  241. }
  242. processEntry(treeEntry, auxEntry, indexEntry);
  243. }
  244. @Override
  245. public void finishVisitTree(Tree tree, Tree auxTree, String curDir) throws IOException {
  246. if (curDir.length() == 0) return;
  247. if (auxTree != null) {
  248. if (index.getEntry(curDir) != null)
  249. removed.add(curDir);
  250. }
  251. }
  252. }).walk();
  253. // if there's a conflict, don't list it under
  254. // to-be-removed, since that messed up our next
  255. // section
  256. removed.removeAll(conflicts);
  257. for (String path : updated.keySet()) {
  258. if (index.getEntry(path) == null) {
  259. File file = new File(root, path);
  260. if (file.isFile())
  261. conflicts.add(path);
  262. else if (file.isDirectory()) {
  263. checkConflictsWithFile(file);
  264. }
  265. }
  266. }
  267. conflicts.removeAll(removed);
  268. }
  269. void processEntry(TreeEntry h, TreeEntry m, Entry i) throws IOException {
  270. ObjectId iId = (i == null ? null : i.getObjectId());
  271. ObjectId mId = (m == null ? null : m.getId());
  272. ObjectId hId = (h == null ? null : h.getId());
  273. String name = (i != null ? i.getName() :
  274. (h != null ? h.getFullName() :
  275. m.getFullName()));
  276. if (i == null) {
  277. /*
  278. I (index) H M Result
  279. -------------------------------------------------------
  280. 0 nothing nothing nothing (does not happen)
  281. 1 nothing nothing exists use M
  282. 2 nothing exists nothing remove path from index
  283. 3 nothing exists exists use M */
  284. if (h == null) {
  285. updated.put(name,mId);
  286. } else if (m == null) {
  287. removed.add(name);
  288. } else {
  289. updated.put(name, mId);
  290. }
  291. } else if (h == null) {
  292. /*
  293. clean I==H I==M H M Result
  294. -----------------------------------------------------
  295. 4 yes N/A N/A nothing nothing keep index
  296. 5 no N/A N/A nothing nothing keep index
  297. 6 yes N/A yes nothing exists keep index
  298. 7 no N/A yes nothing exists keep index
  299. 8 yes N/A no nothing exists fail
  300. 9 no N/A no nothing exists fail */
  301. if (m == null || mId.equals(iId)) {
  302. if (hasParentBlob(merge, name)) {
  303. if (i.isModified(root, true)) {
  304. conflicts.add(name);
  305. } else {
  306. removed.add(name);
  307. }
  308. }
  309. } else {
  310. conflicts.add(name);
  311. }
  312. } else if (m == null) {
  313. /*
  314. 10 yes yes N/A exists nothing remove path from index
  315. 11 no yes N/A exists nothing fail
  316. 12 yes no N/A exists nothing fail
  317. 13 no no N/A exists nothing fail
  318. */
  319. if (hId.equals(iId)) {
  320. if (i.isModified(root, true)) {
  321. conflicts.add(name);
  322. } else {
  323. removed.add(name);
  324. }
  325. } else {
  326. conflicts.add(name);
  327. }
  328. } else {
  329. if (!hId.equals(mId) && !hId.equals(iId)
  330. && !mId.equals(iId)) {
  331. conflicts.add(name);
  332. } else if (hId.equals(iId) && !mId.equals(iId)) {
  333. if (i.isModified(root, true))
  334. conflicts.add(name);
  335. else updated.put(name, mId);
  336. }
  337. }
  338. }
  339. private boolean hasParentBlob(Tree t, String name) throws IOException {
  340. if (name.indexOf("/") == -1) return false;
  341. String parent = name.substring(0, name.lastIndexOf("/"));
  342. if (t.findBlobMember(parent) != null)
  343. return true;
  344. return hasParentBlob(t, parent);
  345. }
  346. private void checkConflictsWithFile(File file) {
  347. if (file.isDirectory()) {
  348. ArrayList<String> childFiles = listFiles(file);
  349. conflicts.addAll(childFiles);
  350. } else {
  351. File parent = file.getParentFile();
  352. while (!parent.equals(root)) {
  353. if (parent.isDirectory())
  354. break;
  355. if (parent.isFile()) {
  356. conflicts.add(Repository.stripWorkDir(root, parent));
  357. break;
  358. }
  359. parent = parent.getParentFile();
  360. }
  361. }
  362. }
  363. }