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]"
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
;
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
;
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
;
62 * Stores Git module configuration.
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() {
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
;
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) {
202 // TODO: does NetBeans provide this with product registration?
203 // if not, then get this information in setup wizard.
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
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
isEmailValid(String email
) {
233 if (this.email
== null) getEmail();
234 if (email
.equals(email
)) return true;
235 if (email
.length() == 0) return false; // cannot be blank
236 return GitMail
.isEmailValid(email
);
239 public Boolean
isUserNameValid(String name
) {
240 if (userName
== null) getUserName();
241 if (name
.equals(userName
)) return true;
242 if (name
.length() == 0) return true;
246 public Boolean
isExecPathValid(String name
) {
247 if (name
.length() == 0) return true;
248 File file
= new File(name
, GitCommand
.GIT_COMMAND
); // NOI18N
249 // I would like to call canExecute but that requires Java SE 6.
250 if(file
.exists() && file
.isFile()) return true;
252 // TODO: add Legacy OS (Win32) support
253 //file = new File(name, GitCommand.GIT_COMMAND + GitCommand.GIT_WINDOWS_EXE); // NOI18N
254 return file
.exists() && file
.isFile();
257 public Properties
getProperties(File file
) {
258 Properties props
= new Properties();
259 GitConfigFiles gitconfig
= new GitConfigFiles(file
);
260 String email
= gitconfig
.getEmail(false);
261 String name
= gitconfig
.getUserName(false);
262 if (email
.length() == 0)
264 if (email
.length() > 0)
265 props
.setProperty("email", email
);
267 props
.setProperty("email", "");
268 if (name
.length() == 0)
269 name
= getUserName();
270 if (name
.length() > 0)
271 props
.setProperty("name", name
); // NOI18N
273 props
.setProperty("name", ""); // NOI18N
274 name
= gitconfig
.getDefaultPull(false);
275 if (name
.length() > 0)
276 props
.setProperty("default-pull", name
); // NOI18N
278 props
.setProperty("default-pull", ""); // NOI18N
279 name
= gitconfig
.getDefaultPush(false);
280 if (name
.length() > 0)
281 props
.setProperty("default-push", name
); // NOI18N
283 props
.setProperty("default-push", ""); // NOI18N
287 public void clearProperties(File file
, String section
) {
288 getGitConfigFiles(file
).clearProperties(section
);
291 public void removeProperty(File file
, String section
, String name
) {
292 getGitConfigFiles(file
).removeProperty(section
, name
);
295 public void setProperty(File file
, String name
, String value
) {
296 getGitConfigFiles(file
).setProperty(name
, value
);
299 public void setProperty(File file
, String section
, String name
, String value
, boolean allowEmpty
) {
300 getGitConfigFiles(file
).setProperty(section
, name
, value
, allowEmpty
);
303 public void setProperty(File file
, String section
, String name
, String value
) {
304 getGitConfigFiles(file
).setProperty(section
, name
, value
);
308 * Get all properties for a particular section
310 public Properties
getProperties(File file
, String section
) {
311 return getGitConfigFiles(file
).getProperties(section
);
314 private GitConfigFiles
getGitConfigFiles(File file
) {
316 return GitConfigFiles
.getInstance();
318 return new GitConfigFiles(file
);
322 public String
getAnnotationFormat() {
323 return (String
) getPreferences().get(KEY_ANNOTATION_FORMAT
, getDefaultAnnotationFormat());
326 public String
getDefaultAnnotationFormat() {
327 return "[{" + GitAnnotator
.ANNOTATION_STATUS
+ "} {" + GitAnnotator
.ANNOTATION_FOLDER
+ "}]"; // NOI18N
330 public void setAnnotationFormat(String annotationFormat
) {
331 getPreferences().put(KEY_ANNOTATION_FORMAT
, annotationFormat
);
334 public boolean getSavePassword() {
335 return getPreferences().getBoolean(SAVE_PASSWORD
, true);
338 public void setSavePassword(boolean bl
) {
339 getPreferences().putBoolean(SAVE_PASSWORD
, bl
);
342 public void setShowCloneCompleted(boolean bl
) {
343 getPreferences().putBoolean(SHOW_CLONE_COMPLETED
, bl
);
346 public void setSetMainProject(boolean bl
) {
347 getPreferences().putBoolean(SET_MAIN_PROJECT
, bl
);
350 public RepositoryConnection
getRepositoryConnection(String url
) {
351 List
<RepositoryConnection
> rcs
= getRecentUrls();
352 for (Iterator
<RepositoryConnection
> it
= rcs
.iterator(); it
.hasNext();) {
353 RepositoryConnection rc
= it
.next();
354 if(url
.equals(rc
.getUrl())) {
361 public void insertRecentUrl(RepositoryConnection rc
) {
362 Preferences prefs
= getPreferences();
364 List
<String
> urlValues
= Utils
.getStringList(prefs
, RECENT_URL
);
365 for (Iterator
<String
> it
= urlValues
.iterator(); it
.hasNext();) {
366 String rcOldString
= it
.next();
367 RepositoryConnection rcOld
= RepositoryConnection
.parse(rcOldString
);
368 if(rcOld
.equals(rc
)) {
369 Utils
.removeFromArray(prefs
, RECENT_URL
, rcOldString
);
372 Utils
.insert(prefs
, RECENT_URL
, RepositoryConnection
.getString(rc
), -1);
375 public void setRecentUrls(List
<RepositoryConnection
> recentUrls
) {
376 List
<String
> urls
= new ArrayList
<String
>(recentUrls
.size());
379 for (Iterator
<RepositoryConnection
> it
= recentUrls
.iterator(); it
.hasNext();) {
381 RepositoryConnection rc
= it
.next();
382 urls
.add(RepositoryConnection
.getString(rc
));
384 Preferences prefs
= getPreferences();
385 Utils
.put(prefs
, RECENT_URL
, urls
);
388 public List
<RepositoryConnection
> getRecentUrls() {
389 Preferences prefs
= getPreferences();
390 List
<String
> urls
= Utils
.getStringList(prefs
, RECENT_URL
);
391 List
<RepositoryConnection
> ret
= new ArrayList
<RepositoryConnection
>(urls
.size());
392 for (Iterator
<String
> it
= urls
.iterator(); it
.hasNext();) {
393 RepositoryConnection rc
= RepositoryConnection
.parse(it
.next());
399 //public void setAnnotationExpresions(List<AnnotationExpression> exps) {
400 // List<String> urlExp = new ArrayList<String>(exps.size());
401 // List<String> annotationExp = new ArrayList<String>(exps.size());
404 // for (Iterator<AnnotationExpression> it = exps.iterator(); it.hasNext();) {
406 // AnnotationExpression exp = it.next();
407 // urlExp.add(exp.getUrlExp());
408 // annotationExp.add(exp.getAnnotationExp());
411 // Preferences prefs = getPreferences();
412 // Utils.put(prefs, URL_EXP, urlExp);
413 // Utils.put(prefs, ANNOTATION_EXP, annotationExp);
416 //public List<AnnotationExpression> getAnnotationExpresions() {
417 // Preferences prefs = getPreferences();
418 // List<String> urlExp = Utils.getStringList(prefs, URL_EXP);
419 // List<String> annotationExp = Utils.getStringList(prefs, ANNOTATION_EXP);
421 // List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(urlExp.size());
422 // for (int i = 0; i < urlExp.size(); i++) {
423 // ret.add(new AnnotationExpression(urlExp.get(i), annotationExp.get(i)));
425 // if(ret.size() < 1) {
426 // ret = getDefaultAnnotationExpresions();
431 //public List<AnnotationExpression> getDefaultAnnotationExpresions() {
432 // List<AnnotationExpression> ret = new ArrayList<AnnotationExpression>(1);
433 // ret.add(new AnnotationExpression(".*/(branches|tags)/(.+?)/.*", "\\2")); // NOI18N
437 // TODO: persist state
439 private TableSorter importTableSorter
;
440 private TableSorter commitTableSorter
;
442 public TableSorter
getImportTableSorter() {
443 return importTableSorter
;
446 public void setImportTableSorter(TableSorter sorter
) {
447 importTableSorter
= sorter
;
450 public TableSorter
getCommitTableSorter() {
451 return commitTableSorter
;
454 public void setCommitTableSorter(TableSorter sorter
) {
455 commitTableSorter
= sorter
;
458 // private methods ~~~~~~~~~~~~~~~~~~
460 private synchronized Set
<String
> getCommitExclusions() {
461 if (exclusions
== null) {
462 exclusions
= new HashSet
<String
>(Utils
.getStringList(getPreferences(), PROP_COMMIT_EXCLUSIONS
));
467 private static Pattern
[] getDefaultFilePatterns() {
468 return new Pattern
[] {
469 Pattern
.compile("cvslog\\..*"), // NOI18N
470 Pattern
.compile("\\.make\\.state"), // NOI18N
471 Pattern
.compile("\\.nse_depinfo"), // NOI18N
472 Pattern
.compile(".*~"), // NOI18N
473 Pattern
.compile("#.*"), // NOI18N
474 Pattern
.compile("\\.#.*"), // NOI18N
475 Pattern
.compile(",.*"), // NOI18N
476 Pattern
.compile("_\\$.*"), // NOI18N
477 Pattern
.compile(".*\\$"), // NOI18N
478 Pattern
.compile(".*\\.old"), // NOI18N
479 Pattern
.compile(".*\\.bak"), // NOI18N
480 Pattern
.compile(".*\\.BAK"), // NOI18N
481 Pattern
.compile(".*\\.orig"), // NOI18N
482 Pattern
.compile(".*\\.rej"), // NOI18N
483 Pattern
.compile(".*\\.del-.*"), // NOI18N
484 Pattern
.compile(".*\\.a"), // NOI18N
485 Pattern
.compile(".*\\.olb"), // NOI18N
486 Pattern
.compile(".*\\.o"), // NOI18N
487 Pattern
.compile(".*\\.obj"), // NOI18N
488 Pattern
.compile(".*\\.so"), // NOI18N
489 Pattern
.compile(".*\\.exe"), // NOI18N
490 Pattern
.compile(".*\\.Z"), // NOI18N
491 Pattern
.compile(".*\\.elc"), // NOI18N
492 Pattern
.compile(".*\\.ln"), // NOI18N