Skip to content

Commit 7cf6dae

Browse files
authored
Merge pull request jenkinsci#256 from MarkEWaite/test-more-cases
Test more cases of fetch options
2 parents d7ed207 + 48c3437 commit 7cf6dae

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ public void execute() throws GitException, InterruptedException {
13821382
// we use origin as reference
13831383
reference = url;
13841384
} else {
1385-
listener.getLogger().println("[WARNING] Both 'shared' and 'reference' is used, shared is ignored.");
1385+
listener.getLogger().println("[WARNING] Both 'shared' and 'reference' are used, shared is ignored.");
13861386
}
13871387
}
13881388

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package org.jenkinsci.plugins.gitclient;
22

3-
import org.eclipse.jgit.transport.RefSpec;
43
import org.eclipse.jgit.transport.URIish;
54

6-
import java.util.List;
7-
85
/**
96
* PushCommand interface.
107
*

src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.hamcrest.MatcherAssert.assertThat;
99
import static org.hamcrest.Matchers.*;
1010
import static org.jenkinsci.plugins.gitclient.StringSharesPrefix.sharesPrefix;
11-
import static org.junit.Assert.assertNotEquals;
11+
import static org.junit.Assert.*;
1212

1313
import hudson.FilePath;
1414
import hudson.Launcher;
@@ -254,7 +254,11 @@ WorkingArea init(boolean bare) throws IOException, InterruptedException {
254254
}
255255

256256
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);
258262
}
259263

260264
void commitEmpty(String msg) throws IOException, InterruptedException {
@@ -543,13 +547,12 @@ public void test_clone_repositoryName() throws IOException, InterruptedException
543547

544548
public void test_clone_shallow() throws Exception
545549
{
546-
w.git.clone_().url(localMirror()).repositoryName("origin").shallow().execute();
550+
w.git.clone_().url(localMirror()).repositoryName("origin").shallow(true).execute();
547551
createRevParseBranch(); // Verify JENKINS-32258 is fixed
548552
w.git.checkout("origin/master", "master");
549553
check_remote_url("origin");
550554
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();
553556
/* JGit does not support shallow clone */
554557
assertEquals("isShallow?", w.igit() instanceof CliGitAPIImpl, w.cgit().isShallowRepository());
555558
final String shallow = ".git" + File.separator + "shallow";
@@ -558,12 +561,11 @@ public void test_clone_shallow() throws Exception
558561

559562
public void test_clone_shallow_with_depth() throws IOException, InterruptedException
560563
{
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();
562565
w.git.checkout("origin/master", "master");
563566
check_remote_url("origin");
564567
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();
567569
/* JGit does not support shallow clone */
568570
final String shallow = ".git" + File.separator + "shallow";
569571
assertEquals("Shallow file existence: " + shallow, w.igit() instanceof CliGitAPIImpl, w.exists(shallow));
@@ -580,6 +582,16 @@ public void test_clone_shared() throws IOException, InterruptedException
580582
assertNoObjectsInRepository();
581583
}
582584

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+
583595
public void test_clone_reference() throws IOException, InterruptedException
584596
{
585597
w.git.clone_().url(localMirror()).repositoryName("origin").reference(localMirror()).execute();
@@ -604,6 +616,11 @@ private void assertNoObjectsInRepository() {
604616

605617
}
606618

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+
607624
private void assertAlternateFilePointsToLocalMirror() throws IOException, InterruptedException {
608625
final String alternates = ".git" + File.separator + "objects" + File.separator + "info" + File.separator + "alternates";
609626

@@ -1033,11 +1050,46 @@ public void test_push_tags() throws Exception {
10331050
assertEquals("bare != working", commit1, bareCommit1);
10341051
assertEquals(commit1, bare.git.getHeadRev(bare.repoPath(), "refs/heads/master"));
10351052

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 */
10371054
w.tag("tag1");
10381055
assertTrue("tag1 wasn't created", w.git.tagExists("tag1"));
1056+
assertEquals("tag1 points to wrong commit", commit1, w.git.revParse("tag1"));
10391057
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();
10411093

10421094
/* Add tag to working repo without pushing it to the bare
10431095
* repo, tests the default behavior when tags() is not added

0 commit comments

Comments
 (0)