eric7/VCS/VersionControl.py

branch
eric7
changeset 8624
5192a2592324
parent 8621
8c9f41115c04
child 8625
d52ae1294878
--- a/eric7/VCS/VersionControl.py	Wed Sep 22 19:52:28 2021 +0200
+++ b/eric7/VCS/VersionControl.py	Thu Sep 23 18:20:31 2021 +0200
@@ -8,15 +8,18 @@
 VCS interfaces.
 """
 
+import contextlib
+import json
 import os
-import contextlib
 
 from PyQt6.QtCore import (
-    QObject, QThread, QMutex, QProcess, Qt, pyqtSignal, QCoreApplication
+    QObject, QThread, QMutex, QProcess, Qt, pyqtSignal, QCoreApplication,
+    QLockFile
 )
 from PyQt6.QtWidgets import QApplication
 
 from EricWidgets import EricMessageBox
+from EricWidgets.EricApplication import ericApp
 
 import Preferences
 
@@ -50,6 +53,9 @@
     canBeCommitted = 1  # Indicates that a file/directory is in the vcs.
     canBeAdded = 2      # Indicates that a file/directory is not in vcs.
     
+    commitHistoryLock = "commitHistory.lock"
+    commitHistoryData = "commitHistory.json"
+    
     def __init__(self, parent=None, name=None):
         """
         Constructor
@@ -200,7 +206,121 @@
         raise RuntimeError('Not implemented')
         
         return False
+    
+    def vcsCommitMessages(self):
+        """
+        Public method to get the list of saved commit messages.
         
+        @return list of saved commit messages
+        @rtype list of str
+        @exception RuntimeError to indicate that this method must be
+            implemented by a subclass
+        """
+        raise RuntimeError('Not implemented')
+        
+        return []
+    
+    def _vcsProjectCommitMessages(self):
+        """
+        Protected method to get the list of saved commit messages.
+        
+        @return list of saved commit messages
+        @rtype list of str
+        """
+        messages = []
+        if Preferences.getVCS("PerProjectCommitHistory"):
+            projectMgmtDir = (
+                ericApp().getObject("Project").getProjectManagementDir
+            )
+            with contextlib.suppress(OSError, json.JSONDecodeError):
+                with open(os.path.join(projectMgmtDir,
+                                       VersionControl.commitHistoryData),
+                          "r") as f:
+                    jsonString = f.read()
+                messages = json.loads(jsonString)
+        
+        return messages
+    
+    def vcsAddCommitMessage(self, message):
+        """
+        Public method to add a commit message to the list of saved messages.
+        
+        @param message message to be added
+        @type str
+        @exception RuntimeError to indicate that this method must be
+            implemented by a subclass
+        """
+        raise RuntimeError('Not implemented')
+    
+    def _vcsAddProjectCommitMessage(self, message):
+        """
+        Protected method to add a commit message to the list of project
+        specific saved messages.
+        
+        @param message message to be added
+        @type str
+        @return flag indicating success
+        @rtype bool
+        """
+        if Preferences.getVCS("PerProjectCommitHistory"):
+            projectMgmtDir = (
+                ericApp().getObject("Project").getProjectManagementDir
+            )
+            lockFile = QLockFile(
+                os.path.join(projectMgmtDir, VersionControl.commitHistoryLock))
+            if lockFile.lock():
+                noMessages = Preferences.getVCS("CommitMessages")
+                messages = self.vcsCommitMessages()
+                if message in messages:
+                    messages.remove(message)
+                messages.insert(0, message)
+                del messages[noMessages:]
+                
+                with contextlib.suppress(TypeError, OSError):
+                    jsonString = json.dumps(messages, indent=2)
+                    with open(os.path.join(projectMgmtDir,
+                                           VersionControl.commitHistoryData),
+                              "w") as f:
+                        f.write(jsonString)
+                lockFile.unlock()
+                return True
+        
+        return False
+    
+    def vcsClearCommitMessages(self):
+        """
+        Public method to clear the list of saved messages.
+        
+        @exception RuntimeError to indicate that this method must be
+            implemented by a subclass
+        """
+        raise RuntimeError('Not implemented')
+    
+    def _vcsClearProjectCommitMessages(self):
+        """
+        Protected method to clear the list of project specific saved messages.
+        
+        @return flag indicating success
+        @rtype bool
+        """
+        if Preferences.getVCS("PerProjectCommitHistory"):
+            projectMgmtDir = (
+                ericApp().getObject("Project").getProjectManagementDir
+            )
+            lockFile = QLockFile(
+                os.path.join(projectMgmtDir, VersionControl.commitHistoryLock))
+            if lockFile.lock():
+                with contextlib.suppress(TypeError, OSError):
+                    jsonString = json.dumps([], indent=2)
+                    with open(os.path.join(projectMgmtDir,
+                                           VersionControl.commitHistoryData),
+                              "w") as f:
+                        f.write(jsonString)
+                lockFile.unlock()
+                return True
+        
+        return False
+    
     def vcsUpdate(self, name, noDialog=False):
         """
         Public method used to update a file/directory in the vcs.

eric ide

mercurial