Search in sources :

Example 1 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project egit by eclipse.

the class RebaseInteractiveView method createPartControl.

@Override
public void createPartControl(Composite parent) {
    GridLayoutFactory.fillDefaults().applyTo(parent);
    final FormToolkit toolkit = new FormToolkit(parent.getDisplay());
    parent.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            toolkit.dispose();
        }
    });
    form = createForm(parent, toolkit);
    createCommandToolBar(form, toolkit);
    planTreeViewer = createPlanTreeViewer(form.getBody(), toolkit);
    planLayout = new PlanLayout();
    planTreeViewer.getTree().getParent().setLayout(planLayout);
    createColumns(planLayout);
    createStepActionToolBar(toolkit);
    createPopupMenu(planTreeViewer);
    setupListeners();
    createLocalDragandDrop();
    planTreeViewer.addDoubleClickListener(new IDoubleClickListener() {

        @Override
        public void doubleClick(DoubleClickEvent event) {
            PlanElement element = (PlanElement) ((IStructuredSelection) event.getSelection()).getFirstElement();
            if (element == null)
                return;
            RepositoryCommit commit = loadCommit(element.getCommit());
            if (commit != null)
                CommitEditor.openQuiet(commit);
        }

        private RepositoryCommit loadCommit(AbbreviatedObjectId abbreviatedObjectId) {
            if (abbreviatedObjectId != null) {
                try (RevWalk walk = new RevWalk(RebaseInteractiveView.this.currentRepository)) {
                    Collection<ObjectId> resolved = walk.getObjectReader().resolve(abbreviatedObjectId);
                    if (resolved.size() == 1) {
                        RevCommit commit = walk.parseCommit(resolved.iterator().next());
                        return new RepositoryCommit(RebaseInteractiveView.this.currentRepository, commit);
                    }
                } catch (IOException e) {
                    return null;
                }
            }
            return null;
        }
    });
    prefListener = new IPreferenceChangeListener() {

        @Override
        public void preferenceChange(PreferenceChangeEvent event) {
            if (!RepositoryUtil.PREFS_DIRECTORIES_REL.equals(event.getKey())) {
                return;
            }
            final Repository repo = currentRepository;
            if (repo == null)
                return;
            if (Activator.getDefault().getRepositoryUtil().contains(repo))
                return;
            // Unselect repository as it has been removed
            Display.getDefault().asyncExec(new Runnable() {

                @Override
                public void run() {
                    currentRepository = null;
                    showRepository(null);
                }
            });
        }
    };
    InstanceScope.INSTANCE.getNode(org.eclipse.egit.core.Activator.getPluginId()).addPreferenceChangeListener(prefListener);
    uiPrefsListener = new IPropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {
            String property = event.getProperty();
            if (UIPreferences.DATE_FORMAT.equals(property) || UIPreferences.DATE_FORMAT_CHOICE.equals(property) || UIPreferences.RESOURCEHISTORY_SHOW_RELATIVE_DATE.equals(property)) {
                refresh();
            }
        }
    };
    Activator.getDefault().getPreferenceStore().addPropertyChangeListener(uiPrefsListener);
    IActionBars actionBars = getViewSite().getActionBars();
    IToolBarManager toolbar = actionBars.getToolBarManager();
    listenOnRepositoryViewSelection = RebaseInteractivePreferences.isReactOnSelection();
    // link with selection
    Action linkSelectionAction = new BooleanPrefAction((IPersistentPreferenceStore) Activator.getDefault().getPreferenceStore(), UIPreferences.REBASE_INTERACTIVE_SYNC_SELECTION, UIText.InteractiveRebaseView_LinkSelection) {

        @Override
        public void apply(boolean value) {
            listenOnRepositoryViewSelection = value;
        }
    };
    linkSelectionAction.setImageDescriptor(UIIcons.ELCL16_SYNCED);
    toolbar.add(linkSelectionAction);
    switchRepositoriesAction = new RepositoryToolbarAction(false, () -> currentRepository, repo -> setInput(new StructuredSelection(repo)));
    toolbar.add(switchRepositoriesAction);
    UIUtils.notifySelectionChangedWithCurrentSelection(selectionChangedListener, getSite());
    getSite().setSelectionProvider(new RepositorySelectionProvider(planTreeViewer, () -> currentRepository));
}
Also used : AdapterUtils(org.eclipse.egit.core.AdapterUtils) UIPreferences(org.eclipse.egit.ui.UIPreferences) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) DND(org.eclipse.swt.dnd.DND) CommitEditor(org.eclipse.egit.ui.internal.commit.CommitEditor) RepositoryState(org.eclipse.jgit.lib.RepositoryState) LocalSelectionTransfer(org.eclipse.jface.util.LocalSelectionTransfer) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) ProcessStepsRebaseCommand(org.eclipse.egit.ui.internal.commands.shared.ProcessStepsRebaseCommand) Composite(org.eclipse.swt.widgets.Composite) PartInitException(org.eclipse.ui.PartInitException) PreferenceBasedDateFormatter(org.eclipse.egit.ui.internal.PreferenceBasedDateFormatter) KeyEvent(org.eclipse.swt.events.KeyEvent) KeyAdapter(org.eclipse.swt.events.KeyAdapter) IEditorPart(org.eclipse.ui.IEditorPart) AbortRebaseCommand(org.eclipse.egit.ui.internal.commands.shared.AbortRebaseCommand) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Separator(org.eclipse.jface.action.Separator) IEditorInput(org.eclipse.ui.IEditorInput) PlatformUI(org.eclipse.ui.PlatformUI) MenuManager(org.eclipse.jface.action.MenuManager) ToolItem(org.eclipse.swt.widgets.ToolItem) AbstractRebaseCommandHandler(org.eclipse.egit.ui.internal.commands.shared.AbstractRebaseCommandHandler) GridLayoutFactory(org.eclipse.jface.layout.GridLayoutFactory) Transfer(org.eclipse.swt.dnd.Transfer) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) UIText(org.eclipse.egit.ui.internal.UIText) PreferenceChangeEvent(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent) SWT(org.eclipse.swt.SWT) BooleanPrefAction(org.eclipse.egit.ui.internal.actions.BooleanPrefAction) ElementType(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.ElementType) RepositorySelectionProvider(org.eclipse.egit.ui.internal.selection.RepositorySelectionProvider) ViewPart(org.eclipse.ui.part.ViewPart) RevCommit(org.eclipse.jgit.revwalk.RevCommit) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Format(org.eclipse.jgit.util.GitDateFormatter.Format) JFaceResources(org.eclipse.jface.resource.JFaceResources) RebaseInteractivePlan(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ArrayList(java.util.ArrayList) IFileEditorInput(org.eclipse.ui.IFileEditorInput) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) PlanElement(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.PlanElement) IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) Font(org.eclipse.swt.graphics.Font) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) IPreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) IViewSite(org.eclipse.ui.IViewSite) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) IActionBars(org.eclipse.ui.IActionBars) CommonUtils(org.eclipse.egit.ui.internal.CommonUtils) Form(org.eclipse.ui.forms.widgets.Form) RepositoryToolbarAction(org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction) ToolTip(org.eclipse.jface.window.ToolTip) Repository(org.eclipse.jgit.lib.Repository) ElementAction(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.ElementAction) DisposeEvent(org.eclipse.swt.events.DisposeEvent) DisposeListener(org.eclipse.swt.events.DisposeListener) IAction(org.eclipse.jface.action.IAction) ToolBar(org.eclipse.swt.widgets.ToolBar) IToolBarManager(org.eclipse.jface.action.IToolBarManager) SWTKeySupport(org.eclipse.jface.bindings.keys.SWTKeySupport) ColumnPixelData(org.eclipse.jface.viewers.ColumnPixelData) GC(org.eclipse.swt.graphics.GC) ISelectionListener(org.eclipse.ui.ISelectionListener) ColumnViewerToolTipSupport(org.eclipse.jface.viewers.ColumnViewerToolTipSupport) RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) IWorkbenchAction(org.eclipse.ui.actions.ActionFactory.IWorkbenchAction) TreeViewerColumn(org.eclipse.jface.viewers.TreeViewerColumn) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) Collection(java.util.Collection) GitDateFormatter(org.eclipse.jgit.util.GitDateFormatter) Display(org.eclipse.swt.widgets.Display) ColumnLabelProvider(org.eclipse.jface.viewers.ColumnLabelProvider) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) List(java.util.List) Tree(org.eclipse.swt.widgets.Tree) ISelection(org.eclipse.jface.viewers.ISelection) TreeViewer(org.eclipse.jface.viewers.TreeViewer) UIIcons(org.eclipse.egit.ui.internal.UIIcons) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ISelectionService(org.eclipse.ui.ISelectionService) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) Activator(org.eclipse.egit.ui.Activator) Image(org.eclipse.swt.graphics.Image) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) IPersistentPreferenceStore(org.eclipse.jface.preference.IPersistentPreferenceStore) ColumnWeightData(org.eclipse.jface.viewers.ColumnWeightData) ContinueRebaseCommand(org.eclipse.egit.ui.internal.commands.shared.ContinueRebaseCommand) IWorkbenchActionConstants(org.eclipse.ui.IWorkbenchActionConstants) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) Action(org.eclipse.jface.action.Action) TreeItem(org.eclipse.swt.widgets.TreeItem) ObjectId(org.eclipse.jgit.lib.ObjectId) IResource(org.eclipse.core.resources.IResource) UIUtils(org.eclipse.egit.ui.UIUtils) TreeColumnLayout(org.eclipse.jface.layout.TreeColumnLayout) LocalResourceManager(org.eclipse.jface.resource.LocalResourceManager) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) RepositoriesView(org.eclipse.egit.ui.internal.repository.RepositoriesView) Control(org.eclipse.swt.widgets.Control) SkipRebaseCommand(org.eclipse.egit.ui.internal.commands.shared.SkipRebaseCommand) DisposeListener(org.eclipse.swt.events.DisposeListener) BooleanPrefAction(org.eclipse.egit.ui.internal.actions.BooleanPrefAction) RepositoryToolbarAction(org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction) ElementAction(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.ElementAction) IAction(org.eclipse.jface.action.IAction) IWorkbenchAction(org.eclipse.ui.actions.ActionFactory.IWorkbenchAction) Action(org.eclipse.jface.action.Action) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RepositorySelectionProvider(org.eclipse.egit.ui.internal.selection.RepositorySelectionProvider) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DisposeEvent(org.eclipse.swt.events.DisposeEvent) PlanElement(org.eclipse.egit.core.internal.rebase.RebaseInteractivePlan.PlanElement) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) IActionBars(org.eclipse.ui.IActionBars) RevCommit(org.eclipse.jgit.revwalk.RevCommit) IPropertyChangeListener(org.eclipse.jface.util.IPropertyChangeListener) RepositoryToolbarAction(org.eclipse.egit.ui.internal.components.RepositoryMenuUtil.RepositoryToolbarAction) PreferenceChangeEvent(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent) IPreferenceChangeListener(org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener) PropertyChangeEvent(org.eclipse.jface.util.PropertyChangeEvent) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) Repository(org.eclipse.jgit.lib.Repository) IToolBarManager(org.eclipse.jface.action.IToolBarManager) Collection(java.util.Collection) BooleanPrefAction(org.eclipse.egit.ui.internal.actions.BooleanPrefAction)

Example 2 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project fuse-karaf by jboss-fuse.

the class GitConflictResolutionIT method reportingDiffs.

@Test
public void reportingDiffs() throws Exception {
    prepareChanges();
    ObjectReader reader = git.getRepository().newObjectReader();
    RevWalk rw = new RevWalk(git.getRepository());
    CanonicalTreeParser ctp1 = new CanonicalTreeParser();
    CanonicalTreeParser ctp2 = new CanonicalTreeParser();
    CanonicalTreeParser ctp3 = new CanonicalTreeParser();
    ctp1.reset(reader, rw.parseCommit(git.getRepository().resolve("master")).getTree());
    ctp2.reset(reader, rw.parseCommit(git.getRepository().resolve("custom")).getTree());
    ctp3.reset(reader, rw.parseCommit(git.getRepository().resolve("patched")).getTree());
    TreeWalk walk = new TreeWalk(reader);
    walk.addTree(ctp1);
    walk.addTree(ctp3);
    walk.setRecursive(true);
    List<DiffEntry> diffs = DiffEntry.scan(walk);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(baos);
    df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM));
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setRepository(git.getRepository());
    df.format(diffs.get(0));
    // System.out.println(new String(baos.toByteArray()));
    AbbreviatedObjectId id1 = diffs.get(0).getOldId();
    AbbreviatedObjectId id2 = diffs.get(0).getNewId();
    byte[] bytes1 = reader.open(id1.toObjectId()).getBytes();
    byte[] bytes2 = reader.open(id2.toObjectId()).getBytes();
    RawText rt1 = new RawText(bytes1);
    RawText rt2 = new RawText(bytes2);
    EditList edits = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).diff(RawTextComparator.DEFAULT, rt1, rt2);
    int aCur = 0;
    for (Edit curEdit : edits) {
        boolean prolog = aCur < curEdit.getBeginA();
        if (prolog) {
            System.out.print("<div class=\"edit unchanged\">");
        }
        while (aCur < curEdit.getBeginA()) {
            System.out.println(rt1.getString(aCur++));
        }
        if (prolog) {
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.INSERT) {
            System.out.print("<div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.REPLACE) {
            System.out.print("<div class=\"edit changed\"><div class=\"edit removed\">");
            for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
                System.out.println(rt1.getString(i));
            }
            System.out.print("</div><div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            aCur = curEdit.getEndA();
            System.out.print("</div></div>");
        }
    }
    boolean prolog = aCur < rt1.size();
    if (prolog) {
        System.out.print("<div class=\"edit unchanged\">");
    }
    while (aCur < rt1.size()) {
        System.out.println(rt1.getString(aCur++));
    }
    if (prolog) {
        System.out.print("</div>");
    }
}
Also used : Edit(org.eclipse.jgit.diff.Edit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RawText(org.eclipse.jgit.diff.RawText) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) EditList(org.eclipse.jgit.diff.EditList) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Test(org.junit.Test)

Example 3 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project fabric8 by jboss-fuse.

the class GitConflictResolutionIT method reportingDiffs.

@Test
public void reportingDiffs() throws Exception {
    prepareChanges();
    ObjectReader reader = git.getRepository().newObjectReader();
    RevWalk rw = new RevWalk(git.getRepository());
    CanonicalTreeParser ctp1 = new CanonicalTreeParser();
    CanonicalTreeParser ctp2 = new CanonicalTreeParser();
    CanonicalTreeParser ctp3 = new CanonicalTreeParser();
    ctp1.reset(reader, rw.parseCommit(git.getRepository().resolve("master")).getTree());
    ctp2.reset(reader, rw.parseCommit(git.getRepository().resolve("custom")).getTree());
    ctp3.reset(reader, rw.parseCommit(git.getRepository().resolve("patched")).getTree());
    TreeWalk walk = new TreeWalk(reader);
    walk.addTree(ctp1);
    walk.addTree(ctp3);
    walk.setRecursive(true);
    List<DiffEntry> diffs = DiffEntry.scan(walk);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DiffFormatter df = new DiffFormatter(baos);
    df.setDiffAlgorithm(DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM));
    df.setDiffComparator(RawTextComparator.DEFAULT);
    df.setRepository(git.getRepository());
    df.format(diffs.get(0));
    // System.out.println(new String(baos.toByteArray()));
    AbbreviatedObjectId id1 = diffs.get(0).getOldId();
    AbbreviatedObjectId id2 = diffs.get(0).getNewId();
    byte[] bytes1 = reader.open(id1.toObjectId()).getBytes();
    byte[] bytes2 = reader.open(id2.toObjectId()).getBytes();
    RawText rt1 = new RawText(bytes1);
    RawText rt2 = new RawText(bytes2);
    EditList edits = DiffAlgorithm.getAlgorithm(DiffAlgorithm.SupportedAlgorithm.HISTOGRAM).diff(RawTextComparator.DEFAULT, rt1, rt2);
    int aCur = 0;
    for (Edit curEdit : edits) {
        boolean prolog = aCur < curEdit.getBeginA();
        if (prolog) {
            System.out.print("<div class=\"edit unchanged\">");
        }
        while (aCur < curEdit.getBeginA()) {
            System.out.println(rt1.getString(aCur++));
        }
        if (prolog) {
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.INSERT) {
            System.out.print("<div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            System.out.print("</div>");
        }
        if (curEdit.getType() == Edit.Type.REPLACE) {
            System.out.print("<div class=\"edit changed\"><div class=\"edit removed\">");
            for (int i = curEdit.getBeginA(); i < curEdit.getEndA(); i++) {
                System.out.println(rt1.getString(i));
            }
            System.out.print("</div><div class=\"edit added\">");
            for (int i = curEdit.getBeginB(); i < curEdit.getEndB(); i++) {
                System.out.println(rt2.getString(i));
            }
            aCur = curEdit.getEndA();
            System.out.print("</div></div>");
        }
    }
    boolean prolog = aCur < rt1.size();
    if (prolog) {
        System.out.print("<div class=\"edit unchanged\">");
    }
    while (aCur < rt1.size()) {
        System.out.println(rt1.getString(aCur++));
    }
    if (prolog) {
        System.out.print("</div>");
    }
}
Also used : Edit(org.eclipse.jgit.diff.Edit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RawText(org.eclipse.jgit.diff.RawText) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) EditList(org.eclipse.jgit.diff.EditList) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Test(org.junit.Test)

Example 4 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project egit by eclipse.

the class StagedChangeCache method build.

/**
 * @param repo
 *            repository which should be scanned
 * @return list of changes in git staging area
 */
public static Map<String, Change> build(Repository repo) {
    try (TreeWalk tw = new TreeWalk(repo)) {
        tw.addTree(new DirCacheIterator(repo.readDirCache()));
        ObjectId headId = repo.resolve(HEAD);
        RevCommit headCommit = null;
        if (headId != null) {
            try (RevWalk rw = new RevWalk(repo)) {
                headCommit = rw.parseCommit(headId);
            }
        }
        AbbreviatedObjectId commitId;
        if (headCommit != null) {
            tw.addTree(headCommit.getTree());
            commitId = AbbreviatedObjectId.fromObjectId(headCommit);
        } else {
            tw.addTree(new EmptyTreeIterator());
            commitId = AbbreviatedObjectId.fromObjectId(zeroId());
        }
        tw.setRecursive(true);
        headCommit = null;
        MutableObjectId idBuf = new MutableObjectId();
        Map<String, Change> result = new HashMap<String, Change>();
        while (tw.next()) {
            if (!shouldIncludeEntry(tw))
                continue;
            Change change = new Change();
            change.name = tw.getNameString();
            change.remoteCommitId = commitId;
            tw.getObjectId(idBuf, 0);
            change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
            tw.getObjectId(idBuf, 1);
            change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);
            calculateAndSetChangeKind(RIGHT, change);
            result.put(tw.getPathString(), change);
        }
        return result;
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
        return new HashMap<String, Change>(0);
    }
}
Also used : MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) HashMap(java.util.HashMap) Change(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Change) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with AbbreviatedObjectId

use of org.eclipse.jgit.lib.AbbreviatedObjectId in project egit by eclipse.

the class SquashCommitsOperation method execute.

@Override
public void execute(IProgressMonitor m) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor pm) throws CoreException {
            SubMonitor progress = SubMonitor.convert(pm, 2);
            progress.subTask(MessageFormat.format(CoreText.SquashCommitsOperation_squashing, Integer.valueOf(commits.size())));
            InteractiveHandler handler = new InteractiveHandler() {

                @Override
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    RevCommit firstCommit = commits.get(0);
                    for (RebaseTodoLine step : steps) {
                        if (isRelevant(step.getCommit())) {
                            try {
                                if (step.getCommit().prefixCompare(firstCommit) == 0)
                                    step.setAction(RebaseTodoLine.Action.PICK);
                                else
                                    step.setAction(RebaseTodoLine.Action.SQUASH);
                            } catch (IllegalTodoFileModification e) {
                            // shouldn't happen
                            }
                        }
                    }
                }

                private boolean isRelevant(AbbreviatedObjectId id) {
                    for (RevCommit commit : commits) {
                        if (id.prefixCompare(commit) == 0)
                            return true;
                    }
                    return false;
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return messageHandler.modifyCommitMessage(oldMessage);
                }
            };
            try (Git git = new Git(repository)) {
                RebaseCommand command = git.rebase().setUpstream(commits.get(0).getParent(0)).runInteractively(handler).setOperation(RebaseCommand.Operation.BEGIN);
                MergeStrategy strategy = Activator.getDefault().getPreferredMergeStrategy();
                if (strategy != null) {
                    command.setStrategy(strategy);
                }
                command.call();
            } catch (GitAPIException e) {
                throw new TeamException(e.getLocalizedMessage(), e.getCause());
            }
            progress.worked(1);
            ProjectUtil.refreshValidProjects(ProjectUtil.getValidOpenProjects(repository), progress.newChild(1));
        }
    };
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, m);
}
Also used : RebaseTodoLine(org.eclipse.jgit.lib.RebaseTodoLine) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) IllegalTodoFileModification(org.eclipse.jgit.errors.IllegalTodoFileModification) RebaseCommand(org.eclipse.jgit.api.RebaseCommand) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) SubMonitor(org.eclipse.core.runtime.SubMonitor) InteractiveHandler(org.eclipse.jgit.api.RebaseCommand.InteractiveHandler) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) TeamException(org.eclipse.team.core.TeamException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Git(org.eclipse.jgit.api.Git) AbbreviatedObjectId(org.eclipse.jgit.lib.AbbreviatedObjectId) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

AbbreviatedObjectId (org.eclipse.jgit.lib.AbbreviatedObjectId)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)2 Edit (org.eclipse.jgit.diff.Edit)2 EditList (org.eclipse.jgit.diff.EditList)2 RawText (org.eclipse.jgit.diff.RawText)2 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IResource (org.eclipse.core.resources.IResource)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1