5
5
import org .eclipse .jgit .api .*;
6
6
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
7
7
import org .eclipse .jgit .api .ResetCommand .ResetType ;
8
+ import org .eclipse .jgit .api .errors .CanceledException ;
9
+ import org .eclipse .jgit .api .errors .DetachedHeadException ;
8
10
import org .eclipse .jgit .api .errors .GitAPIException ;
11
+ import org .eclipse .jgit .api .errors .InvalidConfigurationException ;
12
+ import org .eclipse .jgit .api .errors .InvalidRemoteException ;
13
+ import org .eclipse .jgit .api .errors .NoHeadException ;
9
14
import org .eclipse .jgit .api .errors .RefAlreadyExistsException ;
15
+ import org .eclipse .jgit .api .errors .RefNotAdvertisedException ;
16
+ import org .eclipse .jgit .api .errors .RefNotFoundException ;
17
+ import org .eclipse .jgit .api .errors .TransportException ;
18
+ import org .eclipse .jgit .api .errors .WrongRepositoryStateException ;
10
19
import org .eclipse .jgit .diff .DiffEntry ;
11
20
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
12
21
import org .eclipse .jgit .diff .DiffEntry .Side ;
@@ -132,7 +141,7 @@ public void createBranch(String srcBranchName, String newBranchName, String comm
132
141
Repository gitRepo = git .getRepository ()) {
133
142
134
143
checkout (git , gitRepo , srcBranchName , null );
135
-
144
+
136
145
git
137
146
.branchCreate ()
138
147
.setUpstreamMode (SetupUpstreamMode .TRACK )
@@ -176,7 +185,7 @@ public void deleteBranch(String branchName, String commitMessage) {
176
185
}
177
186
}
178
187
179
- private void push (Git git , RefSpec refSpec ) throws GitAPIException {
188
+ void push (Git git , RefSpec refSpec ) throws GitAPIException {
180
189
PushCommand cmd = git
181
190
.push ();
182
191
if (refSpec != null ) {
@@ -285,18 +294,8 @@ public String getFileContent(String branchName, String fileRelativePath, String
285
294
Repository gitRepo = git .getRepository ();
286
295
RevWalk revWalk = new RevWalk (gitRepo );
287
296
TreeWalk treeWalk = new TreeWalk (gitRepo )) {
288
-
289
- git
290
- .pull ()
291
- .setCredentialsProvider (credentials )
292
- .call ();
293
-
294
- // if executed first then version is considered as modified. So have uncommited change: 19.5-SNAPSHOT -> 18.5-SNAPSHOT
295
- git
296
- .fetch ()
297
- .setRefSpecs (new RefSpec ("+refs/heads/*:refs/heads/*" ))
298
- .setCredentialsProvider (credentials )
299
- .call ();
297
+
298
+ pullAndFetch (git );
300
299
301
300
ObjectId revisionCommitId = gitRepo .resolve (revision == null ? REFS_HEADS + getRealBranchName (branchName ) : revision );
302
301
if (revision == null && revisionCommitId == null ) {
@@ -388,10 +387,9 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
388
387
private void checkout (Git git , Repository gitRepo , String branchName , String revision ) throws Exception {
389
388
String bn = getRealBranchName (branchName );
390
389
CheckoutCommand cmd = git .checkout ();
391
- git
392
- .pull ()
393
- .setCredentialsProvider (credentials )
394
- .call ();
390
+
391
+ pullAndFetch (git );
392
+
395
393
if (revision == null ) {
396
394
cmd
397
395
.setStartPoint ("origin/" + bn )
@@ -411,6 +409,27 @@ private void checkout(Git git, Repository gitRepo, String branchName, String rev
411
409
}
412
410
}
413
411
412
+ private void pullAndFetch (Git git ) throws GitAPIException , WrongRepositoryStateException ,
413
+ InvalidConfigurationException , DetachedHeadException , InvalidRemoteException , CanceledException ,
414
+ RefNotFoundException , RefNotAdvertisedException , NoHeadException , TransportException {
415
+ git
416
+ .pull ()
417
+ .setCredentialsProvider (credentials )
418
+ .call ();
419
+
420
+ // remove local branches and tags which are not exists on remote
421
+ // See https://github.com/scm4j/scm4j-releaser/issues/59
422
+ // if executed first then version is considered as modified. So have uncommited change: 19.5-SNAPSHOT -> 18.5-SNAPSHOT
423
+ git
424
+ .fetch ()
425
+ .setRefSpecs (
426
+ new RefSpec ("+refs/heads/*:refs/heads/*" ),
427
+ new RefSpec ("+refs/tags/*:refs/tags/*" ))
428
+ .setRemoveDeletedRefs (true )
429
+ .setCredentialsProvider (credentials )
430
+ .call ();
431
+ }
432
+
414
433
@ Override
415
434
public List <VCSDiffEntry > getBranchesDiff (String srcBranchName , String dstBranchName ) {
416
435
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
@@ -469,18 +488,9 @@ public Set<String> getBranches(String path) {
469
488
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
470
489
Git git = getLocalGit (wc );
471
490
Repository gitRepo = git .getRepository ()) {
472
-
473
- git
474
- .pull ()
475
- .setCredentialsProvider (credentials )
476
- .call ();
477
- git
478
- .fetch ()
479
- .setRefSpecs (new RefSpec ("+refs/heads/*:refs/heads/*" ))
480
- .setRemoveDeletedRefs (true )
481
- .setCredentialsProvider (credentials )
482
- .call ();
483
-
491
+
492
+ pullAndFetch (git );
493
+
484
494
Collection <Ref > refs = gitRepo .getRefDatabase ().getRefs (REFS_REMOTES_ORIGIN ).values ();
485
495
Set <String > res = new HashSet <>();
486
496
String bn ;
@@ -721,7 +731,7 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
721
731
Repository gitRepo = git .getRepository ();
722
732
RevWalk rw = new RevWalk (gitRepo )) {
723
733
724
- updateLocalTags (git );
734
+ pullAndFetch (git );
725
735
726
736
checkout (git , gitRepo , branchName , null );
727
737
@@ -750,32 +760,14 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
750
760
}
751
761
}
752
762
753
- private void updateLocalTags (Git git ) throws Exception {
754
- // need to remove tags from local repo which are removed in origin
755
-
756
- git
757
- .pull ()
758
- .setCredentialsProvider (credentials )
759
- .call ();
760
- git
761
- .fetch ()
762
- .setRefSpecs (new RefSpec ("+refs/tags/*:refs/tags/*" ))
763
- .setRemoveDeletedRefs (true )
764
- .setCredentialsProvider (credentials )
765
- .call ();
766
-
767
- }
768
-
769
-
770
-
771
763
@ Override
772
764
public List <VCSTag > getTags () {
773
765
try (IVCSLockedWorkingCopy wc = repo .getVCSLockedWorkingCopy ();
774
766
Git git = getLocalGit (wc );
775
767
Repository gitRepo = git .getRepository ();
776
768
RevWalk rw = new RevWalk (gitRepo )) {
777
769
778
- updateLocalTags (git );
770
+ pullAndFetch (git );
779
771
Collection <Ref > tagRefs = gitRepo .getTags ().values ();
780
772
List <VCSTag > res = new ArrayList <>();
781
773
RevCommit revCommit ;
@@ -807,7 +799,7 @@ public void removeTag(String tagName) {
807
799
Repository gitRepo = git .getRepository ();
808
800
RevWalk rw = new RevWalk (gitRepo )) {
809
801
810
- updateLocalTags (git );
802
+ pullAndFetch (git );
811
803
812
804
git
813
805
.tagDelete ()
@@ -844,7 +836,7 @@ public List<VCSTag> getTagsOnRevision(String revision) {
844
836
Repository gitRepo = git .getRepository ();
845
837
RevWalk rw = new RevWalk (gitRepo )) {
846
838
847
- updateLocalTags (git );
839
+ pullAndFetch (git );
848
840
849
841
List <VCSTag > res = new ArrayList <>();
850
842
0 commit comments