@@ -103,6 +103,14 @@ public void close() {
103103 HIO .closeAll (getCloseables ());
104104 }
105105
106+ protected String computeCommit () {
107+ try {
108+ return getGit ().getRepository ().findRef (Constants .HEAD ).getObjectId ().getName ();
109+ } catch (IOException exception ) {
110+ throw new RuntimeIOException (String .format ("Failed to determine commit for %1$s!" , getName ()), exception );
111+ }
112+ }
113+
106114 protected BulldozerDependencies computeDependencies () {
107115 final Map <String , ? extends BulldozerProject > nameToProject = getContext ().getNameToProject ();
108116 final Map <String , ? extends BulldozerProject > groupToProject = getContext ().getGroupToProject ();
@@ -190,28 +198,20 @@ public String getName() {
190198 }
191199
192200 public <T > T loadTemp (IFunction1 <BulldozerTemp , T > getter , IConsumer2 <BulldozerTemp , T > setter , ISupplier <T > generator ) {
193- final String commit ;
194- try {
195- commit = getGit ().getRepository ().findRef (Constants .HEAD ).getObjectId ().getName ();
196- } catch (IOException exception ) {
197- throw new RuntimeIOException (String .format ("Failed to determine commit for %1$s!" , getName ()), exception );
198- }
201+ final String commit = computeCommit ();
199202
200203 final Path path = getDirectory ().resolve (BulldozerTemp .BULLDOZER_TEMP );
201204 BulldozerTemp temp = null ;
202205 if (Files .exists (path )) {
203206 try {
204207 final BulldozerTemp read = getContext ().getObjectMapper ().readValue (path .toFile (), BulldozerTemp .class );
205- if (read .getKey (). equals (commit )) temp = read ;
208+ if (read .isValidForCommit (commit )) temp = read ;
206209 else Files .delete (path );
207210 } catch (IOException exception ) {
208211 log .warn (String .format ("Failed to read bulldozer temp data for %1$s, will regenerate..." , getName ()));
209212 }
210213 }
211- if (temp == null ) {
212- temp = new BulldozerTemp ();
213- temp .setKey (commit );
214- }
214+ if (temp == null ) temp = BulldozerTemp .builder ().commit (commit ).build ();
215215 final T retVal0 = getter .apply (temp );
216216 if (retVal0 != null ) return retVal0 ;
217217
@@ -229,4 +229,21 @@ public <T> T loadTemp(IFunction1<BulldozerTemp, T> getter, IConsumer2<BulldozerT
229229 }
230230 return retVal1 ;
231231 }
232+
233+ public void preserveTemp () {
234+ final String commit = computeCommit ();
235+
236+ final Path path = getDirectory ().resolve (BulldozerTemp .BULLDOZER_TEMP );
237+ if (Files .exists (path )) {
238+ try {
239+ final BulldozerTemp read = getContext ().getObjectMapper ().readValue (path .toFile (), BulldozerTemp .class );
240+ if (!read .isValidForCommit (commit )) {
241+ final BulldozerTemp write = read .toBuilder ().otherCommit (commit ).build ();
242+ getContext ().getObjectMapper ().writeValue (path .toFile (), write );
243+ }
244+ } catch (IOException exception ) {
245+ log .warn (String .format ("Failed to preserve bulldozer temp data for %1$s, will regenerate later..." , getName ()));
246+ }
247+ }
248+ }
232249}
0 commit comments