Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

WorkingTreeIterator.java 42KB

Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
Fix computation of id in WorkingTreeIterator with autocrlf and smudging JGit failed to do checkouts when the index contained smudged entries and autocrlf was on. In such cases the WorkingTreeIterator calculated the SHA1 sometimes on content which was not correctly filtered. The SHA1 was computed on content which two times went through a lf->crlf conversion. We used to tell the treewalk whether it is a checkin or checkout operation and always use the related filters when reading any content. If on windows and autocrlf is true and we do a checkout operation then we always used a lf->crlf conversion on any text content. That's not correct. Even during a checkout we sometimes need the crlf->lf conversion. E.g. when calculating the content-id for working-tree content we need to use crlf->lf filtering although the overall operation type is checkout. Often this bug does not have effects because we seldom compute the content-id of filesystem content during a checkout. But we do need to know whether a file is dirty or not before we overwrite it during a checkout. And if the index entries are smudged we don't trust the index and compute filesystem-content-sha1's explicitly. This caused EGit not to be able to switch branches anymore on Windows when autocrlf was true. EGit denied the checkout because it thought workingtree files are dirty because content-sha1 are computed on wrongly filtered content. Bug: 493360 Change-Id: I1072a57b4c529ba3aaa50b7b02d2b816bb64a9b8 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
8 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  4. * Copyright (C) 2010, Matthias Sohn <matthias.sohn@sap.com>
  5. * Copyright (C) 2012-2013, Robin Rosenberg
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.treewalk;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.nio.ByteBuffer;
  54. import java.nio.CharBuffer;
  55. import java.nio.charset.CharacterCodingException;
  56. import java.nio.charset.CharsetEncoder;
  57. import java.security.MessageDigest;
  58. import java.text.MessageFormat;
  59. import java.util.Arrays;
  60. import java.util.Collections;
  61. import java.util.Comparator;
  62. import org.eclipse.jgit.api.errors.FilterFailedException;
  63. import org.eclipse.jgit.attributes.AttributesNode;
  64. import org.eclipse.jgit.attributes.AttributesRule;
  65. import org.eclipse.jgit.attributes.FilterCommand;
  66. import org.eclipse.jgit.attributes.FilterCommandRegistry;
  67. import org.eclipse.jgit.diff.RawText;
  68. import org.eclipse.jgit.dircache.DirCache;
  69. import org.eclipse.jgit.dircache.DirCacheEntry;
  70. import org.eclipse.jgit.dircache.DirCacheIterator;
  71. import org.eclipse.jgit.errors.CorruptObjectException;
  72. import org.eclipse.jgit.errors.MissingObjectException;
  73. import org.eclipse.jgit.errors.NoWorkTreeException;
  74. import org.eclipse.jgit.ignore.FastIgnoreRule;
  75. import org.eclipse.jgit.ignore.IgnoreNode;
  76. import org.eclipse.jgit.internal.JGitText;
  77. import org.eclipse.jgit.lib.Constants;
  78. import org.eclipse.jgit.lib.CoreConfig;
  79. import org.eclipse.jgit.lib.CoreConfig.CheckStat;
  80. import org.eclipse.jgit.lib.CoreConfig.EolStreamType;
  81. import org.eclipse.jgit.lib.CoreConfig.SymLinks;
  82. import org.eclipse.jgit.lib.FileMode;
  83. import org.eclipse.jgit.lib.ObjectId;
  84. import org.eclipse.jgit.lib.ObjectLoader;
  85. import org.eclipse.jgit.lib.ObjectReader;
  86. import org.eclipse.jgit.lib.Repository;
  87. import org.eclipse.jgit.submodule.SubmoduleWalk;
  88. import org.eclipse.jgit.treewalk.TreeWalk.OperationType;
  89. import org.eclipse.jgit.util.FS;
  90. import org.eclipse.jgit.util.FS.ExecutionResult;
  91. import org.eclipse.jgit.util.Holder;
  92. import org.eclipse.jgit.util.IO;
  93. import org.eclipse.jgit.util.Paths;
  94. import org.eclipse.jgit.util.RawParseUtils;
  95. import org.eclipse.jgit.util.TemporaryBuffer;
  96. import org.eclipse.jgit.util.TemporaryBuffer.LocalFile;
  97. import org.eclipse.jgit.util.io.AutoLFInputStream;
  98. import org.eclipse.jgit.util.io.EolStreamTypeUtil;
  99. /**
  100. * Walks a working directory tree as part of a {@link TreeWalk}.
  101. * <p>
  102. * Most applications will want to use the standard implementation of this
  103. * iterator, {@link FileTreeIterator}, as that does all IO through the standard
  104. * <code>java.io</code> package. Plugins for a Java based IDE may however wish
  105. * to create their own implementations of this class to allow traversal of the
  106. * IDE's project space, as well as benefit from any caching the IDE may have.
  107. *
  108. * @see FileTreeIterator
  109. */
  110. public abstract class WorkingTreeIterator extends AbstractTreeIterator {
  111. private static final int MAX_EXCEPTION_TEXT_SIZE = 10 * 1024;
  112. /** An empty entry array, suitable for {@link #init(Entry[])}. */
  113. protected static final Entry[] EOF = {};
  114. /** Size we perform file IO in if we have to read and hash a file. */
  115. static final int BUFFER_SIZE = 2048;
  116. /**
  117. * Maximum size of files which may be read fully into memory for performance
  118. * reasons.
  119. */
  120. private static final long MAXIMUM_FILE_SIZE_TO_READ_FULLY = 65536;
  121. /** Inherited state of this iterator, describing working tree, etc. */
  122. private final IteratorState state;
  123. /** The {@link #idBuffer()} for the current entry. */
  124. private byte[] contentId;
  125. /** Index within {@link #entries} that {@link #contentId} came from. */
  126. private int contentIdFromPtr;
  127. /** List of entries obtained from the subclass. */
  128. private Entry[] entries;
  129. /** Total number of entries in {@link #entries} that are valid. */
  130. private int entryCnt;
  131. /** Current position within {@link #entries}. */
  132. private int ptr;
  133. /** If there is a .gitignore file present, the parsed rules from it. */
  134. private IgnoreNode ignoreNode;
  135. /**
  136. * cached clean filter command. Use a Ref in order to distinguish between
  137. * the ref not cached yet and the value null
  138. */
  139. private Holder<String> cleanFilterCommandHolder;
  140. /**
  141. * cached eol stream type. Use a Ref in order to distinguish between the ref
  142. * not cached yet and the value null
  143. */
  144. private Holder<EolStreamType> eolStreamTypeHolder;
  145. /** Repository that is the root level being iterated over */
  146. protected Repository repository;
  147. /** Cached canonical length, initialized from {@link #idBuffer()} */
  148. private long canonLen = -1;
  149. /** The offset of the content id in {@link #idBuffer()} */
  150. private int contentIdOffset;
  151. /**
  152. * Create a new iterator with no parent.
  153. *
  154. * @param options
  155. * working tree options to be used
  156. */
  157. protected WorkingTreeIterator(WorkingTreeOptions options) {
  158. super();
  159. state = new IteratorState(options);
  160. }
  161. /**
  162. * Create a new iterator with no parent and a prefix.
  163. * <p>
  164. * The prefix path supplied is inserted in front of all paths generated by
  165. * this iterator. It is intended to be used when an iterator is being
  166. * created for a subsection of an overall repository and needs to be
  167. * combined with other iterators that are created to run over the entire
  168. * repository namespace.
  169. *
  170. * @param prefix
  171. * position of this iterator in the repository tree. The value
  172. * may be null or the empty string to indicate the prefix is the
  173. * root of the repository. A trailing slash ('/') is
  174. * automatically appended if the prefix does not end in '/'.
  175. * @param options
  176. * working tree options to be used
  177. */
  178. protected WorkingTreeIterator(final String prefix,
  179. WorkingTreeOptions options) {
  180. super(prefix);
  181. state = new IteratorState(options);
  182. }
  183. /**
  184. * Create an iterator for a subtree of an existing iterator.
  185. *
  186. * @param p
  187. * parent tree iterator.
  188. */
  189. protected WorkingTreeIterator(final WorkingTreeIterator p) {
  190. super(p);
  191. state = p.state;
  192. repository = p.repository;
  193. }
  194. /**
  195. * Initialize this iterator for the root level of a repository.
  196. * <p>
  197. * This method should only be invoked after calling {@link #init(Entry[])},
  198. * and only for the root iterator.
  199. *
  200. * @param repo
  201. * the repository.
  202. */
  203. protected void initRootIterator(Repository repo) {
  204. repository = repo;
  205. Entry entry;
  206. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  207. entry = ((PerDirectoryIgnoreNode) ignoreNode).entry;
  208. else
  209. entry = null;
  210. ignoreNode = new RootIgnoreNode(entry, repo);
  211. }
  212. /**
  213. * Define the matching {@link DirCacheIterator}, to optimize ObjectIds.
  214. *
  215. * Once the DirCacheIterator has been set this iterator must only be
  216. * advanced by the TreeWalk that is supplied, as it assumes that itself and
  217. * the corresponding DirCacheIterator are positioned on the same file path
  218. * whenever {@link #idBuffer()} is invoked.
  219. *
  220. * @param walk
  221. * the walk that will be advancing this iterator.
  222. * @param treeId
  223. * index of the matching {@link DirCacheIterator}.
  224. */
  225. public void setDirCacheIterator(TreeWalk walk, int treeId) {
  226. state.walk = walk;
  227. state.dirCacheTree = treeId;
  228. }
  229. @Override
  230. public boolean hasId() {
  231. if (contentIdFromPtr == ptr)
  232. return true;
  233. return (mode & FileMode.TYPE_MASK) == FileMode.TYPE_FILE;
  234. }
  235. @Override
  236. public byte[] idBuffer() {
  237. if (contentIdFromPtr == ptr)
  238. return contentId;
  239. if (state.walk != null) {
  240. // If there is a matching DirCacheIterator, we can reuse
  241. // its idBuffer, but only if we appear to be clean against
  242. // the cached index information for the path.
  243. DirCacheIterator i = state.walk.getTree(state.dirCacheTree,
  244. DirCacheIterator.class);
  245. if (i != null) {
  246. DirCacheEntry ent = i.getDirCacheEntry();
  247. if (ent != null && compareMetadata(ent) == MetadataDiff.EQUAL
  248. && ((ent.getFileMode().getBits()
  249. & FileMode.TYPE_MASK) != FileMode.TYPE_GITLINK)) {
  250. contentIdOffset = i.idOffset();
  251. contentIdFromPtr = ptr;
  252. return contentId = i.idBuffer();
  253. }
  254. contentIdOffset = 0;
  255. } else {
  256. contentIdOffset = 0;
  257. }
  258. }
  259. switch (mode & FileMode.TYPE_MASK) {
  260. case FileMode.TYPE_SYMLINK:
  261. case FileMode.TYPE_FILE:
  262. contentIdFromPtr = ptr;
  263. return contentId = idBufferBlob(entries[ptr]);
  264. case FileMode.TYPE_GITLINK:
  265. contentIdFromPtr = ptr;
  266. return contentId = idSubmodule(entries[ptr]);
  267. }
  268. return zeroid;
  269. }
  270. @Override
  271. public boolean isWorkTree() {
  272. return true;
  273. }
  274. /**
  275. * Get submodule id for given entry.
  276. *
  277. * @param e
  278. * @return non-null submodule id
  279. */
  280. protected byte[] idSubmodule(Entry e) {
  281. if (repository == null)
  282. return zeroid;
  283. File directory;
  284. try {
  285. directory = repository.getWorkTree();
  286. } catch (NoWorkTreeException nwte) {
  287. return zeroid;
  288. }
  289. return idSubmodule(directory, e);
  290. }
  291. /**
  292. * Get submodule id using the repository at the location of the entry
  293. * relative to the directory.
  294. *
  295. * @param directory
  296. * @param e
  297. * @return non-null submodule id
  298. */
  299. protected byte[] idSubmodule(File directory, Entry e) {
  300. final Repository submoduleRepo;
  301. try {
  302. submoduleRepo = SubmoduleWalk.getSubmoduleRepository(directory,
  303. e.getName());
  304. } catch (IOException exception) {
  305. return zeroid;
  306. }
  307. if (submoduleRepo == null)
  308. return zeroid;
  309. final ObjectId head;
  310. try {
  311. head = submoduleRepo.resolve(Constants.HEAD);
  312. } catch (IOException exception) {
  313. return zeroid;
  314. } finally {
  315. submoduleRepo.close();
  316. }
  317. if (head == null)
  318. return zeroid;
  319. final byte[] id = new byte[Constants.OBJECT_ID_LENGTH];
  320. head.copyRawTo(id, 0);
  321. return id;
  322. }
  323. private static final byte[] digits = { '0', '1', '2', '3', '4', '5', '6',
  324. '7', '8', '9' };
  325. private static final byte[] hblob = Constants
  326. .encodedTypeString(Constants.OBJ_BLOB);
  327. private byte[] idBufferBlob(final Entry e) {
  328. try {
  329. final InputStream is = e.openInputStream();
  330. if (is == null)
  331. return zeroid;
  332. try {
  333. state.initializeDigestAndReadBuffer();
  334. final long len = e.getLength();
  335. InputStream filteredIs = possiblyFilteredInputStream(e, is, len,
  336. OperationType.CHECKIN_OP);
  337. return computeHash(filteredIs, canonLen);
  338. } finally {
  339. safeClose(is);
  340. }
  341. } catch (IOException err) {
  342. // Can't read the file? Don't report the failure either.
  343. return zeroid;
  344. }
  345. }
  346. private InputStream possiblyFilteredInputStream(final Entry e,
  347. final InputStream is, final long len) throws IOException {
  348. return possiblyFilteredInputStream(e, is, len, null);
  349. }
  350. private InputStream possiblyFilteredInputStream(final Entry e,
  351. final InputStream is, final long len, OperationType opType)
  352. throws IOException {
  353. if (getCleanFilterCommand() == null
  354. && getEolStreamType(opType) == EolStreamType.DIRECT) {
  355. canonLen = len;
  356. return is;
  357. }
  358. if (len <= MAXIMUM_FILE_SIZE_TO_READ_FULLY) {
  359. ByteBuffer rawbuf = IO.readWholeStream(is, (int) len);
  360. rawbuf = filterClean(rawbuf.array(), rawbuf.limit(), opType);
  361. canonLen = rawbuf.limit();
  362. return new ByteArrayInputStream(rawbuf.array(), 0, (int) canonLen);
  363. }
  364. if (getCleanFilterCommand() == null && isBinary(e)) {
  365. canonLen = len;
  366. return is;
  367. }
  368. final InputStream lenIs = filterClean(e.openInputStream(),
  369. opType);
  370. try {
  371. canonLen = computeLength(lenIs);
  372. } finally {
  373. safeClose(lenIs);
  374. }
  375. return filterClean(is, opType);
  376. }
  377. private static void safeClose(final InputStream in) {
  378. try {
  379. in.close();
  380. } catch (IOException err2) {
  381. // Suppress any error related to closing an input
  382. // stream. We don't care, we should not have any
  383. // outstanding data to flush or anything like that.
  384. }
  385. }
  386. private static boolean isBinary(Entry entry) throws IOException {
  387. InputStream in = entry.openInputStream();
  388. try {
  389. return RawText.isBinary(in);
  390. } finally {
  391. safeClose(in);
  392. }
  393. }
  394. private ByteBuffer filterClean(byte[] src, int n, OperationType opType)
  395. throws IOException {
  396. InputStream in = new ByteArrayInputStream(src);
  397. try {
  398. return IO.readWholeStream(filterClean(in, opType), n);
  399. } finally {
  400. safeClose(in);
  401. }
  402. }
  403. private InputStream filterClean(InputStream in) throws IOException {
  404. return filterClean(in, null);
  405. }
  406. private InputStream filterClean(InputStream in, OperationType opType)
  407. throws IOException {
  408. in = handleAutoCRLF(in, opType);
  409. String filterCommand = getCleanFilterCommand();
  410. if (filterCommand != null) {
  411. if (FilterCommandRegistry.isRegistered(filterCommand)) {
  412. LocalFile buffer = new TemporaryBuffer.LocalFile(null);
  413. FilterCommand command = FilterCommandRegistry
  414. .createFilterCommand(filterCommand, repository, in,
  415. buffer);
  416. while (command.run() != -1) {
  417. // loop as long as command.run() tells there is work to do
  418. }
  419. return buffer.openInputStream();
  420. }
  421. FS fs = repository.getFS();
  422. ProcessBuilder filterProcessBuilder = fs.runInShell(filterCommand,
  423. new String[0]);
  424. filterProcessBuilder.directory(repository.getWorkTree());
  425. filterProcessBuilder.environment().put(Constants.GIT_DIR_KEY,
  426. repository.getDirectory().getAbsolutePath());
  427. ExecutionResult result;
  428. try {
  429. result = fs.execute(filterProcessBuilder, in);
  430. } catch (IOException | InterruptedException e) {
  431. throw new IOException(new FilterFailedException(e,
  432. filterCommand, getEntryPathString()));
  433. }
  434. int rc = result.getRc();
  435. if (rc != 0) {
  436. throw new IOException(new FilterFailedException(rc,
  437. filterCommand, getEntryPathString(),
  438. result.getStdout().toByteArray(MAX_EXCEPTION_TEXT_SIZE),
  439. RawParseUtils.decode(result.getStderr()
  440. .toByteArray(MAX_EXCEPTION_TEXT_SIZE))));
  441. }
  442. return result.getStdout().openInputStream();
  443. }
  444. return in;
  445. }
  446. private InputStream handleAutoCRLF(InputStream in, OperationType opType)
  447. throws IOException {
  448. return EolStreamTypeUtil.wrapInputStream(in, getEolStreamType(opType));
  449. }
  450. /**
  451. * Returns the working tree options used by this iterator.
  452. *
  453. * @return working tree options
  454. */
  455. public WorkingTreeOptions getOptions() {
  456. return state.options;
  457. }
  458. @Override
  459. public int idOffset() {
  460. return contentIdOffset;
  461. }
  462. @Override
  463. public void reset() {
  464. if (!first()) {
  465. ptr = 0;
  466. if (!eof())
  467. parseEntry();
  468. }
  469. }
  470. @Override
  471. public boolean first() {
  472. return ptr == 0;
  473. }
  474. @Override
  475. public boolean eof() {
  476. return ptr == entryCnt;
  477. }
  478. @Override
  479. public void next(final int delta) throws CorruptObjectException {
  480. ptr += delta;
  481. if (!eof()) {
  482. parseEntry();
  483. }
  484. }
  485. @Override
  486. public void back(final int delta) throws CorruptObjectException {
  487. ptr -= delta;
  488. parseEntry();
  489. }
  490. private void parseEntry() {
  491. final Entry e = entries[ptr];
  492. mode = e.getMode().getBits();
  493. final int nameLen = e.encodedNameLen;
  494. ensurePathCapacity(pathOffset + nameLen, pathOffset);
  495. System.arraycopy(e.encodedName, 0, path, pathOffset, nameLen);
  496. pathLen = pathOffset + nameLen;
  497. canonLen = -1;
  498. cleanFilterCommandHolder = null;
  499. eolStreamTypeHolder = null;
  500. }
  501. /**
  502. * Get the raw byte length of this entry.
  503. *
  504. * @return size of this file, in bytes.
  505. */
  506. public long getEntryLength() {
  507. return current().getLength();
  508. }
  509. /**
  510. * Get the filtered input length of this entry
  511. *
  512. * @return size of the content, in bytes
  513. * @throws IOException
  514. */
  515. public long getEntryContentLength() throws IOException {
  516. if (canonLen == -1) {
  517. long rawLen = getEntryLength();
  518. if (rawLen == 0)
  519. canonLen = 0;
  520. InputStream is = current().openInputStream();
  521. try {
  522. // canonLen gets updated here
  523. possiblyFilteredInputStream(current(), is, current()
  524. .getLength());
  525. } finally {
  526. safeClose(is);
  527. }
  528. }
  529. return canonLen;
  530. }
  531. /**
  532. * Get the last modified time of this entry.
  533. *
  534. * @return last modified time of this file, in milliseconds since the epoch
  535. * (Jan 1, 1970 UTC).
  536. */
  537. public long getEntryLastModified() {
  538. return current().getLastModified();
  539. }
  540. /**
  541. * Obtain an input stream to read the file content.
  542. * <p>
  543. * Efficient implementations are not required. The caller will usually
  544. * obtain the stream only once per entry, if at all.
  545. * <p>
  546. * The input stream should not use buffering if the implementation can avoid
  547. * it. The caller will buffer as necessary to perform efficient block IO
  548. * operations.
  549. * <p>
  550. * The caller will close the stream once complete.
  551. *
  552. * @return a stream to read from the file.
  553. * @throws IOException
  554. * the file could not be opened for reading.
  555. */
  556. public InputStream openEntryStream() throws IOException {
  557. InputStream rawis = current().openInputStream();
  558. if (getCleanFilterCommand() == null
  559. && getEolStreamType() == EolStreamType.DIRECT)
  560. return rawis;
  561. else
  562. return filterClean(rawis);
  563. }
  564. /**
  565. * Determine if the current entry path is ignored by an ignore rule.
  566. *
  567. * @return true if the entry was ignored by an ignore rule file.
  568. * @throws IOException
  569. * a relevant ignore rule file exists but cannot be read.
  570. */
  571. public boolean isEntryIgnored() throws IOException {
  572. return isEntryIgnored(pathLen);
  573. }
  574. /**
  575. * Determine if the entry path is ignored by an ignore rule.
  576. *
  577. * @param pLen
  578. * the length of the path in the path buffer.
  579. * @return true if the entry is ignored by an ignore rule.
  580. * @throws IOException
  581. * a relevant ignore rule file exists but cannot be read.
  582. */
  583. protected boolean isEntryIgnored(final int pLen) throws IOException {
  584. return isEntryIgnored(pLen, mode, false);
  585. }
  586. /**
  587. * Determine if the entry path is ignored by an ignore rule. Consider
  588. * possible rule negation from child iterator.
  589. *
  590. * @param pLen
  591. * the length of the path in the path buffer.
  592. * @param fileMode
  593. * the original iterator file mode
  594. * @param negatePrevious
  595. * true if the previous matching iterator rule was negation
  596. * @return true if the entry is ignored by an ignore rule.
  597. * @throws IOException
  598. * a relevant ignore rule file exists but cannot be read.
  599. */
  600. private boolean isEntryIgnored(final int pLen, int fileMode,
  601. boolean negatePrevious)
  602. throws IOException {
  603. IgnoreNode rules = getIgnoreNode();
  604. if (rules != null) {
  605. // The ignore code wants path to start with a '/' if possible.
  606. // If we have the '/' in our path buffer because we are inside
  607. // a subdirectory include it in the range we convert to string.
  608. //
  609. int pOff = pathOffset;
  610. if (0 < pOff)
  611. pOff--;
  612. String p = TreeWalk.pathOf(path, pOff, pLen);
  613. switch (rules.isIgnored(p, FileMode.TREE.equals(fileMode),
  614. negatePrevious)) {
  615. case IGNORED:
  616. return true;
  617. case NOT_IGNORED:
  618. return false;
  619. case CHECK_PARENT:
  620. negatePrevious = false;
  621. break;
  622. case CHECK_PARENT_NEGATE_FIRST_MATCH:
  623. negatePrevious = true;
  624. break;
  625. }
  626. }
  627. if (parent instanceof WorkingTreeIterator)
  628. return ((WorkingTreeIterator) parent).isEntryIgnored(pLen, fileMode,
  629. negatePrevious);
  630. return false;
  631. }
  632. private IgnoreNode getIgnoreNode() throws IOException {
  633. if (ignoreNode instanceof PerDirectoryIgnoreNode)
  634. ignoreNode = ((PerDirectoryIgnoreNode) ignoreNode).load();
  635. return ignoreNode;
  636. }
  637. /**
  638. * Retrieves the {@link AttributesNode} for the current entry.
  639. *
  640. * @return {@link AttributesNode} for the current entry.
  641. * @throws IOException
  642. * if an error is raised while parsing the .gitattributes file
  643. * @since 3.7
  644. */
  645. public AttributesNode getEntryAttributesNode() throws IOException {
  646. if (attributesNode instanceof PerDirectoryAttributesNode)
  647. attributesNode = ((PerDirectoryAttributesNode) attributesNode)
  648. .load();
  649. return attributesNode;
  650. }
  651. private static final Comparator<Entry> ENTRY_CMP = new Comparator<Entry>() {
  652. public int compare(Entry a, Entry b) {
  653. return Paths.compare(
  654. a.encodedName, 0, a.encodedNameLen, a.getMode().getBits(),
  655. b.encodedName, 0, b.encodedNameLen, b.getMode().getBits());
  656. }
  657. };
  658. /**
  659. * Constructor helper.
  660. *
  661. * @param list
  662. * files in the subtree of the work tree this iterator operates
  663. * on
  664. */
  665. protected void init(final Entry[] list) {
  666. // Filter out nulls, . and .. as these are not valid tree entries,
  667. // also cache the encoded forms of the path names for efficient use
  668. // later on during sorting and iteration.
  669. //
  670. entries = list;
  671. int i, o;
  672. final CharsetEncoder nameEncoder = state.nameEncoder;
  673. for (i = 0, o = 0; i < entries.length; i++) {
  674. final Entry e = entries[i];
  675. if (e == null)
  676. continue;
  677. final String name = e.getName();
  678. if (".".equals(name) || "..".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$
  679. continue;
  680. if (Constants.DOT_GIT.equals(name))
  681. continue;
  682. if (Constants.DOT_GIT_IGNORE.equals(name))
  683. ignoreNode = new PerDirectoryIgnoreNode(e);
  684. if (Constants.DOT_GIT_ATTRIBUTES.equals(name))
  685. attributesNode = new PerDirectoryAttributesNode(e);
  686. if (i != o)
  687. entries[o] = e;
  688. e.encodeName(nameEncoder);
  689. o++;
  690. }
  691. entryCnt = o;
  692. Arrays.sort(entries, 0, entryCnt, ENTRY_CMP);
  693. contentIdFromPtr = -1;
  694. ptr = 0;
  695. if (!eof())
  696. parseEntry();
  697. else if (pathLen == 0) // see bug 445363
  698. pathLen = pathOffset;
  699. }
  700. /**
  701. * Obtain the current entry from this iterator.
  702. *
  703. * @return the currently selected entry.
  704. */
  705. protected Entry current() {
  706. return entries[ptr];
  707. }
  708. /**
  709. * The result of a metadata-comparison between the current entry and a
  710. * {@link DirCacheEntry}
  711. */
  712. public enum MetadataDiff {
  713. /**
  714. * The entries are equal by metaData (mode, length,
  715. * modification-timestamp) or the <code>assumeValid</code> attribute of
  716. * the index entry is set
  717. */
  718. EQUAL,
  719. /**
  720. * The entries are not equal by metaData (mode, length) or the
  721. * <code>isUpdateNeeded</code> attribute of the index entry is set
  722. */
  723. DIFFER_BY_METADATA,
  724. /** index entry is smudged - can't use that entry for comparison */
  725. SMUDGED,
  726. /**
  727. * The entries are equal by metaData (mode, length) but differ by
  728. * modification-timestamp.
  729. */
  730. DIFFER_BY_TIMESTAMP
  731. }
  732. /**
  733. * Is the file mode of the current entry different than the given raw mode?
  734. *
  735. * @param rawMode
  736. * @return true if different, false otherwise
  737. */
  738. public boolean isModeDifferent(final int rawMode) {
  739. // Determine difference in mode-bits of file and index-entry. In the
  740. // bitwise presentation of modeDiff we'll have a '1' when the two modes
  741. // differ at this position.
  742. int modeDiff = getEntryRawMode() ^ rawMode;
  743. if (modeDiff == 0)
  744. return false;
  745. // Do not rely on filemode differences in case of symbolic links
  746. if (getOptions().getSymLinks() == SymLinks.FALSE)
  747. if (FileMode.SYMLINK.equals(rawMode))
  748. return false;
  749. // Ignore the executable file bits if WorkingTreeOptions tell me to
  750. // do so. Ignoring is done by setting the bits representing a
  751. // EXECUTABLE_FILE to '0' in modeDiff
  752. if (!state.options.isFileMode())
  753. modeDiff &= ~FileMode.EXECUTABLE_FILE.getBits();
  754. return modeDiff != 0;
  755. }
  756. /**
  757. * Compare the metadata (mode, length, modification-timestamp) of the
  758. * current entry and a {@link DirCacheEntry}
  759. *
  760. * @param entry
  761. * the {@link DirCacheEntry} to compare with
  762. * @return a {@link MetadataDiff} which tells whether and how the entries
  763. * metadata differ
  764. */
  765. public MetadataDiff compareMetadata(DirCacheEntry entry) {
  766. if (entry.isAssumeValid())
  767. return MetadataDiff.EQUAL;
  768. if (entry.isUpdateNeeded())
  769. return MetadataDiff.DIFFER_BY_METADATA;
  770. if (isModeDifferent(entry.getRawMode()))
  771. return MetadataDiff.DIFFER_BY_METADATA;
  772. // Don't check for length or lastmodified on folders
  773. int type = mode & FileMode.TYPE_MASK;
  774. if (type == FileMode.TYPE_TREE || type == FileMode.TYPE_GITLINK)
  775. return MetadataDiff.EQUAL;
  776. if (!entry.isSmudged() && entry.getLength() != (int) getEntryLength())
  777. return MetadataDiff.DIFFER_BY_METADATA;
  778. // Git under windows only stores seconds so we round the timestamp
  779. // Java gives us if it looks like the timestamp in index is seconds
  780. // only. Otherwise we compare the timestamp at millisecond precision,
  781. // unless core.checkstat is set to "minimal", in which case we only
  782. // compare the whole second part.
  783. long cacheLastModified = entry.getLastModified();
  784. long fileLastModified = getEntryLastModified();
  785. long lastModifiedMillis = fileLastModified % 1000;
  786. long cacheMillis = cacheLastModified % 1000;
  787. if (getOptions().getCheckStat() == CheckStat.MINIMAL) {
  788. fileLastModified = fileLastModified - lastModifiedMillis;
  789. cacheLastModified = cacheLastModified - cacheMillis;
  790. } else if (cacheMillis == 0)
  791. fileLastModified = fileLastModified - lastModifiedMillis;
  792. // Some Java version on Linux return whole seconds only even when
  793. // the file systems supports more precision.
  794. else if (lastModifiedMillis == 0)
  795. cacheLastModified = cacheLastModified - cacheMillis;
  796. if (fileLastModified != cacheLastModified)
  797. return MetadataDiff.DIFFER_BY_TIMESTAMP;
  798. else if (!entry.isSmudged())
  799. // The file is clean when you look at timestamps.
  800. return MetadataDiff.EQUAL;
  801. else
  802. return MetadataDiff.SMUDGED;
  803. }
  804. /**
  805. * Checks whether this entry differs from a given entry from the
  806. * {@link DirCache}.
  807. *
  808. * File status information is used and if status is same we consider the
  809. * file identical to the state in the working directory. Native git uses
  810. * more stat fields than we have accessible in Java.
  811. *
  812. * @param entry
  813. * the entry from the dircache we want to compare against
  814. * @param forceContentCheck
  815. * True if the actual file content should be checked if
  816. * modification time differs.
  817. * @param reader
  818. * access to repository objects if necessary. Should not be null.
  819. * @return true if content is most likely different.
  820. * @throws IOException
  821. * @since 3.3
  822. */
  823. public boolean isModified(DirCacheEntry entry, boolean forceContentCheck,
  824. ObjectReader reader) throws IOException {
  825. if (entry == null)
  826. return !FileMode.MISSING.equals(getEntryFileMode());
  827. MetadataDiff diff = compareMetadata(entry);
  828. switch (diff) {
  829. case DIFFER_BY_TIMESTAMP:
  830. if (forceContentCheck)
  831. // But we are told to look at content even though timestamps
  832. // tell us about modification
  833. return contentCheck(entry, reader);
  834. else
  835. // We are told to assume a modification if timestamps differs
  836. return true;
  837. case SMUDGED:
  838. // The file is clean by timestamps but the entry was smudged.
  839. // Lets do a content check
  840. return contentCheck(entry, reader);
  841. case EQUAL:
  842. if (mode == FileMode.SYMLINK.getBits()) {
  843. return contentCheck(entry, reader);
  844. }
  845. return false;
  846. case DIFFER_BY_METADATA:
  847. if (mode == FileMode.SYMLINK.getBits())
  848. return contentCheck(entry, reader);
  849. return true;
  850. default:
  851. throw new IllegalStateException(MessageFormat.format(
  852. JGitText.get().unexpectedCompareResult, diff.name()));
  853. }
  854. }
  855. /**
  856. * Get the file mode to use for the current entry when it is to be updated
  857. * in the index.
  858. *
  859. * @param indexIter
  860. * {@link DirCacheIterator} positioned at the same entry as this
  861. * iterator or null if no {@link DirCacheIterator} is available
  862. * at this iterator's current entry
  863. * @return index file mode
  864. */
  865. public FileMode getIndexFileMode(final DirCacheIterator indexIter) {
  866. final FileMode wtMode = getEntryFileMode();
  867. if (indexIter == null) {
  868. return wtMode;
  869. }
  870. final FileMode iMode = indexIter.getEntryFileMode();
  871. if (getOptions().isFileMode() && iMode != FileMode.GITLINK && iMode != FileMode.TREE) {
  872. return wtMode;
  873. }
  874. if (!getOptions().isFileMode()) {
  875. if (FileMode.REGULAR_FILE == wtMode
  876. && FileMode.EXECUTABLE_FILE == iMode) {
  877. return iMode;
  878. }
  879. if (FileMode.EXECUTABLE_FILE == wtMode
  880. && FileMode.REGULAR_FILE == iMode) {
  881. return iMode;
  882. }
  883. }
  884. if (FileMode.GITLINK == iMode
  885. && FileMode.TREE == wtMode) {
  886. return iMode;
  887. }
  888. if (FileMode.TREE == iMode
  889. && FileMode.GITLINK == wtMode) {
  890. return iMode;
  891. }
  892. return wtMode;
  893. }
  894. /**
  895. * Compares the entries content with the content in the filesystem.
  896. * Unsmudges the entry when it is detected that it is clean.
  897. *
  898. * @param entry
  899. * the entry to be checked
  900. * @param reader
  901. * acccess to repository data if necessary
  902. * @return <code>true</code> if the content doesn't match,
  903. * <code>false</code> if it matches
  904. * @throws IOException
  905. */
  906. private boolean contentCheck(DirCacheEntry entry, ObjectReader reader)
  907. throws IOException {
  908. if (getEntryObjectId().equals(entry.getObjectId())) {
  909. // Content has not changed
  910. // We know the entry can't be racily clean because it's still clean.
  911. // Therefore we unsmudge the entry!
  912. // If by any chance we now unsmudge although we are still in the
  913. // same time-slot as the last modification to the index file the
  914. // next index write operation will smudge again.
  915. // Caution: we are unsmudging just by setting the length of the
  916. // in-memory entry object. It's the callers task to detect that we
  917. // have modified the entry and to persist the modified index.
  918. entry.setLength((int) getEntryLength());
  919. return false;
  920. } else {
  921. if (mode == FileMode.SYMLINK.getBits()) {
  922. return !new File(readSymlinkTarget(current())).equals(
  923. new File(readContentAsNormalizedString(entry, reader)));
  924. }
  925. // Content differs: that's a real change, perhaps
  926. if (reader == null) // deprecated use, do no further checks
  927. return true;
  928. switch (getEolStreamType()) {
  929. case DIRECT:
  930. return true;
  931. default:
  932. try {
  933. ObjectLoader loader = reader.open(entry.getObjectId());
  934. if (loader == null)
  935. return true;
  936. // We need to compute the length, but only if it is not
  937. // a binary stream.
  938. long dcInLen;
  939. try (InputStream dcIn = new AutoLFInputStream(
  940. loader.openStream(), true,
  941. true /* abort if binary */)) {
  942. dcInLen = computeLength(dcIn);
  943. } catch (AutoLFInputStream.IsBinaryException e) {
  944. return true;
  945. }
  946. try (InputStream dcIn = new AutoLFInputStream(
  947. loader.openStream(), true)) {
  948. byte[] autoCrLfHash = computeHash(dcIn, dcInLen);
  949. boolean changed = getEntryObjectId()
  950. .compareTo(autoCrLfHash, 0) != 0;
  951. return changed;
  952. }
  953. } catch (IOException e) {
  954. return true;
  955. }
  956. }
  957. }
  958. }
  959. private static String readContentAsNormalizedString(DirCacheEntry entry,
  960. ObjectReader reader) throws MissingObjectException, IOException {
  961. ObjectLoader open = reader.open(entry.getObjectId());
  962. byte[] cachedBytes = open.getCachedBytes();
  963. return FS.detect().normalize(RawParseUtils.decode(cachedBytes));
  964. }
  965. /**
  966. * Reads the target of a symlink as a string. This default implementation
  967. * fully reads the entry's input stream and converts it to a normalized
  968. * string. Subclasses may override to provide more specialized
  969. * implementations.
  970. *
  971. * @param entry
  972. * to read
  973. * @return the entry's content as a normalized string
  974. * @throws IOException
  975. * if the entry cannot be read or does not denote a symlink
  976. * @since 4.6
  977. */
  978. protected String readSymlinkTarget(Entry entry) throws IOException {
  979. if (!entry.getMode().equals(FileMode.SYMLINK)) {
  980. throw new java.nio.file.NotLinkException(entry.getName());
  981. }
  982. long length = entry.getLength();
  983. byte[] content = new byte[(int) length];
  984. try (InputStream is = entry.openInputStream()) {
  985. int bytesRead = IO.readFully(is, content, 0);
  986. return FS.detect()
  987. .normalize(RawParseUtils.decode(content, 0, bytesRead));
  988. }
  989. }
  990. private static long computeLength(InputStream in) throws IOException {
  991. // Since we only care about the length, use skip. The stream
  992. // may be able to more efficiently wade through its data.
  993. //
  994. long length = 0;
  995. for (;;) {
  996. long n = in.skip(1 << 20);
  997. if (n <= 0)
  998. break;
  999. length += n;
  1000. }
  1001. return length;
  1002. }
  1003. private byte[] computeHash(InputStream in, long length) throws IOException {
  1004. final MessageDigest contentDigest = state.contentDigest;
  1005. final byte[] contentReadBuffer = state.contentReadBuffer;
  1006. contentDigest.reset();
  1007. contentDigest.update(hblob);
  1008. contentDigest.update((byte) ' ');
  1009. long sz = length;
  1010. if (sz == 0) {
  1011. contentDigest.update((byte) '0');
  1012. } else {
  1013. final int bufn = contentReadBuffer.length;
  1014. int p = bufn;
  1015. do {
  1016. contentReadBuffer[--p] = digits[(int) (sz % 10)];
  1017. sz /= 10;
  1018. } while (sz > 0);
  1019. contentDigest.update(contentReadBuffer, p, bufn - p);
  1020. }
  1021. contentDigest.update((byte) 0);
  1022. for (;;) {
  1023. final int r = in.read(contentReadBuffer);
  1024. if (r <= 0)
  1025. break;
  1026. contentDigest.update(contentReadBuffer, 0, r);
  1027. sz += r;
  1028. }
  1029. if (sz != length)
  1030. return zeroid;
  1031. return contentDigest.digest();
  1032. }
  1033. /** A single entry within a working directory tree. */
  1034. protected static abstract class Entry {
  1035. byte[] encodedName;
  1036. int encodedNameLen;
  1037. void encodeName(final CharsetEncoder enc) {
  1038. final ByteBuffer b;
  1039. try {
  1040. b = enc.encode(CharBuffer.wrap(getName()));
  1041. } catch (CharacterCodingException e) {
  1042. // This should so never happen.
  1043. throw new RuntimeException(MessageFormat.format(
  1044. JGitText.get().unencodeableFile, getName()));
  1045. }
  1046. encodedNameLen = b.limit();
  1047. if (b.hasArray() && b.arrayOffset() == 0)
  1048. encodedName = b.array();
  1049. else
  1050. b.get(encodedName = new byte[encodedNameLen]);
  1051. }
  1052. public String toString() {
  1053. return getMode().toString() + " " + getName(); //$NON-NLS-1$
  1054. }
  1055. /**
  1056. * Get the type of this entry.
  1057. * <p>
  1058. * <b>Note: Efficient implementation required.</b>
  1059. * <p>
  1060. * The implementation of this method must be efficient. If a subclass
  1061. * needs to compute the value they should cache the reference within an
  1062. * instance member instead.
  1063. *
  1064. * @return a file mode constant from {@link FileMode}.
  1065. */
  1066. public abstract FileMode getMode();
  1067. /**
  1068. * Get the byte length of this entry.
  1069. * <p>
  1070. * <b>Note: Efficient implementation required.</b>
  1071. * <p>
  1072. * The implementation of this method must be efficient. If a subclass
  1073. * needs to compute the value they should cache the reference within an
  1074. * instance member instead.
  1075. *
  1076. * @return size of this file, in bytes.
  1077. */
  1078. public abstract long getLength();
  1079. /**
  1080. * Get the last modified time of this entry.
  1081. * <p>
  1082. * <b>Note: Efficient implementation required.</b>
  1083. * <p>
  1084. * The implementation of this method must be efficient. If a subclass
  1085. * needs to compute the value they should cache the reference within an
  1086. * instance member instead.
  1087. *
  1088. * @return time since the epoch (in ms) of the last change.
  1089. */
  1090. public abstract long getLastModified();
  1091. /**
  1092. * Get the name of this entry within its directory.
  1093. * <p>
  1094. * Efficient implementations are not required. The caller will obtain
  1095. * the name only once and cache it once obtained.
  1096. *
  1097. * @return name of the entry.
  1098. */
  1099. public abstract String getName();
  1100. /**
  1101. * Obtain an input stream to read the file content.
  1102. * <p>
  1103. * Efficient implementations are not required. The caller will usually
  1104. * obtain the stream only once per entry, if at all.
  1105. * <p>
  1106. * The input stream should not use buffering if the implementation can
  1107. * avoid it. The caller will buffer as necessary to perform efficient
  1108. * block IO operations.
  1109. * <p>
  1110. * The caller will close the stream once complete.
  1111. *
  1112. * @return a stream to read from the file.
  1113. * @throws IOException
  1114. * the file could not be opened for reading.
  1115. */
  1116. public abstract InputStream openInputStream() throws IOException;
  1117. }
  1118. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1119. private static class PerDirectoryIgnoreNode extends IgnoreNode {
  1120. final Entry entry;
  1121. PerDirectoryIgnoreNode(Entry entry) {
  1122. super(Collections.<FastIgnoreRule> emptyList());
  1123. this.entry = entry;
  1124. }
  1125. IgnoreNode load() throws IOException {
  1126. IgnoreNode r = new IgnoreNode();
  1127. InputStream in = entry.openInputStream();
  1128. try {
  1129. r.parse(in);
  1130. } finally {
  1131. in.close();
  1132. }
  1133. return r.getRules().isEmpty() ? null : r;
  1134. }
  1135. }
  1136. /** Magic type indicating there may be rules for the top level. */
  1137. private static class RootIgnoreNode extends PerDirectoryIgnoreNode {
  1138. final Repository repository;
  1139. RootIgnoreNode(Entry entry, Repository repository) {
  1140. super(entry);
  1141. this.repository = repository;
  1142. }
  1143. @Override
  1144. IgnoreNode load() throws IOException {
  1145. IgnoreNode r;
  1146. if (entry != null) {
  1147. r = super.load();
  1148. if (r == null)
  1149. r = new IgnoreNode();
  1150. } else {
  1151. r = new IgnoreNode();
  1152. }
  1153. FS fs = repository.getFS();
  1154. String path = repository.getConfig().get(CoreConfig.KEY)
  1155. .getExcludesFile();
  1156. if (path != null) {
  1157. File excludesfile;
  1158. if (path.startsWith("~/")) //$NON-NLS-1$
  1159. excludesfile = fs.resolve(fs.userHome(), path.substring(2));
  1160. else
  1161. excludesfile = fs.resolve(null, path);
  1162. loadRulesFromFile(r, excludesfile);
  1163. }
  1164. File exclude = fs.resolve(repository.getDirectory(),
  1165. Constants.INFO_EXCLUDE);
  1166. loadRulesFromFile(r, exclude);
  1167. return r.getRules().isEmpty() ? null : r;
  1168. }
  1169. private static void loadRulesFromFile(IgnoreNode r, File exclude)
  1170. throws FileNotFoundException, IOException {
  1171. if (FS.DETECTED.exists(exclude)) {
  1172. FileInputStream in = new FileInputStream(exclude);
  1173. try {
  1174. r.parse(in);
  1175. } finally {
  1176. in.close();
  1177. }
  1178. }
  1179. }
  1180. }
  1181. /** Magic type indicating we know rules exist, but they aren't loaded. */
  1182. private static class PerDirectoryAttributesNode extends AttributesNode {
  1183. final Entry entry;
  1184. PerDirectoryAttributesNode(Entry entry) {
  1185. super(Collections.<AttributesRule> emptyList());
  1186. this.entry = entry;
  1187. }
  1188. AttributesNode load() throws IOException {
  1189. AttributesNode r = new AttributesNode();
  1190. InputStream in = entry.openInputStream();
  1191. try {
  1192. r.parse(in);
  1193. } finally {
  1194. in.close();
  1195. }
  1196. return r.getRules().isEmpty() ? null : r;
  1197. }
  1198. }
  1199. private static final class IteratorState {
  1200. /** Options used to process the working tree. */
  1201. final WorkingTreeOptions options;
  1202. /** File name character encoder. */
  1203. final CharsetEncoder nameEncoder;
  1204. /** Digest computer for {@link #contentId} computations. */
  1205. MessageDigest contentDigest;
  1206. /** Buffer used to perform {@link #contentId} computations. */
  1207. byte[] contentReadBuffer;
  1208. /** TreeWalk with a (supposedly) matching DirCacheIterator. */
  1209. TreeWalk walk;
  1210. /** Position of the matching {@link DirCacheIterator}. */
  1211. int dirCacheTree;
  1212. IteratorState(WorkingTreeOptions options) {
  1213. this.options = options;
  1214. this.nameEncoder = Constants.CHARSET.newEncoder();
  1215. }
  1216. void initializeDigestAndReadBuffer() {
  1217. if (contentDigest == null) {
  1218. contentDigest = Constants.newMessageDigest();
  1219. contentReadBuffer = new byte[BUFFER_SIZE];
  1220. }
  1221. }
  1222. }
  1223. /**
  1224. * @return the clean filter command for the current entry or
  1225. * <code>null</code> if no such command is defined
  1226. * @throws IOException
  1227. * @since 4.2
  1228. */
  1229. public String getCleanFilterCommand() throws IOException {
  1230. if (cleanFilterCommandHolder == null) {
  1231. String cmd = null;
  1232. if (state.walk != null) {
  1233. cmd = state.walk
  1234. .getFilterCommand(Constants.ATTR_FILTER_TYPE_CLEAN);
  1235. }
  1236. cleanFilterCommandHolder = new Holder<String>(cmd);
  1237. }
  1238. return cleanFilterCommandHolder.get();
  1239. }
  1240. /**
  1241. * @return the eol stream type for the current entry or <code>null</code> if
  1242. * it cannot be determined. When state or state.walk is null or the
  1243. * {@link TreeWalk} is not based on a {@link Repository} then null
  1244. * is returned.
  1245. * @throws IOException
  1246. * @since 4.3
  1247. */
  1248. public EolStreamType getEolStreamType() throws IOException {
  1249. return getEolStreamType(null);
  1250. }
  1251. /**
  1252. * @param opType
  1253. * The operationtype (checkin/checkout) which should be used
  1254. * @return the eol stream type for the current entry or <code>null</code> if
  1255. * it cannot be determined. When state or state.walk is null or the
  1256. * {@link TreeWalk} is not based on a {@link Repository} then null
  1257. * is returned.
  1258. * @throws IOException
  1259. */
  1260. private EolStreamType getEolStreamType(OperationType opType)
  1261. throws IOException {
  1262. if (eolStreamTypeHolder == null) {
  1263. EolStreamType type=null;
  1264. if (state.walk != null) {
  1265. if (opType != null) {
  1266. type = state.walk.getEolStreamType(opType);
  1267. } else {
  1268. type=state.walk.getEolStreamType();
  1269. }
  1270. } else {
  1271. switch (getOptions().getAutoCRLF()) {
  1272. case FALSE:
  1273. type = EolStreamType.DIRECT;
  1274. break;
  1275. case TRUE:
  1276. case INPUT:
  1277. type = EolStreamType.AUTO_LF;
  1278. break;
  1279. }
  1280. }
  1281. eolStreamTypeHolder = new Holder<EolStreamType>(type);
  1282. }
  1283. return eolStreamTypeHolder.get();
  1284. }
  1285. }