From 43d0daec43ca71a243bb02e50313f5d89d2cd489 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 15 Oct 2022 14:10:08 -0700 Subject: [PATCH] status: ensure that untracked images can be staged Enhance the tracking of modes to better differentiate between untracked files that we can't diff (eg. binary files), untracked diffable files and when we are displaying arbitrary text. This allows us to better handle image files by being able to track when files can be staged vs. files that can be partially staged. Update the context menus for the status and diff widgets to be aware of this distinction. This ensures that image files have a "Stage" entry in their context menu. Closes #1265 Reported-by: Gianni Lerro @glerroo via github.com Signed-off-by: David Aguilar --- CHANGES.rst | 8 +++++--- cola/cmds.py | 14 ++++++++++---- cola/models/main.py | 9 +++++++-- cola/widgets/diff.py | 12 ++++++++---- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f41c1c48..f3d61dff 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,5 +1,8 @@ .. _v4.0.3: +v4.0.3 +====== + Fixes ----- * The config reader has been revamped to better read settings when git config @@ -11,6 +14,8 @@ Fixes been configured. (`#1263 `_) +* Context menu actions for staging files has been added when diffing images. + (`#1265 `_) Translations ------------ @@ -18,9 +23,6 @@ Translations (`#880 `_) -v4.0.2 -====== - .. _v4.0.2: v4.0.2 diff --git a/cola/cmds.py b/cola/cmds.py index 1cbe821e..e1b906fb 100644 --- a/cola/cmds.py +++ b/cola/cmds.py @@ -1061,13 +1061,17 @@ class DeleteRemoteBranch(DeleteBranch): return command % (self.remote, self.branch) -def get_mode(model, staged, modified, unmerged, untracked): +def get_mode(context, filename, staged, modified, unmerged, untracked): + model = context.model if staged: mode = model.mode_index elif modified or unmerged: mode = model.mode_worktree elif untracked: - mode = model.mode_untracked + if gitcmds.is_binary(context, filename): + mode = model.mode_untracked + else: + mode = model.mode_untracked_diff else: mode = model.mode return mode @@ -1116,7 +1120,9 @@ class DiffImage(EditModel): self.new_filename = filename self.new_diff_type = self.get_diff_type(filename) self.new_file_type = main.Types.IMAGE - self.new_mode = get_mode(self.model, staged, modified, unmerged, untracked) + self.new_mode = get_mode( + context, filename, staged, modified, unmerged, untracked + ) self.staged = staged self.modified = modified self.unmerged = unmerged @@ -2749,7 +2755,7 @@ class UntrackedSummary(EditModel): self.new_diff_text = io.getvalue() self.new_diff_type = main.Types.TEXT self.new_file_type = main.Types.TEXT - self.new_mode = self.model.mode_untracked + self.new_mode = self.model.mode_display class VisualizeAll(ContextCommand): diff --git a/cola/models/main.py b/cola/models/main.py index f99efeb7..79003650 100644 --- a/cola/models/main.py +++ b/cola/models/main.py @@ -46,6 +46,7 @@ class MainModel(QtCore.QObject): mode_none = 'none' # Default: nothing's happened, do nothing mode_worktree = 'worktree' # Comparing index to worktree mode_diffstat = 'diffstat' # Showing a diffstat + mode_display = 'display' # Displaying arbitrary information mode_untracked = 'untracked' # Dealing with an untracked file mode_untracked_diff = 'untracked-diff' # Diffing an untracked file mode_index = 'index' # Comparing index to last commit @@ -55,7 +56,7 @@ class MainModel(QtCore.QObject): modes_undoable = set((mode_amend, mode_index, mode_worktree)) # Modes where we can partially stage files - modes_stageable = set((mode_amend, mode_worktree, mode_untracked_diff)) + modes_partially_stageable = set((mode_amend, mode_worktree, mode_untracked_diff)) # Modes where we can partially unstage files modes_unstageable = set((mode_amend, mode_index)) @@ -121,9 +122,13 @@ class MainModel(QtCore.QObject): """Whether we can checkout files from the $head.""" return self.mode in self.modes_undoable + def partially_stageable(self): + """Whether partial staging should be allowed.""" + return self.mode in self.modes_partially_stageable + def stageable(self): """Whether staging should be allowed.""" - return self.mode in self.modes_stageable + return self.partially_stageable() or self.mode == self.modes_untracked def all_branches(self): return self.local_branches + self.remote_branches diff --git a/cola/widgets/diff.py b/cola/widgets/diff.py index 603e51dd..344d08c5 100644 --- a/cola/widgets/diff.py +++ b/cola/widgets/diff.py @@ -735,7 +735,7 @@ class DiffEditor(DiffTextEdit): enabled = False s = self.selection_model.selection() model = self.model - if model.stageable(): + if model.partially_stageable(): item = s.modified[0] if s.modified else None if item in model.submodules: pass @@ -784,7 +784,7 @@ class DiffEditor(DiffTextEdit): self.stage_or_unstage.setText(N_('Unstage')) menu.addAction(self.stage_or_unstage) - if model.stageable(): + if model.partially_stageable(): item = s.modified[0] if s.modified else None if item in model.submodules: path = core.abspath(item) @@ -879,7 +879,11 @@ class DiffEditor(DiffTextEdit): """setPlainText(str) while retaining scrollbar positions""" model = self.model mode = model.mode - highlight = mode not in (model.mode_none, model.mode_untracked) + highlight = mode not in ( + model.mode_none, + model.mode_display, + model.mode_untracked, + ) self.highlighter.set_enabled(highlight) scrollbar = self.verticalScrollBar() @@ -926,7 +930,7 @@ class DiffEditor(DiffTextEdit): def apply_selection(self): model = self.model s = self.selection_model.single_selection() - if model.stageable() and (s.modified or s.untracked): + if model.partially_stageable() and (s.modified or s.untracked): self.process_diff_selection() elif model.unstageable(): self.process_diff_selection(reverse=True) -- 2.11.4.GIT