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

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

Introduction

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

Prototype


public File getDirectory() 

Source Link

Document

Get local metadata directory

Usage

From source file:org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.java

License:Open Source License

private RebaseInteractivePlan(Repository repo) {
    this.repositoryRef = new WeakReference<>(repo);
    this.myGitDir = repo.getDirectory().getAbsoluteFile();
    reparsePlan(repo);/*from  w  ww  .  ja v a 2s  .c om*/
    registerIndexDiffChangeListener(repo);
    registerRefChangedListener();
}

From source file:org.eclipse.egit.core.op.AssumeUnchangedOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor;//from  w  w w.  ja  v a  2  s.  c o  m
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    caches.clear();
    mappings.clear();

    monitor.beginTask(CoreText.AssumeUnchangedOperation_adding, rsrcList.size() * 200);
    try {
        for (IResource resource : rsrcList) {
            assumeValid(resource);
            monitor.worked(200);
        }

        for (Map.Entry<Repository, DirCache> e : caches.entrySet()) {
            final Repository db = e.getKey();
            final DirCache editor = e.getValue();
            monitor.setTaskName(NLS.bind(CoreText.AssumeUnchangedOperation_writingIndex, db.getDirectory()));
            editor.write();
            editor.commit();
        }
    } catch (RuntimeException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } catch (IOException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } finally {
        for (final RepositoryMapping rm : mappings.keySet())
            rm.fireRepositoryChanged();
        caches.clear();
        mappings.clear();
        monitor.done();
    }
}

From source file:org.eclipse.egit.core.op.UntrackOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor;/*from   ww  w  .jav a 2  s  . c o  m*/
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    edits.clear();
    mappings.clear();

    monitor.beginTask(CoreText.UntrackOperation_adding, rsrcList.size() * 200);
    try {
        for (IResource obj : rsrcList) {
            remove(obj);
            monitor.worked(200);
        }

        for (Map.Entry<Repository, DirCacheEditor> e : edits.entrySet()) {
            final Repository db = e.getKey();
            final DirCacheEditor editor = e.getValue();
            monitor.setTaskName(NLS.bind(CoreText.UntrackOperation_writingIndex, db.getDirectory()));
            editor.commit();
        }
    } catch (RuntimeException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } catch (IOException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } finally {
        for (final RepositoryMapping rm : mappings.keySet())
            rm.fireRepositoryChanged();
        edits.clear();
        mappings.clear();
        monitor.done();
    }
}

From source file:org.eclipse.egit.core.project.GitProjectData.java

License:Open Source License

/**
 * Hide our private parts from the navigators other browsers.
 *
 * @throws CoreException//from  w  ww  . j  a  v  a  2 s.  com
 */
public void markTeamPrivateResources() throws CoreException {
    for (final Object rmObj : mappings) {
        final RepositoryMapping rm = (RepositoryMapping) rmObj;
        final IContainer c = rm.getContainer();
        if (c == null)
            continue; // Not fully mapped yet?

        final IResource dotGit = c.findMember(Constants.DOT_GIT);
        if (dotGit != null) {
            try {
                final Repository r = rm.getRepository();
                final File dotGitDir = dotGit.getLocation().toFile().getCanonicalFile();
                if (dotGitDir.equals(r.getDirectory())) {
                    trace("teamPrivate " + dotGit); //$NON-NLS-1$
                    dotGit.setTeamPrivateMember(true);
                }
            } catch (IOException err) {
                throw new CoreException(Activator.error(CoreText.Error_CanonicalFile, err));
            }
        }
    }
}

From source file:org.eclipse.egit.core.RepositoryUtil.java

License:Open Source License

/**
 * Tries to map a commit to a symbolic reference.
 * <p>/*  w w w  .jav a  2 s . co  m*/
 * This value will be cached for the given commit ID unless refresh is
 * specified. The return value will be the full name, e.g.
 * "refs/remotes/someBranch", "refs/tags/v.1.0"
 * <p>
 * Since this mapping is not unique, the following precedence rules are
 * used:
 * <ul>
 * <li>Tags take precedence over branches</li>
 * <li>Local branches take preference over remote branches</li>
 * <li>Newer references take precedence over older ones where time stamps
 * are available</li>
 * <li>If there are still ambiguities, the reference name with the highest
 * lexicographic value will be returned</li>
 * </ul>
 *
 * @param repository
 *            the {@link Repository}
 * @param commitId
 *            a commit
 * @param refresh
 *            if true, the cache will be invalidated
 * @return the symbolic reference, or <code>null</code> if no such reference
 *         can be found
 */
public String mapCommitToRef(Repository repository, String commitId, boolean refresh) {
    synchronized (commitMappingCache) {

        if (!ObjectId.isId(commitId)) {
            return null;
        }

        Map<String, String> cacheEntry = commitMappingCache.get(repository.getDirectory().toString());
        if (!refresh && cacheEntry != null && cacheEntry.containsKey(commitId)) {
            // this may be null in fact
            return cacheEntry.get(commitId);
        }
        if (cacheEntry == null) {
            cacheEntry = new HashMap<String, String>();
            commitMappingCache.put(repository.getDirectory().getPath(), cacheEntry);
        } else {
            cacheEntry.clear();
        }

        Map<String, Date> tagMap = new HashMap<String, Date>();
        try {
            RevWalk rw = new RevWalk(repository);
            Map<String, Ref> tags = repository.getRefDatabase().getRefs(Constants.R_TAGS);
            for (Ref tagRef : tags.values()) {
                RevTag tag = rw.parseTag(repository.resolve(tagRef.getName()));
                if (tag.getObject().name().equals(commitId)) {
                    Date timestamp;
                    if (tag.getTaggerIdent() != null) {
                        timestamp = tag.getTaggerIdent().getWhen();
                    } else {
                        timestamp = null;
                    }
                    tagMap.put(tagRef.getName(), timestamp);
                }
            }
        } catch (IOException e) {
            // ignore here
        }

        String cacheValue = null;

        if (!tagMap.isEmpty()) {
            // we try to obtain the "latest" tag
            Date compareDate = new Date(0);
            for (Map.Entry<String, Date> tagEntry : tagMap.entrySet()) {
                if (tagEntry.getValue() != null && tagEntry.getValue().after(compareDate)) {
                    compareDate = tagEntry.getValue();
                    cacheValue = tagEntry.getKey();
                }
            }
            // if we don't have time stamps, we sort
            if (cacheValue == null) {
                String compareString = ""; //$NON-NLS-1$
                for (String tagName : tagMap.keySet()) {
                    if (tagName.compareTo(compareString) >= 0) {
                        cacheValue = tagName;
                        compareString = tagName;
                    }
                }
            }
        }

        if (cacheValue == null) {
            // we didnt't find a tag, so let's look for local branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_HEADS);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
            } catch (IOException e) {
                // ignore here
            }
            if (!branchNames.isEmpty()) {
                // get the last (sorted) entry
                cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
            }
        }

        if (cacheValue == null) {
            // last try: remote branches
            Set<String> branchNames = new TreeSet<String>();
            // put this into a sorted set
            try {
                Map<String, Ref> remoteBranches = repository.getRefDatabase().getRefs(Constants.R_REMOTES);
                for (Ref branch : remoteBranches.values()) {
                    if (branch.getObjectId().name().equals(commitId)) {
                        branchNames.add(branch.getName());
                    }
                }
                if (!branchNames.isEmpty()) {
                    // get the last (sorted) entry
                    cacheValue = branchNames.toArray(new String[branchNames.size()])[branchNames.size() - 1];
                }
            } catch (IOException e) {
                // ignore here
            }
        }
        cacheEntry.put(commitId, cacheValue);
        return cacheValue;
    }
}

From source file:org.eclipse.egit.core.RepositoryUtil.java

License:Open Source License

/**
 * Return a cached UI "name" for a Repository
 * <p>//from w w w.  ja va 2 s .co  m
 * This uses the name of the parent of the repository's directory.
 *
 * @param repository
 * @return the name
 */
public String getRepositoryName(Repository repository) {
    synchronized (repositoryNameCache) {
        File gitDir = repository.getDirectory();
        if (gitDir != null) {
            String name = repositoryNameCache.get(gitDir.getPath().toString());
            if (name != null)
                return name;
            name = gitDir.getParentFile().getName();
            repositoryNameCache.put(gitDir.getPath().toString(), name);
            return name;
        }
    }
    return ""; //$NON-NLS-1$
}

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

License:Open Source License

/**
 * @param repo/*from  w ww  .j  a  v a2s  .  c om*/
 *            instance of {@link Repository} for with mapping should be
 *            obtained
 * @return instance of {@link GitSyncObjectCache} connected associated with
 *         given repository or {@code null} when such mapping wasn't found
 */
public GitSyncObjectCache get(Repository repo) {
    return cache.get(repo.getDirectory());
}

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

License:Open Source License

/**
 * Create mapping for given repository and returns object associated with
 * this repository. Any other mapping will be overwritten.
 *
 * @param repo// w  w  w .  j  av a 2  s  . c o m
 * @param remoteTree
 * @param baseTree
 * @return new mapping object associated with given {@link Repository}
 */
private GitSyncObjectCache put(Repository repo, ObjectId baseTree, ObjectId remoteTree) {
    ThreeWayDiffEntry entry = new ThreeWayDiffEntry();
    entry.baseId = AbbreviatedObjectId.fromObjectId(baseTree);
    entry.remoteId = AbbreviatedObjectId.fromObjectId(remoteTree);
    GitSyncObjectCache objectCache = new GitSyncObjectCache("", entry); //$NON-NLS-1$
    cache.put(repo.getDirectory(), objectCache);

    return objectCache;
}

From source file:org.eclipse.egit.core.test.GitTestCase.java

License:Open Source License

protected ObjectId createFileCorruptShort(Repository repository, IProject actProject, String name,
        String content) throws IOException {
    ObjectId id = createFile(repository, actProject, name, content);
    File file = new File(repository.getDirectory(),
            "objects/" + id.name().substring(0, 2) + "/" + id.name().substring(2));
    byte[] readFully = IO.readFully(file);
    FileUtils.delete(file);/* www  .j  av a  2  s.c  o  m*/
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    byte[] truncatedData = new byte[readFully.length - 1];
    System.arraycopy(readFully, 0, truncatedData, 0, truncatedData.length);
    fileOutputStream.write(truncatedData);
    fileOutputStream.close();
    return id;
}

From source file:org.eclipse.egit.core.test.op.CloneOperationTest.java

License:Open Source License

@Test
public void testSimplePostCloneTask() throws Exception {
    URIish uri = new URIish("file:///" + repository1.getRepository().getDirectory().toString());
    CloneOperation clop = new CloneOperation(uri, true, null, workdir2, "refs/heads/master", "origin", 0);

    final File[] repoDir = new File[1];
    clop.addPostCloneTask(new PostCloneTask() {

        public void execute(Repository repository, IProgressMonitor monitor) throws CoreException {
            repoDir[0] = repository.getDirectory();

        }//from  w  w  w  . j  av  a 2s  .com
    });
    clop.run(null);
    File newRepoDir = new File(workdir2, Constants.DOT_GIT);
    assertEquals(newRepoDir, repoDir[0]);
}