Skip to content

Commit

Permalink
Add basic submodule init.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Sep 17, 2022
1 parent 9bb4312 commit 98b4188
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
39 changes: 28 additions & 11 deletions dulwich/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import argparse
import optparse
import signal
from typing import Dict, Type
from typing import Dict, Type, Optional

from dulwich import porcelain
from dulwich.client import get_transport_and_path
Expand Down Expand Up @@ -321,14 +321,6 @@ def run(self, args):
porcelain.rev_list(".", args)


class cmd_submodule(Command):
def run(self, args):
parser = optparse.OptionParser()
options, args = parser.parse_args(args)
for path, sha in porcelain.submodule_list("."):
sys.stdout.write(' %s %s\n' % (sha, path))


class cmd_tag(Command):
def run(self, args):
parser = optparse.OptionParser()
Expand Down Expand Up @@ -581,10 +573,11 @@ def run(self, args):

class SuperCommand(Command):

subcommands = {} # type: Dict[str, Type[Command]]
subcommands: Dict[str, Type[Command]] = {}
default_command: Optional[Type[Command]] = None

def run(self, args):
if not args:
if not args and not self.default_command:
print("Supported subcommands: %s" % ", ".join(self.subcommands.keys()))
return False
cmd = args[0]
Expand All @@ -603,6 +596,30 @@ class cmd_remote(SuperCommand):
}


class cmd_submodule_list(Command):
def run(self, argv):
parser = argparse.ArgumentParser()
parser.parse_args(argv)
for path, sha in porcelain.submodule_list("."):
sys.stdout.write(' %s %s\n' % (sha, path))


class cmd_submodule_init(Command):
def run(self, argv):
parser = argparse.ArgumentParser()
parser.parse_args(argv)
porcelain.submodule_init(".")


class cmd_submodule(SuperCommand):

subcommands = {
"init": cmd_submodule_init,
}

default_command = cmd_submodule_init


class cmd_check_ignore(Command):
def run(self, args):
parser = optparse.OptionParser()
Expand Down
6 changes: 6 additions & 0 deletions dulwich/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,12 @@ def sections(self) -> Iterator[Section]:
yield section


def read_submodules(path: str) -> Iterator[Tuple[bytes, bytes, bytes]]:
"""read a .gitmodules file."""
cfg = ConfigFile.from_path(path)
return parse_submodules(cfg)


def parse_submodules(config: ConfigFile) -> Iterator[Tuple[bytes, bytes, bytes]]:
"""Parse a gitmodules GitConfig file, returning submodules.
Expand Down
18 changes: 18 additions & 0 deletions dulwich/porcelain.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
* remote{_add}
* receive-pack
* reset
* submodule_add
* submodule_init
* submodule_list
* rev-list
* tag{_create,_delete,_list}
Expand Down Expand Up @@ -89,6 +91,7 @@
from dulwich.config import (
ConfigFile,
StackedConfig,
read_submodules,
)
from dulwich.diff_tree import (
CHANGE_ADD,
Expand Down Expand Up @@ -989,6 +992,21 @@ def submodule_add(repo, url, path=None, name=None):
config.write_to_path()


def submodule_init(repo):
"""Initialize submodules.
Args:
repo: Path to repository
"""
with open_repo_closing(repo) as r:
config = r.get_config()
gitmodules_path = os.path.join(r.path, '.gitmodules')
for path, url, name in read_submodules(gitmodules_path):
config.set((b'submodule', name), b'active', True)
config.set((b'submodule', name), b'url', url)
config.write_to_path()


def submodule_list(repo):
"""List submodules.
Expand Down
4 changes: 4 additions & 0 deletions dulwich/tests/test_porcelain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,10 @@ def test_add(self):
\tpath = bar
""", f.read())

def test_init(self):
porcelain.submodule_add(self.repo, "../bar.git", "bar")
porcelain.submodule_init(self.repo)


class PushTests(PorcelainTestCase):
def test_simple(self):
Expand Down

0 comments on commit 98b4188

Please sign in to comment.