2727import java .io .IOException ;
2828import java .util .Collections ;
2929import java .util .List ;
30+ import java .util .function .Function ;
3031import javax .inject .Inject ;
3132import net .kyori .indra .git .IndraGitExtension ;
3233import org .eclipse .jgit .api .Git ;
3839import org .eclipse .jgit .lib .Repository ;
3940import org .eclipse .jgit .revwalk .RevObject ;
4041import org .eclipse .jgit .revwalk .RevWalk ;
42+ import org .gradle .api .file .DirectoryProperty ;
4143import org .gradle .api .logging .Logger ;
4244import org .gradle .api .logging .Logging ;
45+ import org .gradle .api .provider .Property ;
4346import org .gradle .api .provider .Provider ;
47+ import org .gradle .api .provider .ProviderFactory ;
48+ import org .gradle .api .provider .ValueSource ;
49+ import org .gradle .api .provider .ValueSourceParameters ;
4450import org .jetbrains .annotations .NotNull ;
4551import org .jetbrains .annotations .Nullable ;
4652
4753public class IndraGitExtensionImpl implements IndraGitExtension {
4854 private static final Logger LOGGER = Logging .getLogger (IndraGitExtensionImpl .class );
49- private final Provider <Git > service ;
55+ private final Provider <IndraGitService > service ;
56+ private final ProviderFactory providers ;
57+ private final File projectDir ;
58+ private final String displayName ;
5059
5160 @ Inject
52- public IndraGitExtensionImpl (final File projectDir , final String displayName , final Provider <IndraGitService > service ) {
53- this .service = service .map (s -> s .git (projectDir , displayName ));
61+ public IndraGitExtensionImpl (final ProviderFactory providers , final File projectDir , final String displayName , final Provider <IndraGitService > service ) {
62+ this .providers = providers ;
63+ this .projectDir = projectDir ;
64+ this .displayName = displayName ;
65+
66+ this .service = service ;
5467 }
5568
56- @ Override
57- public @ Nullable Git git () {
58- return this .service .getOrNull ();
69+ @ SuppressWarnings ("unchecked" )
70+ protected <V > Provider <V > repoQuery (final Function <Git , V > provider ) {
71+ return this .providers .of (GitRepoValueSource .class , spec -> {
72+ final GitRepoValueSource .Params params = spec .getParameters ();
73+
74+ params .getService ().set (this .service );
75+ params .getProjectDirectory ().set (this .projectDir );
76+ params .getProjectDisplayName ().set (this .displayName );
77+ params .getValueProvider ().set (provider );
78+ }).map (v -> (V ) v );
5979 }
6080
61- @ Override
62- public @ NotNull List <Ref > tags () {
63- final @ Nullable Git git = this .git ();
64- if (git == null ) return Collections .emptyList ();
81+ static abstract class GitRepoValueSource implements ValueSource <Object , GitRepoValueSource .Params > {
82+ interface Params extends ValueSourceParameters {
83+ Property <IndraGitService > getService ();
84+ DirectoryProperty getProjectDirectory ();
85+ Property <String > getProjectDisplayName ();
6586
66- try {
67- return git .tagList ().call ();
68- } catch (final GitAPIException ex ) {
69- LOGGER .error ("Failed to query git for a list of tags:" , ex );
70- return Collections .emptyList ();
87+ Property <Function <Git , ?>> getValueProvider ();
88+ }
89+
90+ @ Nullable
91+ @ Override
92+ public Object obtain () {
93+ final Params params = this .getParameters ();
94+ final Git git = params .getService ().get ().git (params .getProjectDirectory ().get ().getAsFile (), params .getProjectDisplayName ().get ());
95+ if (git == null ) return null ;
96+
97+ return params .getValueProvider ().get ().apply (git );
7198 }
7299 }
73100
101+ @ Override
102+ public @ NotNull Provider <Git > git () {
103+ return this .service .map (service -> service .git (this .projectDir , this .displayName ));
104+ }
105+
106+ @ Override
107+ public @ NotNull Provider <? extends List <? extends Ref >> tags () {
108+ return this .git ().<List <Ref >>map (git -> {
109+ try {
110+ return git .tagList ().call ();
111+ } catch (final GitAPIException ex ) {
112+ LOGGER .error ("Failed to query git for a list of tags:" , ex );
113+ return Collections .emptyList ();
114+ }
115+ }).orElse (Collections .emptyList ());
116+ }
117+
74118 public static @ Nullable Ref headTag (final Git git ) {
75119 try {
76120 final @ Nullable Ref head = git .getRepository ().findRef (Constants .HEAD );
@@ -95,66 +139,57 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
95139 }
96140
97141 @ Override
98- public @ Nullable Ref headTag () {
99- final @ Nullable Git git = this .git ();
100- if (git == null ) return null ;
101- return headTag (git );
142+ public @ NotNull Provider <Ref > headTag () {
143+ return this .git ().map (IndraGitExtensionImpl ::headTag );
102144 }
103145
104146 @ Override
105- public @ Nullable String describe () {
106- final @ Nullable Git git = this .git ();
107- if (git == null ) return null ;
108-
109- try {
110- return git .describe ().setTags (true ).setLong (true ).call ();
111- } catch (final RefNotFoundException ex ) {
112- // there is no HEAD when in a git repo without a commit
113- return null ;
114- } catch (final GitAPIException ex ) {
115- LOGGER .error ("Failed to query git for a 'describe' result:" , ex );
116- return null ;
117- }
147+ public @ NotNull Provider <String > describe () {
148+ return this .git ().map (git -> {
149+ try {
150+ return git .describe ().setTags (true ).setLong (true ).call ();
151+ } catch (final RefNotFoundException ex ) {
152+ // there is no HEAD when in a git repo without a commit
153+ return null ;
154+ } catch (final GitAPIException ex ) {
155+ LOGGER .error ("Failed to query git for a 'describe' result:" , ex );
156+ return null ;
157+ }
158+ });
118159 }
119160
120161 @ Override
121- public @ Nullable String branchName () {
122- final @ Nullable Git git = this .git ();
123- if (git == null ) return null ;
124-
125- final @ Nullable Ref branch = this .branch ();
126- return branch == null ? null : Repository .shortenRefName (branch .getName ());
162+ public @ NotNull Provider <String > branchName () {
163+ return this .branch ().map (branch -> Repository .shortenRefName (branch .getName ()));
127164 }
128165
129166 @ Override
130- public @ Nullable Ref branch () {
131- final @ Nullable Git git = this .git ();
132- if (git == null ) return null ;
133-
134- try {
135- final @ Nullable Ref ref = git .getRepository ().exactRef (Constants .HEAD );
136- if (ref == null || !ref .isSymbolic ()) return null ; // no HEAD, or detached HEAD
137-
138- return ref .getTarget ();
139- } catch (final IOException ex ) {
140- LOGGER .error ("Failed to query current branch name from git:" , ex );
141- return null ;
142- }
167+ public @ NotNull Provider <Ref > branch () {
168+ return this .git ().map (git -> {
169+ try {
170+ final @ Nullable Ref ref = git .getRepository ().exactRef (Constants .HEAD );
171+ if (ref == null || !ref .isSymbolic ()) return null ; // no HEAD, or detached HEAD
172+
173+ return ref .getTarget ();
174+ } catch (final IOException ex ) {
175+ LOGGER .error ("Failed to query current branch name from git:" , ex );
176+ return null ;
177+ }
178+ });
143179 }
144180
145181 @ Override
146- public @ Nullable ObjectId commit () {
147- final @ Nullable Git git = this .git ();
148- if (git == null ) return null ;
149-
150- try {
151- final @ Nullable Ref head = git .getRepository ().exactRef (Constants .HEAD );
152- if (head == null ) return null ;
153-
154- return head .getObjectId ();
155- } catch (final IOException ex ) {
156- LOGGER .error ("Failed to query git for the current HEAD commit:" , ex );
157- return null ;
158- }
182+ public @ NotNull Provider <ObjectId > commit () {
183+ return this .git ().map (git -> {
184+ try {
185+ final @ Nullable Ref head = git .getRepository ().exactRef (Constants .HEAD );
186+ if (head == null ) return null ;
187+
188+ return head .getObjectId ();
189+ } catch (final IOException ex ) {
190+ LOGGER .error ("Failed to query git for the current HEAD commit:" , ex );
191+ return null ;
192+ }
193+ });
159194 }
160195}
0 commit comments