Skip to content

Commit d77b92e

Browse files
Mattias Anderssonmichael-o
Mattias Andersson
authored andcommitted
[SCM-1022] jgit push failure is not failing the build
This closes #203
1 parent 6991a0b commit d77b92e

File tree

2 files changed

+86
-1
lines changed
  • maven-scm-providers/maven-scm-providers-git

2 files changed

+86
-1
lines changed

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/checkin/GitCheckInCommandTckTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@
1919
package org.apache.maven.scm.provider.git.command.checkin;
2020

2121
import java.io.File;
22+
import java.io.IOException;
2223

24+
import org.apache.maven.scm.PlexusJUnit4TestSupport;
25+
import org.apache.maven.scm.ScmFileSet;
26+
import org.apache.maven.scm.command.checkin.CheckInScmResult;
2327
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
2428
import org.apache.maven.scm.provider.git.GitScmTestUtils;
2529
import org.apache.maven.scm.repository.ScmRepository;
2630
import org.apache.maven.scm.tck.command.checkin.CheckInCommandTckTest;
31+
import org.codehaus.plexus.util.FileUtils;
32+
import org.junit.Test;
33+
34+
import static org.junit.Assert.assertFalse;
2735

2836
/**
2937
* @author <a href="mailto:[email protected]">Mark Struberg</a>
@@ -44,4 +52,61 @@ protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository reposi
4452
GitScmTestUtils.setDefaulGitConfig(workingDirectory);
4553
}
4654
}
55+
56+
@Test
57+
public void testUpToDatePush() throws Exception {
58+
File checkedOutRepo = getWorkingCopy();
59+
60+
ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
61+
checkoutRepoInto(checkedOutRepo, scmRepository);
62+
63+
// Add a default user to the config
64+
GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);
65+
66+
CheckInScmResult result =
67+
getScmManager().checkIn(scmRepository, new ScmFileSet(checkedOutRepo), "No change commit message");
68+
69+
assertResultIsSuccess(result);
70+
}
71+
72+
@Test
73+
public void testRejectedNonFastForwardPush() throws Exception {
74+
File blockingRepo = PlexusJUnit4TestSupport.getTestFile("target/scm-test/blocking-repo");
75+
File rejectedRepo = PlexusJUnit4TestSupport.getTestFile("target/scm-test/rejected-repo");
76+
77+
ScmRepository scmRepository = getScmManager().makeScmRepository(getScmUrl());
78+
checkoutRepoInto(rejectedRepo, scmRepository);
79+
checkoutRepoInto(blockingRepo, scmRepository);
80+
81+
// Add a default user to the config
82+
GitScmTestUtils.setDefaulGitConfig(rejectedRepo);
83+
GitScmTestUtils.setDefaulGitConfig(blockingRepo);
84+
85+
ScmFileSet blockingFileSet = createWorkspaceChange(rejectedRepo);
86+
87+
CheckInScmResult blockingResult = getScmManager().checkIn(scmRepository, blockingFileSet, "Blocking commit");
88+
assertResultIsSuccess(blockingResult);
89+
90+
ScmFileSet rejectedFileSet = createWorkspaceChange(blockingRepo);
91+
92+
CheckInScmResult checkInScmResult = getScmManager().checkIn(scmRepository, rejectedFileSet, "Rejected commit");
93+
assertFalse(
94+
"check-in should have been rejected since fast forward was not possible", checkInScmResult.isSuccess());
95+
}
96+
97+
private CheckOutScmResult checkoutRepoInto(File workingCopy, ScmRepository scmRepository) throws Exception {
98+
FileUtils.deleteDirectory(workingCopy);
99+
workingCopy.mkdir();
100+
101+
CheckOutScmResult result = getScmManager().checkOut(scmRepository, new ScmFileSet(workingCopy), null);
102+
103+
assertResultIsSuccess(result);
104+
return result;
105+
}
106+
107+
private ScmFileSet createWorkspaceChange(File repo) throws IOException {
108+
File beerFile = new File(repo.getAbsolutePath(), "beer.xml");
109+
FileUtils.fileWrite(beerFile.getAbsolutePath(), "1 litre");
110+
return new ScmFileSet(repo, beerFile.getName());
111+
}
47112
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/checkin/JGitCheckInCommand.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import org.eclipse.jgit.lib.Constants;
4444
import org.eclipse.jgit.lib.UserConfig;
4545
import org.eclipse.jgit.revwalk.RevCommit;
46+
import org.eclipse.jgit.transport.PushResult;
4647
import org.eclipse.jgit.transport.RefSpec;
48+
import org.eclipse.jgit.transport.RemoteRefUpdate;
4749

4850
/**
4951
* This provider uses the following strategy to discover the committer and author name/mail for a commit:
@@ -139,7 +141,19 @@ protected CheckInScmResult executeCheckInCommand(
139141
}
140142
RefSpec refSpec = new RefSpec(Constants.R_HEADS + branch + ":" + Constants.R_HEADS + branch);
141143
logger.info("push changes to remote... " + refSpec);
142-
JGitUtils.push(git, (GitScmProviderRepository) repo, refSpec);
144+
Iterable<PushResult> pushResultList = JGitUtils.push(git, (GitScmProviderRepository) repo, refSpec);
145+
146+
for (PushResult pushResult : pushResultList) {
147+
for (RemoteRefUpdate remoteRefUpdate : pushResult.getRemoteUpdates()) {
148+
if (!isSuccessStatus(remoteRefUpdate.getStatus())) {
149+
return new CheckInScmResult(
150+
"JGit checkin",
151+
"The git-push command failed, with status: " + remoteRefUpdate.getStatus(),
152+
remoteRefUpdate.getMessage(),
153+
false);
154+
}
155+
}
156+
}
143157
}
144158

145159
return new CheckInScmResult("JGit checkin", checkedInFiles);
@@ -150,7 +164,13 @@ protected CheckInScmResult executeCheckInCommand(
150164
}
151165
}
152166

167+
private boolean isSuccessStatus(RemoteRefUpdate.Status remoteRefUpdateStatus) {
168+
return remoteRefUpdateStatus == RemoteRefUpdate.Status.OK
169+
|| remoteRefUpdateStatus == RemoteRefUpdate.Status.UP_TO_DATE;
170+
}
171+
153172
private static final class UserInfo {
173+
154174
final String name;
155175

156176
final String email;

0 commit comments

Comments
 (0)