Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / commit / CommitMessageHistory.java
blob119d77a605f1891d964c2d2090c3f69eb126ffde
1 /*******************************************************************************
2 * Copyright (C) 2012, Kevin Sawicki <kevin@github.com>
3 * and other copyright owners as documented in the project's IP log.
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org.eclipse.egit.ui.internal.commit;
14 import java.io.IOException;
15 import java.io.StringReader;
16 import java.io.StringWriter;
17 import java.util.Collections;
18 import java.util.LinkedHashSet;
19 import java.util.Set;
21 import org.eclipse.egit.ui.UIPreferences;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.WorkbenchException;
25 import org.eclipse.ui.XMLMemento;
27 /**
28 * Provides access to read and add new saved commit messages.
30 public class CommitMessageHistory {
32 private static final String KEY_MESSAGE = "message"; //$NON-NLS-1$
34 private static final String KEY_MESSAGES = "messages"; //$NON-NLS-1$
36 /**
37 * @return saved commit messages
39 public static Set<String> getCommitHistory() {
40 String all = getPreferenceStore().getString(
41 UIPreferences.COMMIT_DIALOG_HISTORY_MESSAGES);
42 if (all.length() == 0)
43 return Collections.emptySet();
44 int max = getCommitHistorySize();
45 if (max < 1)
46 return Collections.emptySet();
47 XMLMemento memento;
48 try {
49 memento = XMLMemento.createReadRoot(new StringReader(all));
50 } catch (WorkbenchException e) {
51 org.eclipse.egit.ui.Activator.logError(
52 "Error reading commit message history", e); //$NON-NLS-1$
53 return Collections.emptySet();
55 Set<String> messages = new LinkedHashSet<>();
56 for (IMemento child : memento.getChildren(KEY_MESSAGE)) {
57 messages.add(child.getTextData());
58 if (messages.size() == max)
59 break;
61 return messages;
64 /**
65 * Save a new commit message.
67 * @param message
69 public static void saveCommitHistory(String message) {
70 if (message == null || message.length() == 0)
71 return;
72 int size = getCommitHistorySize();
73 if (size < 1)
74 return;
76 XMLMemento memento = XMLMemento.createWriteRoot(KEY_MESSAGES);
77 memento.createChild(KEY_MESSAGE).putTextData(message);
79 int count = 1;
80 if (count < size) {
81 Set<String> history = getCommitHistory();
82 history.remove(message);
83 for (String previous : history) {
84 memento.createChild(KEY_MESSAGE).putTextData(previous);
85 count++;
86 if (count == size)
87 break;
90 StringWriter writer = new StringWriter();
91 try {
92 memento.save(writer);
93 getPreferenceStore().setValue(
94 UIPreferences.COMMIT_DIALOG_HISTORY_MESSAGES,
95 writer.toString());
96 } catch (IOException e) {
97 org.eclipse.egit.ui.Activator.logError(
98 "Error writing commit message history", e); //$NON-NLS-1$
102 private static IPreferenceStore getPreferenceStore() {
103 return org.eclipse.egit.ui.Activator.getDefault().getPreferenceStore();
106 private static int getCommitHistorySize() {
107 return getPreferenceStore().getInt(
108 UIPreferences.COMMIT_DIALOG_HISTORY_SIZE);