49
49
import org .opengrok .indexer .util .BufferSink ;
50
50
import org .opengrok .indexer .util .Executor ;
51
51
import org .opengrok .indexer .util .HeadHandler ;
52
+ import org .opengrok .indexer .util .LazilyInstantiate ;
52
53
import org .opengrok .indexer .util .StringUtils ;
53
54
import org .opengrok .indexer .util .Version ;
54
55
@@ -95,6 +96,12 @@ public class GitRepository extends Repository {
95
96
*/
96
97
private static final Version MINIMUM_VERSION = new Version (2 , 1 , 2 );
97
98
99
+ /**
100
+ * This is a static replacement for 'working' field. Effectively, check if git is working once in a JVM
101
+ * instead of calling it for every GitRepository instance.
102
+ */
103
+ private static final LazilyInstantiate <Boolean > GIT_IS_WORKING = LazilyInstantiate .using (GitRepository ::isGitWorking );
104
+
98
105
public GitRepository () {
99
106
type = "git" ;
100
107
/*
@@ -109,6 +116,21 @@ public GitRepository() {
109
116
ignoredFiles .add (".git" );
110
117
}
111
118
119
+ private static boolean isGitWorking () {
120
+ String repoCommand = getCommand (GitRepository .class , CMD_PROPERTY_KEY , CMD_FALLBACK );
121
+ Executor exec = new Executor (new String [] {repoCommand , "--version" });
122
+ if (exec .exec (false ) == 0 ) {
123
+ final String outputVersion = exec .getOutputString ();
124
+ final String version = outputVersion .replaceAll (".*? version (\\ d+(\\ .\\ d+)*).*" , "$1" );
125
+ try {
126
+ return Version .from (version ).compareTo (MINIMUM_VERSION ) >= 0 ;
127
+ } catch (NumberFormatException ex ) {
128
+ LOGGER .log (Level .WARNING , String .format ("Unable to detect git version from %s" , outputVersion ), ex );
129
+ }
130
+ }
131
+ return false ;
132
+ }
133
+
112
134
/**
113
135
* Get an executor to be used for retrieving the history log for the named
114
136
* file.
@@ -521,21 +543,7 @@ boolean isNestable() {
521
543
@ Override
522
544
public boolean isWorking () {
523
545
if (working == null ) {
524
- ensureCommand (CMD_PROPERTY_KEY , CMD_FALLBACK );
525
- Executor exec = new Executor (new String []{RepoCommand , "--version" });
526
-
527
- if (exec .exec (false ) == 0 ) {
528
- final String outputVersion = exec .getOutputString ();
529
- final String version = outputVersion .replaceAll (".*? version (\\ d+(\\ .\\ d+)*).*" , "$1" );
530
- try {
531
- working = Version .from (version ).compareTo (MINIMUM_VERSION ) >= 0 ;
532
- } catch (NumberFormatException ex ) {
533
- LOGGER .log (Level .WARNING , String .format ("Unable to detect git version from %s" , outputVersion ), ex );
534
- working = false ;
535
- }
536
- } else {
537
- working = false ;
538
- }
546
+ working = GIT_IS_WORKING .get ();
539
547
}
540
548
541
549
return working ;
0 commit comments