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
;
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
;
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$
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();
46 return Collections
.emptySet();
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
)
65 * Save a new commit message.
69 public static void saveCommitHistory(String message
) {
70 if (message
== null || message
.length() == 0)
72 int size
= getCommitHistorySize();
76 XMLMemento memento
= XMLMemento
.createWriteRoot(KEY_MESSAGES
);
77 memento
.createChild(KEY_MESSAGE
).putTextData(message
);
81 Set
<String
> history
= getCommitHistory();
82 history
.remove(message
);
83 for (String previous
: history
) {
84 memento
.createChild(KEY_MESSAGE
).putTextData(previous
);
90 StringWriter writer
= new StringWriter();
93 getPreferenceStore().setValue(
94 UIPreferences
.COMMIT_DIALOG_HISTORY_MESSAGES
,
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
);