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

The following examples show how to use org.eclipse.jgit.lib.RefUpdate#update() . 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: GitNoteWriter.java    From git-appraise-eclipse with Eclipse Public License 1.0 5 votes vote down vote up
private void updateRef() throws IOException, InterruptedException, RuntimeException,
                                MissingObjectException, IncorrectObjectTypeException,
                                CorruptObjectException {
  if (baseCommit != null && oursCommit.getTree().equals(baseCommit.getTree())) {
    // If the trees are identical, there is no change in the notes.
    // Avoid saving this commit as it has no new information.
    return;
  }

  int remainingLockFailureCalls = JgitUtils.MAX_LOCK_FAILURE_CALLS;
  RefUpdate refUpdate = JgitUtils.updateRef(repo, oursCommit, baseCommit, ref);

  for (;;) {
    Result result = refUpdate.update();

    if (result == Result.LOCK_FAILURE) {
      if (--remainingLockFailureCalls > 0) {
        Thread.sleep(JgitUtils.SLEEP_ON_LOCK_FAILURE_MS);
      } else {
        throw new RuntimeException("Failed to lock the ref: " + ref);
      }

    } else if (result == Result.REJECTED) {
      RevCommit theirsCommit = revWalk.parseCommit(refUpdate.getOldObjectId());
      NoteMap theirs = NoteMap.read(revWalk.getObjectReader(), theirsCommit);
      NoteMapMerger merger = new NoteMapMerger(repo);
      NoteMap merged = merger.merge(base, ours, theirs);
      RevCommit mergeCommit =
          createCommit(merged, author, "Merged note records\n", theirsCommit, oursCommit);
      refUpdate = JgitUtils.updateRef(repo, mergeCommit, theirsCommit, ref);
      remainingLockFailureCalls = JgitUtils.MAX_LOCK_FAILURE_CALLS;

    } else if (result == Result.IO_FAILURE) {
      throw new RuntimeException("Couldn't create notes because of IO_FAILURE");
    } else {
      break;
    }
  }
}
 
Example 4
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;
	}