8
8
import static org .hamcrest .MatcherAssert .assertThat ;
9
9
import static org .hamcrest .Matchers .*;
10
10
import static org .jenkinsci .plugins .gitclient .StringSharesPrefix .sharesPrefix ;
11
- import static org .junit .Assert .assertNotEquals ;
11
+ import static org .junit .Assert .* ;
12
12
13
13
import hudson .FilePath ;
14
14
import hudson .Launcher ;
@@ -254,7 +254,11 @@ WorkingArea init(boolean bare) throws IOException, InterruptedException {
254
254
}
255
255
256
256
void tag (String tag ) throws IOException , InterruptedException {
257
- cmd ("git tag " + tag );
257
+ tag (tag , false );
258
+ }
259
+
260
+ void tag (String tag , boolean force ) throws IOException , InterruptedException {
261
+ cmd ("git tag" + (force ? " --force " : " " ) + tag );
258
262
}
259
263
260
264
void commitEmpty (String msg ) throws IOException , InterruptedException {
@@ -543,13 +547,12 @@ public void test_clone_repositoryName() throws IOException, InterruptedException
543
547
544
548
public void test_clone_shallow () throws Exception
545
549
{
546
- w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).shallow ().execute ();
550
+ w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).shallow (true ).execute ();
547
551
createRevParseBranch (); // Verify JENKINS-32258 is fixed
548
552
w .git .checkout ("origin/master" , "master" );
549
553
check_remote_url ("origin" );
550
554
assertBranchesExist (w .git .getBranches (), "master" );
551
- final String alternates = ".git" + File .separator + "objects" + File .separator + "info" + File .separator + "alternates" ;
552
- assertFalse ("Alternates file found: " + alternates , w .exists (alternates ));
555
+ assertAlternatesFileNotFound ();
553
556
/* JGit does not support shallow clone */
554
557
assertEquals ("isShallow?" , w .igit () instanceof CliGitAPIImpl , w .cgit ().isShallowRepository ());
555
558
final String shallow = ".git" + File .separator + "shallow" ;
@@ -558,12 +561,11 @@ public void test_clone_shallow() throws Exception
558
561
559
562
public void test_clone_shallow_with_depth () throws IOException , InterruptedException
560
563
{
561
- w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).shallow ().depth (2 ).execute ();
564
+ w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).shallow (true ).depth (2 ).execute ();
562
565
w .git .checkout ("origin/master" , "master" );
563
566
check_remote_url ("origin" );
564
567
assertBranchesExist (w .git .getBranches (), "master" );
565
- final String alternates = ".git" + File .separator + "objects" + File .separator + "info" + File .separator + "alternates" ;
566
- assertFalse ("Alternates file found: " + alternates , w .exists (alternates ));
568
+ assertAlternatesFileNotFound ();
567
569
/* JGit does not support shallow clone */
568
570
final String shallow = ".git" + File .separator + "shallow" ;
569
571
assertEquals ("Shallow file existence: " + shallow , w .igit () instanceof CliGitAPIImpl , w .exists (shallow ));
@@ -580,6 +582,16 @@ public void test_clone_shared() throws IOException, InterruptedException
580
582
assertNoObjectsInRepository ();
581
583
}
582
584
585
+ public void test_clone_unshared () throws IOException , InterruptedException
586
+ {
587
+ w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).shared (false ).execute ();
588
+ createRevParseBranch (); // Verify JENKINS-32258 is fixed
589
+ w .git .checkout ("origin/master" , "master" );
590
+ check_remote_url ("origin" );
591
+ assertBranchesExist (w .git .getBranches (), "master" );
592
+ assertAlternatesFileNotFound ();
593
+ }
594
+
583
595
public void test_clone_reference () throws IOException , InterruptedException
584
596
{
585
597
w .git .clone_ ().url (localMirror ()).repositoryName ("origin" ).reference (localMirror ()).execute ();
@@ -604,6 +616,11 @@ private void assertNoObjectsInRepository() {
604
616
605
617
}
606
618
619
+ private void assertAlternatesFileNotFound () {
620
+ final String alternates = ".git" + File .separator + "objects" + File .separator + "info" + File .separator + "alternates" ;
621
+ assertFalse ("Alternates file found: " + alternates , w .exists (alternates ));
622
+ }
623
+
607
624
private void assertAlternateFilePointsToLocalMirror () throws IOException , InterruptedException {
608
625
final String alternates = ".git" + File .separator + "objects" + File .separator + "info" + File .separator + "alternates" ;
609
626
@@ -1033,11 +1050,46 @@ public void test_push_tags() throws Exception {
1033
1050
assertEquals ("bare != working" , commit1 , bareCommit1 );
1034
1051
assertEquals (commit1 , bare .git .getHeadRev (bare .repoPath (), "refs/heads/master" ));
1035
1052
1036
- /* Add tag to working repo and without pushing it to the bare repo */
1053
+ /* Add tag1 to working repo without pushing it to bare repo */
1037
1054
w .tag ("tag1" );
1038
1055
assertTrue ("tag1 wasn't created" , w .git .tagExists ("tag1" ));
1056
+ assertEquals ("tag1 points to wrong commit" , commit1 , w .git .revParse ("tag1" ));
1039
1057
w .git .push ().ref ("master" ).to (new URIish (bare .repoPath ())).tags (false ).execute ();
1040
- assertFalse ("tag1 wasn't pushed" , bare .cmd ("git tag" ).contains ("tag1" ));
1058
+ assertFalse ("tag1 pushed unexpectedly" , bare .cmd ("git tag" ).contains ("tag1" ));
1059
+
1060
+ /* Push tag1 to bare repo */
1061
+ w .git .push ().ref ("master" ).to (new URIish (bare .repoPath ())).tags (true ).execute ();
1062
+ assertTrue ("tag1 not pushed" , bare .cmd ("git tag" ).contains ("tag1" ));
1063
+
1064
+ /* Create a new commit, move tag1 to that commit, attempt push */
1065
+ w .touch ("file1" , "file1 content " + java .util .UUID .randomUUID ().toString ());
1066
+ w .git .add ("file1" );
1067
+ w .git .commit ("commit2" );
1068
+ ObjectId commit2 = w .head ();
1069
+ w .tag ("tag1" , true ); /* Tag already exists, move from commit1 to commit2 */
1070
+ assertTrue ("tag1 wasn't created" , w .git .tagExists ("tag1" ));
1071
+ assertEquals ("tag1 points to wrong commit" , commit2 , w .git .revParse ("tag1" ));
1072
+ try {
1073
+ w .git .push ().ref ("master" ).to (new URIish (bare .repoPath ())).tags (true ).execute ();
1074
+ /* JGit does not throw exception updating existing tag - ugh */
1075
+ /* CliGit before 1.8 does not throw exception updating existing tag - ugh */
1076
+ if (w .git instanceof CliGitAPIImpl && w .cgit ().isAtLeastVersion (1 , 8 , 0 , 0 )) {
1077
+ fail ("Modern CLI git should throw exception pushing a change to existing tag" );
1078
+ }
1079
+ } catch (GitException ge ) {
1080
+ assertThat (ge .getMessage (), containsString ("already exists" ));
1081
+ }
1082
+ try {
1083
+ w .git .push ().ref ("master" ).to (new URIish (bare .repoPath ())).tags (true ).force (false ).execute ();
1084
+ /* JGit does not throw exception updating existing tag - ugh */
1085
+ /* CliGit before 1.8 does not throw exception updating existing tag - ugh */
1086
+ if (w .git instanceof CliGitAPIImpl && w .cgit ().isAtLeastVersion (1 , 8 , 0 , 0 )) {
1087
+ fail ("Modern CLI git should throw exception pushing a change to existing tag" );
1088
+ }
1089
+ } catch (GitException ge ) {
1090
+ assertThat (ge .getMessage (), containsString ("already exists" ));
1091
+ }
1092
+ w .git .push ().ref ("master" ).to (new URIish (bare .repoPath ())).tags (true ).force (true ).execute ();
1041
1093
1042
1094
/* Add tag to working repo without pushing it to the bare
1043
1095
* repo, tests the default behavior when tags() is not added
0 commit comments