Add static typing for VcsInterface.getFileTemplate

This commit is contained in:
Romain Failliot 2022-01-09 12:18:00 -05:00
parent c224d17e9a
commit bf53f3c417
10 changed files with 45 additions and 29 deletions

View File

@ -20,13 +20,14 @@
import os
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
# Bazaar support
class Bzr(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
# merge conflict
left = name + '.OTHER'
right = name + '.THIS'

View File

@ -22,13 +22,14 @@ import os
from gettext import gettext as _
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
# CVS support
class Cvs(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
return [(name, 'BASE'), (name, None)]
def getCommitTemplate(self, prefs, rev, names):

View File

@ -20,13 +20,14 @@
import os
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
# Darcs support
class Darcs(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
return [(name, ''), (name, None)]
def _getCommitTemplate(self, prefs, names, rev):

View File

@ -27,7 +27,7 @@ from diffuse.vcs.vcs_interface import VcsInterface
# Git support
class Git(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
return [(name, 'HEAD'), (name, None)]
def getCommitTemplate(self, prefs, rev, names):

View File

@ -22,6 +22,7 @@ import os
from typing import Optional
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
@ -51,7 +52,7 @@ class Hg(VcsInterface):
return self.working_rev
return f'p1({rev})'
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
return [(name, self._getPreviousRevision(prefs, None)), (name, None)]
def _getCommitTemplate(self, prefs, names, cmd, rev):

View File

@ -21,27 +21,28 @@ import os
import shlex
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
# Monotone support
class Mtn(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
# FIXME: merge conflicts?
return [(name, 'h:'), (name, None)]
def getCommitTemplate(self, prefs, rev, names):
# build command
vcs_bin = prefs.getString('mtn_bin')
ss = utils.popenReadLines(
lines = utils.popenReadLines(
self.root,
[vcs_bin, 'automate', 'select', '-q', rev],
prefs,
'mtn_bash')
if len(ss) != 1:
if len(lines) != 1:
raise IOError('Ambiguous revision specifier')
args = [vcs_bin, 'automate', 'get_revision', ss[0]]
args = [vcs_bin, 'automate', 'get_revision', lines[0]]
# build list of interesting files
fs = FolderSet(names)
pwd, isabs = os.path.abspath(os.curdir), False
@ -50,15 +51,15 @@ class Mtn(VcsInterface):
# run command
prev = None
removed, added, modified, renamed = {}, {}, {}, {}
ss = utils.popenReadLines(self.root, args, prefs, 'mtn_bash')
lines = utils.popenReadLines(self.root, args, prefs, 'mtn_bash')
i = 0
while i < len(ss):
while i < len(lines):
# process results
s = shlex.split(ss[i])
line_args = shlex.split(lines[i])
i += 1
if len(s) < 2:
if len(line_args) < 2:
continue
arg, arg1 = s[0], s[1]
arg, arg1 = line_args[0], line_args[1]
if arg == 'old_revision' and len(arg1) > 2:
if prev is not None:
break
@ -82,26 +83,27 @@ class Mtn(VcsInterface):
if fs.contains(k):
modified[arg1] = k
elif arg == 'rename':
s = shlex.split(ss[i])
line_args = shlex.split(lines[i])
i += 1
if len(s) > 1 and s[0] == 'to':
if len(line_args) > 1 and line_args[0] == 'to':
# renamed file
k0 = os.path.join(self.root, prefs.convertToNativePath(arg1))
k1 = os.path.join(self.root, prefs.convertToNativePath(s[1]))
k1 = os.path.join(self.root, prefs.convertToNativePath(line_args[1]))
if fs.contains(k0) or fs.contains(k1):
renamed[s[1]] = (arg1, k0, k1)
renamed[line_args[1]] = (arg1, k0, k1)
if removed or renamed:
# remove directories
removed_dirs = set()
for s in utils.popenReadLines(
lines = utils.popenReadLines(
self.root,
[vcs_bin, 'automate', 'get_manifest_of', prev],
prefs,
'mtn_bash'
):
s = shlex.split(s)
if len(s) > 1 and s[0] == 'dir':
removed_dirs.add(s[1])
)
for line in lines:
line_args = shlex.split(line)
if len(line_args) > 1 and line_args[0] == 'dir':
removed_dirs.add(line_args[1])
for k in removed_dirs:
for m in removed, modified:
if k in m:

View File

@ -22,12 +22,13 @@ import os
from gettext import gettext as _
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.vcs_interface import VcsInterface
# RCS support
class Rcs(VcsInterface):
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
args = [
prefs.getString('rcs_bin_rlog'),
'-L',

View File

@ -19,7 +19,7 @@
import os
from typing import Tuple
from typing import Optional, Tuple
from diffuse import utils
from diffuse.vcs.svn import Svn
@ -41,7 +41,7 @@ class Svk(Svn):
return s[0], s[4:]
@staticmethod
def _getPreviousRevision(rev: str) -> str:
def _getPreviousRevision(rev: Optional[str]) -> str:
if rev is None:
return 'HEAD'
if rev.endswith('@'):

View File

@ -24,6 +24,7 @@ from gettext import gettext as _
from typing import Optional, Tuple
from diffuse import utils
from diffuse.preferences import Preferences
from diffuse.vcs.folder_set import FolderSet
from diffuse.vcs.vcs_interface import VcsInterface
@ -54,7 +55,7 @@ class Svn(VcsInterface):
return s[0], s[k:]
@staticmethod
def _getPreviousRevision(rev: str) -> str:
def _getPreviousRevision(rev: Optional[str]) -> str:
if rev is None:
return 'BASE'
m = int(rev)
@ -71,7 +72,7 @@ class Svn(VcsInterface):
break
return self.url
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> VcsInterface.PathRevisionList:
# FIXME: verify this
# merge conflict
escaped_name = utils.globEscape(name)

View File

@ -17,14 +17,22 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from typing import List, Optional, Tuple
from diffuse.preferences import Preferences
class VcsInterface:
"""Interface for the VCSs."""
PathRevisionPair = Tuple[Optional[str], Optional[str]]
PathRevisionList = List[PathRevisionPair]
def __init__(self, root: str):
"""The object will initialized with the repository's root folder."""
self.root = root
def getFileTemplate(self, prefs, name):
def getFileTemplate(self, prefs: Preferences, name: str) -> PathRevisionList:
"""Indicates which revisions to display for a file when none were explicitly
requested."""