@@ -30,6 +30,9 @@ def __init__(self, *, dir: StrPath) -> None:
3030 else :
3131 self .dir = pathlib .Path (dir )
3232
33+ # Initial git-submodule
34+ self .submodule = GitSubmodule (dir = self .dir , cmd = self )
35+
3336 def __repr__ (self ) -> str :
3437 return f"<Git dir={ self .dir } >"
3538
@@ -1665,6 +1668,8 @@ def config(
16651668 check_returncode = False ,
16661669 )
16671670
1671+ submodule : "GitSubmodule"
1672+
16681673 def version (
16691674 self ,
16701675 * ,
@@ -1692,3 +1697,76 @@ def version(
16921697 ["version" , * local_flags ],
16931698 check_returncode = False ,
16941699 )
1700+
1701+
1702+ GitSubmoduleCommandLiteral = Literal [
1703+ "status" ,
1704+ "init" ,
1705+ "deinit" ,
1706+ "update" ,
1707+ "set-branch" ,
1708+ "set-url" ,
1709+ "summary" ,
1710+ "foreach" ,
1711+ "sync" ,
1712+ "absorbgitdirs" ,
1713+ ]
1714+
1715+
1716+ class GitSubmodule :
1717+ def __init__ (self , * , dir : StrPath , cmd : Optional [Git ] = None ) -> None :
1718+ """Lite, typed, pythonic wrapper for git-submodule(1).
1719+
1720+ Parameters
1721+ ----------
1722+ dir :
1723+ Operates as PATH in the corresponding git subcommand.
1724+
1725+ Examples
1726+ --------
1727+ >>> GitSubmodule(dir=tmp_path)
1728+ <GitSubmodule dir=...>
1729+
1730+ >>> GitSubmodule(dir=tmp_path).run(quiet=True)
1731+ 'fatal: not a git repository (or any of the parent directories): .git'
1732+
1733+ >>> GitSubmodule(dir=git_local_clone.dir).run(quiet=True)
1734+ ''
1735+ """
1736+ #: Directory to check out
1737+ self .dir : pathlib .Path
1738+ if isinstance (dir , pathlib .Path ):
1739+ self .dir = dir
1740+ else :
1741+ self .dir = pathlib .Path (dir )
1742+
1743+ self .cmd = cmd if isinstance (cmd , Git ) else Git (dir = self .dir )
1744+
1745+ def __repr__ (self ) -> str :
1746+ return f"<GitSubmodule dir={ self .dir } >"
1747+
1748+ def run (
1749+ self ,
1750+ * ,
1751+ command : Optional [GitSubmoduleCommandLiteral ] = None ,
1752+ quiet : Optional [bool ] = None ,
1753+ cached : Optional [bool ] = None , # Only when no command entered and status
1754+ ** kwargs : Any ,
1755+ ) -> str :
1756+ """Version. Wraps `git submodule <https://git-scm.com/docs/git-submodule>`_.
1757+
1758+ Examples
1759+ --------
1760+ >>> git = GitSubmodule(dir=git_local_clone.dir)
1761+ """
1762+ local_flags : list [str ] = []
1763+
1764+ if quiet is True :
1765+ local_flags .append ("--quiet" )
1766+ if cached is True :
1767+ local_flags .append ("--cached" )
1768+
1769+ return self .cmd .run (
1770+ ["submodule" , * local_flags ],
1771+ check_returncode = False ,
1772+ )
0 commit comments