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,10 +309,24 @@ 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+ // Fetch latest refs from remote (lightweight, only updates refs)
319+ try {
320+ git .fetch_ ().from (new URIish (repositoryUrl ), Collections .emptyList ()).execute ();
321+ LOGGER .info ("Fetched latest refs from remote" );
322+ } catch (Exception e ) {
323+ LOGGER .log (Level .WARNING , "Failed to fetch from remote, using cached refs" , e );
324+ }
325+
326+ // Read local refs (now up-to-date)
312327 List <BranchInfo > localRefs = readLocalRefs (git );
313328 if (!localRefs .isEmpty ()) {
314- LOGGER .info ("Using cached local refs (" + localRefs .size () + " branches)" );
329+ LOGGER .info ("Using local refs (" + localRefs .size () + " branches)" );
315330 return applyLimits (localRefs );
316331 }
317332
0 commit comments