From b7e04608e6bfda06fc15672f691d605677299899 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sat, 25 Apr 2015 01:55:58 -0700 Subject: [PATCH] models.main: improve commit message auto-load behavior Move all of the commit message auto-load logic into the model. When automatically loading the commit message from e.g. .git/MERGE_MSG, remember the current message so that we can restore it later. When we detect that the message paths no longer exist, and the commit message has not been edited by the user, then reset the message back to the original message. Closes #431 Suggested-by: Dave Thomas Signed-off-by: David Aguilar --- cola/models/main.py | 29 +++++++++++++++++++++++++---- cola/utils.py | 8 -------- cola/widgets/main.py | 15 --------------- share/doc/git-cola/relnotes/unreleased.rst | 6 ++++++ share/doc/git-cola/thanks.rst | 1 + 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/cola/models/main.py b/cola/models/main.py index 513cd3ce..e2a600d9 100644 --- a/cola/models/main.py +++ b/cola/models/main.py @@ -3,8 +3,8 @@ """ from __future__ import division, absolute_import, unicode_literals -import os import copy +import os from cola import core from cola import git @@ -76,12 +76,15 @@ class MainModel(Observable): self.remotes = [] self.filter_paths = None - self.commitmsg = '' - self.modified = [] + self.commitmsg = '' # current commit message + self._auto_commitmsg = '' # e.g. .git/MERGE_MSG + self._prev_commitmsg = '' # saved here when clobbered by .git/MERGE_MSG + + self.modified = [] # modified, staged, untracked, unmerged paths self.staged = [] self.untracked = [] self.unmerged = [] - self.upstream_changed = [] + self.upstream_changed = [] # paths that've changed upstream self.staged_deleted = set() self.unstaged_deleted = set() self.submodules = set() @@ -182,6 +185,7 @@ class MainModel(Observable): self._update_remotes() self._update_branches_and_tags() self._update_branch_heads() + self._update_commitmsg() self.notify_observers(self.message_updated) def _update_files(self, update_index=False): @@ -230,6 +234,23 @@ class MainModel(Observable): if self.is_merging and self.mode == self.mode_amend: self.set_mode(self.mode_none) + def _update_commitmsg(self): + """Check for git merge message files, or clear it when the merge completes""" + if self.amending(): + return + # Check if there's a message file in .git/ + merge_msg_path = gitcmds.merge_message_path() + if merge_msg_path: + msg = core.read(merge_msg_path) + if msg != self._auto_commitmsg: + self._auto_commitmsg = msg + self._prev_commitmsg = self.commitmsg + self.set_commitmsg(msg) + + elif self._auto_commitmsg and self._auto_commitmsg == self.commitmsg: + self._auto_commitmsg = '' + self.set_commitmsg(self._prev_commitmsg) + def update_remotes(self): self._update_remotes() self._update_branches_and_tags() diff --git a/cola/utils.py b/cola/utils.py index adfae483..440a591b 100644 --- a/cola/utils.py +++ b/cola/utils.py @@ -12,7 +12,6 @@ import time import traceback from cola import core -import hashlib random.seed(hash(time.time())) @@ -211,13 +210,6 @@ def is_win32(): return sys.platform == 'win32' or sys.platform == 'cygwin' -def checksum(path): - """Return a cheap md5 hexdigest for a path.""" - md5 = hashlib.new('md5') - md5.update(open(path, 'rb').read()) - return md5.hexdigest() - - def expandpath(path): """Expand ~user/ and environment $variables""" path = os.path.expandvars(path) diff --git a/cola/widgets/main.py b/cola/widgets/main.py index e69923be..d6c17387 100644 --- a/cola/widgets/main.py +++ b/cola/widgets/main.py @@ -11,7 +11,6 @@ from PyQt4.QtCore import SIGNAL from cola import cmds from cola import core -from cola import gitcmds from cola import guicmds from cola import gitcfg from cola import qtutils @@ -74,9 +73,6 @@ class MainView(standard.MainWindow): # Change this whenever dockwidgets are removed. self.widget_version = 2 - # Keeps track of merge messages we've seen - self.merge_message_hash = '' - # Runs asynchronous tasks self.task_runner = TaskRunner(self) self.progress = standard.ProgressDialog('', '', self) @@ -631,17 +627,6 @@ class MainView(standard.MainWindow): self.commitmsgeditor.set_mode(self.mode) self.update_actions() - if not self.model.amending(): - # Check if there's a message file in .git/ - merge_msg_path = gitcmds.merge_message_path() - if merge_msg_path is None: - return - merge_msg_hash = utils.checksum(merge_msg_path) - if merge_msg_hash == self.merge_message_hash: - return - self.merge_message_hash = merge_msg_hash - cmds.do(cmds.LoadCommitMessageFromFile, merge_msg_path) - def update_actions(self): is_rebasing = self.model.is_rebasing can_rebase = not is_rebasing diff --git a/share/doc/git-cola/relnotes/unreleased.rst b/share/doc/git-cola/relnotes/unreleased.rst index 54b955bf..bbd59fab 100644 --- a/share/doc/git-cola/relnotes/unreleased.rst +++ b/share/doc/git-cola/relnotes/unreleased.rst @@ -13,6 +13,12 @@ Unreleased Topics * Double-click will now choose a commit in the "Select commit" dialog. +* `git cola` has a feature that reads `.git/MERGE_MSG` and friends for the + commit message when a merge is in-progress. Upon refresh, `git cola` will + now detect when a merge has completed and reset the commit message back to + its previous state. It is only reset if the editor contains a message + that was read from the file and has not been manually edited by the user. + Clone the git-cola repo to get the latest development version: ``git clone git://github.com/git-cola/git-cola.git`` diff --git a/share/doc/git-cola/thanks.rst b/share/doc/git-cola/thanks.rst index 2f9d5c0f..f03c0291 100644 --- a/share/doc/git-cola/thanks.rst +++ b/share/doc/git-cola/thanks.rst @@ -21,6 +21,7 @@ Thanks * Daniel Fahlke * Daniel Harding * Daniel King +* Dave Thomas * David Aguilar * David Martínez Martí * Dennis Gilmore -- 2.11.4.GIT