Java Code Examples for org.eclipse.jgit.lib.Repository#newObjectInserter()

The following examples show how to use org.eclipse.jgit.lib.Repository#newObjectInserter() . 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: GitNoteWriter.java    From git-appraise-eclipse with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Private ctor. Use the static factory methods.
 */
private GitNoteWriter(String reviewHash, final Repository repo, String ref, PersonIdent author) {
  this.ref = ref;
  this.repo = repo;
  this.author = author;

  revWalk = new RevWalk(repo);
  inserter = repo.newObjectInserter();
  reader = repo.newObjectReader();

  try {
    ObjectId reviewRefObjId = repo.resolve(reviewHash);
    this.reviewCommit = revWalk.parseCommit(reviewRefObjId);
  } catch (Exception e) {
    logger.log(Level.SEVERE, "Failed to init note writer for commit " + reviewHash, e);
    throw new RuntimeException(e);
  }
}
 
Example 2
Source File: Merger.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Create a new merge instance for a repository.
 *
 * @param local
 *            the repository this merger will read and write data on.
 */
protected Merger(Repository local) {
	if (local == null) {
		throw new NullPointerException(JGitText.get().repositoryIsRequired);
	}
	db = local;
	inserter = local.newObjectInserter();
	reader = inserter.newReader();
	walk = new RevWalk(reader);
}
 
Example 3
Source File: GitUtils.java    From onedev with MIT License 5 votes vote down vote up
@Nullable
  public static ObjectId rebase(Repository repository, ObjectId source, ObjectId target, PersonIdent committer) {
  	try (	RevWalk revWalk = new RevWalk(repository);
  			ObjectInserter inserter = repository.newObjectInserter();) {
  		RevCommit sourceCommit = revWalk.parseCommit(source);
  		RevCommit targetCommit = revWalk.parseCommit(target);
  		revWalk.setRevFilter(RevFilter.NO_MERGES);
  		List<RevCommit> commits = RevWalkUtils.find(revWalk, sourceCommit, targetCommit);
  		Collections.reverse(commits);
  		RevCommit headCommit = targetCommit;
  		for (RevCommit commit: commits) {
      		ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE.newMerger(repository, true);
      		merger.setBase(commit.getParent(0));
      		if (merger.merge(headCommit, commit)) {
			if (!headCommit.getTree().getId().equals(merger.getResultTreeId())) {
				if (!commit.getTree().getId().equals(merger.getResultTreeId()) 
						|| !commit.getParent(0).equals(headCommit)) {
			        CommitBuilder commitBuilder = new CommitBuilder();
			        commitBuilder.setAuthor(commit.getAuthorIdent());
			        commitBuilder.setCommitter(committer);
			        commitBuilder.setParentId(headCommit);
			        commitBuilder.setMessage(commit.getFullMessage());
			        commitBuilder.setTreeId(merger.getResultTreeId());
			        headCommit = revWalk.parseCommit(inserter.insert(commitBuilder));
				} else {
					headCommit = commit;
				}
			}
      		} else {
      			return null;
      		}
  		}
  		inserter.flush();
  		return headCommit.copy();
  	} catch (IOException e) {
  		throw new RuntimeException(e);
}
  }
 
Example 4
Source File: GitUtils.java    From onedev with MIT License 5 votes vote down vote up
@Nullable
  public static ObjectId merge(Repository repository, ObjectId targetCommitId, ObjectId sourceCommitId, 
  		boolean squash, PersonIdent committer, PersonIdent author, String commitMessage, 
  		boolean useOursOnConflict) {
  	boolean prevUseOursOnConflict = UseOursOnConflict.get();
  	UseOursOnConflict.set(useOursOnConflict);
  	try (	RevWalk revWalk = new RevWalk(repository);
  			ObjectInserter inserter = repository.newObjectInserter();) {
  		RevCommit sourceCommit = revWalk.parseCommit(sourceCommitId);
  		RevCommit targetCommit = revWalk.parseCommit(targetCommitId);
  		Merger merger = MergeStrategy.RECURSIVE.newMerger(repository, true);
  		if (merger.merge(targetCommit, sourceCommit)) {
        CommitBuilder mergedCommit = new CommitBuilder();
        mergedCommit.setAuthor(author);
        mergedCommit.setCommitter(committer);
        if (squash)
        	mergedCommit.setParentId(targetCommit);
        else
        	mergedCommit.setParentIds(targetCommit, sourceCommit);
        mergedCommit.setMessage(commitMessage);
        mergedCommit.setTreeId(merger.getResultTreeId());
        ObjectId mergedCommitId = inserter.insert(mergedCommit);
        inserter.flush();
        return mergedCommitId;
  		} else {
  			return null;
  		}
  	} catch (IOException e) {
  		throw new RuntimeException(e);
} finally {
	UseOursOnConflict.set(prevUseOursOnConflict);
}
  }
 
Example 5
Source File: ObjectSnapshot.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Nonnull
public ObjectId save(Repository repo) throws IOException {
  try(ObjectInserter inserter = repo.newObjectInserter()) {
    ObjectId ret = save(inserter);
    inserter.flush();
    return ret;
  }
}
 
Example 6
Source File: ObjectSnapshot.java    From ParallelGit with Apache License 2.0 5 votes vote down vote up
@Nonnull
public ObjectId save(Repository repo) throws IOException {
  try(ObjectInserter inserter = repo.newObjectInserter()) {
    ObjectId ret = save(inserter);
    inserter.flush();
    return ret;
  }
}