use of org.eclipse.jgit.transport.RemoteRefUpdate in project omegat by omegat-org.
the class GITRemoteRepository2 method commit.
@Override
public String commit(String[] onVersions, String comment) throws Exception {
if (onVersions != null) {
// check versions
String currentVersion = getCurrentVersion();
boolean hasVersion = false;
for (String v : onVersions) {
if (v != null) {
hasVersion = true;
break;
}
}
if (hasVersion) {
boolean found = false;
for (String v : onVersions) {
if (v != null) {
if (v.equals(currentVersion)) {
found = true;
break;
}
}
}
if (!found) {
throw new RuntimeException("Version changed from " + Arrays.toString(onVersions) + " to " + currentVersion);
}
}
}
if (indexIsEmpty(DirCache.read(repository))) {
// Nothing was actually added to the index so we can just return.
Log.logInfoRB("GIT_NO_CHANGES", "upload");
return null;
}
Log.logInfoRB("GIT_START", "upload");
try (Git git = new Git(repository)) {
CommitCommand commitCommand = git.commit();
commitCommand.setMessage(comment);
// read from git config
commitCommand.setSign(null);
RevCommit commit = commitCommand.call();
Iterable<PushResult> results = git.push().setTimeout(TIMEOUT).setRemote(REMOTE).add(getDefaultBranchName(repository)).call();
List<Status> statuses = StreamSupport.stream(results.spliterator(), false).flatMap(r -> r.getRemoteUpdates().stream()).map(RemoteRefUpdate::getStatus).collect(Collectors.toList());
String result;
if (statuses.isEmpty() || statuses.stream().anyMatch(s -> s != RemoteRefUpdate.Status.OK)) {
Log.logWarningRB("GIT_CONFLICT");
result = null;
} else {
result = commit.getName();
}
Log.logDebug(LOGGER, "GIT committed into new version {0} ", result);
Log.logInfoRB("GIT_FINISH", "upload");
return result;
} catch (Exception ex) {
Log.logErrorRB("GIT_ERROR", "upload", ex.getMessage());
if (ex instanceof TransportException) {
throw new NetworkException(ex);
} else {
throw ex;
}
}
}
use of org.eclipse.jgit.transport.RemoteRefUpdate in project gerrit by GerritCodeReview.
the class PushTagIT method pushTag.
private String pushTag(TagType tagType, String tagName, boolean newCommit, boolean force, Status expectedStatus) throws Exception {
if (force) {
testRepo.reset(initialHead);
}
commit(user.getIdent(), "subject");
boolean createTag = tagName == null;
tagName = MoreObjects.firstNonNull(tagName, "v1_" + System.nanoTime());
switch(tagType) {
case LIGHTWEIGHT:
break;
case ANNOTATED:
if (createTag) {
createAnnotatedTag(testRepo, tagName, user.getIdent());
} else {
updateAnnotatedTag(testRepo, tagName, user.getIdent());
}
break;
default:
throw new IllegalStateException("unexpected tag type: " + tagType);
}
if (!newCommit) {
grant(project, "refs/for/refs/heads/master", Permission.SUBMIT, false, REGISTERED_USERS);
pushHead(testRepo, "refs/for/master%submit");
}
String tagRef = tagRef(tagName);
PushResult r = tagType == LIGHTWEIGHT ? pushHead(testRepo, tagRef, false, force) : GitUtil.pushTag(testRepo, tagName, !createTag);
RemoteRefUpdate refUpdate = r.getRemoteUpdate(tagRef);
assertThat(refUpdate.getStatus()).named(tagType.name()).isEqualTo(expectedStatus);
return tagName;
}
use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.
the class PushOperationTest method testUpdateTrackingBranchIfSpecifiedInRemoteRefUpdate.
@Test
public void testUpdateTrackingBranchIfSpecifiedInRemoteRefUpdate() throws Exception {
// Commit on repository 2
IProject project = importProject(repository2, projectName);
RevCommit commit = repository2.addAndCommit(project, new File(workdir2, "test.txt"), "Commit in repository 2");
project.delete(false, false, null);
// We want to push from repository 2 to 1 (because repository 2 already
// has tracking set up)
URIish remote = repository1.getUri();
String trackingRef = "refs/remotes/origin/master";
RemoteRefUpdate update = new RemoteRefUpdate(repository2.getRepository(), "HEAD", "refs/heads/master", false, trackingRef, null);
PushOperationSpecification spec = new PushOperationSpecification();
spec.addURIRefUpdates(remote, Arrays.asList(update));
PushOperation push = new PushOperation(repository2.getRepository(), spec, false, 0);
push.run(null);
PushOperationResult result = push.getOperationResult();
PushResult pushResult = result.getPushResult(remote);
assertNotNull("Expected result to have tracking ref update", pushResult.getTrackingRefUpdate(trackingRef));
ObjectId trackingId = repository2.getRepository().resolve(trackingRef);
assertEquals("Expected tracking branch to be updated", commit.getId(), trackingId);
}
use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.
the class PushOperationTest method testPushWithReusedSpec.
/**
* We should get an {@link IllegalStateException} if the spec was re-used
*
* @throws Exception
*/
@Test
public void testPushWithReusedSpec() throws Exception {
PushOperationSpecification spec = new PushOperationSpecification();
// the remote is repo2
URIish remote = repository2.getUri();
// update master upon master
List<RemoteRefUpdate> refUpdates = new ArrayList<RemoteRefUpdate>();
RemoteRefUpdate update = new RemoteRefUpdate(repository1.getRepository(), "HEAD", "refs/heads/test", false, null, null);
refUpdates.add(update);
spec.addURIRefUpdates(remote, refUpdates);
PushOperation pop = new PushOperation(repository1.getRepository(), spec, false, 0);
pop.run(null);
pop = new PushOperation(repository1.getRepository(), spec, false, 0);
try {
pop.run(null);
fail("Expected Exception not thrown");
} catch (IllegalStateException e) {
// expected
}
}
use of org.eclipse.jgit.transport.RemoteRefUpdate in project egit by eclipse.
the class PushOperationResult method equals.
/**
* This implementation returns true if all following conditions are met:
* <ul>
* <li>both objects result have the same set successfully connected
* repositories (URIs) - unsuccessful connections are discarded, AND <li>
* remote ref updates must match for each successful connection in sense of
* equal remoteName, equal status and equal newObjectId value.</li>
* </ul>
*
* @see Object#equals(Object)
* @param obj
* other push operation result to compare to.
* @return true if object is equal to this one in terms of conditions
* described above, false otherwise.
*/
@Override
public boolean equals(final Object obj) {
if (obj == this)
return true;
if (!(obj instanceof PushOperationResult))
return false;
final PushOperationResult other = (PushOperationResult) obj;
// Check successful connections/URIs two-ways:
final Set<URIish> otherURIs = other.getURIs();
for (final URIish uri : getURIs()) {
if (isSuccessfulConnection(uri) && (!otherURIs.contains(uri) || !other.isSuccessfulConnection(uri)))
return false;
}
for (final URIish uri : other.getURIs()) {
if (other.isSuccessfulConnection(uri) && (!urisEntries.containsKey(uri) || !isSuccessfulConnection(uri)))
return false;
}
for (final URIish uri : getURIs()) {
if (!isSuccessfulConnection(uri))
continue;
final PushResult otherPushResult = other.getPushResult(uri);
for (final RemoteRefUpdate rru : getPushResult(uri).getRemoteUpdates()) {
final RemoteRefUpdate otherRru = otherPushResult.getRemoteUpdate(rru.getRemoteName());
if (otherRru == null)
return false;
if (otherRru.getStatus() != rru.getStatus() || otherRru.getNewObjectId() != rru.getNewObjectId())
return false;
}
}
return true;
}
Aggregations