Set module specification to 1.0.
[nbgit.git] / src / org / netbeans / modules / git / GitModuleConfig.java
blob8fea306647fcfa3dec05ca9a414d495a00a2ed5a
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git;
44 import java.io.File;
45 import java.util.ArrayList;
46 import java.util.Collection;
47 import java.util.HashSet;
48 import java.util.Iterator;
49 import java.util.List;
50 import java.util.Properties;
51 import java.util.Set;
52 import java.util.prefs.Preferences;
53 import java.util.regex.Pattern;
54 import org.netbeans.modules.git.config.GitConfigFiles;
55 import org.netbeans.modules.git.ui.repository.RepositoryConnection;
56 import org.netbeans.modules.git.util.GitCommand;
57 import org.netbeans.modules.versioning.util.TableSorter;
58 import org.netbeans.modules.versioning.util.Utils;
59 import org.openide.util.NbPreferences;
61 /**
62 * Stores Git module configuration.
64 * @author alexbcoles
66 public class GitModuleConfig {
68 public static final String PROP_IGNORED_FILEPATTERNS = "ignoredFilePatterns"; // NOI18N
69 public static final String PROP_COMMIT_EXCLUSIONS = "commitExclusions"; // NOI18N
70 public static final String PROP_DEFAULT_VALUES = "defaultValues"; // NOI18N
71 public static final String PROP_RUN_VERSION = "runVersion"; // NOI18N
72 public static final String KEY_EXECUTABLE_BINARY = "gitExecBinary"; // NOI18N
73 public static final String KEY_EXPORT_FILENAME = "gitExportFilename"; // NOI18N
74 public static final String KEY_EXPORT_FOLDER = "gitExportFolder"; // NOI18N
75 public static final String KEY_IMPORT_FOLDER = "gitImportFolder"; // NOI18N
76 public static final String KEY_ANNOTATION_FORMAT = "annotationFormat"; // NOI18N
77 public static final String SAVE_PASSWORD = "savePassword"; // NOI18N
78 public static final String KEY_BACKUP_ON_REVERTMODS = "backupOnRevert"; // NOI18N
79 public static final String KEY_SHOW_HITORY_MERGES = "showHistoryMerges"; // NOI18N
81 private static final String RECENT_URL = "repository.recentURL"; // NOI18N
82 private static final String SHOW_CLONE_COMPLETED = "cloneCompleted.showCloneCompleted"; // NOI18N
84 private static final String SET_MAIN_PROJECT = "cloneCompleted.setMainProject"; // NOI18N
86 private static final String URL_EXP = "annotator.urlExp"; // NOI18N
87 private static final String ANNOTATION_EXP = "annotator.annotationExp"; // NOI18N
89 public static final String TEXT_ANNOTATIONS_FORMAT_DEFAULT = "{DEFAULT}"; // NOI18N
91 private static final String DEFAULT_EXPORT_FILENAME = "%b_%r_%h"; // NOI18N
92 private static final GitModuleConfig INSTANCE = new GitModuleConfig();
94 private static String email;
95 private static String userName;
97 public static GitModuleConfig getDefault() {
98 return INSTANCE;
101 private Set<String> exclusions;
103 // properties ~~~~~~~~~~~~~~~~~~~~~~~~~
105 public Preferences getPreferences() {
106 return NbPreferences.forModule(GitModuleConfig.class);
109 public boolean getShowCloneCompleted() {
110 return getPreferences().getBoolean(SHOW_CLONE_COMPLETED, true);
113 public boolean getSetMainProject() {
114 return getPreferences().getBoolean(SET_MAIN_PROJECT, true);
117 public Pattern [] getIgnoredFilePatterns() {
118 return getDefaultFilePatterns();
121 public boolean isExcludedFromCommit(String path) {
122 return getCommitExclusions().contains(path);
126 * @param paths collection of paths, of File.getAbsolutePath()
128 public void addExclusionPaths(Collection<String> paths) {
129 Set<String> exclusions = getCommitExclusions();
130 if (exclusions.addAll(paths)) {
131 Utils.put(getPreferences(), PROP_COMMIT_EXCLUSIONS, new ArrayList<String>(exclusions));
136 * @param paths collection of paths, File.getAbsolutePath()
138 public void removeExclusionPaths(Collection<String> paths) {
139 Set<String> exclusions = getCommitExclusions();
140 if (exclusions.removeAll(paths)) {
141 Utils.put(getPreferences(), PROP_COMMIT_EXCLUSIONS, new ArrayList<String>(exclusions));
145 public String getExecutableBinaryPath() {
146 return (String) getPreferences().get(KEY_EXECUTABLE_BINARY, ""); // NOI18N
148 public boolean getBackupOnRevertModifications() {
149 return getPreferences().getBoolean(KEY_BACKUP_ON_REVERTMODS, true);
152 public void setBackupOnRevertModifications(boolean bBackup) {
153 getPreferences().putBoolean(KEY_BACKUP_ON_REVERTMODS, bBackup);
156 public boolean getShowHistoryMerges() {
157 return getPreferences().getBoolean(KEY_SHOW_HITORY_MERGES, true);
160 public void setShowHistoryMerges(boolean bShowMerges) {
161 getPreferences().putBoolean(KEY_SHOW_HITORY_MERGES, bShowMerges);
164 public void setExecutableBinaryPath(String path) {
165 getPreferences().put(KEY_EXECUTABLE_BINARY, path);
168 public String getExportFolder() {
169 return (String) getPreferences().get(KEY_EXPORT_FOLDER, System.getProperty("user.home")); // NOI18N
172 public void setExportFolder(String path) {
173 getPreferences().put(KEY_EXPORT_FOLDER, path);
176 public String getImportFolder() {
177 return (String) getPreferences().get(KEY_IMPORT_FOLDER, System.getProperty("user.home")); // NOI18N
180 public void setImportFolder(String path) {
181 getPreferences().put(KEY_IMPORT_FOLDER, path);
184 public String getExportFilename() {
185 String str = (String) getPreferences().get(KEY_EXPORT_FILENAME, ""); // NOI18N
186 if (str.trim().length() == 0) str = DEFAULT_EXPORT_FILENAME;
187 return str;
190 public void setExportFilename(String path) {
191 getPreferences().put(KEY_EXPORT_FILENAME, path);
195 * This method returns the email address specified in $HOME/.gitconfig
196 * or a default email address if none is found.
198 public String getEmail() {
199 email = GitConfigFiles.getInstance().getEmail();
200 if (email.length() == 0) {
201 // nothing
202 // TODO: does NetBeans provide this with product registration?
203 // if not, then get this information in setup wizard.
205 return email;
209 * This method returns the username specified in $HOME/.gitconfig
210 * or a default username if none is found.
212 public String getUserName() {
213 userName = GitConfigFiles.getInstance().getUserName();
214 if (userName.length() == 0) {
215 String userId = System.getProperty("user.name"); // NOI18N
217 return userName;
220 public void addGitkExtension() {
221 GitConfigFiles.getInstance().setProperty("XXXXX", "");
224 public void setEmail(String email) {
225 GitConfigFiles.getInstance().setEmail(email);
228 public void setUserName(String name) {
229 GitConfigFiles.getInstance().setUserName(name);
232 public Boolean isUserNameValid(String name) {
233 if (userName == null) getUserName();
234 if (name.equals(userName)) return true;
235 if (name.length() == 0) return true;
236 return GitMail.isUserNameValid(name);
239 public Boolean isExecPathValid(String name) {
240 if (name.length() == 0) return true;
241 File file = new File(name, GitCommand.GIT_COMMAND); // NOI18N
242 // I would like to call canExecute but that requires Java SE 6.
243 if(file.exists() && file.isFile()) return true;
245 // TODO: add Legacy OS (Win32) support
246 //file = new File(name, GitCommand.GIT_COMMAND + GitCommand.GIT_WINDOWS_EXE); // NOI18N
247 return file.exists() && file.isFile();
250 public Properties getProperties(File file) {
251 Properties props = new Properties();
252 GitConfigFiles gitconfig = new GitConfigFiles(file);
253 String email = gitconfig.getEmail(false);
254 String name = gitconfig.getUserName(false);
255 if (email.length() == 0)
256 email = getEmail();
257 if (email.length() > 0)
258 props.setProperty("email", email);
259 else
260 props.setProperty("email", "");
261 if (name.length() == 0)
262 name = getUserName();
263 if (name.length() > 0)
264 props.setProperty("name", name); // NOI18N
265 else
266 props.setProperty("name", ""); // NOI18N
267 name = gitconfig.getDefaultPull(false);
268 if (name.length() > 0)
269 props.setProperty("default-pull", name); // NOI18N
270 else
271 props.setProperty("default-pull", ""); // NOI18N
272 name = gitconfig.getDefaultPush(false);
273 if (name.length() > 0)
274 props.setProperty("default-push", name); // NOI18N
275 else
276 props.setProperty("default-push", ""); // NOI18N
277 return props;
280 public void clearProperties(File file, String section) {
281 getGitConfigFiles(file).clearProperties(section);
284 public void removeProperty(File file, String section, String name) {
285 getGitConfigFiles(file).removeProperty(section, name);
288 public void setProperty(File file, String name, String value) {
289 getGitConfigFiles(file).setProperty(name, value);
292 public void setProperty(File file, String section, String name, String value, boolean allowEmpty) {
293 getGitConfigFiles(file).setProperty(section, name, value, allowEmpty);
296 public void setProperty(File file, String section, String name, String value) {
297 getGitConfigFiles(file).setProperty(section, name, value);
301 * Get all properties for a particular section
303 public Properties getProperties(File file, String section) {
304 return getGitConfigFiles(file).getProperties(section);
307 private GitConfigFiles getGitConfigFiles(File file) {
308 if (file == null) {
309 return GitConfigFiles.getInstance();
310 } else {
311 return new GitConfigFiles(file);
315 public String getAnnotationFormat() {
316 return (String) getPreferences().get(KEY_ANNOTATION_FORMAT, getDefaultAnnotationFormat());
319 public String getDefaultAnnotationFormat() {
320 return "[{" + GitAnnotator.ANNOTATION_STATUS + "} {" + GitAnnotator.ANNOTATION_FOLDER + "}]"; // NOI18N
323 public void setAnnotationFormat(String annotationFormat) {
324 getPreferences().put(KEY_ANNOTATION_FORMAT, annotationFormat);
327 public boolean getSavePassword() {
328 return getPreferences().getBoolean(SAVE_PASSWORD, true);
331 public void setSavePassword(boolean bl) {
332 getPreferences().putBoolean(SAVE_PASSWORD, bl);
335 public void setShowCloneCompleted(boolean bl) {
336 getPreferences().putBoolean(SHOW_CLONE_COMPLETED, bl);
339 public void setSetMainProject(boolean bl) {
340 getPreferences().putBoolean(SET_MAIN_PROJECT, bl);
343 public RepositoryConnection getRepositoryConnection(String url) {
344 List<RepositoryConnection> rcs = getRecentUrls();
345 for (Iterator<RepositoryConnection> it = rcs.iterator(); it.hasNext();) {
346 RepositoryConnection rc = it.next();
347 if(url.equals(rc.getUrl())) {
348 return rc;
351 return null;
354 public void insertRecentUrl(RepositoryConnection rc) {
355 Preferences prefs = getPreferences();
357 List<String> urlValues = Utils.getStringList(prefs, RECENT_URL);
358 for (Iterator<String> it = urlValues.iterator(); it.hasNext();) {
359 String rcOldString = it.next();
360 RepositoryConnection rcOld = RepositoryConnection.parse(rcOldString);
361 if(rcOld.equals(rc)) {
362 Utils.removeFromArray(prefs, RECENT_URL, rcOldString);
365 Utils.insert(prefs, RECENT_URL, RepositoryConnection.getString(rc), -1);
368 public void setRecentUrls(List<RepositoryConnection> recentUrls) {
369 List<String> urls = new ArrayList<String>(recentUrls.size());
371 int idx = 0;
372 for (Iterator<RepositoryConnection> it = recentUrls.iterator(); it.hasNext();) {
373 idx++;
374 RepositoryConnection rc = it.next();
375 urls.add(RepositoryConnection.getString(rc));
377 Preferences prefs = getPreferences();
378 Utils.put(prefs, RECENT_URL, urls);
381 public List<RepositoryConnection> getRecentUrls() {
382 Preferences prefs = getPreferences();
383 List<String> urls = Utils.getStringList(prefs, RECENT_URL);
384 List<RepositoryConnection> ret = new ArrayList<RepositoryConnection>(urls.size());
385 for (Iterator<String> it = urls.iterator(); it.hasNext();) {
386 RepositoryConnection rc = RepositoryConnection.parse(it.next());
387 ret.add(rc);
389 return ret;
392 //public void setAnnotationExpresions(List<AnnotationExpression> exps) {
393 // List<String> urlExp = new ArrayList<String>(exps.size());
394 // List<String> annotationExp = new ArrayList<String>(exps.size());
396 // int idx = 0;
397 // for (Iterator<AnnotationExpression> it = exps.iterator(); it.hasNext();) {
398 // idx++;
399 // AnnotationExpression exp = it.next();
400 // urlExp.add(exp.getUrlExp());
401 // annotationExp.add(exp.getAnnotationExp());
402 // }
404 // Preferences prefs = getPreferences();
405 // Utils.put(prefs, URL_EXP, urlExp);
406 // Utils.put(prefs, ANNOTATION_EXP, annotationExp);
409 //public List<AnnotationExpression> getAnnotationExpresions() {
410 // Preferences prefs = getPreferences();
411 // List<String> urlExp = Utils.getStringList(prefs, URL_EXP);
412 // List<String> annotationExp = Utils.getStringList(prefs, ANNOTATION_EXP);
414 // List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(urlExp.size());
415 // for (int i = 0; i < urlExp.size(); i++) {
416 // ret.add(new AnnotationExpression(urlExp.get(i), annotationExp.get(i)));
417 // }
418 // if(ret.size() < 1) {
419 // ret = getDefaultAnnotationExpresions();
420 // }
421 // return ret;
424 //public List<AnnotationExpression> getDefaultAnnotationExpresions() {
425 // List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(1);
426 // ret.add(new AnnotationExpression(".*/(branches|tags)/(.+?)/.*", "\\2")); // NOI18N
427 // return ret;
430 // TODO: persist state
432 private TableSorter importTableSorter;
433 private TableSorter commitTableSorter;
435 public TableSorter getImportTableSorter() {
436 return importTableSorter;
439 public void setImportTableSorter(TableSorter sorter) {
440 importTableSorter = sorter;
443 public TableSorter getCommitTableSorter() {
444 return commitTableSorter;
447 public void setCommitTableSorter(TableSorter sorter) {
448 commitTableSorter = sorter;
451 // private methods ~~~~~~~~~~~~~~~~~~
453 private synchronized Set<String> getCommitExclusions() {
454 if (exclusions == null) {
455 exclusions = new HashSet<String>(Utils.getStringList(getPreferences(), PROP_COMMIT_EXCLUSIONS));
457 return exclusions;
460 private static Pattern[] getDefaultFilePatterns() {
461 return new Pattern [] {
462 Pattern.compile("cvslog\\..*"), // NOI18N
463 Pattern.compile("\\.make\\.state"), // NOI18N
464 Pattern.compile("\\.nse_depinfo"), // NOI18N
465 Pattern.compile(".*~"), // NOI18N
466 Pattern.compile("#.*"), // NOI18N
467 Pattern.compile("\\.#.*"), // NOI18N
468 Pattern.compile(",.*"), // NOI18N
469 Pattern.compile("_\\$.*"), // NOI18N
470 Pattern.compile(".*\\$"), // NOI18N
471 Pattern.compile(".*\\.old"), // NOI18N
472 Pattern.compile(".*\\.bak"), // NOI18N
473 Pattern.compile(".*\\.BAK"), // NOI18N
474 Pattern.compile(".*\\.orig"), // NOI18N
475 Pattern.compile(".*\\.rej"), // NOI18N
476 Pattern.compile(".*\\.del-.*"), // NOI18N
477 Pattern.compile(".*\\.a"), // NOI18N
478 Pattern.compile(".*\\.olb"), // NOI18N
479 Pattern.compile(".*\\.o"), // NOI18N
480 Pattern.compile(".*\\.obj"), // NOI18N
481 Pattern.compile(".*\\.so"), // NOI18N
482 Pattern.compile(".*\\.exe"), // NOI18N
483 Pattern.compile(".*\\.Z"), // NOI18N
484 Pattern.compile(".*\\.elc"), // NOI18N
485 Pattern.compile(".*\\.ln"), // NOI18N