Java Code Examples for org.eclipse.jgit.lib.RefUpdate#setNewObjectId()

The following examples show how to use org.eclipse.jgit.lib.RefUpdate#setNewObjectId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitRepository.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
static void doRefUpdate(org.eclipse.jgit.lib.Repository jGitRepository, RevWalk revWalk,
                        String ref, ObjectId commitId) throws IOException {

    if (ref.startsWith(Constants.R_TAGS)) {
        final Ref oldRef = jGitRepository.exactRef(ref);
        if (oldRef != null) {
            throw new StorageException("tag ref exists already: " + ref);
        }
    }

    final RefUpdate refUpdate = jGitRepository.updateRef(ref);
    refUpdate.setNewObjectId(commitId);

    final Result res = refUpdate.update(revWalk);
    switch (res) {
        case NEW:
        case FAST_FORWARD:
            // Expected
            break;
        default:
            throw new StorageException("unexpected refUpdate state: " + res);
    }
}
 
Example 2
Source File: GitPushEmbedded.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean push(@NotNull Repository repository, @NotNull ObjectId ReceiveId, @NotNull String branch, @NotNull User userInfo) throws SVNException, IOException {
  final RefUpdate refUpdate = repository.updateRef(branch);
  refUpdate.getOldObjectId();
  refUpdate.setNewObjectId(ReceiveId);
  runReceiveHook(repository, refUpdate, SVNErrorCode.REPOS_HOOK_FAILURE, "pre-receive", userInfo);
  runUpdateHook(repository, refUpdate, "update", userInfo);
  final RefUpdate.Result result = refUpdate.update();
  switch (result) {
    case REJECTED:
    case LOCK_FAILURE:
      return false;
    case NEW:
    case FAST_FORWARD:
      runReceiveHook(repository, refUpdate, SVNErrorCode.REPOS_POST_COMMIT_HOOK_FAILED, "post-receive", userInfo);
      return true;
    default:
      log.error("Unexpected push error: {}", result);
      throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, result.name()));
  }
}
 
Example 3
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
private void doCheckoutWithResetAndRetryAndCleanBranch(String branch, String ref) throws GitException {
    try (Repository repo = getRepository()) {
        RefUpdate refUpdate = repo.updateRef(R_HEADS + branch);
        refUpdate.setNewObjectId(repo.resolve(ref));
        switch (refUpdate.forceUpdate()) {
        case NOT_ATTEMPTED:
        case LOCK_FAILURE:
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
        case IO_FAILURE:
        case RENAMED:
            throw new GitException("Could not update " + branch + " to " + ref);
        }

        doCheckoutWithResetAndRetry(branch);
    } catch (IOException e) {
        throw new GitException("Could not checkout " + branch +  " with start point " + ref, e);
    }
}
 
Example 4
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
   @Override
   public void ref(String refName) throws GitException, InterruptedException {
refName = refName.replace(' ', '_');
try (Repository repo = getRepository()) {
    RefUpdate refUpdate = repo.updateRef(refName);
    refUpdate.setNewObjectId(repo.exactRef(Constants.HEAD).getObjectId());
    switch (refUpdate.forceUpdate()) {
    case NOT_ATTEMPTED:
    case LOCK_FAILURE:
    case REJECTED:
    case REJECTED_CURRENT_BRANCH:
    case IO_FAILURE:
    case RENAMED:
	throw new GitException("Could not update " + refName + " to HEAD");
    }
} catch (IOException e) {
    throw new GitException("Could not update " + refName + " to HEAD", e);
}
   }
 
Example 5
Source File: JGitAPIImpl.java    From git-client-plugin with MIT License 6 votes vote down vote up
/** {@inheritDoc} */
   @Override
   public void deleteRef(String refName) throws GitException, InterruptedException {
refName = refName.replace(' ', '_');
try (Repository repo = getRepository()) {
    RefUpdate refUpdate = repo.updateRef(refName);
    // Required, even though this is a forced delete.
    refUpdate.setNewObjectId(repo.exactRef(Constants.HEAD).getObjectId());
    refUpdate.setForceUpdate(true);
    switch (refUpdate.delete()) {
    case NOT_ATTEMPTED:
    case LOCK_FAILURE:
    case REJECTED:
    case REJECTED_CURRENT_BRANCH:
    case IO_FAILURE:
    case RENAMED:
	throw new GitException("Could not delete " + refName);
    }
} catch (IOException e) {
    throw new GitException("Could not delete " + refName, e);
}
   }
 
Example 6
Source File: MergePreview.java    From onedev with MIT License 5 votes vote down vote up
public void syncRef(PullRequest request) {
	Project project = request.getTargetProject();
	ObjectId mergedId = getMergeCommitHash()!=null? ObjectId.fromString(getMergeCommitHash()): null;
	RefUpdate refUpdate = GitUtils.getRefUpdate(project.getRepository(), request.getMergeRef());
	if (mergedId != null && !mergedId.equals((project.getObjectId(request.getMergeRef(), false)))) {
		refUpdate.setNewObjectId(mergedId);
		GitUtils.updateRef(refUpdate);
	} else if (mergeCommitHash == null && project.getObjectId(request.getMergeRef(), false) != null) {
		GitUtils.deleteRef(refUpdate);
	}		
}
 
Example 7
Source File: AbstractGitTest.java    From onedev with MIT License 5 votes vote down vote up
protected void updateRef(String refName, String newValue, @Nullable String oldValue) {
	try {
		RefUpdate update = git.getRepository().updateRef(refName);
		update.setNewObjectId(git.getRepository().resolve(newValue));
		if (oldValue != null)
			update.setExpectedOldObjectId(git.getRepository().resolve(oldValue));
		update.setRefLogIdent(user);
		update.setRefLogMessage("update ref", false);
		GitUtils.updateRef(update);
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: JgitUtils.java    From git-appraise-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Updates the given ref to the new commit.
 */
public static RefUpdate updateRef(Repository repo, ObjectId newObjectId,
    ObjectId expectedOldObjectId, String refName) throws IOException {
  RefUpdate refUpdate = repo.updateRef(refName);
  refUpdate.setNewObjectId(newObjectId);
  if (expectedOldObjectId == null) {
    refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
  } else {
    refUpdate.setExpectedOldObjectId(expectedOldObjectId);
  }
  return refUpdate;
}
 
Example 9
Source File: RepoMerger.java    From git-merge-repos with Apache License 2.0 5 votes vote down vote up
private MergedRef mergeBranch(String branch) throws IOException {

		Map<SubtreeConfig, ObjectId> resolvedRefs = resolveRefs(
				"refs/heads/original/", branch);

		Map<SubtreeConfig, RevCommit> parentCommits = new LinkedHashMap<>();
		try (RevWalk revWalk = new RevWalk(repository)) {
			for (SubtreeConfig config : subtreeConfigs) {
				ObjectId objectId = resolvedRefs.get(config);
				if (objectId != null) {
					RevCommit commit = revWalk.parseCommit(objectId);
					parentCommits.put(config, commit);
				}
			}
		}

		MergedRef mergedRef = getMergedRef("branch", branch, parentCommits.keySet());

		ObjectId mergeCommit = new SubtreeMerger(repository).createMergeCommit(parentCommits,
				mergedRef.getMessage());

		RefUpdate refUpdate = repository.updateRef("refs/heads/" + branch);
		refUpdate.setNewObjectId(mergeCommit);
		refUpdate.update();

		return mergedRef;
	}
 
Example 10
Source File: PullRequest.java    From onedev with MIT License 4 votes vote down vote up
public void writeBaseRef() {
	RefUpdate refUpdate = GitUtils.getRefUpdate(getTargetProject().getRepository(), getBaseRef());
	refUpdate.setNewObjectId(ObjectId.fromString(getBaseCommitHash()));
	GitUtils.updateRef(refUpdate);
}
 
Example 11
Source File: PullRequest.java    From onedev with MIT License 4 votes vote down vote up
public void writeHeadRef() {
	RefUpdate refUpdate = GitUtils.getRefUpdate(getTargetProject().getRepository(), getHeadRef());
	refUpdate.setNewObjectId(ObjectId.fromString(getLatestUpdate().getHeadCommitHash()));
	GitUtils.updateRef(refUpdate);
}
 
Example 12
Source File: PullRequest.java    From onedev with MIT License 4 votes vote down vote up
public void writeMergeRef() {
	RefUpdate refUpdate = GitUtils.getRefUpdate(getTargetProject().getRepository(), getMergeRef());
	refUpdate.setNewObjectId(ObjectId.fromString(getLastMergePreview().getMergeCommitHash()));
	GitUtils.updateRef(refUpdate);
}