1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
"""Provides the main() routine and ColaApplication"""
from functools import partial
import argparse
import os
import random
import signal
import sys
import time
try:
from qtpy import QtCore
except ImportError as error:
sys.stderr.write(
"""
Your Python environment does not have qtpy and PyQt (or PySide).
The following error was encountered when importing "qtpy":
ImportError: {err}
Install qtpy and PyQt (or PySide) into your Python environment.
On a Debian/Ubuntu system you can install these modules using apt:
sudo apt install python3-pyqt5 python3-pyqt5.qtwebengine python3-qtpy
""".format(
err=error
)
)
sys.exit(1)
from qtpy import QtWidgets
from qtpy.QtCore import Qt
try:
# Qt 5.12 / PyQt 5.13 is unable to use QtWebEngineWidgets unless it is
# imported before QApplication is constructed.
from qtpy import QtWebEngineWidgets # noqa
except ImportError:
# QtWebEngineWidgets / QtWebKit is not available -- no big deal.
pass
# Import cola modules
from .i18n import N_
from .interaction import Interaction
from .models import main
from .models import selection
from .widgets import cfgactions
from .widgets import standard
from .widgets import startup
from .settings import Session
from .settings import Settings
from . import cmds
from . import core
from . import compat
from . import fsmonitor
from . import git
from . import gitcfg
from . import guicmds
from . import hidpi
from . import icons
from . import i18n
from . import qtcompat
from . import qtutils
from . import resources
from . import themes
from . import utils
from . import version
def setup_environment():
"""Set environment variables to control git's behavior"""
# Allow Ctrl-C to exit
random.seed(hash(time.time()))
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Session management wants an absolute path when restarting
sys.argv[0] = sys_argv0 = os.path.abspath(sys.argv[0])
# Spoof an X11 display for SSH
os.environ.setdefault('DISPLAY', ':0')
if not core.getenv('SHELL', ''):
for shell in ('/bin/zsh', '/bin/bash', '/bin/sh'):
if os.path.exists(shell):
compat.setenv('SHELL', shell)
break
# Setup the path so that git finds us when we run 'git cola'
path_entries = core.getenv('PATH', '').split(os.pathsep)
bindir = core.decode(os.path.dirname(sys_argv0))
path_entries.append(bindir)
path = os.pathsep.join(path_entries)
compat.setenv('PATH', path)
# We don't ever want a pager
compat.setenv('GIT_PAGER', '')
# Setup *SSH_ASKPASS
git_askpass = core.getenv('GIT_ASKPASS')
ssh_askpass = core.getenv('SSH_ASKPASS')
if git_askpass:
askpass = git_askpass
elif ssh_askpass:
askpass = ssh_askpass
elif sys.platform == 'darwin':
askpass = resources.package_command('ssh-askpass-darwin')
else:
askpass = resources.package_command('ssh-askpass')
compat.setenv('GIT_ASKPASS', askpass)
compat.setenv('SSH_ASKPASS', askpass)
# --- >8 --- >8 ---
# Git v1.7.10 Release Notes
# =========================
#
# Compatibility Notes
# -------------------
#
# * From this release on, the "git merge" command in an interactive
# session will start an editor when it automatically resolves the
# merge for the user to explain the resulting commit, just like the
# "git commit" command does when it wasn't given a commit message.
#
# If you have a script that runs "git merge" and keeps its standard
# input and output attached to the user's terminal, and if you do not
# want the user to explain the resulting merge commits, you can
# export GIT_MERGE_AUTOEDIT environment variable set to "no", like
# this:
#
# #!/bin/sh
# GIT_MERGE_AUTOEDIT=no
# export GIT_MERGE_AUTOEDIT
#
# to disable this behavior (if you want your users to explain their
# merge commits, you do not have to do anything). Alternatively, you
# can give the "--no-edit" option to individual invocations of the
# "git merge" command if you know everybody who uses your script has
# Git v1.7.8 or newer.
# --- >8 --- >8 ---
# Longer-term: Use `git merge --no-commit` so that we always
# have a chance to explain our merges.
compat.setenv('GIT_MERGE_AUTOEDIT', 'no')
def get_icon_themes(context):
"""Return the default icon theme names"""
result = []
icon_themes_env = core.getenv('GIT_COLA_ICON_THEME')
if icon_themes_env:
result.extend([x for x in icon_themes_env.split(':') if x])
icon_themes_cfg = list(reversed(context.cfg.get_all('cola.icontheme')))
if icon_themes_cfg:
result.extend(icon_themes_cfg)
if not result:
result.append('light')
return result
# style note: we use camelCase here since we're masquerading a Qt class
class ColaApplication:
"""The main cola application
ColaApplication handles i18n of user-visible data
"""
def __init__(self, context, argv, locale=None, icon_themes=None, gui_theme=None):
cfgactions.install()
i18n.install(locale)
qtcompat.install()
guicmds.install()
standard.install()
icons.install(icon_themes or get_icon_themes(context))
self.context = context
self.theme = None
self._install_hidpi_config()
self._app = ColaQApplication(context, list(argv))
self._app.setWindowIcon(icons.cola())
self._app.setDesktopFileName('git-cola')
self._install_style(gui_theme)
def _install_style(self, theme_str):
"""Generate and apply a stylesheet to the app"""
if theme_str is None:
theme_str = self.context.cfg.get('cola.theme', default='default')
theme = themes.find_theme(theme_str)
self.theme = theme
self._app.setStyleSheet(theme.build_style_sheet(self._app.palette()))
is_macos_theme = theme_str.startswith('macos-')
if is_macos_theme:
themes.apply_platform_theme(theme_str)
elif theme_str != 'default':
self._app.setPalette(theme.build_palette(self._app.palette()))
def _install_hidpi_config(self):
"""Sets QT HiDPI scaling (requires Qt 5.6)"""
value = self.context.cfg.get('cola.hidpi', default=hidpi.Option.AUTO)
hidpi.apply_choice(value)
def activeWindow(self):
"""QApplication::activeWindow() pass-through"""
return self._app.activeWindow()
def palette(self):
"""QApplication::palette() pass-through"""
return self._app.palette()
def start(self):
"""Wrap exec_() and start the application"""
# Defer connection so that local cola.inotify is honored
context = self.context
monitor = context.fsmonitor
monitor.files_changed.connect(
cmds.run(cmds.Refresh, context), type=Qt.QueuedConnection
)
monitor.config_changed.connect(
cmds.run(cmds.RefreshConfig, context), type=Qt.QueuedConnection
)
# Start the filesystem monitor thread
monitor.start()
return self._app.exec_()
def stop(self):
"""Finalize the application"""
self.context.fsmonitor.stop()
# Workaround QTBUG-52988 by deleting the app manually to prevent a
# crash during app shutdown.
# https://bugreports.qt.io/browse/QTBUG-52988
try:
del self._app
except (AttributeError, RuntimeError):
pass
self._app = None
def exit(self, status):
"""QApplication::exit(status) pass-through"""
return self._app.exit(status)
class ColaQApplication(QtWidgets.QApplication):
"""QApplication implementation for handling custom events"""
def __init__(self, context, argv):
super().__init__(argv)
self.context = context
# Make icons sharp in HiDPI screen
if hasattr(Qt, 'AA_UseHighDpiPixmaps'):
self.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
def event(self, e):
"""Respond to focus events for the cola.refreshonfocus feature"""
if e.type() == QtCore.QEvent.ApplicationActivate:
context = self.context
if context:
cfg = context.cfg
if context.git.is_valid() and cfg.get(
'cola.refreshonfocus', default=False
):
cmds.do(cmds.Refresh, context)
return super().event(e)
def commitData(self, session_mgr):
"""Save session data"""
if not self.context or not self.context.view:
return
view = self.context.view
if not hasattr(view, 'save_state'):
return
sid = session_mgr.sessionId()
skey = session_mgr.sessionKey()
session_id = f'{sid}_{skey}'
session = Session(session_id, repo=core.getcwd())
session.update()
view.save_state(settings=session)
def process_args(args):
"""Process and verify command-line arguments"""
if args.version:
# Accept 'git cola --version' or 'git cola version'
version.print_version()
sys.exit(core.EXIT_SUCCESS)
# Handle session management
restore_session(args)
# Bail out if --repo is not a directory
repo = core.decode(args.repo)
if repo.startswith('file:'):
repo = repo[len('file:') :]
repo = core.realpath(repo)
if not core.isdir(repo):
errmsg = (
N_(
'fatal: "%s" is not a directory. '
'Please specify a correct --repo <path>.'
)
% repo
)
core.print_stderr(errmsg)
sys.exit(core.EXIT_USAGE)
def restore_session(args):
"""Load a session based on the window-manager provided arguments"""
# args.settings is provided when restoring from a session.
args.settings = None
if args.session is None:
return
session = Session(args.session)
if session.load():
args.settings = session
args.repo = session.repo
def application_init(args, update=False, app_name='Git Cola'):
"""Parses the command-line arguments and starts git-cola"""
# Ensure that we're working in a valid git repository.
# If not, try to find one. When found, chdir there.
setup_environment()
process_args(args)
context = new_context(args, app_name=app_name)
timer = context.timer
timer.start('init')
new_worktree(context, args.repo, args.prompt)
if update:
context.model.update_status()
timer.stop('init')
if args.perf:
timer.display('init')
return context
def new_context(args, app_name='Git Cola'):
"""Create top-level ApplicationContext objects"""
context = ApplicationContext(args)
context.settings = args.settings or Settings.read()
context.git = git.create()
context.cfg = gitcfg.create(context)
context.fsmonitor = fsmonitor.create(context)
context.selection = selection.create()
context.model = main.create(context)
context.app_name = app_name
context.app = new_application(context, args)
context.timer = Timer()
return context
def create_context():
"""Create a one-off context from the current directory"""
args = null_args()
return new_context(args)
def application_run(context, view, start=None, stop=None):
"""Run the application main loop"""
initialize_view(context, view)
# Startup callbacks
if start:
start(context, view)
# Start the event loop
result = context.app.start()
# Finish
if stop:
stop(context, view)
context.app.stop()
return result
def initialize_view(context, view):
"""Register the main widget and display it"""
context.set_view(view)
view.show()
if sys.platform == 'darwin':
view.raise_()
def application_start(context, view):
"""Show the GUI and start the main event loop"""
# Store the view for session management
return application_run(context, view, start=default_start, stop=default_stop)
def default_start(context, _view):
"""Scan for the first time"""
QtCore.QTimer.singleShot(0, startup_message)
QtCore.QTimer.singleShot(0, lambda: async_update(context))
def default_stop(_context, _view):
"""All done, cleanup"""
QtCore.QThreadPool.globalInstance().waitForDone()
def add_common_arguments(parser):
"""Add command arguments to the ArgumentParser"""
# We also accept 'git cola version'
parser.add_argument(
'--version', default=False, action='store_true', help='print version number'
)
# Specifies a git repository to open
parser.add_argument(
'-r',
'--repo',
metavar='<repo>',
default=core.getcwd(),
help='open the specified git repository',
)
# Specifies that we should prompt for a repository at startup
parser.add_argument(
'--prompt', action='store_true', default=False, help='prompt for a repository'
)
# Specify the icon theme
parser.add_argument(
'--icon-theme',
metavar='<theme>',
dest='icon_themes',
action='append',
default=[],
help='specify an icon theme (name or directory)',
)
# Resume an X Session Management session
parser.add_argument(
'-session', metavar='<session>', default=None, help=argparse.SUPPRESS
)
# Enable timing information
parser.add_argument(
'--perf', action='store_true', default=False, help=argparse.SUPPRESS
)
# Specify the GUI theme
parser.add_argument(
'--theme', metavar='<name>', default=None, help='specify a GUI theme name'
)
def new_application(context, args):
"""Create a new ColaApplication"""
return ColaApplication(
context, sys.argv, icon_themes=args.icon_themes, gui_theme=args.theme
)
def new_worktree(context, repo, prompt):
"""Find a Git repository, or prompt for one when not found"""
model = context.model
cfg = context.cfg
parent = qtutils.active_window()
valid = False
if not prompt:
valid = model.set_worktree(repo)
if not valid:
# We are not currently in a git repository so we need to find one.
# Before prompting the user for a repository, check if they've
# configured a default repository and attempt to use it.
default_repo = cfg.get('cola.defaultrepo')
if default_repo:
valid = model.set_worktree(default_repo)
while not valid:
# If we've gotten into this loop then that means that neither the
# current directory nor the default repository were available.
# Prompt the user for a repository.
startup_dlg = startup.StartupDialog(context, parent)
gitdir = startup_dlg.find_git_repo()
if not gitdir:
sys.exit(core.EXIT_NOINPUT)
if not core.exists(os.path.join(gitdir, '.git')):
offer_to_create_repo(context, gitdir)
valid = model.set_worktree(gitdir)
continue
valid = model.set_worktree(gitdir)
if not valid:
err = model.error
standard.critical(
N_('Error Opening Repository'),
message=N_('Could not open %s.' % gitdir),
details=err,
)
def offer_to_create_repo(context, gitdir):
"""Offer to create a new repo"""
title = N_('Repository Not Found')
text = N_('%s is not a Git repository.') % gitdir
informative_text = N_('Create a new repository at that location?')
if standard.confirm(title, text, informative_text, N_('Create')):
status, out, err = context.git.init(gitdir)
title = N_('Error Creating Repository')
if status != 0:
Interaction.command_error(title, 'git init', status, out, err)
def async_update(context):
"""Update the model in the background
git-cola should startup as quickly as possible.
"""
update_index = context.cfg.get('cola.updateindex', True)
update_status = partial(context.model.update_status, update_index=update_index)
task = qtutils.SimpleTask(update_status)
context.runtask.start(task)
def startup_message():
"""Print debug startup messages"""
trace = git.GIT_COLA_TRACE
if trace in ('2', 'trace'):
msg1 = 'info: debug level 2: trace mode enabled'
msg2 = 'info: set GIT_COLA_TRACE=1 for less-verbose output'
Interaction.log(msg1)
Interaction.log(msg2)
elif trace:
msg1 = 'info: debug level 1'
msg2 = 'info: set GIT_COLA_TRACE=2 for trace mode'
Interaction.log(msg1)
Interaction.log(msg2)
def initialize():
"""System-level initialization"""
# We support ~/.config/git-cola/git-bindir on Windows for configuring
# a custom location for finding the "git" executable.
git_path = find_git()
if git_path:
prepend_path(git_path)
# The current directory may have been deleted while we are still
# in that directory. We rectify this situation by walking up the
# directory tree and retrying.
#
# This is needed because because Python throws exceptions in lots of
# stdlib functions when in this situation, e.g. os.path.abspath() and
# os.path.realpath(), so it's simpler to mitigate the damage by changing
# the current directory to one that actually exists.
while True:
try:
return core.getcwd()
except OSError:
os.chdir('..')
class Timer:
"""Simple performance timer"""
def __init__(self):
self._data = {}
def start(self, key):
"""Start a timer"""
now = time.time()
self._data[key] = [now, now]
def stop(self, key):
"""Stop a timer and return its elapsed time"""
entry = self._data[key]
entry[1] = time.time()
return self.elapsed(key)
def elapsed(self, key):
"""Return the elapsed time for a timer"""
entry = self._data[key]
return entry[1] - entry[0]
def display(self, key):
"""Display a timer"""
elapsed = self.elapsed(key)
sys.stdout.write(f'{key}: {elapsed:.5f}s\n')
class NullArgs:
"""Stub arguments for interactive API use"""
def __init__(self):
self.icon_themes = []
self.perf = False
self.prompt = False
self.repo = core.getcwd()
self.session = None
self.settings = None
self.theme = None
self.version = False
def null_args():
"""Create a new instance of application arguments"""
return NullArgs()
class ApplicationContext:
"""Context for performing operations on Git and related data models"""
def __init__(self, args):
self.args = args
self.app = None # ColaApplication
self.git = None # git.Git
self.cfg = None # gitcfg.GitConfig
self.model = None # main.MainModel
self.timer = None # Timer
self.runtask = None # qtutils.RunTask
self.settings = None # settings.Settings
self.selection = None # selection.SelectionModel
self.fsmonitor = None # fsmonitor
self.view = None # QWidget
self.browser_windows = [] # list of browse.Browser
def set_view(self, view):
"""Initialize view-specific members"""
self.view = view
self.runtask = qtutils.RunTask(parent=view)
def find_git():
"""Return the path of git.exe, or None if we can't find it."""
if not utils.is_win32():
return None # UNIX systems have git in their $PATH
# If the user wants to use a Git/bin/ directory from a non-standard
# directory then they can write its location into
# ~/.config/git-cola/git-bindir
git_bindir = resources.config_home('git-bindir')
if core.exists(git_bindir):
custom_path = core.read(git_bindir).strip()
if custom_path and core.exists(custom_path):
return custom_path
# Try to find Git's bin/ directory in one of the typical locations
pf = os.environ.get('ProgramFiles', 'C:\\Program Files')
pf32 = os.environ.get('ProgramFiles(x86)', 'C:\\Program Files (x86)')
pf64 = os.environ.get('ProgramW6432', 'C:\\Program Files')
for p in [pf64, pf32, pf, 'C:\\']:
candidate = os.path.join(p, 'Git\\bin')
if os.path.isdir(candidate):
return candidate
return None
def prepend_path(path):
"""Adds git to the PATH. This is needed on Windows."""
path = core.decode(path)
path_entries = core.getenv('PATH', '').split(os.pathsep)
if path not in path_entries:
path_entries.insert(0, path)
compat.setenv('PATH', os.pathsep.join(path_entries))
|