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

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