Skip to content

Commit

Permalink
[FIXED JENKINS-16888] compute ObjectId before checking isCommitInRepo
Browse files Browse the repository at this point in the history
added unit tests to cover 
- fetch from multiple repositories [JENKINS-16914]
- merge basic scenario
  • Loading branch information
ndeloof committed Feb 24, 2013
1 parent 4894b1b commit 128f715
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 75 deletions.
9 changes: 5 additions & 4 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -1328,17 +1328,18 @@ private void computeChangeLog(GitClient git, Revision revToBuild, BuildListener
}
}

private void computeMergeChangeLog(GitClient git, Revision revToBuild, String revFrom, BuildListener listener, FilePath changelogFile) throws IOException, InterruptedException {
if (!git.isCommitInRepo(ObjectId.fromString(revFrom))) {
listener.getLogger().println("Could not record history. Previous build's commit, " + revFrom
private void computeMergeChangeLog(GitClient git, Revision revToBuild, String remoteBranch, BuildListener listener, FilePath changelogFile) throws IOException, InterruptedException {
ObjectId objectId = git.getRepository().resolve(remoteBranch);
if (!git.isCommitInRepo(objectId)) {
listener.getLogger().println("Could not record history. Previous build's commit, " + remoteBranch
+ ", does not exist in the current repository.");
} else {
int histories = 0;

PrintStream out = new PrintStream(changelogFile.write());
try {
for (Branch b : revToBuild.getBranches()) {
putChangelogDiffs(git, b.getName(), revFrom, revToBuild.getSha1().name(), out);
putChangelogDiffs(git, b.getName(), remoteBranch, revToBuild.getSha1().name(), out);
histories++;
}
} catch (GitException ge) {
Expand Down
65 changes: 0 additions & 65 deletions src/main/test/hudson/plugins/git/SubmoduleCombinatorTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/git/AbstractGitTestCase.java
Expand Up @@ -62,7 +62,7 @@ protected void commit(final String fileName, final PersonIdent author, final Per
testRepo.commit(fileName, author, committer, message);
}

protected List<UserRemoteConfig> createRemoteRepositories(String relativeTargetDir) throws IOException {
protected List<UserRemoteConfig> createRemoteRepositories() throws IOException {
return testRepo.remoteConfigs();
}
}
79 changes: 74 additions & 5 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Expand Up @@ -33,17 +33,14 @@
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;

import org.jenkinsci.plugins.gitclient.CliGitAPIImpl;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.*;

/**
* Tests for {@link GitSCM}.
Expand Down Expand Up @@ -656,6 +653,78 @@ public void testEmailCommitter() throws Exception {
assertBuildStatusSuccess(build);
}

public void testFetchFromMultipleRepositories() throws Exception {
FreeStyleProject project = setupSimpleProject("master");

TestGitRepo secondTestRepo = new TestGitRepo("second", this, listener);
List<UserRemoteConfig> remotes = new ArrayList<UserRemoteConfig>();
remotes.addAll(testRepo.remoteConfigs());
remotes.addAll(secondTestRepo.remoteConfigs());

project.setScm(new GitSCM(
null,
remotes,
Collections.singletonList(new BranchSpec("master")),
null,
false, Collections.<SubmoduleConfig>emptyList(), false,
false, new DefaultBuildChooser(), null, null, true, null, null,
null, null, null, false, false, false, false, null, null, false,
null, false, false));

// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);

assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());

final String commitFile2 = "commitFile2";
secondTestRepo.commit(commitFile2, janeDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}

public void testMerge() throws Exception {
FreeStyleProject project = setupSimpleProject("master");

project.setScm(new GitSCM(
null,
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
new UserMergeOptions("origin", "integration"),
false, Collections.<SubmoduleConfig>emptyList(), false,
false, new DefaultBuildChooser(), null, null, true, null, null,
null, null, null, false, false, false, false, null, null, false,
null, false, false));

// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
testRepo.git.branch("integration");
build(project, Result.SUCCESS, "commitFileBase");

testRepo.git.checkout(null, "topic1");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
final FreeStyleBuild build1 = build(project, Result.SUCCESS, commitFile1);
assertTrue(build1.getWorkspace().child(commitFile1).exists());

assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());

testRepo.git.checkout(null, "topic2");
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}



private FreeStyleProject setupProject(String branchString, boolean authorOrCommitter) throws Exception {
return setupProject(branchString, authorOrCommitter, null);
Expand Down Expand Up @@ -690,7 +759,7 @@ private FreeStyleProject setupProject(String branchString, boolean authorOrCommi
FreeStyleProject project = createFreeStyleProject();
project.setScm(new GitSCM(
null,
createRemoteRepositories(relativeTargetDir),
createRemoteRepositories(),
Collections.singletonList(new BranchSpec(branchString)),
null,
false, Collections.<SubmoduleConfig>emptyList(), false,
Expand Down
1 change: 1 addition & 0 deletions src/test/java/hudson/plugins/git/TestGitRepo.java
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.PersonIdent;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
Expand Down

0 comments on commit 128f715

Please sign in to comment.