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.

DirCacheCheckout.java 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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. * Copyright (C) 2010, Chrisian Halstrick <christian.halstrick@sap.com> and
  7. * other copyright owners as documented in the project's IP log.
  8. *
  9. * This program and the accompanying materials are made available under the
  10. * terms of the Eclipse Distribution License v1.0 which accompanies this
  11. * distribution, is reproduced below, and is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.php
  13. *
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice, this
  20. * list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
  27. * contributors may be used to endorse or promote products derived from this
  28. * software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. */
  42. package org.eclipse.jgit.dircache;
  43. import static org.eclipse.jgit.treewalk.TreeWalk.OperationType.CHECKOUT_OP;
  44. import java.io.File;
  45. import java.io.FileOutputStream;
  46. import java.io.IOException;
  47. import java.io.OutputStream;
  48. import java.nio.file.StandardCopyOption;
  49. import java.text.MessageFormat;
  50. import java.util.ArrayList;
  51. import java.util.HashMap;
  52. import java.util.Iterator;
  53. import java.util.List;
  54. import java.util.Map;
  55. import org.eclipse.jgit.api.errors.CanceledException;
  56. import org.eclipse.jgit.api.errors.FilterFailedException;
  57. import org.eclipse.jgit.attributes.FilterCommand;
  58. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  59. import org.eclipse.jgit.errors.CheckoutConflictException;
  60. import org.eclipse.jgit.errors.CorruptObjectException;
  61. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  62. import org.eclipse.jgit.errors.IndexWriteException;
  63. import org.eclipse.jgit.errors.MissingObjectException;
  64. import org.eclipse.jgit.events.WorkingTreeModifiedEvent;
  65. import org.eclipse.jgit.internal.JGitText;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  68. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  69. import org.eclipse.jgit.lib.CoreConfig.SymLinks;
  70. import org.eclipse.jgit.lib.FileMode;
  71. import org.eclipse.jgit.lib.NullProgressMonitor;
  72. import org.eclipse.jgit.lib.ObjectChecker;
  73. import org.eclipse.jgit.lib.ObjectId;
  74. import org.eclipse.jgit.lib.ObjectLoader;
  75. import org.eclipse.jgit.lib.ObjectReader;
  76. import org.eclipse.jgit.lib.ProgressMonitor;
  77. import org.eclipse.jgit.lib.Repository;
  78. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  79. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  80. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  81. import org.eclipse.jgit.treewalk.FileTreeIterator;
  82. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  83. import org.eclipse.jgit.treewalk.TreeWalk;
  84. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  85. import org.eclipse.jgit.treewalk.WorkingTreeOptions;
  86. import org.eclipse.jgit.treewalk.filter.PathFilter;
  87. import org.eclipse.jgit.util.FS;
  88. import org.eclipse.jgit.util.FS.ExecutionResult;
  89. import org.eclipse.jgit.util.FileUtils;
  90. import org.eclipse.jgit.util.IntList;
  91. import org.eclipse.jgit.util.RawParseUtils;
  92. import org.eclipse.jgit.util.SystemReader;
  93. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  94. import org.slf4j.Logger;
  95. import org.slf4j.LoggerFactory;
  96. /**
  97. * This class handles checking out one or two trees merging with the index.
  98. */
  99. public class DirCacheCheckout {
  100. private static Logger LOG = LoggerFactory.getLogger(DirCacheCheckout.class);
  101. private static final int MAX_EXCEPTION_TEXT_SIZE = 10 * 1024;
  102. /**
  103. * Metadata used in checkout process
  104. *
  105. * @since 4.3
  106. */
  107. public static class CheckoutMetadata {
  108. /** git attributes */
  109. public final EolStreamType eolStreamType;
  110. /** filter command to apply */
  111. public final String smudgeFilterCommand;
  112. /**
  113. * @param eolStreamType
  114. * @param smudgeFilterCommand
  115. */
  116. public CheckoutMetadata(EolStreamType eolStreamType,
  117. String smudgeFilterCommand) {
  118. this.eolStreamType = eolStreamType;
  119. this.smudgeFilterCommand = smudgeFilterCommand;
  120. }
  121. static CheckoutMetadata EMPTY = new CheckoutMetadata(
  122. EolStreamType.DIRECT, null);
  123. }
  124. private Repository repo;
  125. private HashMap<String, CheckoutMetadata> updated = new HashMap<>();
  126. private ArrayList<String> conflicts = new ArrayList<>();
  127. private ArrayList<String> removed = new ArrayList<>();
  128. private ObjectId mergeCommitTree;
  129. private DirCache dc;
  130. private DirCacheBuilder builder;
  131. private NameConflictTreeWalk walk;
  132. private ObjectId headCommitTree;
  133. private WorkingTreeIterator workingTree;
  134. private boolean failOnConflict = true;
  135. private ArrayList<String> toBeDeleted = new ArrayList<>();
  136. private boolean emptyDirCache;
  137. private boolean performingCheckout;
  138. private ProgressMonitor monitor = NullProgressMonitor.INSTANCE;
  139. /**
  140. * Get list of updated paths and smudgeFilterCommands
  141. *
  142. * @return a list of updated paths and smudgeFilterCommands
  143. */
  144. public Map<String, CheckoutMetadata> getUpdated() {
  145. return updated;
  146. }
  147. /**
  148. * Get a list of conflicts created by this checkout
  149. *
  150. * @return a list of conflicts created by this checkout
  151. */
  152. public List<String> getConflicts() {
  153. return conflicts;
  154. }
  155. /**
  156. * Get list of paths of files which couldn't be deleted during last call to
  157. * {@link #checkout()}
  158. *
  159. * @return a list of paths (relative to the start of the working tree) of
  160. * files which couldn't be deleted during last call to
  161. * {@link #checkout()} . {@link #checkout()} detected that these
  162. * files should be deleted but the deletion in the filesystem failed
  163. * (e.g. because a file was locked). To have a consistent state of
  164. * the working tree these files have to be deleted by the callers of
  165. * {@link org.eclipse.jgit.dircache.DirCacheCheckout}.
  166. */
  167. public List<String> getToBeDeleted() {
  168. return toBeDeleted;
  169. }
  170. /**
  171. * Get list of all files removed by this checkout
  172. *
  173. * @return a list of all files removed by this checkout
  174. */
  175. public List<String> getRemoved() {
  176. return removed;
  177. }
  178. /**
  179. * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
  180. * and mergeCommitTree) and the index.
  181. *
  182. * @param repo
  183. * the repository in which we do the checkout
  184. * @param headCommitTree
  185. * the id of the tree of the head commit
  186. * @param dc
  187. * the (already locked) Dircache for this repo
  188. * @param mergeCommitTree
  189. * the id of the tree we want to fast-forward to
  190. * @param workingTree
  191. * an iterator over the repositories Working Tree
  192. * @throws java.io.IOException
  193. */
  194. public DirCacheCheckout(Repository repo, ObjectId headCommitTree, DirCache dc,
  195. ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
  196. throws IOException {
  197. this.repo = repo;
  198. this.dc = dc;
  199. this.headCommitTree = headCommitTree;
  200. this.mergeCommitTree = mergeCommitTree;
  201. this.workingTree = workingTree;
  202. this.emptyDirCache = (dc == null) || (dc.getEntryCount() == 0);
  203. }
  204. /**
  205. * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
  206. * and mergeCommitTree) and the index. As iterator over the working tree
  207. * this constructor creates a standard
  208. * {@link org.eclipse.jgit.treewalk.FileTreeIterator}
  209. *
  210. * @param repo
  211. * the repository in which we do the checkout
  212. * @param headCommitTree
  213. * the id of the tree of the head commit
  214. * @param dc
  215. * the (already locked) Dircache for this repo
  216. * @param mergeCommitTree
  217. * the id of the tree we want to fast-forward to
  218. * @throws java.io.IOException
  219. */
  220. public DirCacheCheckout(Repository repo, ObjectId headCommitTree,
  221. DirCache dc, ObjectId mergeCommitTree) throws IOException {
  222. this(repo, headCommitTree, dc, mergeCommitTree, new FileTreeIterator(repo));
  223. }
  224. /**
  225. * Constructs a DirCacheCeckout for checking out one tree, merging with the
  226. * index.
  227. *
  228. * @param repo
  229. * the repository in which we do the checkout
  230. * @param dc
  231. * the (already locked) Dircache for this repo
  232. * @param mergeCommitTree
  233. * the id of the tree we want to fast-forward to
  234. * @param workingTree
  235. * an iterator over the repositories Working Tree
  236. * @throws java.io.IOException
  237. */
  238. public DirCacheCheckout(Repository repo, DirCache dc,
  239. ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
  240. throws IOException {
  241. this(repo, null, dc, mergeCommitTree, workingTree);
  242. }
  243. /**
  244. * Constructs a DirCacheCeckout for checking out one tree, merging with the
  245. * index. As iterator over the working tree this constructor creates a
  246. * standard {@link org.eclipse.jgit.treewalk.FileTreeIterator}
  247. *
  248. * @param repo
  249. * the repository in which we do the checkout
  250. * @param dc
  251. * the (already locked) Dircache for this repo
  252. * @param mergeCommitTree
  253. * the id of the tree of the
  254. * @throws java.io.IOException
  255. */
  256. public DirCacheCheckout(Repository repo, DirCache dc,
  257. ObjectId mergeCommitTree) throws IOException {
  258. this(repo, null, dc, mergeCommitTree, new FileTreeIterator(repo));
  259. }
  260. /**
  261. * Set a progress monitor which can be passed to built-in filter commands,
  262. * providing progress information for long running tasks.
  263. *
  264. * @param monitor
  265. * the {@link ProgressMonitor}
  266. * @since 4.11
  267. */
  268. public void setProgressMonitor(ProgressMonitor monitor) {
  269. this.monitor = monitor != null ? monitor : NullProgressMonitor.INSTANCE;
  270. }
  271. /**
  272. * Scan head, index and merge tree. Used during normal checkout or merge
  273. * operations.
  274. *
  275. * @throws org.eclipse.jgit.errors.CorruptObjectException
  276. * @throws java.io.IOException
  277. */
  278. public void preScanTwoTrees() throws CorruptObjectException, IOException {
  279. removed.clear();
  280. updated.clear();
  281. conflicts.clear();
  282. walk = new NameConflictTreeWalk(repo);
  283. builder = dc.builder();
  284. addTree(walk, headCommitTree);
  285. addTree(walk, mergeCommitTree);
  286. int dciPos = walk.addTree(new DirCacheBuildIterator(builder));
  287. walk.addTree(workingTree);
  288. workingTree.setDirCacheIterator(walk, dciPos);
  289. while (walk.next()) {
  290. processEntry(walk.getTree(0, CanonicalTreeParser.class),
  291. walk.getTree(1, CanonicalTreeParser.class),
  292. walk.getTree(2, DirCacheBuildIterator.class),
  293. walk.getTree(3, WorkingTreeIterator.class));
  294. if (walk.isSubtree())
  295. walk.enterSubtree();
  296. }
  297. }
  298. private void addTree(TreeWalk tw, ObjectId id) throws MissingObjectException, IncorrectObjectTypeException, IOException {
  299. if (id == null)
  300. tw.addTree(new EmptyTreeIterator());
  301. else
  302. tw.addTree(id);
  303. }
  304. /**
  305. * Scan index and merge tree (no HEAD). Used e.g. for initial checkout when
  306. * there is no head yet.
  307. *
  308. * @throws org.eclipse.jgit.errors.MissingObjectException
  309. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  310. * @throws org.eclipse.jgit.errors.CorruptObjectException
  311. * @throws java.io.IOException
  312. */
  313. public void prescanOneTree()
  314. throws MissingObjectException, IncorrectObjectTypeException,
  315. CorruptObjectException, IOException {
  316. removed.clear();
  317. updated.clear();
  318. conflicts.clear();
  319. builder = dc.builder();
  320. walk = new NameConflictTreeWalk(repo);
  321. addTree(walk, mergeCommitTree);
  322. int dciPos = walk.addTree(new DirCacheBuildIterator(builder));
  323. walk.addTree(workingTree);
  324. workingTree.setDirCacheIterator(walk, dciPos);
  325. while (walk.next()) {
  326. processEntry(walk.getTree(0, CanonicalTreeParser.class),
  327. walk.getTree(1, DirCacheBuildIterator.class),
  328. walk.getTree(2, WorkingTreeIterator.class));
  329. if (walk.isSubtree())
  330. walk.enterSubtree();
  331. }
  332. conflicts.removeAll(removed);
  333. }
  334. /**
  335. * Processing an entry in the context of {@link #prescanOneTree()} when only
  336. * one tree is given
  337. *
  338. * @param m the tree to merge
  339. * @param i the index
  340. * @param f the working tree
  341. * @throws IOException
  342. */
  343. void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i,
  344. WorkingTreeIterator f) throws IOException {
  345. if (m != null) {
  346. checkValidPath(m);
  347. // There is an entry in the merge commit. Means: we want to update
  348. // what's currently in the index and working-tree to that one
  349. if (i == null) {
  350. // The index entry is missing
  351. if (f != null && !FileMode.TREE.equals(f.getEntryFileMode())
  352. && !f.isEntryIgnored()) {
  353. if (failOnConflict) {
  354. // don't overwrite an untracked and not ignored file
  355. conflicts.add(walk.getPathString());
  356. } else {
  357. // failOnConflict is false. Putting something to conflicts
  358. // would mean we delete it. Instead we want the mergeCommit
  359. // content to be checked out.
  360. update(m.getEntryPathString(), m.getEntryObjectId(),
  361. m.getEntryFileMode());
  362. }
  363. } else
  364. update(m.getEntryPathString(), m.getEntryObjectId(),
  365. m.getEntryFileMode());
  366. } else if (f == null || !m.idEqual(i)) {
  367. // The working tree file is missing or the merge content differs
  368. // from index content
  369. update(m.getEntryPathString(), m.getEntryObjectId(),
  370. m.getEntryFileMode());
  371. } else if (i.getDirCacheEntry() != null) {
  372. // The index contains a file (and not a folder)
  373. if (f.isModified(i.getDirCacheEntry(), true,
  374. this.walk.getObjectReader())
  375. || i.getDirCacheEntry().getStage() != 0)
  376. // The working tree file is dirty or the index contains a
  377. // conflict
  378. update(m.getEntryPathString(), m.getEntryObjectId(),
  379. m.getEntryFileMode());
  380. else {
  381. // update the timestamp of the index with the one from the
  382. // file if not set, as we are sure to be in sync here.
  383. DirCacheEntry entry = i.getDirCacheEntry();
  384. if (entry.getLastModified() == 0)
  385. entry.setLastModified(f.getEntryLastModified());
  386. keep(entry);
  387. }
  388. } else
  389. // The index contains a folder
  390. keep(i.getDirCacheEntry());
  391. } else {
  392. // There is no entry in the merge commit. Means: we want to delete
  393. // what's currently in the index and working tree
  394. if (f != null) {
  395. // There is a file/folder for that path in the working tree
  396. if (walk.isDirectoryFileConflict()) {
  397. // We put it in conflicts. Even if failOnConflict is false
  398. // this would cause the path to be deleted. Thats exactly what
  399. // we want in this situation
  400. conflicts.add(walk.getPathString());
  401. } else {
  402. // No file/folder conflict exists. All entries are files or
  403. // all entries are folders
  404. if (i != null) {
  405. // ... and the working tree contained a file or folder
  406. // -> add it to the removed set and remove it from
  407. // conflicts set
  408. remove(i.getEntryPathString());
  409. conflicts.remove(i.getEntryPathString());
  410. } else {
  411. // untracked file, neither contained in tree to merge
  412. // nor in index
  413. }
  414. }
  415. } else {
  416. // There is no file/folder for that path in the working tree,
  417. // nor in the merge head.
  418. // The only entry we have is the index entry. Like the case
  419. // where there is a file with the same name, remove it,
  420. }
  421. }
  422. }
  423. /**
  424. * Execute this checkout. A
  425. * {@link org.eclipse.jgit.events.WorkingTreeModifiedEvent} is fired if the
  426. * working tree was modified; even if the checkout fails.
  427. *
  428. * @return <code>false</code> if this method could not delete all the files
  429. * which should be deleted (e.g. because one of the files was
  430. * locked). In this case {@link #getToBeDeleted()} lists the files
  431. * which should be tried to be deleted outside of this method.
  432. * Although <code>false</code> is returned the checkout was
  433. * successful and the working tree was updated for all other files.
  434. * <code>true</code> is returned when no such problem occurred
  435. * @throws java.io.IOException
  436. */
  437. public boolean checkout() throws IOException {
  438. try {
  439. return doCheckout();
  440. } catch (CanceledException ce) {
  441. // should actually be propagated, but this would change a LOT of
  442. // APIs
  443. throw new IOException(ce);
  444. } finally {
  445. try {
  446. dc.unlock();
  447. } finally {
  448. if (performingCheckout) {
  449. WorkingTreeModifiedEvent event = new WorkingTreeModifiedEvent(
  450. getUpdated().keySet(), getRemoved());
  451. if (!event.isEmpty()) {
  452. repo.fireEvent(event);
  453. }
  454. }
  455. }
  456. }
  457. }
  458. private boolean doCheckout() throws CorruptObjectException, IOException,
  459. MissingObjectException, IncorrectObjectTypeException,
  460. CheckoutConflictException, IndexWriteException, CanceledException {
  461. toBeDeleted.clear();
  462. try (ObjectReader objectReader = repo.getObjectDatabase().newReader()) {
  463. if (headCommitTree != null)
  464. preScanTwoTrees();
  465. else
  466. prescanOneTree();
  467. if (!conflicts.isEmpty()) {
  468. if (failOnConflict)
  469. throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()]));
  470. else
  471. cleanUpConflicts();
  472. }
  473. // update our index
  474. builder.finish();
  475. // init progress reporting
  476. int numTotal = removed.size() + updated.size();
  477. monitor.beginTask(JGitText.get().checkingOutFiles, numTotal);
  478. performingCheckout = true;
  479. File file = null;
  480. String last = null;
  481. // when deleting files process them in the opposite order as they have
  482. // been reported. This ensures the files are deleted before we delete
  483. // their parent folders
  484. IntList nonDeleted = new IntList();
  485. for (int i = removed.size() - 1; i >= 0; i--) {
  486. String r = removed.get(i);
  487. file = new File(repo.getWorkTree(), r);
  488. if (!file.delete() && repo.getFS().exists(file)) {
  489. // The list of stuff to delete comes from the index
  490. // which will only contain a directory if it is
  491. // a submodule, in which case we shall not attempt
  492. // to delete it. A submodule is not empty, so it
  493. // is safe to check this after a failed delete.
  494. if (!repo.getFS().isDirectory(file)) {
  495. nonDeleted.add(i);
  496. toBeDeleted.add(r);
  497. }
  498. } else {
  499. if (last != null && !isSamePrefix(r, last))
  500. removeEmptyParents(new File(repo.getWorkTree(), last));
  501. last = r;
  502. }
  503. monitor.update(1);
  504. if (monitor.isCancelled()) {
  505. throw new CanceledException(MessageFormat.format(
  506. JGitText.get().operationCanceled,
  507. JGitText.get().checkingOutFiles));
  508. }
  509. }
  510. if (file != null) {
  511. removeEmptyParents(file);
  512. }
  513. removed = filterOut(removed, nonDeleted);
  514. nonDeleted = null;
  515. Iterator<Map.Entry<String, CheckoutMetadata>> toUpdate = updated
  516. .entrySet().iterator();
  517. Map.Entry<String, CheckoutMetadata> e = null;
  518. try {
  519. while (toUpdate.hasNext()) {
  520. e = toUpdate.next();
  521. String path = e.getKey();
  522. CheckoutMetadata meta = e.getValue();
  523. DirCacheEntry entry = dc.getEntry(path);
  524. if (!FileMode.GITLINK.equals(entry.getRawMode())) {
  525. checkoutEntry(repo, entry, objectReader, false, meta);
  526. }
  527. e = null;
  528. monitor.update(1);
  529. if (monitor.isCancelled()) {
  530. throw new CanceledException(MessageFormat.format(
  531. JGitText.get().operationCanceled,
  532. JGitText.get().checkingOutFiles));
  533. }
  534. }
  535. } catch (Exception ex) {
  536. // We didn't actually modify the current entry nor any that
  537. // might follow.
  538. if (e != null) {
  539. toUpdate.remove();
  540. }
  541. while (toUpdate.hasNext()) {
  542. e = toUpdate.next();
  543. toUpdate.remove();
  544. }
  545. throw ex;
  546. }
  547. monitor.endTask();
  548. // commit the index builder - a new index is persisted
  549. if (!builder.commit())
  550. throw new IndexWriteException();
  551. }
  552. return toBeDeleted.size() == 0;
  553. }
  554. private static ArrayList<String> filterOut(ArrayList<String> strings,
  555. IntList indicesToRemove) {
  556. int n = indicesToRemove.size();
  557. if (n == strings.size()) {
  558. return new ArrayList<>(0);
  559. }
  560. switch (n) {
  561. case 0:
  562. return strings;
  563. case 1:
  564. strings.remove(indicesToRemove.get(0));
  565. return strings;
  566. default:
  567. int length = strings.size();
  568. ArrayList<String> result = new ArrayList<>(length - n);
  569. // Process indicesToRemove from the back; we know that it
  570. // contains indices in descending order.
  571. int j = n - 1;
  572. int idx = indicesToRemove.get(j);
  573. for (int i = 0; i < length; i++) {
  574. if (i == idx) {
  575. idx = (--j >= 0) ? indicesToRemove.get(j) : -1;
  576. } else {
  577. result.add(strings.get(i));
  578. }
  579. }
  580. return result;
  581. }
  582. }
  583. private static boolean isSamePrefix(String a, String b) {
  584. int as = a.lastIndexOf('/');
  585. int bs = b.lastIndexOf('/');
  586. return a.substring(0, as + 1).equals(b.substring(0, bs + 1));
  587. }
  588. private void removeEmptyParents(File f) {
  589. File parentFile = f.getParentFile();
  590. while (parentFile != null && !parentFile.equals(repo.getWorkTree())) {
  591. if (!parentFile.delete())
  592. break;
  593. parentFile = parentFile.getParentFile();
  594. }
  595. }
  596. /**
  597. * Compares whether two pairs of ObjectId and FileMode are equal.
  598. *
  599. * @param id1
  600. * @param mode1
  601. * @param id2
  602. * @param mode2
  603. * @return <code>true</code> if FileModes and ObjectIds are equal.
  604. * <code>false</code> otherwise
  605. */
  606. private boolean equalIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2,
  607. FileMode mode2) {
  608. if (!mode1.equals(mode2))
  609. return false;
  610. return id1 != null ? id1.equals(id2) : id2 == null;
  611. }
  612. /**
  613. * Here the main work is done. This method is called for each existing path
  614. * in head, index and merge. This method decides what to do with the
  615. * corresponding index entry: keep it, update it, remove it or mark a
  616. * conflict.
  617. *
  618. * @param h
  619. * the entry for the head
  620. * @param m
  621. * the entry for the merge
  622. * @param i
  623. * the entry for the index
  624. * @param f
  625. * the file in the working tree
  626. * @throws IOException
  627. */
  628. void processEntry(CanonicalTreeParser h, CanonicalTreeParser m,
  629. DirCacheBuildIterator i, WorkingTreeIterator f) throws IOException {
  630. DirCacheEntry dce = i != null ? i.getDirCacheEntry() : null;
  631. String name = walk.getPathString();
  632. if (m != null)
  633. checkValidPath(m);
  634. if (i == null && m == null && h == null) {
  635. // File/Directory conflict case #20
  636. if (walk.isDirectoryFileConflict())
  637. // TODO: check whether it is always correct to report a conflict here
  638. conflict(name, null, null, null);
  639. // file only exists in working tree -> ignore it
  640. return;
  641. }
  642. ObjectId iId = (i == null ? null : i.getEntryObjectId());
  643. ObjectId mId = (m == null ? null : m.getEntryObjectId());
  644. ObjectId hId = (h == null ? null : h.getEntryObjectId());
  645. FileMode iMode = (i == null ? null : i.getEntryFileMode());
  646. FileMode mMode = (m == null ? null : m.getEntryFileMode());
  647. FileMode hMode = (h == null ? null : h.getEntryFileMode());
  648. /**
  649. * <pre>
  650. * File/Directory conflicts:
  651. * the following table from ReadTreeTest tells what to do in case of directory/file
  652. * conflicts. I give comments here
  653. *
  654. * H I M Clean H==M H==I I==M Result
  655. * ------------------------------------------------------------------
  656. * 1 D D F Y N Y N Update
  657. * 2 D D F N N Y N Conflict
  658. * 3 D F D Y N N Keep
  659. * 4 D F D N N N Conflict
  660. * 5 D F F Y N N Y Keep
  661. * 5b D F F Y N N N Conflict
  662. * 6 D F F N N N Y Keep
  663. * 6b D F F N N N N Conflict
  664. * 7 F D F Y Y N N Update
  665. * 8 F D F N Y N N Conflict
  666. * 9 F D F N N N Conflict
  667. * 10 F D D N N Y Keep
  668. * 11 F D D N N N Conflict
  669. * 12 F F D Y N Y N Update
  670. * 13 F F D N N Y N Conflict
  671. * 14 F F D N N N Conflict
  672. * 15 0 F D N N N Conflict
  673. * 16 0 D F Y N N N Update
  674. * 17 0 D F N N N Conflict
  675. * 18 F 0 D Update
  676. * 19 D 0 F Update
  677. * 20 0 0 F N (worktree=dir) Conflict
  678. * </pre>
  679. */
  680. // The information whether head,index,merge iterators are currently
  681. // pointing to file/folder/non-existing is encoded into this variable.
  682. //
  683. // To decode write down ffMask in hexadecimal form. The last digit
  684. // represents the state for the merge iterator, the second last the
  685. // state for the index iterator and the third last represents the state
  686. // for the head iterator. The hexadecimal constant "F" stands for
  687. // "file", a "D" stands for "directory" (tree), and a "0" stands for
  688. // non-existing. Symbolic links and git links are treated as File here.
  689. //
  690. // Examples:
  691. // ffMask == 0xFFD -> Head=File, Index=File, Merge=Tree
  692. // ffMask == 0xDD0 -> Head=Tree, Index=Tree, Merge=Non-Existing
  693. int ffMask = 0;
  694. if (h != null)
  695. ffMask = FileMode.TREE.equals(hMode) ? 0xD00 : 0xF00;
  696. if (i != null)
  697. ffMask |= FileMode.TREE.equals(iMode) ? 0x0D0 : 0x0F0;
  698. if (m != null)
  699. ffMask |= FileMode.TREE.equals(mMode) ? 0x00D : 0x00F;
  700. // Check whether we have a possible file/folder conflict. Therefore we
  701. // need a least one file and one folder.
  702. if (((ffMask & 0x222) != 0x000)
  703. && (((ffMask & 0x00F) == 0x00D) || ((ffMask & 0x0F0) == 0x0D0) || ((ffMask & 0xF00) == 0xD00))) {
  704. // There are 3*3*3=27 possible combinations of file/folder
  705. // conflicts. Some of them are not-relevant because
  706. // they represent no conflict, e.g. 0xFFF, 0xDDD, ... The following
  707. // switch processes all relevant cases.
  708. switch (ffMask) {
  709. case 0xDDF: // 1 2
  710. if (f != null && isModifiedSubtree_IndexWorkingtree(name)) {
  711. conflict(name, dce, h, m); // 1
  712. } else {
  713. update(name, mId, mMode); // 2
  714. }
  715. break;
  716. case 0xDFD: // 3 4
  717. keep(dce);
  718. break;
  719. case 0xF0D: // 18
  720. remove(name);
  721. break;
  722. case 0xDFF: // 5 5b 6 6b
  723. if (equalIdAndMode(iId, iMode, mId, mMode))
  724. keep(dce); // 5 6
  725. else
  726. conflict(name, dce, h, m); // 5b 6b
  727. break;
  728. case 0xFDD: // 10 11
  729. // TODO: make use of tree extension as soon as available in jgit
  730. // we would like to do something like
  731. // if (!equalIdAndMode(iId, iMode, mId, mMode)
  732. // conflict(name, i.getDirCacheEntry(), h, m);
  733. // But since we don't know the id of a tree in the index we do
  734. // nothing here and wait that conflicts between index and merge
  735. // are found later
  736. break;
  737. case 0xD0F: // 19
  738. update(name, mId, mMode);
  739. break;
  740. case 0xDF0: // conflict without a rule
  741. case 0x0FD: // 15
  742. conflict(name, dce, h, m);
  743. break;
  744. case 0xFDF: // 7 8 9
  745. if (equalIdAndMode(hId, hMode, mId, mMode)) {
  746. if (isModifiedSubtree_IndexWorkingtree(name))
  747. conflict(name, dce, h, m); // 8
  748. else
  749. update(name, mId, mMode); // 7
  750. } else
  751. conflict(name, dce, h, m); // 9
  752. break;
  753. case 0xFD0: // keep without a rule
  754. keep(dce);
  755. break;
  756. case 0xFFD: // 12 13 14
  757. if (equalIdAndMode(hId, hMode, iId, iMode))
  758. if (f != null
  759. && f.isModified(dce, true,
  760. this.walk.getObjectReader()))
  761. conflict(name, dce, h, m); // 13
  762. else
  763. remove(name); // 12
  764. else
  765. conflict(name, dce, h, m); // 14
  766. break;
  767. case 0x0DF: // 16 17
  768. if (!isModifiedSubtree_IndexWorkingtree(name))
  769. update(name, mId, mMode);
  770. else
  771. conflict(name, dce, h, m);
  772. break;
  773. default:
  774. keep(dce);
  775. }
  776. return;
  777. }
  778. if ((ffMask & 0x222) == 0) {
  779. // HEAD, MERGE and index don't contain a file (e.g. all contain a
  780. // folder)
  781. if (f == null || FileMode.TREE.equals(f.getEntryFileMode())) {
  782. // the workingtree entry doesn't exist or also contains a folder
  783. // -> no problem
  784. return;
  785. } else {
  786. // the workingtree entry exists and is not a folder
  787. if (!idEqual(h, m)) {
  788. // Because HEAD and MERGE differ we will try to update the
  789. // workingtree with a folder -> return a conflict
  790. conflict(name, null, null, null);
  791. }
  792. return;
  793. }
  794. }
  795. if ((ffMask == 0x00F) && f != null && FileMode.TREE.equals(f.getEntryFileMode())) {
  796. // File/Directory conflict case #20
  797. conflict(name, null, h, m);
  798. return;
  799. }
  800. if (i == null) {
  801. // Nothing in Index
  802. // At least one of Head, Index, Merge is not empty
  803. // make sure not to overwrite untracked files
  804. if (f != null && !f.isEntryIgnored()) {
  805. // A submodule is not a file. We should ignore it
  806. if (!FileMode.GITLINK.equals(mMode)) {
  807. // a dirty worktree: the index is empty but we have a
  808. // workingtree-file
  809. if (mId == null
  810. || !equalIdAndMode(mId, mMode,
  811. f.getEntryObjectId(), f.getEntryFileMode())) {
  812. conflict(name, null, h, m);
  813. return;
  814. }
  815. }
  816. }
  817. /**
  818. * <pre>
  819. * I (index) H M H==M Result
  820. * -------------------------------------------
  821. * 0 nothing nothing nothing (does not happen)
  822. * 1 nothing nothing exists use M
  823. * 2 nothing exists nothing remove path from index
  824. * 3 nothing exists exists yes keep index if not in initial checkout
  825. * , otherwise use M
  826. * nothing exists exists no fail
  827. * </pre>
  828. */
  829. if (h == null)
  830. // Nothing in Head
  831. // Nothing in Index
  832. // At least one of Head, Index, Merge is not empty
  833. // -> only Merge contains something for this path. Use it!
  834. // Potentially update the file
  835. update(name, mId, mMode); // 1
  836. else if (m == null)
  837. // Nothing in Merge
  838. // Something in Head
  839. // Nothing in Index
  840. // -> only Head contains something for this path and it should
  841. // be deleted. Potentially removes the file!
  842. remove(name); // 2
  843. else { // 3
  844. // Something in Merge
  845. // Something in Head
  846. // Nothing in Index
  847. // -> Head and Merge contain something (maybe not the same) and
  848. // in the index there is nothing (e.g. 'git rm ...' was
  849. // called before). Ignore the cached deletion and use what we
  850. // find in Merge. Potentially updates the file.
  851. if (equalIdAndMode(hId, hMode, mId, mMode)) {
  852. if (emptyDirCache)
  853. update(name, mId, mMode);
  854. else
  855. keep(dce);
  856. } else
  857. conflict(name, dce, h, m);
  858. }
  859. } else {
  860. // Something in Index
  861. if (h == null) {
  862. // Nothing in Head
  863. // Something in Index
  864. /**
  865. * <pre>
  866. * clean I==H I==M H M Result
  867. * -----------------------------------------------------
  868. * 4 yes N/A N/A nothing nothing keep index
  869. * 5 no N/A N/A nothing nothing keep index
  870. *
  871. * 6 yes N/A yes nothing exists keep index
  872. * 7 no N/A yes nothing exists keep index
  873. * 8 yes N/A no nothing exists fail
  874. * 9 no N/A no nothing exists fail
  875. * </pre>
  876. */
  877. if (m == null
  878. || !isModified_IndexTree(name, iId, iMode, mId, mMode,
  879. mergeCommitTree)) {
  880. // Merge contains nothing or the same as Index
  881. // Nothing in Head
  882. // Something in Index
  883. if (m==null && walk.isDirectoryFileConflict()) {
  884. // Nothing in Merge and current path is part of
  885. // File/Folder conflict
  886. // Nothing in Head
  887. // Something in Index
  888. if (dce != null
  889. && (f == null || f.isModified(dce, true,
  890. this.walk.getObjectReader())))
  891. // No file or file is dirty
  892. // Nothing in Merge and current path is part of
  893. // File/Folder conflict
  894. // Nothing in Head
  895. // Something in Index
  896. // -> File folder conflict and Merge wants this
  897. // path to be removed. Since the file is dirty
  898. // report a conflict
  899. conflict(name, dce, h, m);
  900. else
  901. // A file is present and file is not dirty
  902. // Nothing in Merge and current path is part of
  903. // File/Folder conflict
  904. // Nothing in Head
  905. // Something in Index
  906. // -> File folder conflict and Merge wants this path
  907. // to be removed. Since the file is not dirty remove
  908. // file and index entry
  909. remove(name);
  910. } else
  911. // Something in Merge or current path is not part of
  912. // File/Folder conflict
  913. // Merge contains nothing or the same as Index
  914. // Nothing in Head
  915. // Something in Index
  916. // -> Merge contains nothing new. Keep the index.
  917. keep(dce);
  918. } else
  919. // Merge contains something and it is not the same as Index
  920. // Nothing in Head
  921. // Something in Index
  922. // -> Index contains something new (different from Head)
  923. // and Merge is different from Index. Report a conflict
  924. conflict(name, dce, h, m);
  925. } else if (m == null) {
  926. // Nothing in Merge
  927. // Something in Head
  928. // Something in Index
  929. /**
  930. * <pre>
  931. * clean I==H I==M H M Result
  932. * -----------------------------------------------------
  933. * 10 yes yes N/A exists nothing remove path from index
  934. * 11 no yes N/A exists nothing keep file
  935. * 12 yes no N/A exists nothing fail
  936. * 13 no no N/A exists nothing fail
  937. * </pre>
  938. */
  939. if (iMode == FileMode.GITLINK) {
  940. // A submodule in Index
  941. // Nothing in Merge
  942. // Something in Head
  943. // Submodules that disappear from the checkout must
  944. // be removed from the index, but not deleted from disk.
  945. remove(name);
  946. } else {
  947. // Something different from a submodule in Index
  948. // Nothing in Merge
  949. // Something in Head
  950. if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
  951. headCommitTree)) {
  952. // Index contains the same as Head
  953. // Something different from a submodule in Index
  954. // Nothing in Merge
  955. // Something in Head
  956. if (f != null
  957. && f.isModified(dce, true,
  958. this.walk.getObjectReader())) {
  959. // file is dirty
  960. // Index contains the same as Head
  961. // Something different from a submodule in Index
  962. // Nothing in Merge
  963. // Something in Head
  964. if (!FileMode.TREE.equals(f.getEntryFileMode())
  965. && FileMode.TREE.equals(iMode))
  966. // The workingtree contains a file and the index semantically contains a folder.
  967. // Git considers the workingtree file as untracked. Just keep the untracked file.
  968. return;
  969. else
  970. // -> file is dirty and tracked but is should be
  971. // removed. That's a conflict
  972. conflict(name, dce, h, m);
  973. } else
  974. // file doesn't exist or is clean
  975. // Index contains the same as Head
  976. // Something different from a submodule in Index
  977. // Nothing in Merge
  978. // Something in Head
  979. // -> Remove from index and delete the file
  980. remove(name);
  981. } else
  982. // Index contains something different from Head
  983. // Something different from a submodule in Index
  984. // Nothing in Merge
  985. // Something in Head
  986. // -> Something new is in index (and maybe even on the
  987. // filesystem). But Merge wants the path to be removed.
  988. // Report a conflict
  989. conflict(name, dce, h, m);
  990. }
  991. } else {
  992. // Something in Merge
  993. // Something in Head
  994. // Something in Index
  995. if (!equalIdAndMode(hId, hMode, mId, mMode)
  996. && isModified_IndexTree(name, iId, iMode, hId, hMode,
  997. headCommitTree)
  998. && isModified_IndexTree(name, iId, iMode, mId, mMode,
  999. mergeCommitTree))
  1000. // All three contents in Head, Merge, Index differ from each
  1001. // other
  1002. // -> All contents differ. Report a conflict.
  1003. conflict(name, dce, h, m);
  1004. else
  1005. // At least two of the contents of Head, Index, Merge
  1006. // are the same
  1007. // Something in Merge
  1008. // Something in Head
  1009. // Something in Index
  1010. if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
  1011. headCommitTree)
  1012. && isModified_IndexTree(name, iId, iMode, mId, mMode,
  1013. mergeCommitTree)) {
  1014. // Head contains the same as Index. Merge differs
  1015. // Something in Merge
  1016. // For submodules just update the index with the new SHA-1
  1017. if (dce != null
  1018. && FileMode.GITLINK.equals(dce.getFileMode())) {
  1019. // Index and Head contain the same submodule. Merge
  1020. // differs
  1021. // Something in Merge
  1022. // -> Nothing new in index. Move to merge.
  1023. // Potentially updates the file
  1024. // TODO check that we don't overwrite some unsaved
  1025. // file content
  1026. update(name, mId, mMode);
  1027. } else if (dce != null
  1028. && (f != null && f.isModified(dce, true,
  1029. this.walk.getObjectReader()))) {
  1030. // File exists and is dirty
  1031. // Head and Index don't contain a submodule
  1032. // Head contains the same as Index. Merge differs
  1033. // Something in Merge
  1034. // -> Merge wants the index and file to be updated
  1035. // but the file is dirty. Report a conflict
  1036. conflict(name, dce, h, m);
  1037. } else {
  1038. // File doesn't exist or is clean
  1039. // Head and Index don't contain a submodule
  1040. // Head contains the same as Index. Merge differs
  1041. // Something in Merge
  1042. // -> Standard case when switching between branches:
  1043. // Nothing new in index but something different in
  1044. // Merge. Update index and file
  1045. update(name, mId, mMode);
  1046. }
  1047. } else {
  1048. // Head differs from index or merge is same as index
  1049. // At least two of the contents of Head, Index, Merge
  1050. // are the same
  1051. // Something in Merge
  1052. // Something in Head
  1053. // Something in Index
  1054. // Can be formulated as: Either all three states are
  1055. // equal or Merge is equal to Head or Index and differs
  1056. // to the other one.
  1057. // -> In all three cases we don't touch index and file.
  1058. keep(dce);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. private static boolean idEqual(AbstractTreeIterator a,
  1064. AbstractTreeIterator b) {
  1065. if (a == b) {
  1066. return true;
  1067. }
  1068. if (a == null || b == null) {
  1069. return false;
  1070. }
  1071. return a.getEntryObjectId().equals(b.getEntryObjectId());
  1072. }
  1073. /**
  1074. * A conflict is detected - add the three different stages to the index
  1075. * @param path the path of the conflicting entry
  1076. * @param e the previous index entry
  1077. * @param h the first tree you want to merge (the HEAD)
  1078. * @param m the second tree you want to merge
  1079. */
  1080. private void conflict(String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m) {
  1081. conflicts.add(path);
  1082. DirCacheEntry entry;
  1083. if (e != null) {
  1084. entry = new DirCacheEntry(e.getPathString(), DirCacheEntry.STAGE_1);
  1085. entry.copyMetaData(e, true);
  1086. builder.add(entry);
  1087. }
  1088. if (h != null && !FileMode.TREE.equals(h.getEntryFileMode())) {
  1089. entry = new DirCacheEntry(h.getEntryPathString(), DirCacheEntry.STAGE_2);
  1090. entry.setFileMode(h.getEntryFileMode());
  1091. entry.setObjectId(h.getEntryObjectId());
  1092. builder.add(entry);
  1093. }
  1094. if (m != null && !FileMode.TREE.equals(m.getEntryFileMode())) {
  1095. entry = new DirCacheEntry(m.getEntryPathString(), DirCacheEntry.STAGE_3);
  1096. entry.setFileMode(m.getEntryFileMode());
  1097. entry.setObjectId(m.getEntryObjectId());
  1098. builder.add(entry);
  1099. }
  1100. }
  1101. private void keep(DirCacheEntry e) {
  1102. if (e != null && !FileMode.TREE.equals(e.getFileMode()))
  1103. builder.add(e);
  1104. }
  1105. private void remove(String path) {
  1106. removed.add(path);
  1107. }
  1108. private void update(String path, ObjectId mId, FileMode mode)
  1109. throws IOException {
  1110. if (!FileMode.TREE.equals(mode)) {
  1111. updated.put(path, new CheckoutMetadata(
  1112. walk.getEolStreamType(CHECKOUT_OP),
  1113. walk.getFilterCommand(Constants.ATTR_FILTER_TYPE_SMUDGE)));
  1114. DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0);
  1115. entry.setObjectId(mId);
  1116. entry.setFileMode(mode);
  1117. builder.add(entry);
  1118. }
  1119. }
  1120. /**
  1121. * If <code>true</code>, will scan first to see if it's possible to check
  1122. * out, otherwise throw
  1123. * {@link org.eclipse.jgit.errors.CheckoutConflictException}. If
  1124. * <code>false</code>, it will silently deal with the problem.
  1125. *
  1126. * @param failOnConflict
  1127. * a boolean.
  1128. */
  1129. public void setFailOnConflict(boolean failOnConflict) {
  1130. this.failOnConflict = failOnConflict;
  1131. }
  1132. /**
  1133. * This method implements how to handle conflicts when
  1134. * {@link #failOnConflict} is false
  1135. *
  1136. * @throws CheckoutConflictException
  1137. */
  1138. private void cleanUpConflicts() throws CheckoutConflictException {
  1139. // TODO: couldn't we delete unsaved worktree content here?
  1140. for (String c : conflicts) {
  1141. File conflict = new File(repo.getWorkTree(), c);
  1142. if (!conflict.delete())
  1143. throw new CheckoutConflictException(MessageFormat.format(
  1144. JGitText.get().cannotDeleteFile, c));
  1145. removeEmptyParents(conflict);
  1146. }
  1147. for (String r : removed) {
  1148. File file = new File(repo.getWorkTree(), r);
  1149. if (!file.delete())
  1150. throw new CheckoutConflictException(
  1151. MessageFormat.format(JGitText.get().cannotDeleteFile,
  1152. file.getAbsolutePath()));
  1153. removeEmptyParents(file);
  1154. }
  1155. }
  1156. /**
  1157. * Checks whether the subtree starting at a given path differs between Index and
  1158. * workingtree.
  1159. *
  1160. * @param path
  1161. * @return true if the subtrees differ
  1162. * @throws CorruptObjectException
  1163. * @throws IOException
  1164. */
  1165. private boolean isModifiedSubtree_IndexWorkingtree(String path)
  1166. throws CorruptObjectException, IOException {
  1167. try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
  1168. int dciPos = tw.addTree(new DirCacheIterator(dc));
  1169. FileTreeIterator fti = new FileTreeIterator(repo);
  1170. tw.addTree(fti);
  1171. fti.setDirCacheIterator(tw, dciPos);
  1172. tw.setRecursive(true);
  1173. tw.setFilter(PathFilter.create(path));
  1174. DirCacheIterator dcIt;
  1175. WorkingTreeIterator wtIt;
  1176. while (tw.next()) {
  1177. dcIt = tw.getTree(0, DirCacheIterator.class);
  1178. wtIt = tw.getTree(1, WorkingTreeIterator.class);
  1179. if (dcIt == null || wtIt == null)
  1180. return true;
  1181. if (wtIt.isModified(dcIt.getDirCacheEntry(), true,
  1182. this.walk.getObjectReader())) {
  1183. return true;
  1184. }
  1185. }
  1186. return false;
  1187. }
  1188. }
  1189. private boolean isModified_IndexTree(String path, ObjectId iId,
  1190. FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree)
  1191. throws CorruptObjectException, IOException {
  1192. if (iMode != tMode)
  1193. return true;
  1194. if (FileMode.TREE.equals(iMode)
  1195. && (iId == null || ObjectId.zeroId().equals(iId)))
  1196. return isModifiedSubtree_IndexTree(path, rootTree);
  1197. else
  1198. return !equalIdAndMode(iId, iMode, tId, tMode);
  1199. }
  1200. /**
  1201. * Checks whether the subtree starting at a given path differs between Index and
  1202. * some tree.
  1203. *
  1204. * @param path
  1205. * @param tree
  1206. * the tree to compare
  1207. * @return true if the subtrees differ
  1208. * @throws CorruptObjectException
  1209. * @throws IOException
  1210. */
  1211. private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
  1212. throws CorruptObjectException, IOException {
  1213. try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
  1214. tw.addTree(new DirCacheIterator(dc));
  1215. tw.addTree(tree);
  1216. tw.setRecursive(true);
  1217. tw.setFilter(PathFilter.create(path));
  1218. while (tw.next()) {
  1219. AbstractTreeIterator dcIt = tw.getTree(0,
  1220. DirCacheIterator.class);
  1221. AbstractTreeIterator treeIt = tw.getTree(1,
  1222. AbstractTreeIterator.class);
  1223. if (dcIt == null || treeIt == null)
  1224. return true;
  1225. if (dcIt.getEntryRawMode() != treeIt.getEntryRawMode())
  1226. return true;
  1227. if (!dcIt.getEntryObjectId().equals(treeIt.getEntryObjectId()))
  1228. return true;
  1229. }
  1230. return false;
  1231. }
  1232. }
  1233. /**
  1234. * Updates the file in the working tree with content and mode from an entry
  1235. * in the index. The new content is first written to a new temporary file in
  1236. * the same directory as the real file. Then that new file is renamed to the
  1237. * final filename.
  1238. *
  1239. * <p>
  1240. * <b>Note:</b> if the entry path on local file system exists as a non-empty
  1241. * directory, and the target entry type is a link or file, the checkout will
  1242. * fail with {@link java.io.IOException} since existing non-empty directory
  1243. * cannot be renamed to file or link without deleting it recursively.
  1244. * </p>
  1245. *
  1246. * <p>
  1247. * TODO: this method works directly on File IO, we may need another
  1248. * abstraction (like WorkingTreeIterator). This way we could tell e.g.
  1249. * Eclipse that Files in the workspace got changed
  1250. * </p>
  1251. *
  1252. * @param repo
  1253. * repository managing the destination work tree.
  1254. * @param entry
  1255. * the entry containing new mode and content
  1256. * @param or
  1257. * object reader to use for checkout
  1258. * @throws java.io.IOException
  1259. * @since 3.6
  1260. */
  1261. public static void checkoutEntry(Repository repo, DirCacheEntry entry,
  1262. ObjectReader or) throws IOException {
  1263. checkoutEntry(repo, entry, or, false, null);
  1264. }
  1265. /**
  1266. * Updates the file in the working tree with content and mode from an entry
  1267. * in the index. The new content is first written to a new temporary file in
  1268. * the same directory as the real file. Then that new file is renamed to the
  1269. * final filename.
  1270. *
  1271. * <p>
  1272. * <b>Note:</b> if the entry path on local file system exists as a file, it
  1273. * will be deleted and if it exists as a directory, it will be deleted
  1274. * recursively, independently if has any content.
  1275. * </p>
  1276. *
  1277. * <p>
  1278. * TODO: this method works directly on File IO, we may need another
  1279. * abstraction (like WorkingTreeIterator). This way we could tell e.g.
  1280. * Eclipse that Files in the workspace got changed
  1281. * </p>
  1282. *
  1283. * @param repo
  1284. * repository managing the destination work tree.
  1285. * @param entry
  1286. * the entry containing new mode and content
  1287. * @param or
  1288. * object reader to use for checkout
  1289. * @param deleteRecursive
  1290. * true to recursively delete final path if it exists on the file
  1291. * system
  1292. * @param checkoutMetadata
  1293. * containing
  1294. * <ul>
  1295. * <li>smudgeFilterCommand to be run for smudging the entry to be
  1296. * checked out</li>
  1297. * <li>eolStreamType used for stream conversion</li>
  1298. * </ul>
  1299. * @throws java.io.IOException
  1300. * @since 4.2
  1301. */
  1302. public static void checkoutEntry(Repository repo, DirCacheEntry entry,
  1303. ObjectReader or, boolean deleteRecursive,
  1304. CheckoutMetadata checkoutMetadata) throws IOException {
  1305. if (checkoutMetadata == null)
  1306. checkoutMetadata = CheckoutMetadata.EMPTY;
  1307. ObjectLoader ol = or.open(entry.getObjectId());
  1308. File f = new File(repo.getWorkTree(), entry.getPathString());
  1309. File parentDir = f.getParentFile();
  1310. FileUtils.mkdirs(parentDir, true);
  1311. FS fs = repo.getFS();
  1312. WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
  1313. if (entry.getFileMode() == FileMode.SYMLINK
  1314. && opt.getSymLinks() == SymLinks.TRUE) {
  1315. byte[] bytes = ol.getBytes();
  1316. String target = RawParseUtils.decode(bytes);
  1317. if (deleteRecursive && f.isDirectory()) {
  1318. FileUtils.delete(f, FileUtils.RECURSIVE);
  1319. }
  1320. fs.createSymLink(f, target);
  1321. entry.setLength(bytes.length);
  1322. entry.setLastModified(fs.lastModified(f));
  1323. return;
  1324. }
  1325. String name = f.getName();
  1326. if (name.length() > 200) {
  1327. name = name.substring(0, 200);
  1328. }
  1329. File tmpFile = File.createTempFile(
  1330. "._" + name, null, parentDir); //$NON-NLS-1$
  1331. EolStreamType nonNullEolStreamType;
  1332. if (checkoutMetadata.eolStreamType != null) {
  1333. nonNullEolStreamType = checkoutMetadata.eolStreamType;
  1334. } else if (opt.getAutoCRLF() == AutoCRLF.TRUE) {
  1335. nonNullEolStreamType = EolStreamType.AUTO_CRLF;
  1336. } else {
  1337. nonNullEolStreamType = EolStreamType.DIRECT;
  1338. }
  1339. try (OutputStream channel = EolStreamTypeUtil.wrapOutputStream(
  1340. new FileOutputStream(tmpFile), nonNullEolStreamType)) {
  1341. if (checkoutMetadata.smudgeFilterCommand != null) {
  1342. if (FilterCommandRegistry
  1343. .isRegistered(checkoutMetadata.smudgeFilterCommand)) {
  1344. runBuiltinFilterCommand(repo, checkoutMetadata, ol,
  1345. channel);
  1346. } else {
  1347. runExternalFilterCommand(repo, entry, checkoutMetadata, ol,
  1348. fs, channel);
  1349. }
  1350. } else {
  1351. ol.copyTo(channel);
  1352. }
  1353. }
  1354. // The entry needs to correspond to the on-disk filesize. If the content
  1355. // was filtered (either by autocrlf handling or smudge filters) ask the
  1356. // filesystem again for the length. Otherwise the objectloader knows the
  1357. // size
  1358. if (checkoutMetadata.eolStreamType == EolStreamType.DIRECT
  1359. && checkoutMetadata.smudgeFilterCommand == null) {
  1360. entry.setLength(ol.getSize());
  1361. } else {
  1362. entry.setLength(tmpFile.length());
  1363. }
  1364. if (opt.isFileMode() && fs.supportsExecute()) {
  1365. if (FileMode.EXECUTABLE_FILE.equals(entry.getRawMode())) {
  1366. if (!fs.canExecute(tmpFile))
  1367. fs.setExecute(tmpFile, true);
  1368. } else {
  1369. if (fs.canExecute(tmpFile))
  1370. fs.setExecute(tmpFile, false);
  1371. }
  1372. }
  1373. try {
  1374. if (deleteRecursive && f.isDirectory()) {
  1375. FileUtils.delete(f, FileUtils.RECURSIVE);
  1376. }
  1377. FileUtils.rename(tmpFile, f, StandardCopyOption.ATOMIC_MOVE);
  1378. } catch (IOException e) {
  1379. throw new IOException(
  1380. MessageFormat.format(JGitText.get().renameFileFailed,
  1381. tmpFile.getPath(), f.getPath()),
  1382. e);
  1383. } finally {
  1384. if (tmpFile.exists()) {
  1385. FileUtils.delete(tmpFile);
  1386. }
  1387. }
  1388. entry.setLastModified(fs.lastModified(f));
  1389. }
  1390. // Run an external filter command
  1391. private static void runExternalFilterCommand(Repository repo,
  1392. DirCacheEntry entry,
  1393. CheckoutMetadata checkoutMetadata, ObjectLoader ol, FS fs,
  1394. OutputStream channel) throws IOException {
  1395. ProcessBuilder filterProcessBuilder = fs.runInShell(
  1396. checkoutMetadata.smudgeFilterCommand, new String[0]);
  1397. filterProcessBuilder.directory(repo.getWorkTree());
  1398. filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
  1399. repo.getDirectory().getAbsolutePath());
  1400. ExecutionResult result;
  1401. int rc;
  1402. try {
  1403. // TODO: wire correctly with AUTOCRLF
  1404. result = fs.execute(filterProcessBuilder, ol.openStream());
  1405. rc = result.getRc();
  1406. if (rc == 0) {
  1407. result.getStdout().writeTo(channel,
  1408. NullProgressMonitor.INSTANCE);
  1409. }
  1410. } catch (IOException | InterruptedException e) {
  1411. throw new IOException(new FilterFailedException(e,
  1412. checkoutMetadata.smudgeFilterCommand,
  1413. entry.getPathString()));
  1414. }
  1415. if (rc != 0) {
  1416. throw new IOException(new FilterFailedException(rc,
  1417. checkoutMetadata.smudgeFilterCommand,
  1418. entry.getPathString(),
  1419. result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
  1420. RawParseUtils.decode(result.getStderr()
  1421. .toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
  1422. }
  1423. }
  1424. // Run a builtin filter command
  1425. private static void runBuiltinFilterCommand(Repository repo,
  1426. CheckoutMetadata checkoutMetadata, ObjectLoader ol,
  1427. OutputStream channel) throws MissingObjectException, IOException {
  1428. FilterCommand command = null;
  1429. try {
  1430. command = FilterCommandRegistry.createFilterCommand(
  1431. checkoutMetadata.smudgeFilterCommand, repo, ol.openStream(),
  1432. channel);
  1433. } catch (IOException e) {
  1434. LOG.error(JGitText.get().failedToDetermineFilterDefinition, e);
  1435. // In case an IOException occurred during creating of the command
  1436. // then proceed as if there would not have been a builtin filter.
  1437. ol.copyTo(channel);
  1438. }
  1439. if (command != null) {
  1440. while (command.run() != -1) {
  1441. // loop as long as command.run() tells there is work to do
  1442. }
  1443. }
  1444. }
  1445. @SuppressWarnings("deprecation")
  1446. private static void checkValidPath(CanonicalTreeParser t)
  1447. throws InvalidPathException {
  1448. ObjectChecker chk = new ObjectChecker()
  1449. .setSafeForWindows(SystemReader.getInstance().isWindows())
  1450. .setSafeForMacOS(SystemReader.getInstance().isMacOS());
  1451. for (CanonicalTreeParser i = t; i != null; i = i.getParent())
  1452. checkValidPathSegment(chk, i);
  1453. }
  1454. private static void checkValidPathSegment(ObjectChecker chk,
  1455. CanonicalTreeParser t) throws InvalidPathException {
  1456. try {
  1457. int ptr = t.getNameOffset();
  1458. int end = ptr + t.getNameLength();
  1459. chk.checkPathSegment(t.getEntryPathBuffer(), ptr, end);
  1460. } catch (CorruptObjectException err) {
  1461. String path = t.getEntryPathString();
  1462. InvalidPathException i = new InvalidPathException(path);
  1463. i.initCause(err);
  1464. throw i;
  1465. }
  1466. }
  1467. }