1 """Provides the main() routine and ColaApplication"""
2 from functools
import partial
10 from qtpy
import QtCore
11 except ImportError as error
:
14 Your Python environment does not have qtpy and PyQt (or PySide).
15 The following error was encountered when importing "qtpy":
19 Install qtpy and PyQt (or PySide) into your Python environment.
20 On a Debian/Ubuntu system you can install these modules using apt:
22 sudo apt install python3-pyqt5 python3-pyqt5.qtwebengine python3-qtpy
30 from qtpy
import QtWidgets
31 from qtpy
.QtCore
import Qt
34 # Qt 5.12 / PyQt 5.13 is unable to use QtWebEngineWidgets unless it is
35 # imported before QApplication is constructed.
36 from qtpy
import QtWebEngineWidgets
# noqa
38 # QtWebEngineWidgets / QtWebKit is not available -- no big deal.
43 from .interaction
import Interaction
44 from .models
import main
45 from .models
import selection
46 from .widgets
import cfgactions
47 from .widgets
import standard
48 from .widgets
import startup
49 from .settings
import Session
50 from .settings
import Settings
54 from . import fsmonitor
61 from . import qtcompat
63 from . import resources
69 def setup_environment():
70 """Set environment variables to control git's behavior"""
71 # Allow Ctrl-C to exit
72 signal
.signal(signal
.SIGINT
, signal
.SIG_DFL
)
74 # Session management wants an absolute path when restarting
75 sys
.argv
[0] = sys_argv0
= os
.path
.abspath(sys
.argv
[0])
77 # Spoof an X11 display for SSH
78 os
.environ
.setdefault('DISPLAY', ':0')
80 if not core
.getenv('SHELL', ''):
81 for shell
in ('/bin/zsh', '/bin/bash', '/bin/sh'):
82 if os
.path
.exists(shell
):
83 compat
.setenv('SHELL', shell
)
86 # Setup the path so that git finds us when we run 'git cola'
87 path_entries
= core
.getenv('PATH', '').split(os
.pathsep
)
88 bindir
= core
.decode(os
.path
.dirname(sys_argv0
))
89 path_entries
.append(bindir
)
90 path
= os
.pathsep
.join(path_entries
)
91 compat
.setenv('PATH', path
)
93 # We don't ever want a pager
94 compat
.setenv('GIT_PAGER', '')
97 git_askpass
= core
.getenv('GIT_ASKPASS')
98 ssh_askpass
= core
.getenv('SSH_ASKPASS')
100 askpass
= git_askpass
102 askpass
= ssh_askpass
103 elif sys
.platform
== 'darwin':
104 askpass
= resources
.package_command('ssh-askpass-darwin')
106 askpass
= resources
.package_command('ssh-askpass')
108 compat
.setenv('GIT_ASKPASS', askpass
)
109 compat
.setenv('SSH_ASKPASS', askpass
)
112 # Git v1.7.10 Release Notes
113 # =========================
115 # Compatibility Notes
116 # -------------------
118 # * From this release on, the "git merge" command in an interactive
119 # session will start an editor when it automatically resolves the
120 # merge for the user to explain the resulting commit, just like the
121 # "git commit" command does when it wasn't given a commit message.
123 # If you have a script that runs "git merge" and keeps its standard
124 # input and output attached to the user's terminal, and if you do not
125 # want the user to explain the resulting merge commits, you can
126 # export GIT_MERGE_AUTOEDIT environment variable set to "no", like
130 # GIT_MERGE_AUTOEDIT=no
131 # export GIT_MERGE_AUTOEDIT
133 # to disable this behavior (if you want your users to explain their
134 # merge commits, you do not have to do anything). Alternatively, you
135 # can give the "--no-edit" option to individual invocations of the
136 # "git merge" command if you know everybody who uses your script has
137 # Git v1.7.8 or newer.
139 # Longer-term: Use `git merge --no-commit` so that we always
140 # have a chance to explain our merges.
141 compat
.setenv('GIT_MERGE_AUTOEDIT', 'no')
144 def get_icon_themes(context
):
145 """Return the default icon theme names"""
148 icon_themes_env
= core
.getenv('GIT_COLA_ICON_THEME')
150 result
.extend([x
for x
in icon_themes_env
.split(':') if x
])
152 icon_themes_cfg
= list(reversed(context
.cfg
.get_all('cola.icontheme')))
154 result
.extend(icon_themes_cfg
)
157 result
.append('light')
162 # style note: we use camelCase here since we're masquerading a Qt class
163 class ColaApplication
:
164 """The main cola application
166 ColaApplication handles i18n of user-visible data
169 def __init__(self
, context
, argv
, locale
=None, icon_themes
=None, gui_theme
=None):
175 icons
.install(icon_themes
or get_icon_themes(context
))
177 self
.context
= context
179 self
._install
_hidpi
_config
()
180 self
._app
= ColaQApplication(context
, list(argv
))
181 self
._app
.setWindowIcon(icons
.cola())
182 self
._app
.setDesktopFileName('git-cola')
183 self
._install
_style
(gui_theme
)
185 def _install_style(self
, theme_str
):
186 """Generate and apply a stylesheet to the app"""
187 if theme_str
is None:
188 theme_str
= self
.context
.cfg
.get('cola.theme', default
='default')
189 theme
= themes
.find_theme(theme_str
)
191 self
._app
.setStyleSheet(theme
.build_style_sheet(self
._app
.palette()))
193 is_macos_theme
= theme_str
.startswith('macos-')
195 themes
.apply_platform_theme(theme_str
)
196 elif theme_str
!= 'default':
197 self
._app
.setPalette(theme
.build_palette(self
._app
.palette()))
199 def _install_hidpi_config(self
):
200 """Sets QT HiDPI scaling (requires Qt 5.6)"""
201 value
= self
.context
.cfg
.get('cola.hidpi', default
=hidpi
.Option
.AUTO
)
202 hidpi
.apply_choice(value
)
204 def activeWindow(self
):
205 """QApplication::activeWindow() pass-through"""
206 return self
._app
.activeWindow()
209 """QApplication::palette() pass-through"""
210 return self
._app
.palette()
213 """Wrap exec_() and start the application"""
214 # Defer connection so that local cola.inotify is honored
215 context
= self
.context
216 monitor
= context
.fsmonitor
217 monitor
.files_changed
.connect(
218 cmds
.run(cmds
.Refresh
, context
), type=Qt
.QueuedConnection
220 monitor
.config_changed
.connect(
221 cmds
.run(cmds
.RefreshConfig
, context
), type=Qt
.QueuedConnection
223 # Start the filesystem monitor thread
225 return self
._app
.exec_()
228 """Finalize the application"""
229 self
.context
.fsmonitor
.stop()
230 # Workaround QTBUG-52988 by deleting the app manually to prevent a
231 # crash during app shutdown.
232 # https://bugreports.qt.io/browse/QTBUG-52988
235 except (AttributeError, RuntimeError):
239 def exit(self
, status
):
240 """QApplication::exit(status) pass-through"""
241 return self
._app
.exit(status
)
244 class ColaQApplication(QtWidgets
.QApplication
):
245 """QApplication implementation for handling custom events"""
247 def __init__(self
, context
, argv
):
248 super().__init
__(argv
)
249 self
.context
= context
250 # Make icons sharp in HiDPI screen
251 if hasattr(Qt
, 'AA_UseHighDpiPixmaps'):
252 self
.setAttribute(Qt
.AA_UseHighDpiPixmaps
, True)
255 """Respond to focus events for the cola.refreshonfocus feature"""
256 if e
.type() == QtCore
.QEvent
.ApplicationActivate
:
257 context
= self
.context
260 if context
.git
.is_valid() and cfg
.get(
261 'cola.refreshonfocus', default
=False
263 cmds
.do(cmds
.Refresh
, context
)
264 return super().event(e
)
266 def commitData(self
, session_mgr
):
267 """Save session data"""
268 if not self
.context
or not self
.context
.view
:
270 view
= self
.context
.view
271 if not hasattr(view
, 'save_state'):
273 sid
= session_mgr
.sessionId()
274 skey
= session_mgr
.sessionKey()
275 session_id
= f
'{sid}_{skey}'
276 session
= Session(session_id
, repo
=core
.getcwd())
278 view
.save_state(settings
=session
)
281 def process_args(args
):
282 """Process and verify command-line arguments"""
284 # Accept 'git cola --version' or 'git cola version'
285 version
.print_version()
286 sys
.exit(core
.EXIT_SUCCESS
)
288 # Handle session management
289 restore_session(args
)
291 # Bail out if --repo is not a directory
292 repo
= core
.decode(args
.repo
)
293 if repo
.startswith('file:'):
294 repo
= repo
[len('file:') :]
295 repo
= core
.realpath(repo
)
296 if not core
.isdir(repo
):
299 'fatal: "%s" is not a directory. '
300 'Please specify a correct --repo <path>.'
304 core
.print_stderr(errmsg
)
305 sys
.exit(core
.EXIT_USAGE
)
308 def restore_session(args
):
309 """Load a session based on the window-manager provided arguments"""
310 # args.settings is provided when restoring from a session.
312 if args
.session
is None:
314 session
= Session(args
.session
)
316 args
.settings
= session
317 args
.repo
= session
.repo
320 def application_init(args
, update
=False):
321 """Parses the command-line arguments and starts git-cola"""
322 # Ensure that we're working in a valid git repository.
323 # If not, try to find one. When found, chdir there.
327 context
= new_context(args
)
328 timer
= context
.timer
331 new_worktree(context
, args
.repo
, args
.prompt
)
334 context
.model
.update_status()
338 timer
.display('init')
342 def new_context(args
):
343 """Create top-level ApplicationContext objects"""
344 context
= ApplicationContext(args
)
345 context
.settings
= args
.settings
or Settings
.read()
346 context
.git
= git
.create()
347 context
.cfg
= gitcfg
.create(context
)
348 context
.fsmonitor
= fsmonitor
.create(context
)
349 context
.selection
= selection
.create()
350 context
.model
= main
.create(context
)
351 context
.app
= new_application(context
, args
)
352 context
.timer
= Timer()
357 def application_run(context
, view
, start
=None, stop
=None):
358 """Run the application main loop"""
359 initialize_view(context
, view
)
363 # Start the event loop
364 result
= context
.app
.start()
373 def initialize_view(context
, view
):
374 """Register the main widget and display it"""
375 context
.set_view(view
)
377 if sys
.platform
== 'darwin':
381 def application_start(context
, view
):
382 """Show the GUI and start the main event loop"""
383 # Store the view for session management
384 return application_run(context
, view
, start
=default_start
, stop
=default_stop
)
387 def default_start(context
, _view
):
388 """Scan for the first time"""
389 QtCore
.QTimer
.singleShot(0, startup_message
)
390 QtCore
.QTimer
.singleShot(0, lambda: async_update(context
))
393 def default_stop(_context
, _view
):
394 """All done, cleanup"""
395 QtCore
.QThreadPool
.globalInstance().waitForDone()
398 def add_common_arguments(parser
):
399 """Add command arguments to the ArgumentParser"""
400 # We also accept 'git cola version'
402 '--version', default
=False, action
='store_true', help='print version number'
405 # Specifies a git repository to open
410 default
=core
.getcwd(),
411 help='open the specified git repository',
414 # Specifies that we should prompt for a repository at startup
416 '--prompt', action
='store_true', default
=False, help='prompt for a repository'
419 # Specify the icon theme
426 help='specify an icon theme (name or directory)',
429 # Resume an X Session Management session
431 '-session', metavar
='<session>', default
=None, help=argparse
.SUPPRESS
434 # Enable timing information
436 '--perf', action
='store_true', default
=False, help=argparse
.SUPPRESS
439 # Specify the GUI theme
441 '--theme', metavar
='<name>', default
=None, help='specify an GUI theme name'
445 def new_application(context
, args
):
446 """Create a new ColaApplication"""
447 return ColaApplication(
448 context
, sys
.argv
, icon_themes
=args
.icon_themes
, gui_theme
=args
.theme
452 def new_worktree(context
, repo
, prompt
):
453 """Find a Git repository, or prompt for one when not found"""
454 model
= context
.model
456 parent
= qtutils
.active_window()
460 valid
= model
.set_worktree(repo
)
462 # We are not currently in a git repository so we need to find one.
463 # Before prompting the user for a repository, check if they've
464 # configured a default repository and attempt to use it.
465 default_repo
= cfg
.get('cola.defaultrepo')
467 valid
= model
.set_worktree(default_repo
)
470 # If we've gotten into this loop then that means that neither the
471 # current directory nor the default repository were available.
472 # Prompt the user for a repository.
473 startup_dlg
= startup
.StartupDialog(context
, parent
)
474 gitdir
= startup_dlg
.find_git_repo()
476 sys
.exit(core
.EXIT_NOINPUT
)
478 if not core
.exists(os
.path
.join(gitdir
, '.git')):
479 offer_to_create_repo(context
, gitdir
)
480 valid
= model
.set_worktree(gitdir
)
483 valid
= model
.set_worktree(gitdir
)
487 N_('Error Opening Repository'),
488 message
=N_('Could not open %s.' % gitdir
),
493 def offer_to_create_repo(context
, gitdir
):
494 """Offer to create a new repo"""
495 title
= N_('Repository Not Found')
496 text
= N_('%s is not a Git repository.') % gitdir
497 informative_text
= N_('Create a new repository at that location?')
498 if standard
.confirm(title
, text
, informative_text
, N_('Create')):
499 status
, out
, err
= context
.git
.init(gitdir
)
500 title
= N_('Error Creating Repository')
502 Interaction
.command_error(title
, 'git init', status
, out
, err
)
505 def async_update(context
):
506 """Update the model in the background
508 git-cola should startup as quickly as possible.
510 update_status
= partial(context
.model
.update_status
, update_index
=True)
511 task
= qtutils
.SimpleTask(update_status
)
512 context
.runtask
.start(task
)
515 def startup_message():
516 """Print debug startup messages"""
517 trace
= git
.GIT_COLA_TRACE
518 if trace
in ('2', 'trace'):
519 msg1
= 'info: debug level 2: trace mode enabled'
520 msg2
= 'info: set GIT_COLA_TRACE=1 for less-verbose output'
521 Interaction
.log(msg1
)
522 Interaction
.log(msg2
)
524 msg1
= 'info: debug level 1'
525 msg2
= 'info: set GIT_COLA_TRACE=2 for trace mode'
526 Interaction
.log(msg1
)
527 Interaction
.log(msg2
)
531 """System-level initialization"""
532 # We support ~/.config/git-cola/git-bindir on Windows for configuring
533 # a custom location for finding the "git" executable.
534 git_path
= find_git()
536 prepend_path(git_path
)
538 # The current directory may have been deleted while we are still
539 # in that directory. We rectify this situation by walking up the
540 # directory tree and retrying.
542 # This is needed because because Python throws exceptions in lots of
543 # stdlib functions when in this situation, e.g. os.path.abspath() and
544 # os.path.realpath(), so it's simpler to mitigate the damage by changing
545 # the current directory to one that actually exists.
554 """Simple performance timer"""
559 def start(self
, key
):
562 self
._data
[key
] = [now
, now
]
565 """Stop a timer and return its elapsed time"""
566 entry
= self
._data
[key
]
567 entry
[1] = time
.time()
568 return self
.elapsed(key
)
570 def elapsed(self
, key
):
571 """Return the elapsed time for a timer"""
572 entry
= self
._data
[key
]
573 return entry
[1] - entry
[0]
575 def display(self
, key
):
576 """Display a timer"""
577 elapsed
= self
.elapsed(key
)
578 sys
.stdout
.write(f
'{key}: {elapsed:.5f}s\n')
582 """Stub arguments for interactive API use"""
585 self
.icon_themes
= []
588 self
.repo
= core
.getcwd()
596 """Create a new instance of application arguments"""
600 class ApplicationContext
:
601 """Context for performing operations on Git and related data models"""
603 def __init__(self
, args
):
605 self
.app
= None # ColaApplication
606 self
.git
= None # git.Git
607 self
.cfg
= None # gitcfg.GitConfig
608 self
.model
= None # main.MainModel
609 self
.timer
= None # Timer
610 self
.runtask
= None # qtutils.RunTask
611 self
.settings
= None # settings.Settings
612 self
.selection
= None # selection.SelectionModel
613 self
.fsmonitor
= None # fsmonitor
614 self
.view
= None # QWidget
615 self
.browser_windows
= [] # list of browse.Browser
617 def set_view(self
, view
):
618 """Initialize view-specific members"""
620 self
.runtask
= qtutils
.RunTask(parent
=view
)
624 """Return the path of git.exe, or None if we can't find it."""
625 if not utils
.is_win32():
626 return None # UNIX systems have git in their $PATH
628 # If the user wants to use a Git/bin/ directory from a non-standard
629 # directory then they can write its location into
630 # ~/.config/git-cola/git-bindir
631 git_bindir
= resources
.config_home('git-bindir')
632 if core
.exists(git_bindir
):
633 custom_path
= core
.read(git_bindir
).strip()
634 if custom_path
and core
.exists(custom_path
):
637 # Try to find Git's bin/ directory in one of the typical locations
638 pf
= os
.environ
.get('ProgramFiles', 'C:\\Program Files')
639 pf32
= os
.environ
.get('ProgramFiles(x86)', 'C:\\Program Files (x86)')
640 pf64
= os
.environ
.get('ProgramW6432', 'C:\\Program Files')
641 for p
in [pf64
, pf32
, pf
, 'C:\\']:
642 candidate
= os
.path
.join(p
, 'Git\\bin')
643 if os
.path
.isdir(candidate
):
649 def prepend_path(path
):
650 """Adds git to the PATH. This is needed on Windows."""
651 path
= core
.decode(path
)
652 path_entries
= core
.getenv('PATH', '').split(os
.pathsep
)
653 if path
not in path_entries
:
654 path_entries
.insert(0, path
)
655 compat
.setenv('PATH', os
.pathsep
.join(path_entries
))