1 from __future__
import division
, absolute_import
, unicode_literals
6 from .. import observable
8 from ..cmd
import Command
11 AUTOCOMPLETE_PATHS
= 'cola.autocompletepaths'
12 AUTOTEMPLATE
= 'cola.autoloadcommittemplate'
13 BACKGROUND_EDITOR
= 'cola.backgroundeditor'
14 BLAME_VIEWER
= 'cola.blameviewer'
15 BOLD_HEADERS
= 'cola.boldheaders'
16 CHECK_CONFLICTS
= 'cola.checkconflicts'
17 CHECK_PUBLISHED_COMMITS
= 'cola.checkpublishedcommits'
18 COMMENT_CHAR
= 'core.commentchar'
19 DIFFCONTEXT
= 'gui.diffcontext'
20 DIFFTOOL
= 'diff.tool'
21 DISPLAY_UNTRACKED
= 'gui.displayuntracked'
23 EXPANDTAB
= 'cola.expandtab'
24 FONTDIFF
= 'cola.fontdiff'
26 HISTORY_BROWSER
= 'gui.historybrowser'
27 ICON_THEME
= 'cola.icontheme'
28 LINEBREAK
= 'cola.linebreak'
29 MAXRECENT
= 'cola.maxrecent'
30 MERGE_DIFFSTAT
= 'merge.diffstat'
31 MERGE_KEEPBACKUP
= 'merge.keepbackup'
32 MERGE_SUMMARY
= 'merge.summary'
33 MERGE_VERBOSITY
= 'merge.verbosity'
34 MERGETOOL
= 'merge.tool'
35 RESIZE_BROWSER_COLUMNS
= 'cola.resizebrowsercolumns'
36 SAFE_MODE
= 'cola.safemode'
37 SAVEWINDOWSETTINGS
= 'cola.savewindowsettings'
38 SHOW_PATH
= 'cola.showpath'
39 SORT_BOOKMARKS
= 'cola.sortbookmarks'
40 SPELL_CHECK
= 'cola.spellcheck'
41 STATUS_INDENT
= 'cola.statusindent'
42 STATUS_SHOW_TOTALS
= 'cola.statusshowtotals'
44 TABWIDTH
= 'cola.tabwidth'
45 TEXTWIDTH
= 'cola.textwidth'
46 USER_EMAIL
= 'user.email'
47 USER_NAME
= 'user.name'
50 class Defaults(object):
51 """Read-only class for holding defaults that get overridden"""
53 # These should match Git's defaults for git-defined values.
55 blame_viewer
= 'git gui blame'
57 check_conflicts
= True
58 check_published_commits
= True
60 display_untracked
= True
65 history_browser
= 'gitk'
66 icon_theme
= 'default'
71 merge_keep_backup
= True
74 resize_browser_columns
= False
75 save_window_settings
= True
77 autocomplete_paths
= True
84 hidpi
= hidpi
.Option
.AUTO
86 status_show_totals
= False
89 def blame_viewer(context
):
90 """Return the configured "blame" viewer"""
91 default
= Defaults
.blame_viewer
92 return context
.cfg
.get(BLAME_VIEWER
, default
=default
)
95 def bold_headers(context
):
96 """Should we bold the Status column headers?"""
97 return context
.cfg
.get(BOLD_HEADERS
, default
=Defaults
.bold_headers
)
100 def check_conflicts(context
):
101 """Should we check for merge conflict markers in unmerged files?"""
102 return context
.cfg
.get(CHECK_CONFLICTS
, default
=Defaults
.check_conflicts
)
105 def check_published_commits(context
):
106 """Should we check for published commits when amending?"""
107 return context
.cfg
.get(
108 CHECK_PUBLISHED_COMMITS
, default
=Defaults
.check_published_commits
112 def display_untracked(context
):
113 """Should we display untracked files?"""
114 return context
.cfg
.get(DISPLAY_UNTRACKED
, default
=Defaults
.display_untracked
)
118 """Return the configured editor"""
119 app
= context
.cfg
.get(EDITOR
, default
=Defaults
.editor
)
120 return _remap_editor(app
)
123 def background_editor(context
):
124 """Return the configured non-blocking background editor"""
125 app
= context
.cfg
.get(BACKGROUND_EDITOR
, default
=editor(context
))
126 return _remap_editor(app
)
129 def _remap_editor(app
):
130 """Remap a configured editorinto a visual editor name"""
131 # We do this for vim users because this configuration is convenient.
132 return {'vim': 'gvim -f'}.get(app
, app
)
135 def comment_char(context
):
136 """Return the configured git commit comment character"""
137 return context
.cfg
.get(COMMENT_CHAR
, default
=Defaults
.comment_char
)
140 def default_history_browser():
141 """Return the default history browser (e.g. git-dag, gitk)"""
143 # On Windows, a sensible default is "python git-cola dag"
144 # which is different than `gitk` below, but is preferred
145 # because we don't have to guess paths.
146 git_cola
= sys
.argv
[0].replace('\\', '/')
147 python
= sys
.executable
.replace('\\', '/')
148 cwd
= core
.getcwd().replace('\\', '/')
149 argv
= [python
, git_cola
, 'dag', '--repo', cwd
]
150 argv
= core
.prep_for_subprocess(argv
)
151 default
= core
.list2cmdline(argv
)
153 # The `gitk` script can be launched as-is on unix
154 default
= Defaults
.history_browser
158 def history_browser(context
):
159 """Return the configured history browser"""
160 default
= default_history_browser()
161 return context
.cfg
.get(HISTORY_BROWSER
, default
=default
)
164 def linebreak(context
):
165 """Should we word-wrap lines in the commit message editor?"""
166 return context
.cfg
.get(LINEBREAK
, default
=Defaults
.linebreak
)
169 def maxrecent(context
):
170 """Return the configured maximum number of Recent Repositories"""
171 value
= Defaults
.maxrecent
173 value
= context
.cfg
.get(MAXRECENT
, default
=value
)
177 def spellcheck(context
):
178 """Should we spellcheck commit messages?"""
179 return context
.cfg
.get(SPELL_CHECK
, default
=Defaults
.spellcheck
)
182 def expandtab(context
):
183 """Should we expand tabs in commit messages?"""
184 return context
.cfg
.get(EXPANDTAB
, default
=Defaults
.expandtab
)
187 def sort_bookmarks(context
):
188 """Should we sort bookmarks by name?"""
189 return context
.cfg
.get(SORT_BOOKMARKS
, default
=Defaults
.sort_bookmarks
)
192 def tabwidth(context
):
193 """Return the configured tab width in the commit message editor"""
194 return context
.cfg
.get(TABWIDTH
, default
=Defaults
.tabwidth
)
197 def textwidth(context
):
198 """Return the configured text width for word wrapping commit messages"""
199 return context
.cfg
.get(TEXTWIDTH
, default
=Defaults
.textwidth
)
202 def status_indent(context
):
203 """Should we indent items in the status widget?"""
204 return context
.cfg
.get(STATUS_INDENT
, default
=Defaults
.status_indent
)
207 def status_show_totals(context
):
208 """Should we display count totals in the status widget headers?"""
209 return context
.cfg
.get(STATUS_SHOW_TOTALS
, default
=Defaults
.status_show_totals
)
212 class PreferencesModel(observable
.Observable
):
213 """Interact with repo-local and user-global git config preferences"""
215 message_config_updated
= 'config_updated'
217 def __init__(self
, context
):
218 observable
.Observable
.__init
__(self
)
219 self
.context
= context
220 self
.config
= context
.cfg
222 def set_config(self
, source
, config
, value
):
223 """Set a configuration value"""
225 self
.config
.set_repo(config
, value
)
227 self
.config
.set_user(config
, value
)
228 message
= self
.message_config_updated
229 self
.notify_observers(message
, source
, config
, value
)
231 def get_config(self
, source
, config
):
232 """Get a configured value"""
234 value
= self
.config
.get_repo(config
)
236 value
= self
.config
.get(config
)
240 class SetConfig(Command
):
241 """Store a gitconfig value"""
245 def __init__(self
, model
, source
, config
, value
):
249 self
.old_value
= None
253 """Modify the model and store the updated configuration"""
254 self
.old_value
= self
.model
.get_config(self
.source
, self
.config
)
255 self
.model
.set_config(self
.source
, self
.config
, self
.value
)
258 """Restore the configuration change to its original value"""
259 if self
.old_value
is None:
261 self
.model
.set_config(self
.source
, self
.config
, self
.old_value
)