@@ -207,7 +207,7 @@ public Void invoke(org.eclipse.jgit.lib.Repository repo, hudson.remoting.Virtual
207207 org .eclipse .jgit .revwalk .RevCommit commit = walk .parseCommit (ref .getObjectId ());
208208 long commitTime = commit .getCommitTime () * 1000L ;
209209 branchInfos .add (new BranchInfo (branchName , commitTime ));
210- } catch (Exception e ) {
210+ } catch (IOException e ) {
211211 // If we can't parse commit, treat as old
212212 branchInfos .add (new BranchInfo (branchName , 0L ));
213213 }
@@ -231,28 +231,6 @@ public Void invoke(org.eclipse.jgit.lib.Repository repo, hudson.remoting.Virtual
231231 return branchInfos .subList (0 , maxBranchCount );
232232 }
233233
234- List <BranchInfo > limitedList = new ArrayList <>();
235- int count = 0 ;
236-
237- // First pass: add all branches that fit in the limit
238- for (BranchInfo info : branchInfos ) {
239- boolean isAlwaysIncluded = matchesAlwaysInclude (info .getName ());
240-
241- if (count < maxBranchCount || isAlwaysIncluded ) {
242- limitedList .add (info );
243- if (!isAlwaysIncluded ) {
244- count ++; // Only count towards limit if NOT always included
245- // Wait, if we don't count always included branches, we might end up with many branches.
246- // Alternative strategy: Always included branches take priority, then fill up to maxBranchCount with others.
247- } else {
248- // If it IS always included, we add it. Does it consume a slot?
249- // The requirement: "符合正则的分支不是最近活跃的分支也可以展示选项"
250- // It implies they should be added EVEN IF they would fall out of the Top N.
251- // So they are exceptions to the limit.
252- }
253- }
254- }
255-
256234 // Let's refine the logic:
257235 // 1. Identify branches that MUST be included.
258236 // 2. Identify other branches that are candidates.
@@ -338,25 +316,28 @@ private boolean matchesBranchFilter(String branchName) {
338316 }
339317
340318 private File createTempDirectory () throws IOException {
341- File tempDir = File .createTempFile ("jenkins-git-branches-" , "" );
342- tempDir .delete ();
343- tempDir .mkdirs ();
344- return tempDir ;
319+ return java .nio .file .Files .createTempDirectory ("jenkins-git-branches-" ).toFile ();
345320 }
346321
347322 private void deleteDirectory (File directory ) {
348323 if (directory != null && directory .exists ()) {
349- File [] files = directory .listFiles ();
350- if (files != null ) {
351- for (File file : files ) {
352- if (file .isDirectory ()) {
353- deleteDirectory (file );
354- } else {
355- file .delete ();
324+ try {
325+ java .nio .file .Files .walkFileTree (directory .toPath (), new java .nio .file .SimpleFileVisitor <java .nio .file .Path >() {
326+ @ Override
327+ public java .nio .file .FileVisitResult visitFile (java .nio .file .Path file , java .nio .file .attribute .BasicFileAttributes attrs ) throws IOException {
328+ java .nio .file .Files .delete (file );
329+ return java .nio .file .FileVisitResult .CONTINUE ;
356330 }
357- }
331+
332+ @ Override
333+ public java .nio .file .FileVisitResult postVisitDirectory (java .nio .file .Path dir , IOException exc ) throws IOException {
334+ java .nio .file .Files .delete (dir );
335+ return java .nio .file .FileVisitResult .CONTINUE ;
336+ }
337+ });
338+ } catch (IOException e ) {
339+ LOGGER .log (Level .WARNING , "Failed to delete temp directory: " + directory , e );
358340 }
359- directory .delete ();
360341 }
361342 }
362343
0 commit comments