Example usage for org.eclipse.jgit.lib Repository stripWorkDir

List of usage examples for org.eclipse.jgit.lib Repository stripWorkDir

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository stripWorkDir.

Prototype

@NonNull
public static String stripWorkDir(File workDir, File file) 

Source Link

Document

Strip work dir and return normalized repository path.

Usage

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

License:Open Source License

/**
 * Comparing two remote folders that have same path should return true
 *
 * @throws Exception//w  w w .  java2  s.  co m
 */
@Test
public void shouldReturnTrueWhenComparingRemoteVariantContainerWithContainer() throws Exception {
    // when
    GitResourceVariantComparator grvc = new GitResourceVariantComparator(null);

    // given
    File file1 = testRepo.createFile(iProject, "test1" + File.separator + "keep1");
    testRepo.track(file1);
    testRepo.addToIndex(testRepo.getIFile(iProject, file1));
    RevCommit commit = testRepo.commit("initial commit");

    String path1 = Repository.stripWorkDir(repo.getWorkTree(), new File(file1.getParent()));

    GitFolderResourceVariant base = new GitFolderResourceVariant(repo, null, commit.getTree(), path1);
    GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, null, commit.getTree(), path1);

    // then
    assertTrue(grvc.compare(base, remote));
}

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

License:Open Source License

/**
 * Comparing two remote files that have different git ObjectId should return false.
 *
 * @throws Exception//  ww  w .  j  a  va  2s.  com
 */
@Test
public void shouldReturnFalseWhenComparingRemoteVariantWithDifferentObjectId() throws Exception {
    // when
    GitResourceVariantComparator grvc = new GitResourceVariantComparator(null);

    // given
    File file = testRepo.createFile(iProject, "test-file");
    RevCommit baseCommit = testRepo.appendContentAndCommit(iProject, file, "a", "initial commit");
    RevCommit remoteCommit = testRepo.appendContentAndCommit(iProject, file, "bc", "second commit");

    String path = Repository.stripWorkDir(repo.getWorkTree(), file);
    GitBlobResourceVariant base = new GitBlobResourceVariant(repo, null, baseCommit.getTree(), path);

    GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, null, remoteCommit.getTree(), path);

    // then
    assertFalse(grvc.compare(base, remote));
}

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

License:Open Source License

/**
 * Comparing two remote files that have the same git ObjectId should return
 * true./*w ww  .  j ava2s  . c o  m*/
 * @throws Exception
 */
@Test
public void shouldReturnTrueWhenComparingRemoteVariant() throws Exception {
    // when
    GitResourceVariantComparator grvc = new GitResourceVariantComparator(null);

    // given
    File file = testRepo.createFile(iProject, "test-file");
    RevCommit commit = testRepo.appendContentAndCommit(iProject, file, "a", "initial commit");

    String path = Repository.stripWorkDir(repo.getWorkTree(), file);
    GitBlobResourceVariant base = new GitBlobResourceVariant(repo, null, commit.getTree(), path);

    GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, null, commit.getTree(), path);

    // then
    assertTrue(grvc.compare(base, remote));
}

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

License:Open Source License

private String getPath(final IResource resource, Repository repo) {
    return Repository.stripWorkDir(repo.getWorkTree(), resource.getLocation().toFile());
}

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

License:Open Source License

private ObjectId findFileId(RevCommit commit, IFile mainJava) throws Exception {
    TreeWalk tw = new TreeWalk(repo);
    tw.reset();/* w w  w  .ja  v a2  s.co m*/
    tw.setRecursive(true);
    String path = Repository.stripWorkDir(repo.getWorkTree(), mainJava.getLocation().toFile());
    tw.setFilter(PathFilter.create(path));
    int nth = tw.addTree(commit.getTree());
    tw.next();

    return tw.getObjectId(nth);
}

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

License:Open Source License

@Override
protected int calculateKind() throws TeamException {
    String localPath;//from   w w w.j a  v  a 2 s.c  om
    Repository repo = gsd.getRepository();
    if (getLocal().exists()) {
        File local = getLocal().getLocation().toFile();
        localPath = Repository.stripWorkDir(repo.getWorkTree(), local);
    } else if (getRemote() != null)
        localPath = ((GitResourceVariant) getRemote()).getFullPath().toString();
    else if (getBase() != null)
        localPath = ((GitResourceVariant) getBase()).getFullPath().toString();
    else
        // we cannot determinate local path therefore we cannot set proper
        // value for PathFilter, so we use standard calulateKind()
        // implementation
        return super.calculateKind();

    if (localPath.length() == 0)
        return IN_SYNC;

    TreeWalk tw = new TreeWalk(repo);
    tw.setFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.create(localPath)));
    tw.setRecursive(true);

    try {
        int srcNth = tw.addTree(gsd.getSrcRevCommit().getTree());
        int dstNth = tw.addTree(gsd.getDstRevCommit().getTree());

        if (tw.next()) {
            return calculateKindImpl(repo, tw, srcNth, dstNth);
        }
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
    }

    return IN_SYNC;
}

From source file:org.eclipse.egit.ui.internal.repository.tree.command.SubmoduleCommand.java

License:Open Source License

/**
 * Get submodule from selected nodes//from w w w.  j  ava2 s  . com
 * <p>
 * Keys with null values denote repositories where all submodules should be
 * used for the current command being executed
 *
 * @param nodes
 * @return non-null but possibly empty map of parent repository's to
 *         submodule paths
 */
protected Map<Repository, List<String>> getSubmodules(final List<RepositoryTreeNode<?>> nodes) {
    final Map<Repository, List<String>> repoPaths = new HashMap<Repository, List<String>>();
    for (RepositoryTreeNode<?> node : nodes) {
        if (node.getType() == RepositoryTreeNodeType.REPO) {
            Repository parent = node.getParent().getRepository();
            String path = Repository.stripWorkDir(parent.getWorkTree(), node.getRepository().getWorkTree());
            List<String> paths = repoPaths.get(parent);
            if (paths == null) {
                paths = new ArrayList<String>();
                repoPaths.put(parent, paths);
            }
            paths.add(path);
        }
    }
    for (RepositoryTreeNode<?> node : nodes)
        if (node.getType() == RepositoryTreeNodeType.SUBMODULES)
            // Clear paths so all submodules are updated
            repoPaths.put(node.getParent().getRepository(), null);
    return repoPaths;
}

From source file:org.eclipse.egit.ui.internal.synchronize.GitModelSynchronizeParticipant.java

License:Open Source License

private ICompareInput getFileFromGit(GitSynchronizeData gsd, IPath location) {
    Repository repo = gsd.getRepository();
    File workTree = repo.getWorkTree();
    String repoRelativeLocation = Repository.stripWorkDir(workTree, location.toFile());

    TreeWalk tw = new TreeWalk(repo);
    tw.setRecursive(true);//  www.  ja v  a2 s.  c o m
    tw.setFilter(PathFilter.create(repoRelativeLocation.toString()));
    RevCommit baseCommit = gsd.getSrcRevCommit();
    RevCommit remoteCommit = gsd.getDstRevCommit();

    try {
        int baseNth = tw.addTree(baseCommit.getTree());
        int remoteNth = tw.addTree(remoteCommit.getTree());

        if (tw.next()) {
            ComparisonDataSource baseData = new ComparisonDataSource(baseCommit, tw.getObjectId(baseNth));
            ComparisonDataSource remoteData = new ComparisonDataSource(remoteCommit, tw.getObjectId(remoteNth));
            return new GitCompareInput(repo, baseData, baseData, remoteData, repoRelativeLocation);
        }
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
    }

    return null;
}

From source file:org.eclipse.egit.ui.internal.synchronize.model.GitModelBlob.java

License:Open Source License

/**
 *
 * @param parent//from  w ww  .j  a  v  a 2 s .c om
 *            parent of this object
 * @param commit
 *            remote commit
 * @param ancestorId
 *            common ancestor id
 * @param baseId
 *            id of base object variant
 * @param remoteId
 *            id of remote object variants
 * @param name
 *            human readable blob name (file name)
 * @throws IOException
 */
public GitModelBlob(GitModelObjectContainer parent, RevCommit commit, ObjectId ancestorId, ObjectId baseId,
        ObjectId remoteId, String name) throws IOException {
    // only direction is important for us, therefore we mask rest of bits in kind
    super(parent, commit, parent.getKind() & (LEFT | RIGHT));
    this.name = name;
    this.baseId = baseId;
    this.remoteId = remoteId;
    this.ancestorId = ancestorId;
    location = getParent().getLocation().append(name);
    gitPath = Repository.stripWorkDir(getRepository().getWorkTree(), getLocation().toFile());
}

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

License:Open Source License

protected String toPath(File file) {
    return Repository.stripWorkDir(repository.getWorkDir(), file);
}