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

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

Introduction

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

Prototype

@NonNull
public static String shortenRefName(String refName) 

Source Link

Document

Get a shortened more user friendly ref name

Usage

From source file:org.eclipse.egit.ui.internal.push.PushBranchWizard.java

License:Open Source License

private void configureUpstream() throws IOException {
    if (this.ref == null) {
        // Don't configure upstream for detached HEAD
        return;/*from   w ww  .ja  v  a  2 s  .  co  m*/
    }
    String remoteName = getRemoteName();
    String fullRemoteBranchName = pushBranchPage.getFullRemoteReference();
    String localBranchName = Repository.shortenRefName(this.ref.getName());

    StoredConfig config = repository.getConfig();
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_REMOTE,
            remoteName);
    config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName, ConfigConstants.CONFIG_KEY_MERGE,
            fullRemoteBranchName);
    BranchRebaseMode rebaseMode = pushBranchPage.getUpstreamConfig();
    if (rebaseMode != null) {
        config.setEnum(ConfigConstants.CONFIG_BRANCH_SECTION, localBranchName,
                ConfigConstants.CONFIG_KEY_REBASE, rebaseMode);
    }

    config.save();
}

From source file:org.eclipse.egit.ui.internal.reflog.ReflogView.java

License:Open Source License

private void updateRefLink(final String name) {
    IToolBarManager toolbar = form.getToolBarManager();
    toolbar.removeAll();//from   w ww. j  av a  2  s.c o m

    ControlContribution refLabelControl = new ControlContribution("refLabel") { //$NON-NLS-1$
        @Override
        protected Control createControl(Composite cParent) {
            Composite composite = toolkit.createComposite(cParent);
            composite.setLayout(new RowLayout());
            composite.setBackground(null);

            final ImageHyperlink refLink = new ImageHyperlink(composite, SWT.NONE);
            Image image = UIIcons.BRANCH.createImage();
            UIUtils.hookDisposal(refLink, image);
            refLink.setImage(image);
            refLink.setFont(JFaceResources.getBannerFont());
            refLink.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
            refLink.addHyperlinkListener(new HyperlinkAdapter() {
                @Override
                public void linkActivated(HyperlinkEvent event) {
                    Repository repository = getRepository();
                    if (repository == null)
                        return;
                    RefSelectionDialog dialog = new RefSelectionDialog(refLink.getShell(), repository);
                    if (Window.OK == dialog.open())
                        showReflogFor(repository, dialog.getRefName());
                }
            });
            refLink.setText(Repository.shortenRefName(name));

            return composite;
        }
    };
    toolbar.add(refLabelControl);
    toolbar.update(true);
}

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

License:Open Source License

/**
 * @param repository/* ww w.j  a va  2 s.  c  o  m*/
 *            the repository
 * @param fullBranchName
 *            the full name of the branch to show
 * @param page
 *            the page showing the properties
 */
public BranchPropertySource(Repository repository, String fullBranchName, PropertySheetPage page) {
    myPage = page;
    myBranchName = Repository.shortenRefName(fullBranchName);
    myRepository = repository;

    synchronized (myPage) {
        // check if the actions are already there, if not, create them
        IActionBars bars = myPage.getSite().getActionBars();
        IToolBarManager mgr = bars.getToolBarManager();

        editAction = ((ActionContributionItem) mgr.find(EDITACTIONID));
        if (editAction != null)
            ((EditAction) editAction.getAction()).setSource(this);
        else {
            editAction = new ActionContributionItem(
                    new EditAction(UIText.RepositoryPropertySource_EditConfigButton, UIIcons.EDITCONFIG, this));

            mgr.add(new Separator());
            mgr.add(editAction);
        }

        mgr.update(false);
    }
}

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

License:Open Source License

public StyledString getStyledText(Object element) {
    if (!(element instanceof RepositoryTreeNode))
        return null;

    RepositoryTreeNode node = (RepositoryTreeNode) element;

    try {/*from w ww.  jav  a  2 s  .  c  o  m*/
        switch (node.getType()) {
        case REPO:
            Repository repository = (Repository) node.getObject();
            File directory = repository.getDirectory();
            StyledString string = new StyledString(directory.getParentFile().getName());
            string.append(" - " + directory.getAbsolutePath(), StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
            String branch = repository.getBranch();
            if (repository.getRepositoryState() != RepositoryState.SAFE)
                branch += " - " + repository.getRepositoryState().getDescription(); //$NON-NLS-1$
            string.append(" [" + branch + "]", StyledString.DECORATIONS_STYLER); //$NON-NLS-1$//$NON-NLS-2$
            return string;
        case ADDITIONALREF:
            Ref ref = (Ref) node.getObject();
            // shorten the name
            StyledString refName = new StyledString(Repository.shortenRefName(ref.getName()));
            if (ref.isSymbolic()) {
                refName.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
                refName.append(ref.getLeaf().getName(), StyledString.QUALIFIER_STYLER);
                refName.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
                refName.append(ObjectId.toString(ref.getLeaf().getObjectId()), StyledString.QUALIFIER_STYLER);
            } else {
                refName.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
                refName.append(ObjectId.toString(ref.getObjectId()), StyledString.QUALIFIER_STYLER);

            }
            return refName;
        case WORKINGDIR:
            StyledString dirString = new StyledString(UIText.RepositoriesView_WorkingDir_treenode);
            dirString.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
            if (node.getRepository().isBare()) {
                dirString.append(UIText.RepositoriesViewLabelProvider_BareRepositoryMessage,
                        StyledString.QUALIFIER_STYLER);
            } else {
                dirString.append(node.getRepository().getWorkTree().getAbsolutePath(),
                        StyledString.QUALIFIER_STYLER);
            }
            return dirString;
        case PUSH:
            // fall through
        case FETCH:
            // fall through
        case FILE:
            // fall through
        case FOLDER:
            // fall through
        case BRANCHES:
            // fall through
        case LOCAL:
            // fall through
        case REMOTETRACKING:
            // fall through
        case BRANCHHIERARCHY:
            // fall through
        case TAGS:
            // fall through;
        case ADDITIONALREFS:
            // fall through
        case REMOTES:
            // fall through
        case REMOTE:
            // fall through
        case ERROR:
            // fall through
        case REF:
            // fall through
        case TAG: {
            String label = getSimpleText(node);
            if (label != null)
                return new StyledString(label);
        }

        }
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
    }

    return null;

}

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

License:Open Source License

private String getSimpleText(RepositoryTreeNode node) {
    switch (node.getType()) {
    case REPO://from   w  ww .  jav a2 s  . c  om
        File directory = ((Repository) node.getObject()).getDirectory();
        StringBuilder sb = new StringBuilder();
        sb.append(directory.getParentFile().getName());
        sb.append(" - "); //$NON-NLS-1$
        sb.append(directory.getAbsolutePath());
        return sb.toString();
    case FILE:
        // fall through
    case FOLDER:
        return ((File) node.getObject()).getName();
    case BRANCHES:
        return UIText.RepositoriesView_Branches_Nodetext;
    case LOCAL:
        return UIText.RepositoriesViewLabelProvider_LocalNodetext;
    case REMOTETRACKING:
        return UIText.RepositoriesViewLabelProvider_RemoteTrackingNodetext;
    case BRANCHHIERARCHY:
        IPath fullPath = (IPath) node.getObject();
        return fullPath.lastSegment();
    case TAGS:
        return UIText.RepositoriesViewLabelProvider_TagsNodeText;
    case ADDITIONALREFS:
        return UIText.RepositoriesViewLabelProvider_SymbolicRefNodeText;
    case REMOTES:
        return UIText.RepositoriesView_RemotesNodeText;
    case REF:
        // fall through
    case TAG: {
        Ref ref = (Ref) node.getObject();
        // shorten the name
        String refName = Repository.shortenRefName(ref.getName());
        if (node.getParent().getType() == RepositoryTreeNodeType.BRANCHHIERARCHY) {
            int index = refName.lastIndexOf('/');
            refName = refName.substring(index + 1);
        }
        return refName;
    }
    case ADDITIONALREF: {
        Ref ref = (Ref) node.getObject();
        // shorten the name
        String refName = Repository.shortenRefName(ref.getName());
        if (ref.isSymbolic()) {
            refName = refName + " - " //$NON-NLS-1$
                    + ref.getLeaf().getName() + " - " + ObjectId.toString(ref.getLeaf().getObjectId()); //$NON-NLS-1$
        } else {
            refName = refName + " - " //$NON-NLS-1$
                    + ObjectId.toString(ref.getObjectId());
        }
        return refName;
    }
    case WORKINGDIR:
        if (node.getRepository().isBare())
            return UIText.RepositoriesView_WorkingDir_treenode + " - " //$NON-NLS-1$
                    + UIText.RepositoriesViewLabelProvider_BareRepositoryMessage;
        else
            return UIText.RepositoriesView_WorkingDir_treenode + " - " //$NON-NLS-1$
                    + node.getRepository().getWorkTree().getAbsolutePath();
    case REMOTE:
        // fall through
    case PUSH:
        // fall through
    case FETCH:
        // fall through
    case ERROR:
        return (String) node.getObject();

    }
    return null;
}

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

License:Open Source License

public void createControl(Composite parent) {
    Composite displayArea = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().numColumns(2).equalWidth(false).applyTo(displayArea);
    GridDataFactory.fillDefaults().grab(true, true).applyTo(displayArea);

    RevCommit currentCommit = getLatestCommit(current);

    Label currentLabel = new Label(displayArea, SWT.NONE);
    currentLabel.setText(UIText.SelectResetTypePage_labelCurrentHead);
    currentLabel.setToolTipText(UIText.SelectResetTypePage_tooltipCurrentHead);

    CLabel currentValue = new CLabel(displayArea, SWT.NONE);
    GridDataFactory.swtDefaults().applyTo(currentValue);
    Image currentIcon = getIcon(current);
    UIUtils.hookDisposal(currentValue, currentIcon);
    currentValue.setImage(currentIcon);/*  w ww  .j av  a2s.  c o  m*/
    currentValue.setText(Repository.shortenRefName(current));

    if (currentCommit != null) {
        if (isCommit(current))
            currentValue.setText(formatCommit(currentCommit));
        else {
            new Label(displayArea, SWT.NONE);
            CLabel commitLabel = new CLabel(displayArea, SWT.NONE);
            Image commitIcon = UIIcons.CHANGESET.createImage();
            UIUtils.hookDisposal(commitLabel, commitIcon);
            commitLabel.setImage(commitIcon);
            commitLabel.setText(formatCommit(currentCommit));
        }
    }

    boolean resetToSelf = current.equals(target);
    if (!resetToSelf) {
        RevCommit targetCommit = getLatestCommit(target);

        Label targetLabel = new Label(displayArea, SWT.NONE);
        targetLabel.setText(UIText.SelectResetTypePage_labelResettingTo);
        targetLabel.setToolTipText(UIText.SelectResetTypePage_tooltipResettingTo);

        CLabel targetValue = new CLabel(displayArea, SWT.NONE);
        Image targetIcon = getIcon(target);
        UIUtils.hookDisposal(targetValue, targetIcon);
        targetValue.setImage(targetIcon);
        targetValue.setText(Repository.shortenRefName(target));

        if (targetCommit != null) {
            if (isCommit(target))
                targetValue.setText(formatCommit(targetCommit));
            else {
                new Label(displayArea, SWT.NONE);
                CLabel commitLabel = new CLabel(displayArea, SWT.NONE);
                Image commitIcon = UIIcons.CHANGESET.createImage();
                UIUtils.hookDisposal(commitLabel, commitIcon);
                commitLabel.setImage(commitIcon);
                commitLabel.setText(formatCommit(targetCommit));
            }
        }
    }

    Group g = new Group(displayArea, SWT.NONE);
    g.setText(UIText.ResetTargetSelectionDialog_ResetTypeGroup);
    GridDataFactory.fillDefaults().grab(true, false).span(2, 1).indent(0, 5).applyTo(g);
    GridLayoutFactory.swtDefaults().applyTo(g);

    if (!resetToSelf) {
        Button soft = new Button(g, SWT.RADIO);
        soft.setText(UIText.ResetTargetSelectionDialog_ResetTypeSoftButton);
        soft.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (((Button) event.widget).getSelection())
                    resetType = ResetType.SOFT;
            }
        });
    }

    Button medium = new Button(g, SWT.RADIO);
    medium.setSelection(true);
    medium.setText(resetToSelf ? UIText.ResetTargetSelectionDialog_ResetTypeHEADMixedButton
            : UIText.ResetTargetSelectionDialog_ResetTypeMixedButton);
    medium.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (((Button) event.widget).getSelection())
                resetType = ResetType.MIXED;
        }
    });

    Button hard = new Button(g, SWT.RADIO);
    hard.setText(resetToSelf ? UIText.ResetTargetSelectionDialog_ResetTypeHEADHardButton
            : UIText.ResetTargetSelectionDialog_ResetTypeHardButton);
    hard.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            if (((Button) event.widget).getSelection())
                resetType = ResetType.HARD;
        }
    });

    Dialog.applyDialogFont(displayArea);
    setControl(displayArea);
}

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

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final List<RefNode> nodes = getSelectedNodes(event);
    if (nodes.size() == 1) {
        RefNode node = nodes.get(0);//from  ww  w  .j  a  va  2s .com
        String branchName = Repository.shortenRefName(node.getObject().getName());
        BranchConfigurationDialog dlg = new BranchConfigurationDialog(getShell(event), branchName,
                node.getRepository());
        dlg.open();
    }
    return null;
}

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

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final List<TagNode> tags = getSelectedNodes(event);
    if (tags.isEmpty())
        return null;

    // Confirm deletion of selected tags
    final AtomicBoolean confirmed = new AtomicBoolean();
    final Shell shell = getActiveShell(event);
    shell.getDisplay().syncExec(new Runnable() {

        public void run() {
            String message;/*from w ww. ja  va2s  .  com*/
            if (tags.size() > 1)
                message = MessageFormat.format(UIText.DeleteTagCommand_messageConfirmMultipleTag,
                        Integer.valueOf(tags.size()));
            else
                message = MessageFormat.format(UIText.DeleteTagCommand_messageConfirmSingleTag,
                        Repository.shortenRefName(tags.get(0).getObject().getName()));
            confirmed.set(MessageDialog.openConfirm(shell, UIText.DeleteTagCommand_titleConfirm, message));
        }
    });
    if (!confirmed.get())
        return null;

    Job job = new Job(UIText.DeleteTagCommand_taskName) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            monitor.beginTask(UIText.DeleteTagCommand_taskName, tags.size());
            for (TagNode tag : tags) {
                final Repository repo = tag.getRepository();
                final String tagName = tag.getObject().getName();
                final DeleteTagOperation op = new DeleteTagOperation(repo, tagName);
                monitor.subTask(tagName);
                try {
                    op.execute(monitor);
                } catch (CoreException e) {
                    Activator.logError(e.getLocalizedMessage(), e);
                }
                monitor.worked(1);
            }
            monitor.done();
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (family.equals(JobFamilies.TAG))
                return true;
            return super.belongsTo(family);
        }
    };
    job.setUser(true);
    job.schedule();
    return null;
}

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

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final List<RefNode> nodes = getSelectedNodes(event);
    RefNode refNode = nodes.get(0);//from  www  .  ja v a 2 s . c om

    Shell shell = getShell(event);
    String oldName = refNode.getObject().getName();
    String prefix;
    if (oldName.startsWith(Constants.R_HEADS))
        prefix = Constants.R_HEADS;
    else if (oldName.startsWith(Constants.R_REMOTES))
        prefix = Constants.R_REMOTES;
    else
        throw new ExecutionException(NLS.bind(UIText.RenameBranchCommand_WrongNameMessage, oldName));
    Repository db = refNode.getRepository();
    IInputValidator inputValidator = ValidationUtils.getRefNameInputValidator(db, prefix, true);
    String defaultValue = Repository.shortenRefName(oldName);
    InputDialog newNameDialog = new InputDialog(shell, UIText.RepositoriesView_RenameBranchTitle,
            NLS.bind(UIText.RepositoriesView_RenameBranchMessage, defaultValue), defaultValue, inputValidator);
    if (newNameDialog.open() == Window.OK) {
        try {
            String newName = newNameDialog.getValue();
            new RenameBranchOperation(db, refNode.getObject(), newName).execute(null);
        } catch (CoreException e) {
            Activator.handleError(UIText.RepositoriesView_RenameBranchFailure, e, true);
        }
    }
    return null;
}

From source file:org.eclipse.egit.ui.UIUtils.java

License:Open Source License

/**
 * Adds a content proposal for {@link Ref}s (branches, tags...) to a text
 * field/*  w ww. j av a 2 s .  c  o  m*/
 *
 * @param textField
 *            the text field
 * @param repository
 *            the repository
 * @param refListProvider
 *            provides the {@link Ref}s to show in the proposal
 */
public static final void addRefContentProposalToText(final Text textField, final Repository repository,
        final IRefListProvider refListProvider) {
    KeyStroke stroke;
    try {
        stroke = KeyStroke.getInstance("M1+SPACE"); //$NON-NLS-1$
        UIUtils.addBulbDecorator(textField, NLS.bind(UIText.UIUtils_PressShortcutMessage, stroke.format()));
    } catch (ParseException e1) {
        Activator.handleError(e1.getMessage(), e1, false);
        stroke = null;
        UIUtils.addBulbDecorator(textField, UIText.UIUtils_StartTypingForPreviousValuesMessage);
    }

    IContentProposalProvider cp = new IContentProposalProvider() {
        public IContentProposal[] getProposals(String contents, int position) {
            List<IContentProposal> resultList = new ArrayList<IContentProposal>();

            // make the simplest possible pattern check: allow "*"
            // for multiple characters
            String patternString = contents;
            // ignore spaces in the beginning
            while (patternString.length() > 0 && patternString.charAt(0) == ' ') {
                patternString = patternString.substring(1);
            }

            // we quote the string as it may contain spaces
            // and other stuff colliding with the Pattern
            patternString = Pattern.quote(patternString);

            patternString = patternString.replaceAll("\\x2A", ".*"); //$NON-NLS-1$ //$NON-NLS-2$

            // make sure we add a (logical) * at the end
            if (!patternString.endsWith(".*")) { //$NON-NLS-1$
                patternString = patternString + ".*"; //$NON-NLS-1$
            }

            // let's compile a case-insensitive pattern (assumes ASCII only)
            Pattern pattern;
            try {
                pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
            } catch (PatternSyntaxException e) {
                pattern = null;
            }

            List<Ref> proposals = refListProvider.getRefList();

            if (proposals != null)
                for (final Ref ref : proposals) {
                    final String shortenedName = Repository.shortenRefName(ref.getName());
                    if (pattern != null && !pattern.matcher(ref.getName()).matches()
                            && !pattern.matcher(shortenedName).matches())
                        continue;

                    IContentProposal propsal = new RefContentProposal(repository, ref);
                    resultList.add(propsal);
                }

            return resultList.toArray(new IContentProposal[resultList.size()]);
        }
    };

    ContentProposalAdapter adapter = new ContentProposalAdapter(textField, new TextContentAdapter(), cp, stroke,
            UIUtils.VALUE_HELP_ACTIVATIONCHARS);
    // set the acceptance style to always replace the complete content
    adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);
}