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

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