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
|
"""Widgets for manipulating git stashes"""
from qtpy.QtCore import Qt
from ..i18n import N_
from ..interaction import Interaction
from ..models import stash
from ..qtutils import get
from .. import cmds
from .. import hotkeys
from .. import icons
from .. import qtutils
from . import defs
from . import diff
from . import standard
from . import text
def view(context, show=True):
"""Launches a stash dialog using the provided model + view"""
model = stash.StashModel(context)
stash_view = StashView(context, model, parent=qtutils.active_window())
if show:
stash_view.show()
stash_view.raise_()
return stash_view
class StashView(standard.Dialog):
def __init__(self, context, model, parent=None):
standard.Dialog.__init__(self, parent=parent)
self.context = context
self.model = model
self.stashes = []
self.revids = []
self.names = []
self.setWindowTitle(N_('Stash'))
if parent is not None:
self.setWindowModality(Qt.WindowModal)
self.stash_list = standard.ListWidget(parent=self)
self.stash_text = diff.DiffTextEdit(context, self)
self.rename_button = qtutils.create_button(
text=N_('Rename'),
tooltip=N_('Rename the selected stash'),
icon=icons.edit(),
)
self.apply_button = qtutils.create_button(
text=N_('Apply'), tooltip=N_('Apply the selected stash'), icon=icons.ok()
)
self.save_button = qtutils.create_button(
text=N_('Save'),
tooltip=N_('Save modified state to new stash'),
icon=icons.save(),
default=True,
)
self.drop_button = qtutils.create_button(
text=N_('Drop'), tooltip=N_('Drop the selected stash'), icon=icons.discard()
)
self.pop_button = qtutils.create_button(
text=N_('Pop'),
tooltip=N_('Apply and drop the selected stash (git stash pop)'),
icon=icons.discard(),
)
self.help_button = qtutils.create_button(
text=N_('Help'), tooltip=N_('Show help\nShortcut: ?'), icon=icons.question()
)
self.close_button = qtutils.close_button()
# A list of tuples [(display_text, tooltip), ...] containing the save modes.
save_modes = stash.SaveModes.get()
self.save_modes = qtutils.combo([mode[0] for mode in save_modes])
# Change the combobox's tooltip when the selection changes.
self.save_modes.currentIndexChanged.connect(
lambda idx: self.save_modes.setToolTip(save_modes[idx][1])
)
# Set tooltips for the individual items in the tooltip.
for idx, mode in enumerate(save_modes):
self.save_modes.setItemData(idx, mode[1], Qt.ToolTipRole)
self.recreate_index = qtutils.checkbox(
text=N_('Recreate Index'),
tooltip=N_('Recreate the index when restoring stashes'),
)
# Arrange layouts
self.splitter = qtutils.splitter(
Qt.Horizontal, self.stash_list, self.stash_text
)
self.splitter.setChildrenCollapsible(False)
self.action_layout = qtutils.hbox(
defs.no_margin,
defs.button_spacing,
self.rename_button,
self.pop_button,
self.drop_button,
self.apply_button,
qtutils.STRETCH,
self.save_modes,
self.save_button,
)
self.bottom_layout = qtutils.hbox(
defs.margin,
defs.button_spacing,
self.help_button,
self.recreate_index,
qtutils.STRETCH,
self.close_button,
)
self.main_layt = qtutils.vbox(
defs.margin,
defs.spacing,
self.splitter,
self.action_layout,
self.bottom_layout,
)
self.setLayout(self.main_layt)
self.splitter.setSizes([self.width() // 3, self.width() * 2 // 3])
# Save stash with Ctrl+S
self.save_action = qtutils.add_action(
self, N_('Save'), self.stash_save, hotkeys.SAVE
)
# Apply stash with Ctrl+Enter
self.apply_action = qtutils.add_action(
self, N_('Apply'), self.stash_apply, hotkeys.APPLY
)
# Pop stash with Ctrl+Backspace
self.pop_action = qtutils.add_action(
self, N_('Pop'), self.stash_pop, hotkeys.DELETE_FILE_SECONDARY
)
# Drop stash with Ctrl+Shift+Backspace
self.drop_action = qtutils.add_action(
self, N_('Delete'), self.stash_drop, hotkeys.DELETE_FILE
)
self.choose_all_changes_action = qtutils.add_action(
self,
N_('All changes'),
lambda: self.save_modes.set_index_if_enabled(stash.SaveModes.ALL),
hotkeys.CTRL_1,
hotkeys.STASH_ALL,
)
self.choose_staged_only_action = qtutils.add_action(
self,
N_('Staged only'),
lambda: self.save_modes.set_index_if_enabled(stash.SaveModes.STAGED),
hotkeys.CTRL_2,
hotkeys.STASH_STAGED,
)
self.choose_unstaged_only_action = qtutils.add_action(
self,
N_('Unstaged only'),
lambda: self.save_modes.set_index_if_enabled(stash.SaveModes.UNSTAGED),
hotkeys.CTRL_3,
hotkeys.STASH_UNSTAGED,
)
self.show_help_action = qtutils.add_action(
self, N_('Show Help'), lambda: show_help(context), hotkeys.QUESTION
)
self.stash_list.itemSelectionChanged.connect(self.item_selected)
qtutils.connect_button(self.save_button, self.stash_save)
qtutils.connect_button(self.rename_button, self.stash_rename)
qtutils.connect_button(self.apply_button, self.stash_apply)
qtutils.connect_button(self.pop_button, self.stash_pop)
qtutils.connect_button(self.drop_button, self.stash_drop)
qtutils.connect_button(self.close_button, self.close_and_rescan)
qtutils.connect_button(self.help_button, lambda: show_help(context))
self.init_size(parent=parent)
self.update_from_model()
self.update_actions()
def close_and_rescan(self):
cmds.do(cmds.Rescan, self.context)
self.reject()
def selected_stash(self):
"""Returns the stash name of the currently selected stash"""
list_widget = self.stash_list
stash_list = self.revids
return qtutils.selected_item(list_widget, stash_list)
def selected_name(self):
list_widget = self.stash_list
stash_list = self.names
return qtutils.selected_item(list_widget, stash_list)
def item_selected(self):
"""Shows the current stash in the main view."""
self.update_actions()
selection = self.selected_stash()
if not selection:
return
diff_text = self.model.stash_diff(selection)
self.stash_text.setPlainText(diff_text)
def update_actions(self):
is_staged = self.model.is_staged()
is_changed = self.model.is_changed()
self.save_modes.set_item_enabled(stash.SaveModes.STAGED, is_staged)
self.save_modes.set_item_enabled(stash.SaveModes.UNSTAGED, is_staged)
self.save_modes.setEnabled(is_changed)
self.save_button.setEnabled(is_changed)
is_selected = bool(self.selected_stash())
self.apply_action.setEnabled(is_selected)
self.drop_action.setEnabled(is_selected)
self.pop_action.setEnabled(is_selected)
self.recreate_index.setEnabled(is_selected)
self.rename_button.setEnabled(is_selected)
self.apply_button.setEnabled(is_selected)
self.drop_button.setEnabled(is_selected)
self.pop_button.setEnabled(is_selected)
def update_from_model(self):
"""Initiates git queries on the model and updates the view"""
stashes, revids, author_dates, names = self.model.stash_info()
self.stashes = stashes
self.revids = revids
self.names = names
displayed = False
self.stash_list.clear()
self.stash_list.addItems(self.stashes)
if self.stash_list.count() > 0:
for i in range(self.stash_list.count()):
self.stash_list.item(i).setToolTip(author_dates[i])
item = self.stash_list.item(0)
self.stash_list.setCurrentItem(item)
displayed = True
# "Stash Index" depends on staged changes, so disable this option
# if there are no staged changes.
is_staged = self.model.is_staged()
if stash.should_stash_staged(self.save_modes.current_index()) and not is_staged:
self.save_modes.set_index(stash.SaveModes.ALL)
return displayed
def stash_rename(self):
"""Renames the currently selected stash"""
selection = self.selected_stash()
name = self.selected_name()
new_name, ok = qtutils.prompt(
N_('Enter a new name for the stash'),
text=name,
title=N_('Rename Stash'),
parent=self,
)
if not ok or not new_name:
return
if new_name == name:
Interaction.information(
N_('No change made'), N_('The stash has not been renamed')
)
return
context = self.context
cmds.do(stash.RenameStash, context, selection, new_name)
self.update_from_model()
def stash_pop(self):
self.stash_apply(pop=True)
def stash_apply(self, pop=False):
"""Applies the currently selected stash"""
selection = self.selected_stash()
if not selection:
return
context = self.context
index = get(self.recreate_index)
cmds.do(stash.ApplyStash, context, selection, index, pop)
self.update_from_model()
def stash_save(self):
"""Saves the worktree in a stash
This prompts the user for a stash name and creates
a git stash named accordingly.
"""
stash_name, ok = qtutils.prompt(
N_('Enter a name for the stash'), title=N_('Save Stash'), parent=self
)
if not ok or not stash_name:
return
context = self.context
keep_index = stash.should_keep_index(self.save_modes.current_index())
stash_index = stash.should_stash_staged(self.save_modes.current_index())
if stash_index:
cmds.do(stash.StashIndex, context, stash_name)
else:
cmds.do(stash.SaveStash, context, stash_name, keep_index)
self.update_from_model()
def stash_drop(self):
"""Drops the currently selected stash"""
selection = self.selected_stash()
name = self.selected_name()
if not selection:
return
if not Interaction.confirm(
N_('Drop Stash?'),
N_('Recovering a dropped stash is not possible.'),
N_('Drop the "%s" stash?') % name,
N_('Drop Stash'),
default=True,
icon=icons.discard(),
):
return
cmds.do(stash.DropStash, self.context, selection)
if not self.update_from_model():
self.stash_text.setPlainText('')
def export_state(self):
"""Export persistent settings"""
state = super().export_state()
current_index = self.save_modes.current_index()
state['keep_index'] = stash.should_keep_index(current_index)
state['stash_index'] = stash.should_stash_staged(current_index)
state['recreate_index'] = get(self.recreate_index)
state['sizes'] = get(self.splitter)
return state
def apply_state(self, state):
"""Apply persistent settings"""
result = super().apply_state(state)
keep_index = bool(state.get('keep_index', True))
recreate_index = bool(state.get('recreate_index', True))
stash_index = bool(state.get('stash_index', False))
# It would be simpler to have a "save_mode" instead of individual booleans.
# This is done for compatibility with older versions.
self.recreate_index.setChecked(recreate_index)
if stash_index:
self.save_modes.set_index(stash.SaveModes.STAGED)
elif keep_index:
self.save_modes.set_index(stash.SaveModes.UNSTAGED)
else:
self.save_modes.set_index(stash.SaveModes.ALL)
try:
self.splitter.setSizes(state['sizes'])
except (KeyError, ValueError, AttributeError):
pass
return result
def show_help(context):
help_text = N_(
"""
Keyboard Shortcuts Actions
------------------ --------------------------------
? show help
esc close and exit
ctrl + 1, alt + a set save mode to "All changes"
ctrl + 2, alt + s set save mode to "Staged only"
ctrl + 3, alt + u set save mode to "Unstaged only"
ctrl + enter apply stash
ctrl + s save stash
"""
)
title = N_('Help')
return text.text_dialog(context, help_text, title)
|