Skip to content

Commit 4ebb6e4

Browse files
authored
Use non-deprecated JGit methods to get references (jenkinsci#1191)
The JGit 7.0.0 upgrade reminded me that it is unwise to depend on deprecated methods from JGit because they will eventually be removed. Better to stop using the deprecated methods now and avoid problems in the future.
1 parent 4029a41 commit 4ebb6e4

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,10 +3016,15 @@ public Set<Branch> getBranches() throws GitException, InterruptedException {
30163016
@Override
30173017
public Set<Branch> getRemoteBranches() throws GitException, InterruptedException {
30183018
try (Repository db = getRepository()) {
3019-
Map<String, Ref> refs = db.getAllRefs();
3019+
List<Ref> refs;
3020+
try {
3021+
refs = db.getRefDatabase().getRefs();
3022+
} catch (IOException ioe) {
3023+
throw new GitException(ioe);
3024+
}
30203025
Set<Branch> branches = new HashSet<>();
30213026

3022-
for (Ref candidate : refs.values()) {
3027+
for (Ref candidate : refs) {
30233028
if (candidate.getName().startsWith(Constants.R_REMOTES)) {
30243029
Branch buildBranch = new Branch(candidate);
30253030
if (!GitClient.quietRemoteBranches) {

src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,7 +2090,7 @@ public void prune(RemoteConfig repository) throws GitException {
20902090

20912091
Set<String> branches = listRemoteBranches(remote);
20922092

2093-
for (Ref r : new ArrayList<>(gitRepo.getAllRefs().values())) {
2093+
for (Ref r : new ArrayList<>(gitRepo.getRefDatabase().getRefs())) {
20942094
if (r.getName().startsWith(prefix) && !branches.contains(r.getName())) {
20952095
// delete this ref
20962096
RefUpdate update = gitRepo.updateRef(r.getName());
@@ -2347,7 +2347,7 @@ public void execute() throws GitException {
23472347
out.add(c.copy());
23482348

23492349
if (all) {
2350-
for (Ref r : repo.getAllRefs().values()) {
2350+
for (Ref r : repo.getRefDatabase().getRefs()) {
23512351
c = walk.parseCommit(r.getObjectId());
23522352
out.add(c.copy());
23532353
}
@@ -2730,7 +2730,13 @@ public List<Branch> getBranchesContaining(String revspec, boolean allBranches)
27302730
private List<Ref> getAllBranchRefs(boolean originBranches) throws GitException {
27312731
List<Ref> branches = new ArrayList<>();
27322732
try (Repository repo = getRepository()) {
2733-
for (Ref r : repo.getAllRefs().values()) {
2733+
List<Ref> refs;
2734+
try {
2735+
refs = repo.getRefDatabase().getRefs();
2736+
} catch (IOException ioe) {
2737+
throw new GitException(ioe);
2738+
}
2739+
for (Ref r : refs) {
27342740
final String branchName = r.getName();
27352741
if (branchName.startsWith(R_HEADS) || (originBranches && branchName.startsWith(R_REMOTES))) {
27362742
branches.add(r);
@@ -2799,7 +2805,7 @@ private void markAllRefs(RevWalk walk) throws GitException, IOException {
27992805
*/
28002806
private void markRefs(RevWalk walk, Predicate<Ref> filter) throws GitException, IOException {
28012807
try (Repository repo = getRepository()) {
2802-
for (Ref r : repo.getAllRefs().values()) {
2808+
for (Ref r : repo.getRefDatabase().getRefs()) {
28032809
if (filter.test(r)) {
28042810
RevCommit c = walk.parseCommit(r.getObjectId());
28052811
walk.markStart(c);
@@ -2919,7 +2925,7 @@ public String describe(String tip) throws GitException, InterruptedException {
29192925
w.setRetainBody(false);
29202926

29212927
Map<ObjectId, Ref> tags = new HashMap<>();
2922-
for (Ref r : repo.getTags().values()) {
2928+
for (Ref r : repo.getRefDatabase().getRefsByPrefix(R_TAGS)) {
29232929
ObjectId key = repo.getRefDatabase().peel(r).getPeeledObjectId();
29242930
if (key == null) {
29252931
key = r.getObjectId();

0 commit comments

Comments
 (0)