1 /*******************************************************************************
2 * Copyright (c) 2011 GitHub Inc.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
12 *******************************************************************************/
13 package org
.eclipse
.egit
.ui
.internal
.commit
;
16 import java
.io
.IOException
;
18 import org
.eclipse
.core
.runtime
.IAdaptable
;
19 import org
.eclipse
.egit
.core
.Activator
;
20 import org
.eclipse
.jgit
.lib
.ObjectId
;
21 import org
.eclipse
.jgit
.lib
.Repository
;
22 import org
.eclipse
.jgit
.revwalk
.RevCommit
;
23 import org
.eclipse
.jgit
.revwalk
.RevWalk
;
24 import org
.eclipse
.ui
.IElementFactory
;
25 import org
.eclipse
.ui
.IMemento
;
28 * Element factory for saving and restoring the state of a
29 * {@link CommitEditorInput} instance.
31 public class CommitEditorInputFactory
implements IElementFactory
{
36 public static final String ID
= "org.eclipse.egit.ui.internal.commit.CommitEditorInputFactory"; //$NON-NLS-1$
41 public static final String COMMIT
= "commit"; //$NON-NLS-1$
46 public static final String PATH
= "path"; //$NON-NLS-1$
51 public static final String STASH
= "stash"; //$NON-NLS-1$
54 * Save state of input to memento
59 public static void saveState(IMemento memento
, CommitEditorInput input
) {
60 RepositoryCommit commit
= input
.getCommit();
61 memento
.putString(COMMIT
, commit
.getRevCommit().name());
62 memento
.putString(PATH
, commit
.getRepository().getDirectory()
64 memento
.putBoolean(STASH
, commit
.isStash());
68 * Get repository from memento
73 protected Repository
getRepository(IMemento memento
) {
74 String path
= memento
.getString(PATH
);
78 File gitDir
= new File(path
);
83 return Activator
.getDefault().getRepositoryCache()
84 .lookupRepository(gitDir
);
85 } catch (IOException e
) {
91 * Get commit from memento and repository
97 protected RepositoryCommit
getCommit(IMemento memento
, Repository repository
) {
98 String id
= memento
.getString(COMMIT
);
102 try (RevWalk walk
= new RevWalk(repository
)) {
103 RevCommit commit
= walk
.parseCommit(ObjectId
.fromString(id
));
104 for (RevCommit parent
: commit
.getParents())
105 walk
.parseBody(parent
);
106 RepositoryCommit repositoryCommit
= new RepositoryCommit(
108 Boolean isStash
= memento
.getBoolean(STASH
);
110 repositoryCommit
.setStash(isStash
.booleanValue());
111 return repositoryCommit
;
112 } catch (IOException e
) {
118 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
121 public IAdaptable
createElement(IMemento memento
) {
122 Repository repository
= getRepository(memento
);
123 if (repository
== null)
126 RepositoryCommit commit
= getCommit(memento
, repository
);
130 return new CommitEditorInput(commit
);