19
19
import java .io .IOException ;
20
20
import java .util .HashMap ;
21
21
import java .util .Map ;
22
- import java .util .Objects ;
23
22
import java .util .Optional ;
24
23
25
24
import javax .annotation .Nullable ;
@@ -137,8 +136,7 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
137
136
private final static int INDEX = 1 ;
138
137
private final static int WORKDIR = 2 ;
139
138
140
- Map <Project , Repository > gitRoots = new HashMap <>();
141
- Map <File , Repository > gitRepositories = new HashMap <>();
139
+ Map <File , Repository > gitRoots = new HashMap <>();
142
140
Table <Repository , String , ObjectId > rootTreeShaCache = HashBasedTable .create ();
143
141
Map <Project , ObjectId > subtreeShaCache = new HashMap <>();
144
142
@@ -148,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
148
146
* We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it.
149
147
*/
150
148
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 );
152
154
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 );
170
157
}
171
158
return repo ;
172
159
}
@@ -175,32 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException {
175
162
176
163
protected abstract @ Nullable Project getParent (Project project );
177
164
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
-
204
165
/**
205
166
* Fast way to return treeSha of the given ref against the git repository which stores the given project.
206
167
* Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method
0 commit comments