11/*
2- * Copyright 2020-2021 DiffPlug
2+ * Copyright 2020-2022 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
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,7 +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 <>();
139+ Map <File , Repository > gitRoots = new HashMap <>();
141140 Table <Repository , String , ObjectId > rootTreeShaCache = HashBasedTable .create ();
142141 Map <Project , ObjectId > subtreeShaCache = new HashMap <>();
143142
@@ -147,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
147146 * We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it.
148147 */
149148 protected Repository repositoryFor (Project project ) throws IOException {
150- 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 );
151154 if (repo == null ) {
152- if (isGitRoot (getDir (project ))) {
153- repo = createRepo (getDir (project ));
154- } else {
155- Project parentProj = getParent (project );
156- if (parentProj == null ) {
157- repo = traverseParentsUntil (getDir (project ).getParentFile (), null );
158- if (repo == null ) {
159- throw new IllegalArgumentException ("Cannot find git repository in any parent directory" );
160- }
161- } else {
162- repo = traverseParentsUntil (getDir (project ).getParentFile (), getDir (parentProj ));
163- if (repo == null ) {
164- repo = repositoryFor (parentProj );
165- }
166- }
167- }
168- gitRoots .put (project , repo );
155+ repo = FileRepositoryBuilder .create (projectGitDir );
156+ gitRoots .put (projectGitDir , repo );
169157 }
170158 return repo ;
171159 }
@@ -174,26 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException {
174162
175163 protected abstract @ Nullable Project getParent (Project project );
176164
177- private static @ Nullable Repository traverseParentsUntil (File startWith , @ Nullable File file ) throws IOException {
178- while (startWith != null && !Objects .equals (startWith , file )) {
179- if (isGitRoot (startWith )) {
180- return createRepo (startWith );
181- } else {
182- startWith = startWith .getParentFile ();
183- }
184- }
185- return null ;
186- }
187-
188- private static boolean isGitRoot (File dir ) {
189- File dotGit = GitWorkarounds .getDotGitDir (dir );
190- return dotGit != null && RepositoryCache .FileKey .isGitRepository (dotGit , FS .DETECTED );
191- }
192-
193- static Repository createRepo (File dir ) throws IOException {
194- return FileRepositoryBuilder .create (GitWorkarounds .getDotGitDir (dir ));
195- }
196-
197165 /**
198166 * Fast way to return treeSha of the given ref against the git repository which stores the given project.
199167 * Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method
0 commit comments