Skip to content

Commit 8c1886a

Browse files
committed
[findbugs] Remove warnings in SubmoduleCombination/SubmoduleConfig
Removes inefficent Map entry iteration and String += in loops.
1 parent b81c4d8 commit 8c1886a

2 files changed

Lines changed: 40 additions & 40 deletions

File tree

src/main/java/hudson/plugins/git/SubmoduleCombinator.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Iterator;
1515
import java.util.List;
1616
import java.util.Map;
17+
import java.util.Map.Entry;
1718

1819
import org.eclipse.jgit.lib.ObjectId;
1920

@@ -59,18 +60,18 @@ public void createSubmoduleCombinations() throws GitException, IOException {
5960
}
6061

6162
// Remove any uninteresting branches
62-
63-
64-
65-
for (IndexEntry entry : moduleBranches.keySet()) {
66-
listener.getLogger().print("Submodule " + entry.getFile() + " branches");
67-
for (Revision br : moduleBranches.get(entry)) {
68-
listener.getLogger().print(" " + br.toString());
6963

70-
}
71-
listener.getLogger().print("\n");
64+
65+
66+
for (Entry<IndexEntry, Collection<Revision>> submodule : moduleBranches
67+
.entrySet()) {
68+
listener.getLogger().print(
69+
"Submodule " + submodule.getKey().getFile() + " branches");
70+
for (Revision br : submodule.getValue())
71+
listener.getLogger().print(" " + br.toString());
72+
listener.getLogger().print('\n');
7273
}
73-
74+
7475
// Make all the possible combinations
7576
List<Map<IndexEntry, Revision>> combinations = createCombinations(moduleBranches);
7677

@@ -110,12 +111,12 @@ public void createSubmoduleCombinations() throws GitException, IOException {
110111
int min = Integer.MAX_VALUE;
111112

112113
// But let's see if we can find the most appropriate place to create the branch
113-
for (ObjectId sha : entriesMap.keySet()) {
114-
List<IndexEntry> entries = entriesMap.get(sha);
115-
int value = difference(combination, entries);
114+
for (Entry<ObjectId, List<IndexEntry>> entry : entriesMap
115+
.entrySet()) {
116+
int value = difference(combination, entry.getValue());
116117
if (value > 0 && value < min) {
117118
min = value;
118-
sha1 = sha;
119+
sha1 = entry.getKey();
119120
}
120121

121122
if (min == 1) break; // look no further
@@ -151,36 +152,40 @@ protected void makeCombination(Map<IndexEntry, Revision> settings) {
151152
String name = "combine-" + tid + "-" + (idx++);
152153
git.branch(name);
153154
git.checkout(name);
154-
155-
String commit = "Jenkins generated combination of:\n";
156-
157-
for (IndexEntry submodule : settings.keySet()) {
158-
Revision branch = settings.get(submodule);
159-
commit += " " + submodule.getFile() + " " + branch.toString() + "\n";
155+
156+
StringBuilder commit = new StringBuilder(
157+
"Jenkins generated combination of:\n");
158+
159+
for (Entry<IndexEntry, Revision> setting : settings.entrySet()) {
160+
commit.append(' ').append(' ');
161+
commit.append(setting.getKey().getFile());
162+
commit.append(' ');
163+
commit.append(setting.getValue());
164+
commit.append('\n');
160165
}
161-
166+
162167
listener.getLogger().print(commit);
163-
164-
165-
for (IndexEntry submodule : settings.keySet()) {
166-
Revision branch = settings.get(submodule);
168+
169+
for (Entry<IndexEntry, Revision> setting : settings.entrySet()) {
170+
IndexEntry submodule = setting.getKey();
171+
Revision branch = setting.getValue();
167172
File subdir = new File(workspace, submodule.getFile());
168-
IGitAPI subGit = new GitAPI(git.getGitExe(), new FilePath(subdir), listener, git.getEnvironment());
169-
173+
IGitAPI subGit = new GitAPI(git.getGitExe(), new FilePath(subdir),
174+
listener, git.getEnvironment());
175+
170176
subGit.checkout(branch.sha1.name());
171177
git.add(submodule.file);
172-
173178
}
174179

175180
try {
176181
File f = File.createTempFile("gitcommit", ".txt");
177182
FileOutputStream fos = null;
178183
try {
179184
fos = new FileOutputStream(f);
180-
fos.write(commit.getBytes());
181-
}
182-
finally {
183-
fos.close();
185+
fos.write(commit.toString().getBytes());
186+
} finally {
187+
if (fos != null)
188+
fos.close();
184189
}
185190
git.commit(f);
186191
f.delete();

src/main/java/hudson/plugins/git/SubmoduleConfig.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package hudson.plugins.git;
22

3+
import com.google.common.base.Joiner;
4+
35
import java.util.regex.Pattern;
46

57
public class SubmoduleConfig implements java.io.Serializable {
@@ -40,13 +42,6 @@ public boolean branchMatchesInterest(Branch br) {
4042
}
4143

4244
public String getBranchesString() {
43-
String ret = "";
44-
45-
for (String branch : branches) {
46-
if (ret.length() > 0) ret += ",";
47-
ret += branch;
48-
}
49-
return ret;
50-
45+
return Joiner.on(',').join(branches);
5146
}
5247
}

0 commit comments

Comments
 (0)