Example usage for org.eclipse.jgit.dircache DirCache read

List of usage examples for org.eclipse.jgit.dircache DirCache read

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCache read.

Prototype

public static DirCache read(Repository repository) throws CorruptObjectException, IOException 

Source Link

Document

Create a new in-core index representation and read an index from disk.

Usage

From source file:edu.nju.cs.inform.jgit.unfinished.ListIndex.java

License:Apache License

public static void main(String[] args) throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // DirCache contains all files of the repository
        DirCache index = DirCache.read(repository);
        System.out.println("DirCache has " + index.getEntryCount() + " items");
        for (int i = 0; i < index.getEntryCount(); i++) {
            // the number after the AnyObjectId is the "stage", see the constants in DirCacheEntry
            System.out.println("Item " + i + ": " + index.getEntry(i));
        }/* w  w  w.j ava 2  s  .  c  om*/

        //
        System.out.println("Now printing staged items...");
        for (int i = 0; i < index.getEntryCount(); i++) {
            DirCacheEntry entry = index.getEntry(i);
            if (entry.getStage() != DirCacheEntry.STAGE_0) {
                System.out.println("Item " + i + ": " + entry);
            }
        }
    }
}

From source file:org.eclipse.egit.core.synchronize.GitSyncCache.java

License:Open Source License

private static void loadDataFromGit(GitSynchronizeData gsd, TreeFilter filter, GitSyncObjectCache repoCache) {
    Repository repo = gsd.getRepository();
    TreeWalk tw = new TreeWalk(repo);
    if (filter != null)
        tw.setFilter(filter);//  w ww.  j  a v a2  s  . c o  m

    try {
        // setup local tree
        FileTreeIterator fti = null;
        if (gsd.shouldIncludeLocal()) {
            fti = new FileTreeIterator(repo);
            tw.addTree(fti);
            if (filter != null)
                tw.setFilter(AndTreeFilter.create(filter, new NotIgnoredFilter(0)));
            else
                tw.setFilter(new NotIgnoredFilter(0));
        } else if (gsd.getSrcRevCommit() != null)
            tw.addTree(gsd.getSrcRevCommit().getTree());
        else
            tw.addTree(new EmptyTreeIterator());

        // setup base tree
        if (gsd.getCommonAncestorRev() != null)
            tw.addTree(gsd.getCommonAncestorRev().getTree());
        else
            tw.addTree(new EmptyTreeIterator());

        // setup remote tree
        if (gsd.getDstRevCommit() != null)
            tw.addTree(gsd.getDstRevCommit().getTree());
        else
            tw.addTree(new EmptyTreeIterator());

        DirCacheIterator dci = null;
        if (fti != null) {
            dci = new DirCacheIterator(DirCache.read(repo));
            tw.addTree(dci);
            fti.setDirCacheIterator(tw, 3);
        }
        List<ThreeWayDiffEntry> diffEntrys = ThreeWayDiffEntry.scan(tw);
        tw.release();

        for (ThreeWayDiffEntry diffEntry : diffEntrys)
            repoCache.addMember(diffEntry);
    } catch (Exception e) {
        Activator.logError(e.getMessage(), e);
    }
}

From source file:org.nbgit.client.CheckoutBuilder.java

License:Open Source License

/**
 * Set file to be checked out to a specific destination.
 *
 * @param file to be checked out.//from  w  w w .j  a v  a2 s .co m
 * @param destination where the file should be checked out.
 * @return the builder.
 * @throws FileNotFoundException if the file cannot be resolved.
 * @throws IOException if checking of file existance fails.
 */
public CheckoutBuilder file(File file, File destination) throws IOException, FileNotFoundException {
    String path = toPath(file);
    ObjectId blobId = null;
    int modeBits = 0;

    if (tree != null) {
        TreeEntry entry = tree.findBlobMember(path);
        if (entry != null) {
            blobId = entry.getId();
            modeBits = entry.getMode().getBits();
        }
    } else {
        if (index == null)
            index = DirCache.read(repository);
        DirCacheEntry entry = index.getEntry(path);
        if (entry != null) {
            blobId = entry.getObjectId();
            modeBits = entry.getRawMode();
        }
    }
    if (blobId == null)
        throw new FileNotFoundException(path);
    fileMappings.put(RevisionEntry.create(path, blobId, modeBits), destination);
    return this;
}

From source file:org.nbgit.junit.RepositoryTestCase.java

License:Open Source License

protected void compareIndexFiles() throws Exception {
    refDirCache(DirCache.read(repository));
    compareReferenceFiles();
}

From source file:org.nbgit.util.GitCommand.java

License:Open Source License

public static Map<File, StatusInfo> getAllStatus(File root, File dir) throws IOException {
    String relPath = getRelative(root, dir);

    Repository repo = Git.getInstance().getRepository(root);
    Map<File, StatusInfo> files = new HashMap<File, StatusInfo>();

    try {/*from   www .j a  v  a2 s  .c om*/
        IndexDiff index = new IndexDiff(repo);
        index.diff();

        put(index.getAdded(), relPath, files, root, StatusInfo.STATUS_VERSIONED_ADDEDLOCALLY);
        put(index.getRemoved(), relPath, files, root, StatusInfo.STATUS_VERSIONED_REMOVEDLOCALLY);
        put(index.getMissing(), relPath, files, root, StatusInfo.STATUS_VERSIONED_DELETEDLOCALLY);
        put(index.getChanged(), relPath, files, root, StatusInfo.STATUS_VERSIONED_MODIFIEDLOCALLY);
        put(index.getModified(), relPath, files, root, StatusInfo.STATUS_VERSIONED_MODIFIEDLOCALLY);

        final FileTreeIterator workTree = new FileTreeIterator(repo.getWorkDir());
        final TreeWalk walk = new TreeWalk(repo);
        DirCache cache = DirCache.read(repo);

        walk.reset(); // drop the first empty tree
        walk.setRecursive(true);
        walk.addTree(workTree);

        int share = SharabilityQuery.getSharability(dir);
        if (share == SharabilityQuery.NOT_SHARABLE) {
            return files;
        }
        while (walk.next()) {
            String path = walk.getPathString();

            if (relPath.length() > 0 && !path.startsWith(relPath)) {
                continue;
            }
            if (index.getAdded().contains(path) || index.getRemoved().contains(path)
                    || index.getMissing().contains(path) || index.getChanged().contains(path)
                    || index.getModified().contains(path)) {
                continue;
            }
            DirCacheEntry entry = cache.getEntry(path);
            File file = new File(root, path);

            int status;
            if (entry != null) {
                status = StatusInfo.STATUS_VERSIONED_UPTODATE;
            } else {
                if (share == SharabilityQuery.MIXED
                        && SharabilityQuery.getSharability(file) == SharabilityQuery.NOT_SHARABLE) {
                    continue;
                }
                status = StatusInfo.STATUS_NOTVERSIONED_NEWLOCALLY;
            }

            files.put(file, new StatusInfo(status, null, false));
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    return files;
}

From source file:org.nbgit.util.GitCommand.java

License:Open Source License

public static Map<File, StatusInfo> getInterestingStatus(File root, File dir) {
    String relPath = getRelative(root, dir);

    Repository repo = Git.getInstance().getRepository(root);
    IndexDiff index;/*from  ww w .  ja v a 2  s . co  m*/

    Map<File, StatusInfo> files = new HashMap<File, StatusInfo>();

    try {
        index = new IndexDiff(repo);
        index.diff();

        put(index.getAdded(), relPath, files, root, StatusInfo.STATUS_VERSIONED_ADDEDLOCALLY);
        put(index.getRemoved(), relPath, files, root, StatusInfo.STATUS_VERSIONED_REMOVEDLOCALLY);
        put(index.getMissing(), relPath, files, root, StatusInfo.STATUS_VERSIONED_DELETEDLOCALLY);
        put(index.getChanged(), relPath, files, root, StatusInfo.STATUS_VERSIONED_MODIFIEDLOCALLY);
        put(index.getModified(), relPath, files, root, StatusInfo.STATUS_VERSIONED_MODIFIEDLOCALLY);

        final FileTreeIterator workTree = new FileTreeIterator(repo.getWorkDir());
        final TreeWalk walk = new TreeWalk(repo);

        walk.reset(); // drop the first empty tree
        walk.setRecursive(true);
        walk.addTree(workTree);

        DirCache cache = DirCache.read(repo);

        while (walk.next()) {
            String path = walk.getPathString();

            if (relPath.length() > 0 && !path.startsWith(relPath)) {
                continue;
            }
            if (index.getAdded().contains(path) || index.getRemoved().contains(path)
                    || index.getMissing().contains(path) || index.getChanged().contains(path)
                    || index.getModified().contains(path)) {
                continue;
            }
            DirCacheEntry entry = cache.getEntry(path);
            if (entry != null) {
                continue;
            }
            int status = StatusInfo.STATUS_NOTVERSIONED_NEWLOCALLY;
            File file = new File(root, path);
            files.put(file, new StatusInfo(status, null, false));
        }

    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    return files;
}

From source file:org.omegat.core.team2.impl.GITRemoteRepository2.java

License:Open Source License

@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;/*from   ww  w .  jav  a  2s  .  co  m*/
                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)) {
        RevCommit commit = git.commit().setMessage(comment).call();
        Iterable<PushResult> results = git.push().setRemote(REMOTE).add(LOCAL_BRANCH).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;
        }
    }
}