1919import java .io .IOException ;
2020import java .util .HashMap ;
2121import java .util .Map ;
22- import java .util .Objects ;
2322import java .util .Optional ;
2423
2524import javax .annotation .Nullable ;
@@ -137,8 +136,7 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
137136 private final static int INDEX = 1 ;
138137 private final static int WORKDIR = 2 ;
139138
140- Map <Project , Repository > gitRoots = new HashMap <>();
141- Map <File , Repository > gitRepositories = new HashMap <>();
139+ Map <File , Repository > gitRoots = new HashMap <>();
142140 Table <Repository , String , ObjectId > rootTreeShaCache = HashBasedTable .create ();
143141 Map <Project , ObjectId > subtreeShaCache = new HashMap <>();
144142
@@ -148,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
148146 * We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it.
149147 */
150148 protected Repository repositoryFor (Project project ) throws IOException {
151- Repository repo = gitRoots .get (project );
149+ File projectGitDir = GitWorkarounds .getDotGitDir (getDir (project ));
150+ if (projectGitDir == null || !RepositoryCache .FileKey .isGitRepository (projectGitDir , FS .DETECTED )) {
151+ throw new IllegalArgumentException ("Cannot find git repository in any parent directory" );
152+ }
153+ Repository repo = gitRoots .get (projectGitDir );
152154 if (repo == null ) {
153- if (isGitRoot (getDir (project ))) {
154- repo = createRepo (getDir (project ));
155- } else {
156- Project parentProj = getParent (project );
157- if (parentProj == null ) {
158- repo = traverseParentsUntil (getDir (project ).getParentFile (), null );
159- if (repo == null ) {
160- throw new IllegalArgumentException ("Cannot find git repository in any parent directory" );
161- }
162- } else {
163- repo = traverseParentsUntil (getDir (project ).getParentFile (), getDir (parentProj ));
164- if (repo == null ) {
165- repo = repositoryFor (parentProj );
166- }
167- }
168- }
169- gitRoots .put (project , repo );
155+ repo = FileRepositoryBuilder .create (projectGitDir );
156+ gitRoots .put (projectGitDir , repo );
170157 }
171158 return repo ;
172159 }
@@ -175,32 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException {
175162
176163 protected abstract @ Nullable Project getParent (Project project );
177164
178- private @ Nullable Repository traverseParentsUntil (File startWith , @ Nullable File file ) throws IOException {
179- while (startWith != null && !Objects .equals (startWith , file )) {
180- if (isGitRoot (startWith )) {
181- return createRepo (startWith );
182- } else {
183- startWith = startWith .getParentFile ();
184- }
185- }
186- return null ;
187- }
188-
189- private static boolean isGitRoot (File dir ) {
190- File dotGit = GitWorkarounds .getDotGitDir (dir );
191- return dotGit != null && RepositoryCache .FileKey .isGitRepository (dotGit , FS .DETECTED );
192- }
193-
194- Repository createRepo (File dir ) throws IOException {
195- File dotGitDir = GitWorkarounds .getDotGitDir (dir );
196- Repository repo = gitRepositories .get (dotGitDir );
197- if (repo == null ) {
198- repo = FileRepositoryBuilder .create (dotGitDir );
199- gitRepositories .put (dotGitDir , repo );
200- }
201- return repo ;
202- }
203-
204165 /**
205166 * Fast way to return treeSha of the given ref against the git repository which stores the given project.
206167 * Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method
0 commit comments