Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DirCacheCheckout.java 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 java.io.File;
  44. import java.io.FileOutputStream;
  45. import java.io.IOException;
  46. import java.io.OutputStream;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.HashMap;
  50. import java.util.List;
  51. import java.util.Map;
  52. import org.eclipse.jgit.errors.CheckoutConflictException;
  53. import org.eclipse.jgit.errors.CorruptObjectException;
  54. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  55. import org.eclipse.jgit.errors.IndexWriteException;
  56. import org.eclipse.jgit.errors.MissingObjectException;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.CoreConfig.AutoCRLF;
  59. import org.eclipse.jgit.lib.CoreConfig.SymLinks;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.ObjectChecker;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectLoader;
  64. import org.eclipse.jgit.lib.ObjectReader;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  67. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  68. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  69. import org.eclipse.jgit.treewalk.FileTreeIterator;
  70. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  71. import org.eclipse.jgit.treewalk.TreeWalk;
  72. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  73. import org.eclipse.jgit.treewalk.WorkingTreeOptions;
  74. import org.eclipse.jgit.treewalk.filter.PathFilter;
  75. import org.eclipse.jgit.util.FS;
  76. import org.eclipse.jgit.util.FileUtils;
  77. import org.eclipse.jgit.util.RawParseUtils;
  78. import org.eclipse.jgit.util.SystemReader;
  79. import org.eclipse.jgit.util.io.AutoCRLFOutputStream;
  80. /**
  81. * This class handles checking out one or two trees merging with the index.
  82. */
  83. public class DirCacheCheckout {
  84. private Repository repo;
  85. private HashMap<String, ObjectId> updated = new HashMap<String, ObjectId>();
  86. private ArrayList<String> conflicts = new ArrayList<String>();
  87. private ArrayList<String> removed = new ArrayList<String>();
  88. private ObjectId mergeCommitTree;
  89. private DirCache dc;
  90. private DirCacheBuilder builder;
  91. private NameConflictTreeWalk walk;
  92. private ObjectId headCommitTree;
  93. private WorkingTreeIterator workingTree;
  94. private boolean failOnConflict = true;
  95. private ArrayList<String> toBeDeleted = new ArrayList<String>();
  96. private boolean emptyDirCache;
  97. /**
  98. * @return a list of updated paths and objectIds
  99. */
  100. public Map<String, ObjectId> getUpdated() {
  101. return updated;
  102. }
  103. /**
  104. * @return a list of conflicts created by this checkout
  105. */
  106. public List<String> getConflicts() {
  107. return conflicts;
  108. }
  109. /**
  110. * @return a list of paths (relative to the start of the working tree) of
  111. * files which couldn't be deleted during last call to
  112. * {@link #checkout()} . {@link #checkout()} detected that these
  113. * files should be deleted but the deletion in the filesystem failed
  114. * (e.g. because a file was locked). To have a consistent state of
  115. * the working tree these files have to be deleted by the callers of
  116. * {@link DirCacheCheckout}.
  117. */
  118. public List<String> getToBeDeleted() {
  119. return toBeDeleted;
  120. }
  121. /**
  122. * @return a list of all files removed by this checkout
  123. */
  124. public List<String> getRemoved() {
  125. return removed;
  126. }
  127. /**
  128. * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
  129. * and mergeCommitTree) and the index.
  130. *
  131. * @param repo
  132. * the repository in which we do the checkout
  133. * @param headCommitTree
  134. * the id of the tree of the head commit
  135. * @param dc
  136. * the (already locked) Dircache for this repo
  137. * @param mergeCommitTree
  138. * the id of the tree we want to fast-forward to
  139. * @param workingTree
  140. * an iterator over the repositories Working Tree
  141. * @throws IOException
  142. */
  143. public DirCacheCheckout(Repository repo, ObjectId headCommitTree, DirCache dc,
  144. ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
  145. throws IOException {
  146. this.repo = repo;
  147. this.dc = dc;
  148. this.headCommitTree = headCommitTree;
  149. this.mergeCommitTree = mergeCommitTree;
  150. this.workingTree = workingTree;
  151. this.emptyDirCache = (dc == null) || (dc.getEntryCount() == 0);
  152. }
  153. /**
  154. * Constructs a DirCacheCeckout for merging and checking out two trees (HEAD
  155. * and mergeCommitTree) and the index. As iterator over the working tree
  156. * this constructor creates a standard {@link FileTreeIterator}
  157. *
  158. * @param repo
  159. * the repository in which we do the checkout
  160. * @param headCommitTree
  161. * the id of the tree of the head commit
  162. * @param dc
  163. * the (already locked) Dircache for this repo
  164. * @param mergeCommitTree
  165. * the id of the tree we want to fast-forward to
  166. * @throws IOException
  167. */
  168. public DirCacheCheckout(Repository repo, ObjectId headCommitTree,
  169. DirCache dc, ObjectId mergeCommitTree) throws IOException {
  170. this(repo, headCommitTree, dc, mergeCommitTree, new FileTreeIterator(repo));
  171. }
  172. /**
  173. * Constructs a DirCacheCeckout for checking out one tree, merging with the
  174. * index.
  175. *
  176. * @param repo
  177. * the repository in which we do the checkout
  178. * @param dc
  179. * the (already locked) Dircache for this repo
  180. * @param mergeCommitTree
  181. * the id of the tree we want to fast-forward to
  182. * @param workingTree
  183. * an iterator over the repositories Working Tree
  184. * @throws IOException
  185. */
  186. public DirCacheCheckout(Repository repo, DirCache dc,
  187. ObjectId mergeCommitTree, WorkingTreeIterator workingTree)
  188. throws IOException {
  189. this(repo, null, dc, mergeCommitTree, workingTree);
  190. }
  191. /**
  192. * Constructs a DirCacheCeckout for checking out one tree, merging with the
  193. * index. As iterator over the working tree this constructor creates a
  194. * standard {@link FileTreeIterator}
  195. *
  196. * @param repo
  197. * the repository in which we do the checkout
  198. * @param dc
  199. * the (already locked) Dircache for this repo
  200. * @param mergeCommitTree
  201. * the id of the tree of the
  202. * @throws IOException
  203. */
  204. public DirCacheCheckout(Repository repo, DirCache dc,
  205. ObjectId mergeCommitTree) throws IOException {
  206. this(repo, null, dc, mergeCommitTree, new FileTreeIterator(repo));
  207. }
  208. /**
  209. * Scan head, index and merge tree. Used during normal checkout or merge
  210. * operations.
  211. *
  212. * @throws CorruptObjectException
  213. * @throws IOException
  214. */
  215. public void preScanTwoTrees() throws CorruptObjectException, IOException {
  216. removed.clear();
  217. updated.clear();
  218. conflicts.clear();
  219. walk = new NameConflictTreeWalk(repo);
  220. builder = dc.builder();
  221. addTree(walk, headCommitTree);
  222. addTree(walk, mergeCommitTree);
  223. walk.addTree(new DirCacheBuildIterator(builder));
  224. walk.addTree(workingTree);
  225. while (walk.next()) {
  226. processEntry(walk.getTree(0, CanonicalTreeParser.class),
  227. walk.getTree(1, CanonicalTreeParser.class),
  228. walk.getTree(2, DirCacheBuildIterator.class),
  229. walk.getTree(3, WorkingTreeIterator.class));
  230. if (walk.isSubtree())
  231. walk.enterSubtree();
  232. }
  233. }
  234. private void addTree(TreeWalk tw, ObjectId id) throws MissingObjectException, IncorrectObjectTypeException, IOException {
  235. if (id == null)
  236. tw.addTree(new EmptyTreeIterator());
  237. else
  238. tw.addTree(id);
  239. }
  240. /**
  241. * Scan index and merge tree (no HEAD). Used e.g. for initial checkout when
  242. * there is no head yet.
  243. *
  244. * @throws MissingObjectException
  245. * @throws IncorrectObjectTypeException
  246. * @throws CorruptObjectException
  247. * @throws IOException
  248. */
  249. public void prescanOneTree()
  250. throws MissingObjectException, IncorrectObjectTypeException,
  251. CorruptObjectException, IOException {
  252. removed.clear();
  253. updated.clear();
  254. conflicts.clear();
  255. builder = dc.builder();
  256. walk = new NameConflictTreeWalk(repo);
  257. addTree(walk, mergeCommitTree);
  258. walk.addTree(new DirCacheBuildIterator(builder));
  259. walk.addTree(workingTree);
  260. while (walk.next()) {
  261. processEntry(walk.getTree(0, CanonicalTreeParser.class),
  262. walk.getTree(1, DirCacheBuildIterator.class),
  263. walk.getTree(2, WorkingTreeIterator.class));
  264. if (walk.isSubtree())
  265. walk.enterSubtree();
  266. }
  267. conflicts.removeAll(removed);
  268. }
  269. /**
  270. * Processing an entry in the context of {@link #prescanOneTree()} when only
  271. * one tree is given
  272. *
  273. * @param m the tree to merge
  274. * @param i the index
  275. * @param f the working tree
  276. * @throws IOException
  277. */
  278. void processEntry(CanonicalTreeParser m, DirCacheBuildIterator i,
  279. WorkingTreeIterator f) throws IOException {
  280. if (m != null) {
  281. checkValidPath(m);
  282. // There is an entry in the merge commit. Means: we want to update
  283. // what's currently in the index and working-tree to that one
  284. if (i == null) {
  285. // The index entry is missing
  286. if (f != null && !FileMode.TREE.equals(f.getEntryFileMode())
  287. && !f.isEntryIgnored()) {
  288. // don't overwrite an untracked and not ignored file
  289. conflicts.add(walk.getPathString());
  290. } else
  291. update(m.getEntryPathString(), m.getEntryObjectId(),
  292. m.getEntryFileMode());
  293. } else if (f == null || !m.idEqual(i)) {
  294. // The working tree file is missing or the merge content differs
  295. // from index content
  296. update(m.getEntryPathString(), m.getEntryObjectId(),
  297. m.getEntryFileMode());
  298. } else if (i.getDirCacheEntry() != null) {
  299. // The index contains a file (and not a folder)
  300. if (f.isModified(i.getDirCacheEntry(), true,
  301. this.walk.getObjectReader())
  302. || i.getDirCacheEntry().getStage() != 0)
  303. // The working tree file is dirty or the index contains a
  304. // conflict
  305. update(m.getEntryPathString(), m.getEntryObjectId(),
  306. m.getEntryFileMode());
  307. else {
  308. // update the timestamp of the index with the one from the
  309. // file if not set, as we are sure to be in sync here.
  310. DirCacheEntry entry = i.getDirCacheEntry();
  311. if (entry.getLastModified() == 0)
  312. entry.setLastModified(f.getEntryLastModified());
  313. keep(entry);
  314. }
  315. } else
  316. // The index contains a folder
  317. keep(i.getDirCacheEntry());
  318. } else {
  319. // There is no entry in the merge commit. Means: we want to delete
  320. // what's currently in the index and working tree
  321. if (f != null) {
  322. // There is a file/folder for that path in the working tree
  323. if (walk.isDirectoryFileConflict()) {
  324. conflicts.add(walk.getPathString());
  325. } else {
  326. // No file/folder conflict exists. All entries are files or
  327. // all entries are folders
  328. if (i != null) {
  329. // ... and the working tree contained a file or folder
  330. // -> add it to the removed set and remove it from
  331. // conflicts set
  332. remove(i.getEntryPathString());
  333. conflicts.remove(i.getEntryPathString());
  334. } else {
  335. // untracked file, neither contained in tree to merge
  336. // nor in index
  337. }
  338. }
  339. } else {
  340. // There is no file/folder for that path in the working tree,
  341. // nor in the merge head.
  342. // The only entry we have is the index entry. Like the case
  343. // where there is a file with the same name, remove it,
  344. }
  345. }
  346. }
  347. /**
  348. * Execute this checkout
  349. *
  350. * @return <code>false</code> if this method could not delete all the files
  351. * which should be deleted (e.g. because of of the files was
  352. * locked). In this case {@link #getToBeDeleted()} lists the files
  353. * which should be tried to be deleted outside of this method.
  354. * Although <code>false</code> is returned the checkout was
  355. * successful and the working tree was updated for all other files.
  356. * <code>true</code> is returned when no such problem occurred
  357. *
  358. * @throws IOException
  359. */
  360. public boolean checkout() throws IOException {
  361. try {
  362. return doCheckout();
  363. } finally {
  364. dc.unlock();
  365. }
  366. }
  367. private boolean doCheckout() throws CorruptObjectException, IOException,
  368. MissingObjectException, IncorrectObjectTypeException,
  369. CheckoutConflictException, IndexWriteException {
  370. toBeDeleted.clear();
  371. try (ObjectReader objectReader = repo.getObjectDatabase().newReader()) {
  372. if (headCommitTree != null)
  373. preScanTwoTrees();
  374. else
  375. prescanOneTree();
  376. if (!conflicts.isEmpty()) {
  377. if (failOnConflict)
  378. throw new CheckoutConflictException(conflicts.toArray(new String[conflicts.size()]));
  379. else
  380. cleanUpConflicts();
  381. }
  382. // update our index
  383. builder.finish();
  384. File file = null;
  385. String last = null;
  386. // when deleting files process them in the opposite order as they have
  387. // been reported. This ensures the files are deleted before we delete
  388. // their parent folders
  389. for (int i = removed.size() - 1; i >= 0; i--) {
  390. String r = removed.get(i);
  391. file = new File(repo.getWorkTree(), r);
  392. if (!file.delete() && repo.getFS().exists(file)) {
  393. // The list of stuff to delete comes from the index
  394. // which will only contain a directory if it is
  395. // a submodule, in which case we shall not attempt
  396. // to delete it. A submodule is not empty, so it
  397. // is safe to check this after a failed delete.
  398. if (!repo.getFS().isDirectory(file))
  399. toBeDeleted.add(r);
  400. } else {
  401. if (last != null && !isSamePrefix(r, last))
  402. removeEmptyParents(new File(repo.getWorkTree(), last));
  403. last = r;
  404. }
  405. }
  406. if (file != null)
  407. removeEmptyParents(file);
  408. for (String path : updated.keySet()) {
  409. DirCacheEntry entry = dc.getEntry(path);
  410. if (!FileMode.GITLINK.equals(entry.getRawMode()))
  411. checkoutEntry(repo, entry, objectReader);
  412. }
  413. // commit the index builder - a new index is persisted
  414. if (!builder.commit())
  415. throw new IndexWriteException();
  416. }
  417. return toBeDeleted.size() == 0;
  418. }
  419. private static boolean isSamePrefix(String a, String b) {
  420. int as = a.lastIndexOf('/');
  421. int bs = b.lastIndexOf('/');
  422. return a.substring(0, as + 1).equals(b.substring(0, bs + 1));
  423. }
  424. private void removeEmptyParents(File f) {
  425. File parentFile = f.getParentFile();
  426. while (parentFile != null && !parentFile.equals(repo.getWorkTree())) {
  427. if (!parentFile.delete())
  428. break;
  429. parentFile = parentFile.getParentFile();
  430. }
  431. }
  432. /**
  433. * Compares whether two pairs of ObjectId and FileMode are equal.
  434. *
  435. * @param id1
  436. * @param mode1
  437. * @param id2
  438. * @param mode2
  439. * @return <code>true</code> if FileModes and ObjectIds are equal.
  440. * <code>false</code> otherwise
  441. */
  442. private boolean equalIdAndMode(ObjectId id1, FileMode mode1, ObjectId id2,
  443. FileMode mode2) {
  444. if (!mode1.equals(mode2))
  445. return false;
  446. return id1 != null ? id1.equals(id2) : id2 == null;
  447. }
  448. /**
  449. * Here the main work is done. This method is called for each existing path
  450. * in head, index and merge. This method decides what to do with the
  451. * corresponding index entry: keep it, update it, remove it or mark a
  452. * conflict.
  453. *
  454. * @param h
  455. * the entry for the head
  456. * @param m
  457. * the entry for the merge
  458. * @param i
  459. * the entry for the index
  460. * @param f
  461. * the file in the working tree
  462. * @throws IOException
  463. */
  464. void processEntry(CanonicalTreeParser h, CanonicalTreeParser m,
  465. DirCacheBuildIterator i, WorkingTreeIterator f) throws IOException {
  466. DirCacheEntry dce = i != null ? i.getDirCacheEntry() : null;
  467. String name = walk.getPathString();
  468. if (m != null)
  469. checkValidPath(m);
  470. if (i == null && m == null && h == null) {
  471. // File/Directory conflict case #20
  472. if (walk.isDirectoryFileConflict())
  473. // TODO: check whether it is always correct to report a conflict here
  474. conflict(name, null, null, null);
  475. // file only exists in working tree -> ignore it
  476. return;
  477. }
  478. ObjectId iId = (i == null ? null : i.getEntryObjectId());
  479. ObjectId mId = (m == null ? null : m.getEntryObjectId());
  480. ObjectId hId = (h == null ? null : h.getEntryObjectId());
  481. FileMode iMode = (i == null ? null : i.getEntryFileMode());
  482. FileMode mMode = (m == null ? null : m.getEntryFileMode());
  483. FileMode hMode = (h == null ? null : h.getEntryFileMode());
  484. /**
  485. * <pre>
  486. * File/Directory conflicts:
  487. * the following table from ReadTreeTest tells what to do in case of directory/file
  488. * conflicts. I give comments here
  489. *
  490. * H I M Clean H==M H==I I==M Result
  491. * ------------------------------------------------------------------
  492. * 1 D D F Y N Y N Update
  493. * 2 D D F N N Y N Conflict
  494. * 3 D F D Y N N Keep
  495. * 4 D F D N N N Conflict
  496. * 5 D F F Y N N Y Keep
  497. * 5b D F F Y N N N Conflict
  498. * 6 D F F N N N Y Keep
  499. * 6b D F F N N N N Conflict
  500. * 7 F D F Y Y N N Update
  501. * 8 F D F N Y N N Conflict
  502. * 9 F D F N N N Conflict
  503. * 10 F D D N N Y Keep
  504. * 11 F D D N N N Conflict
  505. * 12 F F D Y N Y N Update
  506. * 13 F F D N N Y N Conflict
  507. * 14 F F D N N N Conflict
  508. * 15 0 F D N N N Conflict
  509. * 16 0 D F Y N N N Update
  510. * 17 0 D F N N N Conflict
  511. * 18 F 0 D Update
  512. * 19 D 0 F Update
  513. * 20 0 0 F N (worktree=dir) Conflict
  514. * </pre>
  515. */
  516. // The information whether head,index,merge iterators are currently
  517. // pointing to file/folder/non-existing is encoded into this variable.
  518. //
  519. // To decode write down ffMask in hexadecimal form. The last digit
  520. // represents the state for the merge iterator, the second last the
  521. // state for the index iterator and the third last represents the state
  522. // for the head iterator. The hexadecimal constant "F" stands for
  523. // "file", a "D" stands for "directory" (tree), and a "0" stands for
  524. // non-existing. Symbolic links and git links are treated as File here.
  525. //
  526. // Examples:
  527. // ffMask == 0xFFD -> Head=File, Index=File, Merge=Tree
  528. // ffMask == 0xDD0 -> Head=Tree, Index=Tree, Merge=Non-Existing
  529. int ffMask = 0;
  530. if (h != null)
  531. ffMask = FileMode.TREE.equals(hMode) ? 0xD00 : 0xF00;
  532. if (i != null)
  533. ffMask |= FileMode.TREE.equals(iMode) ? 0x0D0 : 0x0F0;
  534. if (m != null)
  535. ffMask |= FileMode.TREE.equals(mMode) ? 0x00D : 0x00F;
  536. // Check whether we have a possible file/folder conflict. Therefore we
  537. // need a least one file and one folder.
  538. if (((ffMask & 0x222) != 0x000)
  539. && (((ffMask & 0x00F) == 0x00D) || ((ffMask & 0x0F0) == 0x0D0) || ((ffMask & 0xF00) == 0xD00))) {
  540. // There are 3*3*3=27 possible combinations of file/folder
  541. // conflicts. Some of them are not-relevant because
  542. // they represent no conflict, e.g. 0xFFF, 0xDDD, ... The following
  543. // switch processes all relevant cases.
  544. switch (ffMask) {
  545. case 0xDDF: // 1 2
  546. if (f != null && isModifiedSubtree_IndexWorkingtree(name)) {
  547. conflict(name, dce, h, m); // 1
  548. } else {
  549. update(name, mId, mMode); // 2
  550. }
  551. break;
  552. case 0xDFD: // 3 4
  553. keep(dce);
  554. break;
  555. case 0xF0D: // 18
  556. remove(name);
  557. break;
  558. case 0xDFF: // 5 5b 6 6b
  559. if (equalIdAndMode(iId, iMode, mId, mMode))
  560. keep(dce); // 5 6
  561. else
  562. conflict(name, dce, h, m); // 5b 6b
  563. break;
  564. case 0xFDD: // 10 11
  565. // TODO: make use of tree extension as soon as available in jgit
  566. // we would like to do something like
  567. // if (!equalIdAndMode(iId, iMode, mId, mMode)
  568. // conflict(name, i.getDirCacheEntry(), h, m);
  569. // But since we don't know the id of a tree in the index we do
  570. // nothing here and wait that conflicts between index and merge
  571. // are found later
  572. break;
  573. case 0xD0F: // 19
  574. update(name, mId, mMode);
  575. break;
  576. case 0xDF0: // conflict without a rule
  577. case 0x0FD: // 15
  578. conflict(name, dce, h, m);
  579. break;
  580. case 0xFDF: // 7 8 9
  581. if (equalIdAndMode(hId, hMode, mId, mMode)) {
  582. if (isModifiedSubtree_IndexWorkingtree(name))
  583. conflict(name, dce, h, m); // 8
  584. else
  585. update(name, mId, mMode); // 7
  586. } else
  587. conflict(name, dce, h, m); // 9
  588. break;
  589. case 0xFD0: // keep without a rule
  590. keep(dce);
  591. break;
  592. case 0xFFD: // 12 13 14
  593. if (equalIdAndMode(hId, hMode, iId, iMode))
  594. if (f != null
  595. && f.isModified(dce, true,
  596. this.walk.getObjectReader()))
  597. conflict(name, dce, h, m); // 13
  598. else
  599. remove(name); // 12
  600. else
  601. conflict(name, dce, h, m); // 14
  602. break;
  603. case 0x0DF: // 16 17
  604. if (!isModifiedSubtree_IndexWorkingtree(name))
  605. update(name, mId, mMode);
  606. else
  607. conflict(name, dce, h, m);
  608. break;
  609. default:
  610. keep(dce);
  611. }
  612. return;
  613. }
  614. // if we have no file at all then there is nothing to do
  615. if ((ffMask & 0x222) == 0
  616. && (f == null || FileMode.TREE.equals(f.getEntryFileMode())))
  617. return;
  618. if ((ffMask == 0x00F) && f != null && FileMode.TREE.equals(f.getEntryFileMode())) {
  619. // File/Directory conflict case #20
  620. conflict(name, null, h, m);
  621. return;
  622. }
  623. if (i == null) {
  624. // Nothing in Index
  625. // At least one of Head, Index, Merge is not empty
  626. // make sure not to overwrite untracked files
  627. if (f != null && !f.isEntryIgnored()) {
  628. // A submodule is not a file. We should ignore it
  629. if (!FileMode.GITLINK.equals(mMode)) {
  630. // a dirty worktree: the index is empty but we have a
  631. // workingtree-file
  632. if (mId == null
  633. || !equalIdAndMode(mId, mMode,
  634. f.getEntryObjectId(), f.getEntryFileMode())) {
  635. conflict(name, null, h, m);
  636. return;
  637. }
  638. }
  639. }
  640. /**
  641. * <pre>
  642. * I (index) H M H==M Result
  643. * -------------------------------------------
  644. * 0 nothing nothing nothing (does not happen)
  645. * 1 nothing nothing exists use M
  646. * 2 nothing exists nothing remove path from index
  647. * 3 nothing exists exists yes keep index if not in initial checkout
  648. * , otherwise use M
  649. * nothing exists exists no fail
  650. * </pre>
  651. */
  652. if (h == null)
  653. // Nothing in Head
  654. // Nothing in Index
  655. // At least one of Head, Index, Merge is not empty
  656. // -> only Merge contains something for this path. Use it!
  657. // Potentially update the file
  658. update(name, mId, mMode); // 1
  659. else if (m == null)
  660. // Nothing in Merge
  661. // Something in Head
  662. // Nothing in Index
  663. // -> only Head contains something for this path and it should
  664. // be deleted. Potentially removes the file!
  665. remove(name); // 2
  666. else { // 3
  667. // Something in Merge
  668. // Something in Head
  669. // Nothing in Index
  670. // -> Head and Merge contain something (maybe not the same) and
  671. // in the index there is nothing (e.g. 'git rm ...' was
  672. // called before). Ignore the cached deletion and use what we
  673. // find in Merge. Potentially updates the file.
  674. if (equalIdAndMode(hId, hMode, mId, mMode)) {
  675. if (emptyDirCache)
  676. update(name, mId, mMode);
  677. else
  678. keep(dce);
  679. } else
  680. conflict(name, dce, h, m);
  681. }
  682. } else {
  683. // Something in Index
  684. if (h == null) {
  685. // Nothing in Head
  686. // Something in Index
  687. /**
  688. * <pre>
  689. * clean I==H I==M H M Result
  690. * -----------------------------------------------------
  691. * 4 yes N/A N/A nothing nothing keep index
  692. * 5 no N/A N/A nothing nothing keep index
  693. *
  694. * 6 yes N/A yes nothing exists keep index
  695. * 7 no N/A yes nothing exists keep index
  696. * 8 yes N/A no nothing exists fail
  697. * 9 no N/A no nothing exists fail
  698. * </pre>
  699. */
  700. if (m == null
  701. || !isModified_IndexTree(name, iId, iMode, mId, mMode,
  702. mergeCommitTree)) {
  703. // Merge contains nothing or the same as Index
  704. // Nothing in Head
  705. // Something in Index
  706. if (m==null && walk.isDirectoryFileConflict()) {
  707. // Nothing in Merge and current path is part of
  708. // File/Folder conflict
  709. // Nothing in Head
  710. // Something in Index
  711. if (dce != null
  712. && (f == null || f.isModified(dce, true,
  713. this.walk.getObjectReader())))
  714. // No file or file is dirty
  715. // Nothing in Merge and current path is part of
  716. // File/Folder conflict
  717. // Nothing in Head
  718. // Something in Index
  719. // -> File folder conflict and Merge wants this
  720. // path to be removed. Since the file is dirty
  721. // report a conflict
  722. conflict(name, dce, h, m);
  723. else
  724. // A file is present and file is not dirty
  725. // Nothing in Merge and current path is part of
  726. // File/Folder conflict
  727. // Nothing in Head
  728. // Something in Index
  729. // -> File folder conflict and Merge wants this path
  730. // to be removed. Since the file is not dirty remove
  731. // file and index entry
  732. remove(name);
  733. } else
  734. // Something in Merge or current path is not part of
  735. // File/Folder conflict
  736. // Merge contains nothing or the same as Index
  737. // Nothing in Head
  738. // Something in Index
  739. // -> Merge contains nothing new. Keep the index.
  740. keep(dce);
  741. } else
  742. // Merge contains something and it is not the same as Index
  743. // Nothing in Head
  744. // Something in Index
  745. // -> Index contains something new (different from Head)
  746. // and Merge is different from Index. Report a conflict
  747. conflict(name, dce, h, m);
  748. } else if (m == null) {
  749. // Nothing in Merge
  750. // Something in Head
  751. // Something in Index
  752. /**
  753. * <pre>
  754. * clean I==H I==M H M Result
  755. * -----------------------------------------------------
  756. * 10 yes yes N/A exists nothing remove path from index
  757. * 11 no yes N/A exists nothing keep file
  758. * 12 yes no N/A exists nothing fail
  759. * 13 no no N/A exists nothing fail
  760. * </pre>
  761. */
  762. if (iMode == FileMode.GITLINK) {
  763. // A submodule in Index
  764. // Nothing in Merge
  765. // Something in Head
  766. // Submodules that disappear from the checkout must
  767. // be removed from the index, but not deleted from disk.
  768. remove(name);
  769. } else {
  770. // Something different from a submodule in Index
  771. // Nothing in Merge
  772. // Something in Head
  773. if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
  774. headCommitTree)) {
  775. // Index contains the same as Head
  776. // Something different from a submodule in Index
  777. // Nothing in Merge
  778. // Something in Head
  779. if (f != null
  780. && f.isModified(dce, true,
  781. this.walk.getObjectReader())) {
  782. // file is dirty
  783. // Index contains the same as Head
  784. // Something different from a submodule in Index
  785. // Nothing in Merge
  786. // Something in Head
  787. if (!FileMode.TREE.equals(f.getEntryFileMode())
  788. && FileMode.TREE.equals(iMode))
  789. // The workingtree contains a file and the index semantically contains a folder.
  790. // Git considers the workingtree file as untracked. Just keep the untracked file.
  791. return;
  792. else
  793. // -> file is dirty and tracked but is should be
  794. // removed. That's a conflict
  795. conflict(name, dce, h, m);
  796. } else
  797. // file doesn't exist or is clean
  798. // Index contains the same as Head
  799. // Something different from a submodule in Index
  800. // Nothing in Merge
  801. // Something in Head
  802. // -> Remove from index and delete the file
  803. remove(name);
  804. } else
  805. // Index contains something different from Head
  806. // Something different from a submodule in Index
  807. // Nothing in Merge
  808. // Something in Head
  809. // -> Something new is in index (and maybe even on the
  810. // filesystem). But Merge wants the path to be removed.
  811. // Report a conflict
  812. conflict(name, dce, h, m);
  813. }
  814. } else {
  815. // Something in Merge
  816. // Something in Head
  817. // Something in Index
  818. if (!equalIdAndMode(hId, hMode, mId, mMode)
  819. && isModified_IndexTree(name, iId, iMode, hId, hMode,
  820. headCommitTree)
  821. && isModified_IndexTree(name, iId, iMode, mId, mMode,
  822. mergeCommitTree))
  823. // All three contents in Head, Merge, Index differ from each
  824. // other
  825. // -> All contents differ. Report a conflict.
  826. conflict(name, dce, h, m);
  827. else
  828. // At least two of the contents of Head, Index, Merge
  829. // are the same
  830. // Something in Merge
  831. // Something in Head
  832. // Something in Index
  833. if (!isModified_IndexTree(name, iId, iMode, hId, hMode,
  834. headCommitTree)
  835. && isModified_IndexTree(name, iId, iMode, mId, mMode,
  836. mergeCommitTree)) {
  837. // Head contains the same as Index. Merge differs
  838. // Something in Merge
  839. // For submodules just update the index with the new SHA-1
  840. if (dce != null
  841. && FileMode.GITLINK.equals(dce.getFileMode())) {
  842. // Index and Head contain the same submodule. Merge
  843. // differs
  844. // Something in Merge
  845. // -> Nothing new in index. Move to merge.
  846. // Potentially updates the file
  847. // TODO check that we don't overwrite some unsaved
  848. // file content
  849. update(name, mId, mMode);
  850. } else if (dce != null
  851. && (f != null && f.isModified(dce, true,
  852. this.walk.getObjectReader()))) {
  853. // File exists and is dirty
  854. // Head and Index don't contain a submodule
  855. // Head contains the same as Index. Merge differs
  856. // Something in Merge
  857. // -> Merge wants the index and file to be updated
  858. // but the file is dirty. Report a conflict
  859. conflict(name, dce, h, m);
  860. } else {
  861. // File doesn't exist or is clean
  862. // Head and Index don't contain a submodule
  863. // Head contains the same as Index. Merge differs
  864. // Something in Merge
  865. // -> Standard case when switching between branches:
  866. // Nothing new in index but something different in
  867. // Merge. Update index and file
  868. update(name, mId, mMode);
  869. }
  870. } else {
  871. // Head differs from index or merge is same as index
  872. // At least two of the contents of Head, Index, Merge
  873. // are the same
  874. // Something in Merge
  875. // Something in Head
  876. // Something in Index
  877. // Can be formulated as: Either all three states are
  878. // equal or Merge is equal to Head or Index and differs
  879. // to the other one.
  880. // -> In all three cases we don't touch index and file.
  881. keep(dce);
  882. }
  883. }
  884. }
  885. }
  886. /**
  887. * A conflict is detected - add the three different stages to the index
  888. * @param path the path of the conflicting entry
  889. * @param e the previous index entry
  890. * @param h the first tree you want to merge (the HEAD)
  891. * @param m the second tree you want to merge
  892. */
  893. private void conflict(String path, DirCacheEntry e, AbstractTreeIterator h, AbstractTreeIterator m) {
  894. conflicts.add(path);
  895. DirCacheEntry entry;
  896. if (e != null) {
  897. entry = new DirCacheEntry(e.getPathString(), DirCacheEntry.STAGE_1);
  898. entry.copyMetaData(e, true);
  899. builder.add(entry);
  900. }
  901. if (h != null && !FileMode.TREE.equals(h.getEntryFileMode())) {
  902. entry = new DirCacheEntry(h.getEntryPathString(), DirCacheEntry.STAGE_2);
  903. entry.setFileMode(h.getEntryFileMode());
  904. entry.setObjectId(h.getEntryObjectId());
  905. builder.add(entry);
  906. }
  907. if (m != null && !FileMode.TREE.equals(m.getEntryFileMode())) {
  908. entry = new DirCacheEntry(m.getEntryPathString(), DirCacheEntry.STAGE_3);
  909. entry.setFileMode(m.getEntryFileMode());
  910. entry.setObjectId(m.getEntryObjectId());
  911. builder.add(entry);
  912. }
  913. }
  914. private void keep(DirCacheEntry e) {
  915. if (e != null && !FileMode.TREE.equals(e.getFileMode()))
  916. builder.add(e);
  917. }
  918. private void remove(String path) {
  919. removed.add(path);
  920. }
  921. private void update(String path, ObjectId mId, FileMode mode) {
  922. if (!FileMode.TREE.equals(mode)) {
  923. updated.put(path, mId);
  924. DirCacheEntry entry = new DirCacheEntry(path, DirCacheEntry.STAGE_0);
  925. entry.setObjectId(mId);
  926. entry.setFileMode(mode);
  927. builder.add(entry);
  928. }
  929. }
  930. /**
  931. * If <code>true</code>, will scan first to see if it's possible to check
  932. * out, otherwise throw {@link CheckoutConflictException}. If
  933. * <code>false</code>, it will silently deal with the problem.
  934. *
  935. * @param failOnConflict
  936. */
  937. public void setFailOnConflict(boolean failOnConflict) {
  938. this.failOnConflict = failOnConflict;
  939. }
  940. /**
  941. * This method implements how to handle conflicts when
  942. * {@link #failOnConflict} is false
  943. *
  944. * @throws CheckoutConflictException
  945. */
  946. private void cleanUpConflicts() throws CheckoutConflictException {
  947. // TODO: couldn't we delete unsaved worktree content here?
  948. for (String c : conflicts) {
  949. File conflict = new File(repo.getWorkTree(), c);
  950. if (!conflict.delete())
  951. throw new CheckoutConflictException(MessageFormat.format(
  952. JGitText.get().cannotDeleteFile, c));
  953. removeEmptyParents(conflict);
  954. }
  955. for (String r : removed) {
  956. File file = new File(repo.getWorkTree(), r);
  957. if (!file.delete())
  958. throw new CheckoutConflictException(
  959. MessageFormat.format(JGitText.get().cannotDeleteFile,
  960. file.getAbsolutePath()));
  961. removeEmptyParents(file);
  962. }
  963. }
  964. /**
  965. * Checks whether the subtree starting at a given path differs between Index and
  966. * workingtree.
  967. *
  968. * @param path
  969. * @return true if the subtrees differ
  970. * @throws CorruptObjectException
  971. * @throws IOException
  972. */
  973. private boolean isModifiedSubtree_IndexWorkingtree(String path)
  974. throws CorruptObjectException, IOException {
  975. try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
  976. tw.addTree(new DirCacheIterator(dc));
  977. tw.addTree(new FileTreeIterator(repo));
  978. tw.setRecursive(true);
  979. tw.setFilter(PathFilter.create(path));
  980. DirCacheIterator dcIt;
  981. WorkingTreeIterator wtIt;
  982. while (tw.next()) {
  983. dcIt = tw.getTree(0, DirCacheIterator.class);
  984. wtIt = tw.getTree(1, WorkingTreeIterator.class);
  985. if (dcIt == null || wtIt == null)
  986. return true;
  987. if (wtIt.isModified(dcIt.getDirCacheEntry(), true,
  988. this.walk.getObjectReader())) {
  989. return true;
  990. }
  991. }
  992. return false;
  993. }
  994. }
  995. private boolean isModified_IndexTree(String path, ObjectId iId,
  996. FileMode iMode, ObjectId tId, FileMode tMode, ObjectId rootTree)
  997. throws CorruptObjectException, IOException {
  998. if (iMode != tMode)
  999. return true;
  1000. if (FileMode.TREE.equals(iMode)
  1001. && (iId == null || ObjectId.zeroId().equals(iId)))
  1002. return isModifiedSubtree_IndexTree(path, rootTree);
  1003. else
  1004. return !equalIdAndMode(iId, iMode, tId, tMode);
  1005. }
  1006. /**
  1007. * Checks whether the subtree starting at a given path differs between Index and
  1008. * some tree.
  1009. *
  1010. * @param path
  1011. * @param tree
  1012. * the tree to compare
  1013. * @return true if the subtrees differ
  1014. * @throws CorruptObjectException
  1015. * @throws IOException
  1016. */
  1017. private boolean isModifiedSubtree_IndexTree(String path, ObjectId tree)
  1018. throws CorruptObjectException, IOException {
  1019. try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
  1020. tw.addTree(new DirCacheIterator(dc));
  1021. tw.addTree(tree);
  1022. tw.setRecursive(true);
  1023. tw.setFilter(PathFilter.create(path));
  1024. while (tw.next()) {
  1025. AbstractTreeIterator dcIt = tw.getTree(0,
  1026. DirCacheIterator.class);
  1027. AbstractTreeIterator treeIt = tw.getTree(1,
  1028. AbstractTreeIterator.class);
  1029. if (dcIt == null || treeIt == null)
  1030. return true;
  1031. if (dcIt.getEntryRawMode() != treeIt.getEntryRawMode())
  1032. return true;
  1033. if (!dcIt.getEntryObjectId().equals(treeIt.getEntryObjectId()))
  1034. return true;
  1035. }
  1036. return false;
  1037. }
  1038. }
  1039. /**
  1040. * Updates the file in the working tree with content and mode from an entry
  1041. * in the index. The new content is first written to a new temporary file in
  1042. * the same directory as the real file. Then that new file is renamed to the
  1043. * final filename.
  1044. *
  1045. * <p>
  1046. * TODO: this method works directly on File IO, we may need another
  1047. * abstraction (like WorkingTreeIterator). This way we could tell e.g.
  1048. * Eclipse that Files in the workspace got changed
  1049. * </p>
  1050. *
  1051. * @param repo
  1052. * repository managing the destination work tree.
  1053. * @param entry
  1054. * the entry containing new mode and content
  1055. * @param or
  1056. * object reader to use for checkout
  1057. * @throws IOException
  1058. * @since 3.6
  1059. */
  1060. public static void checkoutEntry(Repository repo, DirCacheEntry entry,
  1061. ObjectReader or) throws IOException {
  1062. ObjectLoader ol = or.open(entry.getObjectId());
  1063. File f = new File(repo.getWorkTree(), entry.getPathString());
  1064. File parentDir = f.getParentFile();
  1065. FileUtils.mkdirs(parentDir, true);
  1066. FS fs = repo.getFS();
  1067. WorkingTreeOptions opt = repo.getConfig().get(WorkingTreeOptions.KEY);
  1068. if (entry.getFileMode() == FileMode.SYMLINK
  1069. && opt.getSymLinks() == SymLinks.TRUE) {
  1070. byte[] bytes = ol.getBytes();
  1071. String target = RawParseUtils.decode(bytes);
  1072. fs.createSymLink(f, target);
  1073. entry.setLength(bytes.length);
  1074. entry.setLastModified(fs.lastModified(f));
  1075. return;
  1076. }
  1077. File tmpFile = File.createTempFile(
  1078. "._" + f.getName(), null, parentDir); //$NON-NLS-1$
  1079. OutputStream channel = new FileOutputStream(tmpFile);
  1080. if (opt.getAutoCRLF() == AutoCRLF.TRUE)
  1081. channel = new AutoCRLFOutputStream(channel);
  1082. try {
  1083. ol.copyTo(channel);
  1084. } finally {
  1085. channel.close();
  1086. }
  1087. entry.setLength(opt.getAutoCRLF() == AutoCRLF.TRUE ? //
  1088. tmpFile.length() // AutoCRLF wants on-disk-size
  1089. : (int) ol.getSize());
  1090. if (opt.isFileMode() && fs.supportsExecute()) {
  1091. if (FileMode.EXECUTABLE_FILE.equals(entry.getRawMode())) {
  1092. if (!fs.canExecute(tmpFile))
  1093. fs.setExecute(tmpFile, true);
  1094. } else {
  1095. if (fs.canExecute(tmpFile))
  1096. fs.setExecute(tmpFile, false);
  1097. }
  1098. }
  1099. try {
  1100. FileUtils.rename(tmpFile, f);
  1101. } catch (IOException e) {
  1102. throw new IOException(MessageFormat.format(
  1103. JGitText.get().renameFileFailed, tmpFile.getPath(),
  1104. f.getPath()));
  1105. }
  1106. entry.setLastModified(f.lastModified());
  1107. }
  1108. @SuppressWarnings("deprecation")
  1109. private static void checkValidPath(CanonicalTreeParser t)
  1110. throws InvalidPathException {
  1111. ObjectChecker chk = new ObjectChecker()
  1112. .setSafeForWindows(SystemReader.getInstance().isWindows())
  1113. .setSafeForMacOS(SystemReader.getInstance().isMacOS());
  1114. for (CanonicalTreeParser i = t; i != null; i = i.getParent())
  1115. checkValidPathSegment(chk, i);
  1116. }
  1117. /**
  1118. * Check if path is a valid path for a checked out file name or ref name.
  1119. *
  1120. * @param path
  1121. * @throws InvalidPathException
  1122. * if the path is invalid
  1123. * @since 3.3
  1124. */
  1125. static void checkValidPath(String path) throws InvalidPathException {
  1126. try {
  1127. SystemReader.getInstance().checkPath(path);
  1128. } catch (CorruptObjectException e) {
  1129. InvalidPathException p = new InvalidPathException(path);
  1130. p.initCause(e);
  1131. throw p;
  1132. }
  1133. }
  1134. private static void checkValidPathSegment(ObjectChecker chk,
  1135. CanonicalTreeParser t) throws InvalidPathException {
  1136. try {
  1137. int ptr = t.getNameOffset();
  1138. int end = ptr + t.getNameLength();
  1139. chk.checkPathSegment(t.getEntryPathBuffer(), ptr, end);
  1140. } catch (CorruptObjectException err) {
  1141. String path = t.getEntryPathString();
  1142. InvalidPathException i = new InvalidPathException(path);
  1143. i.initCause(err);
  1144. throw i;
  1145. }
  1146. }
  1147. }