1
1
/*
2
- * Copyright 2020-2021 DiffPlug
2
+ * Copyright 2020-2022 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
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,7 +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 <>();
139
+ Map <File , Repository > gitRoots = new HashMap <>();
141
140
Table <Repository , String , ObjectId > rootTreeShaCache = HashBasedTable .create ();
142
141
Map <Project , ObjectId > subtreeShaCache = new HashMap <>();
143
142
@@ -147,25 +146,14 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
147
146
* We cache the Repository for every Project in {@code gitRoots}, and use dynamic programming to populate it.
148
147
*/
149
148
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 );
151
154
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 );
169
157
}
170
158
return repo ;
171
159
}
@@ -174,26 +162,6 @@ protected Repository repositoryFor(Project project) throws IOException {
174
162
175
163
protected abstract @ Nullable Project getParent (Project project );
176
164
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
-
197
165
/**
198
166
* Fast way to return treeSha of the given ref against the git repository which stores the given project.
199
167
* Because of parallel project evaluation, there may be races here, so we synchronize on ourselves. However, this method
0 commit comments