3131import org .kohsuke .stapler .QueryParameter ;
3232import org .kohsuke .stapler .Stapler ;
3333import org .kohsuke .stapler .StaplerRequest ;
34+ import org .eclipse .jgit .transport .URIish ;
3435
3536import java .io .File ;
3637import java .io .IOException ;
@@ -161,15 +162,15 @@ public List<BranchInfo> fetchBranches() {
161162 /**
162163 * Fetches branches from the remote Git repository.
163164 * Strategy:
164- * 1. Try workspace fetch + for-each-ref (fast + time-sorted) - PREFERRED
165+ * 1. Try workspace fetch + for-each-ref (lightweight fetch + time-sorted) - PREFERRED
165166 * 2. If no workspace: use ls-remote (fast, alphabetical) or clone (slow, time-sorted)
166167 */
167168 private List <BranchInfo > fetchBranchesInternal () throws IOException , InterruptedException {
168169 if (repositoryUrl == null || repositoryUrl .isEmpty ()) {
169170 throw new IOException ("Repository URL is not configured" );
170171 }
171172
172- // First, always try workspace-based fetch (fast + preserves time sorting)
173+ // First, always try workspace-based fetch (lightweight fetch + preserves time sorting)
173174 List <BranchInfo > result = tryFetchFromWorkspace ();
174175 if (result != null ) {
175176 LOGGER .info ("Fetched branches from workspace with time-based sorting" );
@@ -293,8 +294,8 @@ private List<BranchInfo> tryFetchFromWorkspace() {
293294 }
294295
295296 /**
296- * Fetch branches from an existing workspace by reading local refs only .
297- * No network operation - instant response .
297+ * Fetch branches from an existing workspace.
298+ * Performs a lightweight fetch to update refs, then reads local refs .
298299 * Returns null if no local refs found (caller should fall back to other methods).
299300 */
300301 private List <BranchInfo > fetchBranchesFromWorkspace (FilePath workspace ) throws IOException , InterruptedException {
@@ -308,15 +309,23 @@ private List<BranchInfo> fetchBranchesFromWorkspace(FilePath workspace) throws I
308309 .using ("jgit" )
309310 .getClient ();
310311
311- // Only read existing local refs (instant, no network)
312+ // Add credentials if available
313+ StandardCredentials credentials = getCredentials ();
314+ if (credentials != null ) {
315+ git .addCredentials (repositoryUrl , credentials );
316+ }
317+
318+ // Read cached local refs (instant, no network)
312319 List <BranchInfo > localRefs = readLocalRefs (git );
320+
321+ // If we have cached refs, use them directly (fast path)
313322 if (!localRefs .isEmpty ()) {
314323 LOGGER .info ("Using cached local refs (" + localRefs .size () + " branches)" );
315324 return applyLimits (localRefs );
316325 }
317326
318- // No local refs found , return null to fall back to other methods
319- LOGGER .info ("No local refs found in workspace, will use fallback method" );
327+ // No cached refs in workspace , return null to fall back to ls-remote
328+ LOGGER .info ("No cached refs in workspace, falling back to other method" );
320329 return null ;
321330 }
322331
0 commit comments