Search in sources :

Example 1 with CreateBranchWizard

use of org.eclipse.egit.ui.internal.repository.CreateBranchWizard in project egit by eclipse.

the class CreateBranchOnCommitHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Repository repo = getRepository(event);
    IStructuredSelection selection = getSelection(event);
    IWizard wiz = null;
    List<Ref> branches = getBranches(selection, repo);
    if (branches.isEmpty()) {
        PlotCommit commit = (PlotCommit) selection.getFirstElement();
        wiz = new CreateBranchWizard(repo, commit.name());
    } else {
        // prefer to create new branch based on a remote tracking branch
        Collections.sort(branches, new Comparator<Ref>() {

            @Override
            public int compare(Ref o1, Ref o2) {
                String refName1 = o1.getName();
                String refName2 = o2.getName();
                if (refName1.startsWith(Constants.R_REMOTES)) {
                    if (refName2.startsWith(Constants.R_HEADS))
                        return -1;
                    else
                        return refName1.compareTo(refName2);
                } else {
                    if (refName2.startsWith(Constants.R_REMOTES))
                        return 1;
                    else
                        return refName1.compareTo(refName2);
                }
            }
        });
        Ref branch = branches.get(0).getLeaf();
        wiz = new CreateBranchWizard(repo, branch.getName());
    }
    WizardDialog dlg = new WizardDialog(HandlerUtil.getActiveShellChecked(event), wiz);
    dlg.setHelpAvailable(false);
    dlg.open();
    return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) PlotCommit(org.eclipse.jgit.revplot.PlotCommit) IWizard(org.eclipse.jface.wizard.IWizard) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) WizardDialog(org.eclipse.jface.wizard.WizardDialog) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard)

Example 2 with CreateBranchWizard

use of org.eclipse.egit.ui.internal.repository.CreateBranchWizard in project egit by eclipse.

the class BranchSelectionAndEditDialog method createNewButton.

/**
 * Creates button for creating a new branch.
 *
 * @param parent
 * @return button
 */
protected Button createNewButton(Composite parent) {
    newButton = createButton(parent, NEW, UIText.BranchSelectionAndEditDialog_NewBranch, false);
    setButtonLayoutData(newButton);
    newButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // try to read default source ref from git config
            // in the case that no base is selected in dialog
            String base = refNameFromDialog();
            if (base == null) {
                String sourceRef = repo.getConfig().getString(ConfigConstants.CONFIG_WORKFLOW_SECTION, null, ConfigConstants.CONFIG_KEY_DEFBRANCHSTARTPOINT);
                try {
                    Ref ref = repo.findRef(sourceRef);
                    if (ref != null) {
                        base = ref.getName();
                    }
                } catch (IOException e1) {
                // base = null;
                }
            }
            CreateBranchWizard wiz = new CreateBranchWizard(repo, base);
            if (new WizardDialog(getShell(), wiz).open() == Window.OK) {
                String newRefName = wiz.getNewBranchName();
                try {
                    branchTree.refresh();
                    markRef(Constants.R_HEADS + newRefName);
                    if (newRefName.equals(repo.getBranch()))
                        // close branch selection dialog when new branch was
                        // already checked out from new branch wizard
                        BranchSelectionAndEditDialog.this.okPressed();
                } catch (Throwable e1) {
                    reportError(e1, UIText.BranchSelectionAndEditDialog_ErrorCouldNotCreateNewRef, newRefName);
                }
            }
        }
    });
    return newButton;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) SelectionEvent(org.eclipse.swt.events.SelectionEvent) IOException(java.io.IOException) WizardDialog(org.eclipse.jface.wizard.WizardDialog) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard)

Example 3 with CreateBranchWizard

use of org.eclipse.egit.ui.internal.repository.CreateBranchWizard in project egit by eclipse.

the class SwitchToMenu method createDynamicMenu.

private void createDynamicMenu(Menu menu, final Repository repository) {
    MenuItem newBranch = new MenuItem(menu, SWT.PUSH);
    newBranch.setText(UIText.SwitchToMenu_NewBranchMenuLabel);
    newBranch.setImage(newBranchImage);
    newBranch.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String sourceRef = repository.getConfig().getString(ConfigConstants.CONFIG_WORKFLOW_SECTION, null, ConfigConstants.CONFIG_KEY_DEFBRANCHSTARTPOINT);
            CreateBranchWizard wiz = null;
            try {
                Ref ref = null;
                if (sourceRef != null) {
                    ref = repository.findRef(sourceRef);
                }
                if (ref != null) {
                    wiz = new CreateBranchWizard(repository, ref.getName());
                } else {
                    wiz = new CreateBranchWizard(repository, repository.getFullBranch());
                }
            } catch (IOException e1) {
            // Ignore
            }
            if (wiz == null) {
                wiz = new CreateBranchWizard(repository);
            }
            WizardDialog dlg = new WizardDialog(e.display.getActiveShell(), wiz);
            dlg.setHelpAvailable(false);
            dlg.open();
        }
    });
    createSeparator(menu);
    try {
        String currentBranch = repository.getFullBranch();
        Map<String, Ref> localBranches = repository.getRefDatabase().getRefs(Constants.R_HEADS);
        TreeMap<String, Ref> sortedRefs = new TreeMap<>(CommonUtils.STRING_ASCENDING_COMPARATOR);
        // Add the MAX_NUM_MENU_ENTRIES most recently used branches first
        ReflogReader reflogReader = repository.getReflogReader(Constants.HEAD);
        List<ReflogEntry> reflogEntries;
        if (reflogReader == null) {
            reflogEntries = Collections.emptyList();
        } else {
            reflogEntries = reflogReader.getReverseEntries();
        }
        for (ReflogEntry entry : reflogEntries) {
            CheckoutEntry checkout = entry.parseCheckout();
            if (checkout != null) {
                Ref ref = localBranches.get(checkout.getFromBranch());
                if (ref != null)
                    if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
                        sortedRefs.put(checkout.getFromBranch(), ref);
                ref = localBranches.get(checkout.getToBranch());
                if (ref != null)
                    if (sortedRefs.size() < MAX_NUM_MENU_ENTRIES)
                        sortedRefs.put(checkout.getToBranch(), ref);
            }
        }
        // Add the recently used branches to the menu, in alphabetical order
        int itemCount = 0;
        for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
            itemCount++;
            final String shortName = entry.getKey();
            final String fullName = entry.getValue().getName();
            createMenuItem(menu, repository, currentBranch, fullName, shortName);
            // Do not duplicate branch names
            localBranches.remove(shortName);
        }
        if (itemCount < MAX_NUM_MENU_ENTRIES) {
            // local branches
            if (itemCount > 0 && localBranches.size() > 0)
                createSeparator(menu);
            // Now add more other branches if we have only a few branch switches
            // Sort the remaining local branches
            sortedRefs.clear();
            sortedRefs.putAll(localBranches);
            for (final Entry<String, Ref> entry : sortedRefs.entrySet()) {
                itemCount++;
                // protect ourselves against a huge sub-menu
                if (itemCount > MAX_NUM_MENU_ENTRIES)
                    break;
                final String fullName = entry.getValue().getName();
                final String shortName = entry.getKey();
                createMenuItem(menu, repository, currentBranch, fullName, shortName);
            }
        }
        if (itemCount > 0)
            createSeparator(menu);
        MenuItem others = new MenuItem(menu, SWT.PUSH);
        others.setText(UIText.SwitchToMenu_OtherMenuLabel);
        others.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                CheckoutDialog dialog = new CheckoutDialog(e.display.getActiveShell(), repository);
                if (dialog.open() == Window.OK) {
                    BranchOperationUI.checkout(repository, dialog.getRefName()).start();
                }
            }
        });
    } catch (IOException e) {
        Activator.handleError(e.getMessage(), e, true);
    }
}
Also used : ReflogReader(org.eclipse.jgit.lib.ReflogReader) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) CheckoutDialog(org.eclipse.egit.ui.internal.dialogs.CheckoutDialog) MenuItem(org.eclipse.swt.widgets.MenuItem) IOException(java.io.IOException) TreeMap(java.util.TreeMap) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard) Ref(org.eclipse.jgit.lib.Ref) CheckoutEntry(org.eclipse.jgit.lib.CheckoutEntry) ReflogEntry(org.eclipse.jgit.lib.ReflogEntry) SelectionEvent(org.eclipse.swt.events.SelectionEvent) WizardDialog(org.eclipse.jface.wizard.WizardDialog)

Example 4 with CreateBranchWizard

use of org.eclipse.egit.ui.internal.repository.CreateBranchWizard in project egit by eclipse.

the class CreateBranchHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    List<RepositoryCommit> commits = getCommits(event);
    if (commits.size() == 1) {
        RepositoryCommit commit = commits.get(0);
        WizardDialog dlg = new WizardDialog(HandlerUtil.getActiveShellChecked(event), new CreateBranchWizard(commit.getRepository(), commit.getRevCommit().name()));
        dlg.setHelpAvailable(false);
        dlg.open();
    }
    return null;
}
Also used : RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) WizardDialog(org.eclipse.jface.wizard.WizardDialog) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard)

Example 5 with CreateBranchWizard

use of org.eclipse.egit.ui.internal.repository.CreateBranchWizard in project egit by eclipse.

the class BranchOperationUI method getTargetWithCheckoutRemoteTrackingDialogInUI.

private String getTargetWithCheckoutRemoteTrackingDialogInUI() {
    String[] buttons = new String[] { UIText.BranchOperationUI_CheckoutRemoteTrackingAsLocal, UIText.BranchOperationUI_CheckoutRemoteTrackingCommit, IDialogConstants.CANCEL_LABEL };
    MessageDialog questionDialog = new MessageDialog(getShell(), UIText.BranchOperationUI_CheckoutRemoteTrackingTitle, null, UIText.BranchOperationUI_CheckoutRemoteTrackingQuestion, MessageDialog.QUESTION, buttons, 0);
    int result = questionDialog.open();
    if (result == 0) {
        // Check out as new local branch
        CreateBranchWizard wizard = new CreateBranchWizard(repository, target);
        WizardDialog createBranchDialog = new WizardDialog(getShell(), wizard);
        createBranchDialog.open();
        return null;
    } else if (result == 1) {
        // Check out commit
        return target;
    } else {
        // Cancel
        return null;
    }
}
Also used : MessageDialog(org.eclipse.jface.dialogs.MessageDialog) WizardDialog(org.eclipse.jface.wizard.WizardDialog) CreateBranchWizard(org.eclipse.egit.ui.internal.repository.CreateBranchWizard)

Aggregations

CreateBranchWizard (org.eclipse.egit.ui.internal.repository.CreateBranchWizard)6 WizardDialog (org.eclipse.jface.wizard.WizardDialog)6 Ref (org.eclipse.jgit.lib.Ref)4 IOException (java.io.IOException)3 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 TreeMap (java.util.TreeMap)1 RepositoryCommit (org.eclipse.egit.ui.internal.commit.RepositoryCommit)1 CheckoutDialog (org.eclipse.egit.ui.internal.dialogs.CheckoutDialog)1 RepositoryTreeNode (org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode)1 MessageDialog (org.eclipse.jface.dialogs.MessageDialog)1 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)1 IWizard (org.eclipse.jface.wizard.IWizard)1 CheckoutEntry (org.eclipse.jgit.lib.CheckoutEntry)1 ReflogEntry (org.eclipse.jgit.lib.ReflogEntry)1 ReflogReader (org.eclipse.jgit.lib.ReflogReader)1 Repository (org.eclipse.jgit.lib.Repository)1 PlotCommit (org.eclipse.jgit.revplot.PlotCommit)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1