Java Code Examples for org.eclipse.jgit.dircache.DirCacheBuilder#commit()

The following examples show how to use org.eclipse.jgit.dircache.DirCacheBuilder#commit() . 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: CheckoutIndexCommand.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void run() throws GitException {
    Repository repository = getRepository();
    DirCache cache = null;
    try {
        // cache must be locked because checkout index may modify its entries
        cache = repository.lockDirCache();
        DirCacheBuilder builder = cache.builder();
        if (cache.getEntryCount() > 0) {
            builder.keep(0, cache.getEntryCount());
        }
        builder.finish();
        new CheckoutIndex(repository, cache, roots, recursively, listener, monitor, true).checkout();
        // cache must be saved to disk because checkout index may modify its entries
        builder.commit();
    } catch (IOException ex) {
        throw new GitException(ex);
    } finally {
        if (cache != null) {
            cache.unlock();
        }
    }
}
 
Example 2
Source File: CheckoutRevisionCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void mergeConflicts (List<String> conflicts, DirCache cache) throws GitException {
    DirCacheBuilder builder = cache.builder();
    DirCacheBuildIterator dci = new DirCacheBuildIterator(builder);
    ObjectDatabase od = null;
    DiffAlgorithm.SupportedAlgorithm diffAlg = getRepository().getConfig().getEnum(
                    ConfigConstants.CONFIG_DIFF_SECTION, null,
                    ConfigConstants.CONFIG_KEY_ALGORITHM,
                    DiffAlgorithm.SupportedAlgorithm.HISTOGRAM);
    MergeAlgorithm merger = new MergeAlgorithm(DiffAlgorithm.getAlgorithm(diffAlg));
    try (TreeWalk walk = new TreeWalk(getRepository());) {
        od = getRepository().getObjectDatabase();
        walk.addTree(dci);
        walk.setFilter(PathFilterGroup.create(Utils.getPathFilters(conflicts)));
        String lastPath = null;
        DirCacheEntry[] entries = new DirCacheEntry[3];
        walk.setRecursive(true);
        while (walk.next()) {
            DirCacheEntry e = walk.getTree(0, DirCacheIterator.class).getDirCacheEntry();
            String path = e.getPathString();
            if (lastPath != null && !lastPath.equals(path)) {
                resolveEntries(merger, lastPath, entries, od, builder);
            }
            if (e.getStage() == 0) {
                DirCacheIterator c = walk.getTree(0, DirCacheIterator.class);
                builder.add(c.getDirCacheEntry());
            } else {
                entries[e.getStage() - 1] = e;
                lastPath = path;
            }
        }
        resolveEntries(merger, lastPath, entries, od, builder);
        builder.commit();
    } catch (IOException ex) {
        throw new GitException(ex);
    } finally {
        if (od != null) {
            od.close();
        }
    }
}
 
Example 3
Source File: ResetTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testResetConflict () throws Exception {
    File file = new File(workDir, "file");
    write(file, "init");
    File[] files = new File[] { file };
    add(files);
    commit(files);

    DirCache index = repository.lockDirCache();
    DirCacheBuilder builder = index.builder();
    DirCacheEntry e = index.getEntry(file.getName());
    DirCacheEntry e1 = new DirCacheEntry(file.getName(), 1);
    e1.setCreationTime(e.getCreationTime());
    e1.setFileMode(e.getFileMode());
    e1.setLastModified(e.getLastModified());
    e1.setLength(e.getLength());
    e1.setObjectId(e.getObjectId());
    builder.add(e1);
    builder.finish();
    builder.commit();
    
    GitClient client = getClient(workDir);
    Map<File, GitStatus> status = client.getStatus(files, NULL_PROGRESS_MONITOR);
    assertTrue(status.get(file).isConflict());
    assertEquals(GitConflictDescriptor.Type.BOTH_DELETED, status.get(file).getConflictDescriptor().getType());
    
    client.reset(files, "HEAD", true, NULL_PROGRESS_MONITOR);
    status = client.getStatus(files, NULL_PROGRESS_MONITOR);
    assertFalse(status.get(file).isConflict());
}