Skip to content

Commit

Permalink
Merge pull request #23 from jenkinsci/bugfix/JENKINS-34574
Browse files Browse the repository at this point in the history
After cleaned workspace doesn't show branches
  • Loading branch information
klimas7 committed May 3, 2016
2 parents b1cdf26 + 32aabac commit cca0236
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
Expand Up @@ -247,39 +247,20 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC

for (RemoteConfig repository : git.getRepositories()) {
LOGGER.log(Level.INFO, "generateContents contenttype " + type + " RemoteConfig " + repository.getURIs());
GitClient newgit = getGitClient(project, git, environment);
GitClient gitClient = getGitClient(project, git, environment);
for (URIish remoteURL : repository.getURIs()) {
FilePath wsDir = null;
if (project.getSomeBuildWithWorkspace() != null) {
wsDir = project.getSomeBuildWithWorkspace().getWorkspace();
if (wsDir == null || !wsDir.exists()) {
LOGGER.log(Level.WARNING, "generateContents create wsDir " + wsDir + " for " + remoteURL);
wsDir.mkdirs();
if (!wsDir.exists()) {
LOGGER.log(Level.SEVERE, "generateContents wsDir.mkdirs() failed.");
String errMsg = "!Failed To Create Workspace";
return Collections.singletonMap(errMsg, errMsg);
}
newgit.init();
newgit.clone(remoteURL.toASCIIString(), "origin", false, null);
LOGGER.log(Level.INFO, "generateContents clone done");
}
} else {
// probably our first build. We cannot yet fill in any
// values.
LOGGER.log(Level.INFO, "getSomeBuildWithWorkspace is null");
String errMsg = "!No workspace. Please build the project at least once";
return Collections.singletonMap(errMsg, errMsg);
}

Map<String, String> errMsg = initWorkspace(project, gitClient, remoteURL);
if (errMsg != null) return errMsg;

long time = -System.currentTimeMillis();

FetchCommand fetch = newgit.fetch_().prune().from(remoteURL, repository.getFetchRefSpecs());
FetchCommand fetch = gitClient.fetch_().prune().from(remoteURL, repository.getFetchRefSpecs());
fetch.execute();

LOGGER.finest("Took " + (time + System.currentTimeMillis()) + "ms to fetch");
if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(newgit, branch);
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(gitClient, branch);
List<RevisionInfo> revisions = revisionInfoFactory.getRevisions();

for (RevisionInfo revision : revisions) {
Expand All @@ -288,7 +269,7 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC
}
if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {

Set<String> tagSet = newgit.getTagNames(tagFilter);
Set<String> tagSet = gitClient.getTagNames(tagFilter);
ArrayList<String> orderedTagNames;

if (this.getSortMode().getIsSorting()) {
Expand All @@ -315,7 +296,7 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC
errorMessage = "Specified branchFilter is not a valid regex. Setting to '.*'";
branchFilterPattern = Pattern.compile(".*");
}
for (Branch branch : newgit.getRemoteBranches()) {
for (Branch branch : gitClient.getRemoteBranches()) {
String branchName = branch.getName();
if (branchFilterPattern.matcher(branchName).matches()) {
branchSet.add(branchName);
Expand Down Expand Up @@ -344,6 +325,35 @@ public Map<String, String> generateContents(AbstractProject<?, ?> project, GitSC
return paramList;
}

private Map<String, String> initWorkspace(AbstractProject<?, ?> project, GitClient gitClient, URIish remoteURL) throws IOException, InterruptedException {
if (project.getSomeBuildWithWorkspace() != null) {
FilePath wsDir = project.getSomeBuildWithWorkspace().getWorkspace();
if (isEmptyWorkspace(wsDir)) {
LOGGER.log(Level.WARNING, "generateContents create wsDir " + wsDir + " for " + remoteURL);
wsDir.mkdirs();
if (!wsDir.exists()) {
LOGGER.log(Level.SEVERE, "generateContents wsDir.mkdirs() failed.");
String errMsg = "!Failed To Create Workspace";
return Collections.singletonMap(errMsg, errMsg);
}
gitClient.init();
gitClient.clone(remoteURL.toASCIIString(), "origin", false, null);
LOGGER.log(Level.INFO, "generateContents clone done");
}
} else {
// probably our first build. We cannot yet fill in any
// values.
LOGGER.log(Level.INFO, "getSomeBuildWithWorkspace is null");
String errMsg = "!No workspace. Please build the project at least once";
return Collections.singletonMap(errMsg, errMsg);
}
return null;
}

private boolean isEmptyWorkspace(FilePath workspaceDir) throws IOException, InterruptedException {
return workspaceDir == null || !workspaceDir.exists() || workspaceDir.list().size() == 0;
}

private GitClient getGitClient(final AbstractProject<?, ?> project, GitSCM git, EnvVars environment) throws IOException, InterruptedException {
int nextBuildNumber = project.getNextBuildNumber();

Expand Down
Expand Up @@ -5,7 +5,7 @@
<f:entry title="Description" field="description">
<f:textbox />
</f:entry>
<f:entry name="type" title="Parameter Type" field="type">
<f:entry title="Parameter Type" field="type">
<select name="type">
<j:choose>
<j:when test="${instance.type eq 'PT_TAG'}">
Expand Down Expand Up @@ -54,7 +54,7 @@
<f:textbox />
</f:entry>

<f:entry name="sortMode" title="Sort mode" field="sortMode">
<f:entry title="Sort mode" field="sortMode">
<f:enum>${it}</f:enum>
</f:entry>

Expand Down
@@ -1,7 +1,7 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<f:entry name="value" title="${it.name}" description="${it.description}">
<f:entry title="${it.name}" description="${it.description}">
${it.value}
</f:entry>
</j:jelly>

0 comments on commit cca0236

Please sign in to comment.