Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / commit / CommitEditorInputFactory.java
blob0702838245c7435907b0c564bdb2290f09410bf9
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
10 * Contributors:
11 * Kevin Sawicki (GitHub Inc.) - initial API and implementation
12 *******************************************************************************/
13 package org.eclipse.egit.ui.internal.commit;
15 import java.io.File;
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;
27 /**
28 * Element factory for saving and restoring the state of a
29 * {@link CommitEditorInput} instance.
31 public class CommitEditorInputFactory implements IElementFactory {
33 /**
34 * ID
36 public static final String ID = "org.eclipse.egit.ui.internal.commit.CommitEditorInputFactory"; //$NON-NLS-1$
38 /**
39 * COMMIT
41 public static final String COMMIT = "commit"; //$NON-NLS-1$
43 /**
44 * PATH
46 public static final String PATH = "path"; //$NON-NLS-1$
48 /**
49 * STASH
51 public static final String STASH = "stash"; //$NON-NLS-1$
53 /**
54 * Save state of input to memento
56 * @param memento
57 * @param input
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()
63 .getAbsolutePath());
64 memento.putBoolean(STASH, commit.isStash());
67 /**
68 * Get repository from memento
70 * @param memento
71 * @return repository
73 protected Repository getRepository(IMemento memento) {
74 String path = memento.getString(PATH);
75 if (path == null)
76 return null;
78 File gitDir = new File(path);
79 if (!gitDir.exists())
80 return null;
82 try {
83 return Activator.getDefault().getRepositoryCache()
84 .lookupRepository(gitDir);
85 } catch (IOException e) {
86 return null;
90 /**
91 * Get commit from memento and repository
93 * @param memento
94 * @param repository
95 * @return rev commit
97 protected RepositoryCommit getCommit(IMemento memento, Repository repository) {
98 String id = memento.getString(COMMIT);
99 if (id == null)
100 return null;
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(
107 repository, commit);
108 Boolean isStash = memento.getBoolean(STASH);
109 if (isStash != null)
110 repositoryCommit.setStash(isStash.booleanValue());
111 return repositoryCommit;
112 } catch (IOException e) {
113 return null;
118 * @see org.eclipse.ui.IElementFactory#createElement(org.eclipse.ui.IMemento)
120 @Override
121 public IAdaptable createElement(IMemento memento) {
122 Repository repository = getRepository(memento);
123 if (repository == null)
124 return null;
126 RepositoryCommit commit = getCommit(memento, repository);
127 if (commit == null)
128 return null;
130 return new CommitEditorInput(commit);