Skip to content

Commit c5437fb

Browse files
author
jicheng
committed
v1.3.5: 性能优化 - 工作空间分支获取改为轻量级 fetch
- 工作空间模式下先执行轻量级 fetch 更新 refs - 保持读取本地 refs 的快速响应特性 - fetch 失败时回退到缓存的本地 refs - 添加凭据支持以确保 fetch 成功 - 更新注释说明新的获取策略
1 parent ee50db5 commit c5437fb

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>io.jenkins.plugins</groupId>
1313
<artifactId>active-git-branches-plugin</artifactId>
14-
<version>1.3.0</version>
14+
<version>1.3.5</version>
1515
<packaging>hpi</packaging>
1616

1717
<name>Active Git Branches Parameter</name>

src/main/java/io/jenkins/plugins/activegitbranches/ActiveGitBranchesParameterDefinition.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.kohsuke.stapler.QueryParameter;
3232
import org.kohsuke.stapler.Stapler;
3333
import org.kohsuke.stapler.StaplerRequest;
34+
import org.eclipse.jgit.transport.URIish;
3435

3536
import java.io.File;
3637
import 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

Comments
 (0)