Example usage for org.eclipse.jgit.lib Constants HEAD

List of usage examples for org.eclipse.jgit.lib Constants HEAD

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants HEAD.

Prototype

String HEAD

To view the source code for org.eclipse.jgit.lib Constants HEAD.

Click Source Link

Document

Special name for the "HEAD" symbolic-ref.

Usage

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

License:Open Source License

private RevObject getTagTarget(ObjectId objectId, Repository repo) throws ExecutionException {
    try {//from w  w  w . ja  v a 2s  . c  om
        RevWalk rw = new RevWalk(repo);
        try {
            if (objectId == null) {
                return rw.parseAny(repo.resolve(Constants.HEAD));

            } else {
                return rw.parseAny(objectId);
            }
        } finally {
            rw.release();
        }
    } catch (IOException e) {
        throw new ExecutionException(UIText.TagAction_unableToResolveHeadObjectId, e);
    }
}

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

License:Open Source License

private boolean canMerge(final Repository repository) {
    String message = null;//ww  w .  j av a2 s. com
    Exception ex = null;
    try {
        Ref head = repository.getRef(Constants.HEAD);
        if (head == null || !head.isSymbolic())
            message = UIText.MergeAction_HeadIsNoBranch;
        else if (!repository.getRepositoryState().equals(RepositoryState.SAFE))
            message = NLS.bind(UIText.MergeAction_WrongRepositoryState, repository.getRepositoryState());
    } catch (IOException e) {
        message = e.getMessage();
        ex = e;
    }

    if (message != null) {
        Activator.handleError(UIText.MergeAction_CannotMerge, ex, true);
    }
    return (message == null);
}

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

License:Open Source License

private boolean checkRepositoryHasHead(Object element) {
    if (element instanceof RepositoryTreeNode) {
        RepositoryTreeNode<?> treeNode = (RepositoryTreeNode<?>) element;
        Repository repo = treeNode.getRepository();
        try {//from  w ww. j  av a 2s .c  om
            Ref ref = repo.getRef(Constants.HEAD);
            return ref != null && ref.getObjectId() != null;
        } catch (IOException e) {
            // ignore and report false
            return false;
        }
    }
    return false;
}

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

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final RepositoryTreeNode node = getSelectedNodes(event).get(0);
    final String refName = getRefName(node);
    if (refName == null)
        return null;

    String secondRefName = Constants.HEAD;
    if (getSelectedNodes(event).size() == 2) {
        secondRefName = getRefName(getSelectedNodes(event).get(1));
        if (secondRefName == null)
            return null;
    }/*from  ww  w .  ja v a  2 s. c o  m*/

    final String secondRefNameParam = secondRefName;

    final boolean includeLocal = getSelectedNodes(event).size() == 1;

    final Repository repo = node.getRepository();
    Job job = new Job(NLS.bind(UIText.SelectSynchronizeResourceDialog_selectProject, repo.getDirectory())) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            GitSynchronizeData data;
            try {
                data = new GitSynchronizeData(repo, secondRefNameParam, refName, includeLocal);

                Set<IProject> projects = data.getProjects();
                IResource[] resources = projects.toArray(new IResource[projects.size()]);

                GitModelSynchronize.launch(data, resources);
            } catch (IOException e) {
                Activator.logError(e.getMessage(), e);
            }

            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();

    return null;
}

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

License:Open Source License

public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {

    if (!(receiver instanceof RepositoryTreeNode))
        return false;
    RepositoryTreeNode node = (RepositoryTreeNode) receiver;

    if (property.equals("isBare")) //$NON-NLS-1$
        return node.getRepository().isBare();

    if (property.equals("isSafe")) //$NON-NLS-1$
        return node.getRepository().getRepositoryState() == RepositoryState.SAFE;

    if (property.equals("isRefCheckedOut")) { //$NON-NLS-1$
        if (!(node.getObject() instanceof Ref))
            return false;
        Ref ref = (Ref) node.getObject();
        try {/*from   w  w w  .j  av a 2  s  .c  o m*/
            if (ref.getName().startsWith(Constants.R_REFS)) {
                return ref.getName().equals(node.getRepository().getFullBranch());
            } else if (ref.getName().equals(Constants.HEAD))
                return true;
            else {
                String leafname = ref.getLeaf().getName();
                if (leafname.startsWith(Constants.R_REFS)
                        && leafname.equals(node.getRepository().getFullBranch()))
                    return true;
                else
                    ref.getLeaf().getObjectId().equals(node.getRepository().resolve(Constants.HEAD));
            }
        } catch (IOException e) {
            return false;
        }
    }
    if (property.equals("isLocalBranch")) { //$NON-NLS-1$
        if (!(node.getObject() instanceof Ref))
            return false;
        Ref ref = (Ref) node.getObject();
        return ref.getName().startsWith(Constants.R_HEADS);
    }
    if (property.equals("fetchExists")) { //$NON-NLS-1$
        if (node instanceof RemoteNode) {
            String configName = ((RemoteNode) node).getObject();

            RemoteConfig rconfig;
            try {
                rconfig = new RemoteConfig(node.getRepository().getConfig(), configName);
            } catch (URISyntaxException e2) {
                return false;
            }
            // we need to have a fetch ref spec and a fetch URI
            return !rconfig.getFetchRefSpecs().isEmpty() && !rconfig.getURIs().isEmpty();
        }
    }
    if (property.equals("pushExists")) { //$NON-NLS-1$
        if (node instanceof RemoteNode) {
            String configName = ((RemoteNode) node).getObject();

            RemoteConfig rconfig;
            try {
                rconfig = new RemoteConfig(node.getRepository().getConfig(), configName);
            } catch (URISyntaxException e2) {
                return false;
            }
            // we need to have at least a push ref spec and any URI
            return !rconfig.getPushRefSpecs().isEmpty()
                    && (!rconfig.getPushURIs().isEmpty() || !rconfig.getURIs().isEmpty());
        }
    }
    if (property.equals("canMerge")) { //$NON-NLS-1$
        Repository rep = node.getRepository();
        if (rep.getRepositoryState() != RepositoryState.SAFE)
            return false;
        try {
            String branch = rep.getFullBranch();
            if (branch == null)
                return false; // fail gracefully...
            return branch.startsWith(Constants.R_HEADS);
        } catch (IOException e) {
            return false;
        }
    }

    if (property.equals("canAbortRebase")) //$NON-NLS-1$
        switch (node.getRepository().getRepositoryState()) {
        case REBASING_INTERACTIVE:
            return true;
        default:
            return false;
        }
    return false;
}

From source file:org.eclipse.egit.ui.internal.search.CommitSearchQuery.java

License:Open Source License

private void walkRepository(Repository repository, Pattern pattern, IProgressMonitor monitor)
        throws IOException {
    RevWalk walk = new RevWalk(repository);
    try {/*from w w  w  .  j a va  2 s.c  o  m*/
        walk.setRetainBody(true);
        List<RevCommit> commits = new LinkedList<RevCommit>();
        if (this.settings.isAllBranches()) {
            for (Ref ref : repository.getRefDatabase().getRefs(Constants.R_HEADS).values())
                if (!ref.isSymbolic())
                    commits.add(walk.parseCommit(ref.getObjectId()));
            for (Ref ref : repository.getRefDatabase().getRefs(Constants.R_REMOTES).values())
                if (!ref.isSymbolic())
                    commits.add(walk.parseCommit(ref.getObjectId()));
        } else {
            ObjectId headCommit = repository.resolve(Constants.HEAD);
            if (headCommit != null)
                commits.add(walk.parseCommit(headCommit));
        }

        if (!commits.isEmpty()) {
            walk.markStart(commits);
            for (RevCommit commit : walk) {
                if (monitor.isCanceled())
                    throw new OperationCanceledException();
                for (SearchMatcher matcher : this.matchers)
                    if (matcher.matches(pattern, commit)) {
                        result.addResult(new RepositoryCommit(repository, commit));
                        break;
                    }
            }
        }
    } finally {
        walk.dispose();
    }
}

From source file:org.eclipse.egit.ui.internal.staging.StagingView.java

License:Open Source License

private void replaceWith(String[] files, boolean headRevision) {
    if (files == null || files.length == 0)
        return;/* w w  w.j  a  va  2  s  .  c  om*/
    CheckoutCommand checkoutCommand = new Git(currentRepository).checkout();
    if (headRevision)
        checkoutCommand.setStartPoint(Constants.HEAD);
    for (String path : files)
        checkoutCommand.addPath(path);
    try {
        checkoutCommand.call();
    } catch (Exception e) {
        Activator.handleError(UIText.StagingView_checkoutFailed, e, true);
    }
}

From source file:org.eclipse.egit.ui.internal.staging.StagingView.java

License:Open Source License

private void unstage(IStructuredSelection selection) {
    if (selection.isEmpty())
        return;/* www  .  j  a va2  s .c  om*/

    RevCommit headRev = null;
    try {
        final Ref head = currentRepository.getRef(Constants.HEAD);
        // head.getObjectId() is null if the repository does not contain any
        // commit
        if (head.getObjectId() != null)
            headRev = new RevWalk(currentRepository).parseCommit(head.getObjectId());
    } catch (IOException e1) {
        // TODO fix text
        MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                UIText.CommitAction_ErrorReadingMergeMsg);
        return;
    }

    final DirCache dirCache;
    final DirCacheEditor edit;
    try {
        dirCache = currentRepository.lockDirCache();
        edit = dirCache.editor();
    } catch (IOException e) {
        // TODO fix text
        MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                UIText.CommitAction_ErrorReadingMergeMsg);
        return;
    }

    try {
        updateDirCache(selection, headRev, edit);

        try {
            edit.commit();
        } catch (IOException e) {
            // TODO fix text
            MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                    UIText.CommitAction_ErrorReadingMergeMsg);
        }
    } finally {
        dirCache.unlock();
    }
}

From source file:org.eclipse.egit.ui.internal.submodules.SubmoduleFolderTest.java

License:Open Source License

/**
 * Tests that a CompareWithHeadAction on a file from a submodule folder does
 * open the right compare editor, comparing against the version from the
 * submodule (as opposed to the version from the parent repo).
 *
 * @throws Exception/*from  w w w .j  a  va2s. com*/
 */
@Test
public void compareWithHeadInSubmoduleFolder() throws Exception {
    // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=446344#c11
    // If the compare editor's title does not contain the HEAD id of
    // the subrepo, then either no compare editor got opened, or
    // it was opened using the parent repo.
    IFolder childProjectFolder = childFolder.getFolder(CHILDPROJECT);
    IFolder folder = childProjectFolder.getFolder(FOLDER);
    IFile file = folder.getFile(FILE1);
    touch(PROJ1, file.getProjectRelativePath().toOSString(), "Modified");
    SWTBotTree projectExplorerTree = TestUtil.getExplorerTree();
    SWTBotTreeItem node = TestUtil.navigateTo(projectExplorerTree, file.getFullPath().segments());
    node.select();
    Ref headRef = subRepository.findRef(Constants.HEAD);
    final String headId = headRef.getObjectId().abbreviate(6).name();
    ContextMenuHelper.clickContextMenuSync(projectExplorerTree, "Compare With",
            util.getPluginLocalizedValue("CompareWithHeadAction_label"));
    bot.waitUntil(waitForEditor(new BaseMatcher<IEditorReference>() {

        @Override
        public boolean matches(Object item) {
            return (item instanceof IEditorReference) && ((IEditorReference) item).getTitle().contains(headId);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Wait for editor containing " + headId);
        }
    }), 5000);
}

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

License:Open Source License

/**
 * Launches Git Model synchronize operation for synchronizing a set of
 * resources in the workspace with a remote reference.
 *
 * @param file/* www . java  2  s  . c o  m*/
 *            the file to synchronize (will also be used to lookup resouce
 *            mappings)
 * @param repository
 *            the repository
 * @param refName
 *            the reference to synchronize with
 * @throws IOException
 */
public static final void synchronizeModelWithWorkspace(IFile file, Repository repository, String refName)
        throws IOException {
    synchronizeModel(file, new GitSynchronizeData(repository, Constants.HEAD, refName, true));
}