1
1
package org .scm4j .vcs ;
2
2
3
- import java .io .ByteArrayOutputStream ;
4
- import java .io .File ;
5
- import java .io .FileWriter ;
6
- import java .io .IOException ;
7
- import java .io .InputStream ;
8
- import java .net .Authenticator ;
9
- import java .net .InetSocketAddress ;
10
- import java .net .PasswordAuthentication ;
11
- import java .net .Proxy ;
12
- import java .net .Proxy .Type ;
13
- import java .net .ProxySelector ;
14
- import java .net .SocketAddress ;
15
- import java .net .URI ;
16
- import java .nio .charset .StandardCharsets ;
17
- import java .util .ArrayList ;
18
- import java .util .Collection ;
19
- import java .util .Collections ;
20
- import java .util .HashSet ;
21
- import java .util .List ;
22
- import java .util .Set ;
23
-
24
3
import org .apache .commons .io .FileUtils ;
25
4
import org .apache .commons .io .IOUtils ;
26
- import org .eclipse .jgit .api .CheckoutCommand ;
5
+ import org .eclipse .jgit .api .* ;
27
6
import org .eclipse .jgit .api .CreateBranchCommand .SetupUpstreamMode ;
28
- import org .eclipse .jgit .api .Git ;
29
- import org .eclipse .jgit .api .LogCommand ;
30
- import org .eclipse .jgit .api .MergeResult ;
31
- import org .eclipse .jgit .api .PushCommand ;
32
7
import org .eclipse .jgit .api .ResetCommand .ResetType ;
33
8
import org .eclipse .jgit .api .errors .GitAPIException ;
34
9
import org .eclipse .jgit .api .errors .RefAlreadyExistsException ;
35
10
import org .eclipse .jgit .diff .DiffEntry ;
36
11
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
37
12
import org .eclipse .jgit .diff .DiffEntry .Side ;
38
13
import org .eclipse .jgit .diff .DiffFormatter ;
39
- import org .eclipse .jgit .lib .Constants ;
40
- import org .eclipse .jgit .lib .ObjectId ;
41
- import org .eclipse .jgit .lib .ObjectLoader ;
42
- import org .eclipse .jgit .lib .ObjectReader ;
43
- import org .eclipse .jgit .lib .Ref ;
44
- import org .eclipse .jgit .lib .Repository ;
45
- import org .eclipse .jgit .revwalk .RevCommit ;
46
- import org .eclipse .jgit .revwalk .RevObject ;
47
- import org .eclipse .jgit .revwalk .RevSort ;
48
- import org .eclipse .jgit .revwalk .RevTag ;
49
- import org .eclipse .jgit .revwalk .RevTree ;
50
- import org .eclipse .jgit .revwalk .RevWalk ;
14
+ import org .eclipse .jgit .lib .*;
15
+ import org .eclipse .jgit .revwalk .*;
51
16
import org .eclipse .jgit .storage .file .FileRepositoryBuilder ;
52
17
import org .eclipse .jgit .transport .CredentialsProvider ;
53
18
import org .eclipse .jgit .transport .RefSpec ;
54
19
import org .eclipse .jgit .transport .UsernamePasswordCredentialsProvider ;
55
20
import org .eclipse .jgit .treewalk .CanonicalTreeParser ;
56
21
import org .eclipse .jgit .treewalk .TreeWalk ;
57
22
import org .eclipse .jgit .treewalk .filter .PathFilter ;
58
- import org .scm4j .vcs .api .IVCS ;
59
- import org .scm4j .vcs .api .VCSChangeType ;
60
- import org .scm4j .vcs .api .VCSCommit ;
61
- import org .scm4j .vcs .api .VCSDiffEntry ;
62
- import org .scm4j .vcs .api .VCSMergeResult ;
63
- import org .scm4j .vcs .api .VCSTag ;
64
- import org .scm4j .vcs .api .WalkDirection ;
65
- import org .scm4j .vcs .api .exceptions .EVCSBranchExists ;
66
- import org .scm4j .vcs .api .exceptions .EVCSBranchNotFound ;
67
- import org .scm4j .vcs .api .exceptions .EVCSException ;
68
- import org .scm4j .vcs .api .exceptions .EVCSFileNotFound ;
69
- import org .scm4j .vcs .api .exceptions .EVCSTagExists ;
23
+ import org .scm4j .vcs .api .*;
24
+ import org .scm4j .vcs .api .exceptions .*;
70
25
import org .scm4j .vcs .api .workingcopy .IVCSLockedWorkingCopy ;
71
26
import org .scm4j .vcs .api .workingcopy .IVCSRepositoryWorkspace ;
72
27
import org .scm4j .vcs .api .workingcopy .IVCSWorkspace ;
73
28
29
+ import java .io .*;
30
+ import java .net .*;
31
+ import java .net .Proxy .Type ;
32
+ import java .nio .charset .StandardCharsets ;
33
+ import java .util .*;
34
+
74
35
public class GitVCS implements IVCS {
75
36
76
37
public static final String GIT_VCS_TYPE_STRING = "git" ;
@@ -94,7 +55,7 @@ public void setCredentials(CredentialsProvider credentials) {
94
55
private String getRealBranchName (String branchName ) {
95
56
return branchName == null ? MASTER_BRANCH_NAME : branchName ;
96
57
}
97
-
58
+
98
59
protected Git getLocalGit (String folder ) throws Exception {
99
60
Repository gitRepo = new FileRepositoryBuilder ()
100
61
.setGitDir (new File (folder , ".git" ))
@@ -337,12 +298,12 @@ public String getFileContent(String branchName, String fileRelativePath, String
337
298
.pull ()
338
299
.setCredentialsProvider (credentials )
339
300
.call (); //TODO: add test when we receive correct file version if we change it from another LWC
340
-
301
+
341
302
ObjectId revisionCommitId = gitRepo .resolve (revision == null ? "refs/heads/" + getRealBranchName (branchName ) : revision );
342
303
if (revision == null && revisionCommitId == null ) {
343
- throw new EVCSBranchNotFound (getRepoUrl (), getRealBranchName (branchName ));
304
+ throw new EVCSBranchNotFound (getRepoUrl (), getRealBranchName (branchName ));
344
305
}
345
-
306
+
346
307
RevCommit commit = revWalk .parseCommit (revisionCommitId );
347
308
RevTree tree = commit .getTree ();
348
309
treeWalk .addTree (tree );
@@ -373,7 +334,7 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
373
334
Repository gitRepo = git .getRepository ()) {
374
335
375
336
checkout (git , gitRepo , branchName , null );
376
-
337
+
377
338
File file = new File (wc .getFolder (), filePath );
378
339
if (!file .exists ()) {
379
340
FileUtils .forceMkdir (file .getParentFile ());
@@ -408,11 +369,11 @@ public VCSCommit setFileContent(String branchName, String filePath, String conte
408
369
void checkout (Git git , Repository gitRepo , String branchName , String revision ) throws Exception {
409
370
String bn = getRealBranchName (branchName );
410
371
CheckoutCommand cmd = git .checkout ();
372
+ git
373
+ .pull ()
374
+ .setCredentialsProvider (credentials )
375
+ .call ();
411
376
if (revision == null ) {
412
- git
413
- .pull ()
414
- .setCredentialsProvider (credentials )
415
- .call ();
416
377
cmd
417
378
.setStartPoint ("origin/" + bn )
418
379
.setCreateBranch (gitRepo .exactRef ("refs/heads/" + bn ) == null )
@@ -490,11 +451,18 @@ public Set<String> getBranches(String path) {
490
451
Git git = getLocalGit (wc );
491
452
Repository gitRepo = git .getRepository ()) {
492
453
454
+ git
455
+ .fetch ()
456
+ .setRefSpecs (new RefSpec ("+refs/heads/*:refs/heads/*" ))
457
+ .setRemoveDeletedRefs (true )
458
+ .setCredentialsProvider (credentials )
459
+ .call ();
460
+
493
461
git
494
462
.pull ()
495
463
.setCredentialsProvider (credentials )
496
- .call (); //TODO: add test when we receive correct branches list if we change it from another LWC
497
-
464
+ .call ();
465
+
498
466
Collection <Ref > refs = gitRepo .getRefDatabase ().getRefs (REFS_REMOTES_ORIGIN ).values ();
499
467
Set <String > res = new HashSet <>();
500
468
String bn ;
@@ -749,22 +717,24 @@ public VCSTag createTag(String branchName, String tagName, String tagMessage, St
749
717
750
718
updateLocalTags (git );
751
719
720
+ checkout (git , gitRepo , branchName , null );
721
+
752
722
RevCommit commitToTag = revisionToTag == null ? null : rw .parseCommit (ObjectId .fromString (revisionToTag ));
753
-
723
+
754
724
Ref ref = git
755
725
.tag ()
756
726
.setAnnotated (true )
757
727
.setMessage (tagMessage )
758
728
.setName (tagName )
759
729
.setObjectId (commitToTag )
760
730
.call ();
761
-
731
+
762
732
push (git , new RefSpec (ref .getName ()));
763
-
733
+
764
734
RevTag revTag = rw .parseTag (ref .getObjectId ());
765
735
RevCommit revCommit = rw .parseCommit (ref .getObjectId ());
766
736
VCSCommit relatedCommit = getVCSCommit (revCommit );
767
- return new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit );
737
+ return new VCSTag (revTag .getTagName (), revTag .getFullMessage (), revTag .getTaggerIdent ().getName (), relatedCommit );
768
738
} catch (RefAlreadyExistsException e ) {
769
739
throw new EVCSTagExists (e );
770
740
} catch (GitAPIException e ) {
0 commit comments